Add Markdown converter output
This commit is contained in:
1 parent
5474ee1a31
commit
15ff70b4c3
5 files changed
+108
-2
No files matched your search
@@ -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
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
#include "convertmarkdown.h"
|
||||
|
||||
#include <cerrno>
|
||||
#include <cstring>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
|
||||
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;
|
||||
}
|
||||
@@ -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
|
||||
@@ -4,12 +4,13 @@
|
||||
#include <string>
|
||||
|
||||
#include "convertlatex.h"
|
||||
#include "convertmarkdown.h"
|
||||
#include "typefactory.h"
|
||||
|
||||
using ConverterFactory = TypeFactory<std::string, Converter, std::string>;
|
||||
|
||||
static constexpr std::array<const char *, 1> kSupportedTypes = {
|
||||
"latex"
|
||||
static constexpr std::array<const char *, 2> kSupportedTypes = {
|
||||
"latex", "md"
|
||||
};
|
||||
|
||||
struct CliArgs {
|
||||
@@ -33,6 +34,7 @@ static void print_supported_types() {
|
||||
static ConverterFactory build_converter_factory() {
|
||||
ConverterFactory factory;
|
||||
factory.registerType<ConverterLatex> ("latex");
|
||||
factory.registerType<ConverterMarkdown> ("md");
|
||||
return factory;
|
||||
}
|
||||
|
||||
|
||||
@@ -5,10 +5,12 @@ CONFIG -= qt
|
||||
|
||||
SOURCES += \
|
||||
convertlatex.cpp \
|
||||
convertmarkdown.cpp \
|
||||
converter.cpp \
|
||||
main.cpp
|
||||
|
||||
HEADERS += \
|
||||
convertlatex.h \
|
||||
convertmarkdown.h \
|
||||
converter.h \
|
||||
typefactory.h
|
||||
Reference in new issue
Block a user