diff --git a/CMakeLists.txt b/CMakeLists.txt index 4fa82c3..e4f6f95 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,6 +15,8 @@ function(kindle2notes_enable_warnings target) endfunction() add_executable(kindle2notes + typefactory/convertasciidoc.cpp + typefactory/convertasciidoc.h typefactory/convertlatex.cpp typefactory/convertlatex.h typefactory/convertmarkdown.cpp diff --git a/README.md b/README.md index 01696b0..d173196 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,7 @@ Supported output formats: - LaTeX (`--type=latex`) - Markdown (`--type=md`) - Org mode (`--type=org`) +- AsciiDoc (`--type=adoc`) Highlights are grouped by book title while preserving the order in which books first appear in the input file. @@ -82,6 +83,25 @@ Highlighted text line 2 *** Notes ``` +### AsciiDoc + +Books and clipping metadata become section headings. Highlight text is placed +in a quote block followed by an empty notes section: + +```adoc +== Book Title + +=== - Your Highlight on Location 123-125 | Added on ... + +[quote] +____ +Highlighted text line 1 +Highlighted text line 2 +____ + +==== Notes +``` + ## Architecture Input parsing is implemented as a stage-driven pipeline with one handler for @@ -140,6 +160,15 @@ Generate Org mode: --type=org ``` +Generate AsciiDoc: + +```bash +./build/kindle2notes \ + --input "My Clippings.txt" \ + --output notes.adoc \ + --type=adoc +``` + Short options are also available: ```bash @@ -150,7 +179,7 @@ Short options are also available: | --- | --- | --- | | `--input` | `-i` | Path to the Kindle clippings file | | `--output` | `-o` | Path to the generated file | -| `--type` | `-t` | Output format: `latex`, `md`, or `org` | +| `--type` | `-t` | Output format: `latex`, `md`, `org`, or `adoc` | | `--help` | `-h` | Show usage information | If an unsupported output type is requested, the program prints the list of diff --git a/typefactory/convertasciidoc.cpp b/typefactory/convertasciidoc.cpp new file mode 100644 index 0000000..248a96b --- /dev/null +++ b/typefactory/convertasciidoc.cpp @@ -0,0 +1,46 @@ +#include "convertasciidoc.h" + +#include +#include +#include +#include + +bool ConverterAsciiDoc::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 (const auto &title : book_titles) { + output << "== " << title << "\n\n"; + + for (std::size_t quote_index = 0; + quote_index < quote_count (title); + ++quote_index) { + output << "=== " << quote_meta (title, quote_index) << "\n\n"; + output << "[quote]\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) + output << text_lines[line_index] << '\n'; + + output << "____\n\n"; + output << "==== Notes\n\n"; + } + } + + set_error_code (0); + return true; +} diff --git a/typefactory/convertasciidoc.h b/typefactory/convertasciidoc.h new file mode 100644 index 0000000..12fee45 --- /dev/null +++ b/typefactory/convertasciidoc.h @@ -0,0 +1,13 @@ +#ifndef CONVERTASCIIDOC_H +#define CONVERTASCIIDOC_H + +#include "converter.h" + +class ConverterAsciiDoc final : public Converter { +public: + using Converter::Converter; + + bool save (const std::string &output_file_name) override; +}; + +#endif // CONVERTASCIIDOC_H diff --git a/typefactory/kindle2notes.pro b/typefactory/kindle2notes.pro index 5da7b81..53822b3 100644 --- a/typefactory/kindle2notes.pro +++ b/typefactory/kindle2notes.pro @@ -5,6 +5,7 @@ CONFIG -= app_bundle CONFIG -= qt SOURCES += \ + convertasciidoc.cpp \ convertlatex.cpp \ convertmarkdown.cpp \ convertorg.cpp \ @@ -12,6 +13,7 @@ SOURCES += \ main.cpp HEADERS += \ + convertasciidoc.h \ convertlatex.h \ convertmarkdown.h \ convertorg.h \ diff --git a/typefactory/main.cpp b/typefactory/main.cpp index 8eccf67..4a4cb62 100644 --- a/typefactory/main.cpp +++ b/typefactory/main.cpp @@ -3,6 +3,7 @@ #include #include +#include "convertasciidoc.h" #include "convertlatex.h" #include "convertmarkdown.h" #include "convertorg.h" @@ -10,8 +11,8 @@ using ConverterFactory = TypeFactory; -static constexpr std::array kSupportedTypes = { - "latex", "md", "org" +static constexpr std::array kSupportedTypes = { + "latex", "md", "org", "adoc" }; struct CliArgs { @@ -34,6 +35,7 @@ static void print_supported_types() { static ConverterFactory build_converter_factory() { ConverterFactory factory; + factory.registerType ("adoc"); factory.registerType ("latex"); factory.registerType ("md"); factory.registerType ("org"); diff --git a/typefactory/typefactory.pro b/typefactory/typefactory.pro index 5da7b81..53822b3 100644 --- a/typefactory/typefactory.pro +++ b/typefactory/typefactory.pro @@ -5,6 +5,7 @@ CONFIG -= app_bundle CONFIG -= qt SOURCES += \ + convertasciidoc.cpp \ convertlatex.cpp \ convertmarkdown.cpp \ convertorg.cpp \ @@ -12,6 +13,7 @@ SOURCES += \ main.cpp HEADERS += \ + convertasciidoc.h \ convertlatex.h \ convertmarkdown.h \ convertorg.h \