47 lines
1.2 KiB
C++
47 lines
1.2 KiB
C++
#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;
|
|
}
|