From 15ff70b4c3fdd2e54a8d24834ce52aa5633f92dd Mon Sep 17 00:00:00 2001 From: deeaitch Date: Fri, 28 Aug 2026 00:32:21 -0400 Subject: [PATCH] Add Markdown converter output --- CMakeLists.txt | 2 + typefactory/convertmarkdown.cpp | 87 +++++++++++++++++++++++++++++++++ typefactory/convertmarkdown.h | 13 +++++ typefactory/main.cpp | 6 ++- typefactory/typefactory.pro | 2 + 5 files changed, 108 insertions(+), 2 deletions(-) create mode 100644 typefactory/convertmarkdown.cpp create mode 100644 typefactory/convertmarkdown.h diff --git a/CMakeLists.txt b/CMakeLists.txt index b8c7f61..81899d7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,6 +17,8 @@ endfunction() add_executable(kindle2latex_typefactory typefactory/convertlatex.cpp typefactory/convertlatex.h + typefactory/convertmarkdown.cpp + typefactory/convertmarkdown.h typefactory/converter.cpp typefactory/converter.h typefactory/main.cpp diff --git a/typefactory/convertmarkdown.cpp b/typefactory/convertmarkdown.cpp new file mode 100644 index 0000000..dcfbdbd --- /dev/null +++ b/typefactory/convertmarkdown.cpp @@ -0,0 +1,87 @@ +#include "convertmarkdown.h" + +#include +#include +#include +#include + +static bool markdown_needs_escape (char ch) { + switch (ch) { + case '\\': + case '`': + case '*': + case '_': + case '{': + case '}': + case '[': + case ']': + case '<': + case '>': + case '(': + case ')': + case '#': + case '+': + case '-': + case '.': + case '!': + case '|': + return true; + default: + return false; + } +} + +static std::string markdown_escape (const std::string &s) { + std::string out; + out.reserve (s.size() + s.size() / 8); + for (char ch : s) { + if (markdown_needs_escape (ch)) + out.push_back ('\\'); + out.push_back (ch); + } + return out; +} + +bool ConverterMarkdown::save (const std::string &output_file_name) { + if (!parse()) + return false; + + std::ofstream output (output_file_name, std::ios::trunc); + if (!output.is_open()) { + std::cerr << "Failed to open output file: " + << output_file_name + << " (" << std::strerror (errno) << ")\n"; + set_error_code (3); + return false; + } + + const auto &book_titles = titles(); + for (std::size_t i = 0; i < book_titles.size(); ++i) { + const auto &title = book_titles[i]; + output << "# " << markdown_escape (title) << "\n\n"; + + for (std::size_t quote_index = 0; + quote_index < quote_count (title); + ++quote_index) { + output << "## " << markdown_escape (quote_meta (title, quote_index)) + << "\n\n"; + + const auto &text_lines = quote_text_lines (title, quote_index); + std::size_t line_index = 0; + if (!text_lines.empty() && text_lines.front().empty()) + line_index = 1; + + for (; line_index < text_lines.size(); ++line_index) { + if (text_lines[line_index].empty()) + output << ">\n"; + else + output << "> " << markdown_escape (text_lines[line_index]) << '\n'; + } + + output << "\n### Notes\n\n"; + } + } + + set_error_code (0); + return true; +} diff --git a/typefactory/convertmarkdown.h b/typefactory/convertmarkdown.h new file mode 100644 index 0000000..8b20ade --- /dev/null +++ b/typefactory/convertmarkdown.h @@ -0,0 +1,13 @@ +#ifndef CONVERTMARKDOWN_H +#define CONVERTMARKDOWN_H + +#include "converter.h" + +class ConverterMarkdown final : public Converter { +public: + using Converter::Converter; + + bool save (const std::string &output_file_name) override; +}; + +#endif // CONVERTMARKDOWN_H diff --git a/typefactory/main.cpp b/typefactory/main.cpp index 1fa140f..71b3db6 100644 --- a/typefactory/main.cpp +++ b/typefactory/main.cpp @@ -4,12 +4,13 @@ #include #include "convertlatex.h" +#include "convertmarkdown.h" #include "typefactory.h" using ConverterFactory = TypeFactory; -static constexpr std::array kSupportedTypes = { - "latex" +static constexpr std::array kSupportedTypes = { + "latex", "md" }; struct CliArgs { @@ -33,6 +34,7 @@ static void print_supported_types() { static ConverterFactory build_converter_factory() { ConverterFactory factory; factory.registerType ("latex"); + factory.registerType ("md"); return factory; } diff --git a/typefactory/typefactory.pro b/typefactory/typefactory.pro index bf78f6c..8101ea3 100644 --- a/typefactory/typefactory.pro +++ b/typefactory/typefactory.pro @@ -5,10 +5,12 @@ CONFIG -= qt SOURCES += \ convertlatex.cpp \ + convertmarkdown.cpp \ converter.cpp \ main.cpp HEADERS += \ convertlatex.h \ + convertmarkdown.h \ converter.h \ typefactory.h