Add AsciiDoc converter output
This commit is contained in:
1 parent
e5cb11c659
commit
804876656d
7 files changed
+99
-3
No files matched your search
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
#include "convertasciidoc.h"
|
||||
|
||||
#include <cerrno>
|
||||
#include <cstring>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
|
||||
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;
|
||||
}
|
||||
@@ -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
|
||||
@@ -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 \
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#include "convertasciidoc.h"
|
||||
#include "convertlatex.h"
|
||||
#include "convertmarkdown.h"
|
||||
#include "convertorg.h"
|
||||
@@ -10,8 +11,8 @@
|
||||
|
||||
using ConverterFactory = TypeFactory<std::string, Converter, std::string>;
|
||||
|
||||
static constexpr std::array<const char *, 3> kSupportedTypes = {
|
||||
"latex", "md", "org"
|
||||
static constexpr std::array<const char *, 4> 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<ConverterAsciiDoc> ("adoc");
|
||||
factory.registerType<ConverterLatex> ("latex");
|
||||
factory.registerType<ConverterMarkdown> ("md");
|
||||
factory.registerType<ConverterOrg> ("org");
|
||||
|
||||
@@ -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 \
|
||||
|
||||
Reference in new issue
Block a user