Add virtual converter output interface
This commit is contained in:
1 parent
28c9752f18
commit
0f4da8e695
7 files changed
+144
-86
No files matched your search
+24
-82
@@ -18,49 +18,6 @@ static std::string rstrip_cr (std::string s) {
|
||||
return s;
|
||||
}
|
||||
|
||||
static std::string latex_escape (const std::string &s) {
|
||||
std::string out;
|
||||
out.reserve (s.size() + s.size() / 8);
|
||||
for (unsigned char ch : s) {
|
||||
switch (ch) {
|
||||
case '\\':
|
||||
out += "\\textbackslash{}";
|
||||
break;
|
||||
case '{':
|
||||
out += "\\{";
|
||||
break;
|
||||
case '}':
|
||||
out += "\\}";
|
||||
break;
|
||||
case '%':
|
||||
out += "\\%";
|
||||
break;
|
||||
case '$':
|
||||
out += "\\$";
|
||||
break;
|
||||
case '#':
|
||||
out += "\\#";
|
||||
break;
|
||||
case '&':
|
||||
out += "\\&";
|
||||
break;
|
||||
case '_':
|
||||
out += "\\_";
|
||||
break;
|
||||
case '^':
|
||||
out += "\\textasciicircum{}";
|
||||
break;
|
||||
case '~':
|
||||
out += "\\textasciitilde{}";
|
||||
break;
|
||||
default:
|
||||
out.push_back (static_cast<char> (ch));
|
||||
break;
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
struct Quote {
|
||||
std::string meta;
|
||||
std::vector<std::string> text_lines;
|
||||
@@ -211,6 +168,30 @@ int Converter::error_code() const noexcept {
|
||||
return error_code_;
|
||||
}
|
||||
|
||||
void Converter::set_error_code (int error_code) noexcept {
|
||||
error_code_ = error_code;
|
||||
}
|
||||
|
||||
const std::vector<std::string> &Converter::titles() const noexcept {
|
||||
return impl_->context.order;
|
||||
}
|
||||
|
||||
std::size_t Converter::quote_count (const std::string &title) const {
|
||||
const auto it = impl_->context.by_title.find (title);
|
||||
return it == impl_->context.by_title.end() ? 0 : it->second.size();
|
||||
}
|
||||
|
||||
const std::string &Converter::quote_meta (const std::string &title,
|
||||
std::size_t index) const {
|
||||
return impl_->context.by_title.at (title).at (index).meta;
|
||||
}
|
||||
|
||||
const std::vector<std::string> &Converter::quote_text_lines (
|
||||
const std::string &title,
|
||||
std::size_t index) const {
|
||||
return impl_->context.by_title.at (title).at (index).text_lines;
|
||||
}
|
||||
|
||||
bool Converter::parse() {
|
||||
std::ifstream input (input_file_name_);
|
||||
if (!input.is_open()) {
|
||||
@@ -256,42 +237,3 @@ bool Converter::parse() {
|
||||
error_code_ = 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Converter::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";
|
||||
error_code_ = 3;
|
||||
return false;
|
||||
}
|
||||
|
||||
const ParseContext &ctx = impl_->context;
|
||||
for (std::size_t i = 0; i < ctx.order.size(); ++i) {
|
||||
const auto &title = ctx.order[i];
|
||||
output << "\\section {" << latex_escape (title) << "}\n";
|
||||
|
||||
const auto it = ctx.by_title.find (title);
|
||||
if (it == ctx.by_title.end())
|
||||
continue;
|
||||
|
||||
for (const auto "e : it->second) {
|
||||
output << " \\subsection {" << latex_escape (quote.meta) << "}\n";
|
||||
|
||||
for (const auto &text_line : quote.text_lines)
|
||||
output << " " << latex_escape (text_line) << "\n";
|
||||
|
||||
output << " \\subsubsection{notes}\n\n";
|
||||
}
|
||||
|
||||
if (i + 1 < ctx.order.size())
|
||||
output << "\n";
|
||||
}
|
||||
|
||||
error_code_ = 0;
|
||||
return true;
|
||||
}
|
||||
Reference in new issue
Block a user