Add Org mode converter output

This commit is contained in:
deeaitch committed 2026-08-28 01:14:51 -04:00
1 parent 432a1dc223
commit e5cb11c659
7 files changed
+98 -3

No files matched your search

+2
View File
@@ -19,6 +19,8 @@ add_executable(kindle2notes
typefactory/convertlatex.h
typefactory/convertmarkdown.cpp
typefactory/convertmarkdown.h
typefactory/convertorg.cpp
typefactory/convertorg.h
typefactory/converter.cpp
typefactory/converter.h
typefactory/main.cpp
+29 -1
View File
@@ -7,6 +7,7 @@ Supported output formats:
- LaTeX (`--type=latex`)
- Markdown (`--type=md`)
- Org mode (`--type=org`)
Highlights are grouped by book title while preserving the order in which
books first appear in the input file.
@@ -63,6 +64,24 @@ Markdown block quote, followed by an empty notes heading:
Empty lines inside highlights are preserved as `>` lines, and Markdown syntax
characters are escaped automatically.
### Org mode
Books and clipping metadata become Org headings. Highlight text is placed in
a quote block followed by an empty notes heading:
```org
* Book Title
** - Your Highlight on Location 123-125 | Added on ...
#+begin_quote
Highlighted text line 1
Highlighted text line 2
#+end_quote
*** Notes
```
## Architecture
Input parsing is implemented as a stage-driven pipeline with one handler for
@@ -112,6 +131,15 @@ Generate Markdown:
--type=md
```
Generate Org mode:
```bash
./build/kindle2notes \
--input "My Clippings.txt" \
--output notes.org \
--type=org
```
Short options are also available:
```bash
@@ -122,7 +150,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` or `md` |
| `--type` | `-t` | Output format: `latex`, `md`, or `org` |
| `--help` | `-h` | Show usage information |
If an unsupported output type is requested, the program prints the list of
+46
View File
@@ -0,0 +1,46 @@
#include "convertorg.h"
#include <cerrno>
#include <cstring>
#include <fstream>
#include <iostream>
bool ConverterOrg::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 << "#+begin_quote\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 << "#+end_quote\n\n";
output << "*** Notes\n\n";
}
}
set_error_code (0);
return true;
}
+13
View File
@@ -0,0 +1,13 @@
#ifndef CONVERTORG_H
#define CONVERTORG_H
#include "converter.h"
class ConverterOrg final : public Converter {
public:
using Converter::Converter;
bool save (const std::string &output_file_name) override;
};
#endif // CONVERTORG_H
+2
View File
@@ -7,11 +7,13 @@ CONFIG -= qt
SOURCES += \
convertlatex.cpp \
convertmarkdown.cpp \
convertorg.cpp \
converter.cpp \
main.cpp
HEADERS += \
convertlatex.h \
convertmarkdown.h \
convertorg.h \
converter.h \
typefactory.h
+4 -2
View File
@@ -5,12 +5,13 @@
#include "convertlatex.h"
#include "convertmarkdown.h"
#include "convertorg.h"
#include "typefactory.h"
using ConverterFactory = TypeFactory<std::string, Converter, std::string>;
static constexpr std::array<const char *, 2> kSupportedTypes = {
"latex", "md"
static constexpr std::array<const char *, 3> kSupportedTypes = {
"latex", "md", "org"
};
struct CliArgs {
@@ -35,6 +36,7 @@ static ConverterFactory build_converter_factory() {
ConverterFactory factory;
factory.registerType<ConverterLatex> ("latex");
factory.registerType<ConverterMarkdown> ("md");
factory.registerType<ConverterOrg> ("org");
return factory;
}
+2
View File
@@ -7,11 +7,13 @@ CONFIG -= qt
SOURCES += \
convertlatex.cpp \
convertmarkdown.cpp \
convertorg.cpp \
converter.cpp \
main.cpp
HEADERS += \
convertlatex.h \
convertmarkdown.h \
convertorg.h \
converter.h \
typefactory.h