Refactored AsciiDoc export into a class.

This commit is contained in:
tastytea 2019-06-06 16:16:57 +02:00
parent 008a5fe0f2
commit 98bb87ed93
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
4 changed files with 174 additions and 162 deletions

View File

@ -29,11 +29,13 @@ using std::regex;
using std::regex_replace;
using tagpair = std::pair<string,vector<Database::entry>>;
void export_adoc(const vector<Database::entry> &entries, ostream &out)
namespace Export
{
void AsciiDoc::print() const
{
try
{
out << "= Visited things\n"
_out << "= Visited things\n"
<< ":Author: remwharead " << global::version << endl
<< ":Date: "
<< timepoint_to_string(system_clock::now()) << endl
@ -43,7 +45,7 @@ void export_adoc(const vector<Database::entry> &entries, ostream &out)
std::map<string,vector<Database::entry>> alltags;
string day;
for (const Database::entry &entry : entries)
for (const Database::entry &entry : _entries)
{
const string datetime = timepoint_to_string(entry.datetime);
const string newday = datetime.substr(0, datetime.find('T'));
@ -51,25 +53,25 @@ void export_adoc(const vector<Database::entry> &entries, ostream &out)
if (newday != day)
{
day = newday;
out << "== " << day << endl << endl;
_out << "== " << day << endl << endl;
}
out << "[[dt_" << datetime << "]]\n";
out << "* link:" << entry.uri;
_out << "[[dt_" << datetime << "]]\n";
_out << "* link:" << entry.uri;
if (!entry.title.empty())
{
out << '[' << entry.title << ']';
_out << '[' << entry.title << ']';
}
else
{
out << "[]";
_out << "[]";
}
out << " +" << endl;
_out << " +" << endl;
out << '_' << time.substr(0, 5) << '_';
_out << '_' << time.substr(0, 5) << '_';
if (!entry.archive_uri.empty())
{
out << " (link:" << entry.archive_uri << "[archived version])";
_out << " (link:" << entry.archive_uri << "[archived version])";
}
bool separator = false;
@ -81,7 +83,7 @@ void export_adoc(const vector<Database::entry> &entries, ostream &out)
}
if (!separator)
{
out << "\n| ";
_out << "\n| ";
separator = true;
}
@ -95,23 +97,23 @@ void export_adoc(const vector<Database::entry> &entries, ostream &out)
alltags.insert({ tag, { entry } });
}
out << "xref:t_" << replace_in_tags(tag) << "[" << tag << ']';
_out << "xref:t_" << replace_in_tags(tag) << "[" << tag << ']';
if (tag != *(entry.tags.rbegin()))
{
out << ", ";
_out << ", ";
}
}
if (!entry.description.empty())
{
out << " +" << endl << entry.description;
_out << " +" << endl << entry.description;
}
out << endl << endl;
_out << endl << endl;
}
if (!alltags.empty())
{
adoc_print_tags(alltags, out);
print_tags(alltags);
}
}
catch (std::exception &e)
@ -120,7 +122,7 @@ void export_adoc(const vector<Database::entry> &entries, ostream &out)
}
}
const string replace_in_tags(string text)
const string AsciiDoc::replace_in_tags(string text) const
{
// TODO: Find a better solution.
const std::map<const string, const string> searchreplace =
@ -152,10 +154,10 @@ const string replace_in_tags(string text)
return text;
}
void adoc_print_tags(const std::map<string,vector<Database::entry>> &tags,
ostream &out)
void AsciiDoc::print_tags(
const std::map<string,vector<Database::entry>> &tags) const
{
out << "== Tags\n\n";
_out << "== Tags\n\n";
vector<tagpair> sortedtags(tags.size());
std::move(tags.begin(), tags.end(), sortedtags.begin());
std::sort(sortedtags.begin(), sortedtags.end(),
@ -185,13 +187,13 @@ void adoc_print_tags(const std::map<string,vector<Database::entry>> &tags,
{
if (!othertags)
{
out << "=== Less used tags\n\n";
_out << "=== Less used tags\n\n";
othertags = true;
}
out << "=";
_out << "=";
}
out << "=== [[t_" << replace_in_tags(tag.first) << "]]"
_out << "=== [[t_" << replace_in_tags(tag.first) << "]]"
<< tag.first << endl;
for (const Database::entry &entry : tag.second)
{
@ -202,10 +204,11 @@ void adoc_print_tags(const std::map<string,vector<Database::entry>> &tags,
{
title = "++" + entry.uri + "++";
}
out << endl << "* xref:dt_" << datetime
_out << endl << "* xref:dt_" << datetime
<< '[' << title << "] _(" << date << ")_" << endl;
}
out << endl;
_out << endl;
}
_out << endl;
}
out << endl;
}

View File

@ -45,18 +45,27 @@ namespace Export
using ExportBase::ExportBase;
void print() const;
private:
//! replaces " with "".
const string quote(string field) const;
};
class AsciiDoc : protected ExportBase
{
public:
using ExportBase::ExportBase;
void print() const;
private:
//! Replaces characters in tags that asciidoctor doesn't like.
const string replace_in_tags(string text) const;
void print_tags(const std::map<string,vector<Database::entry>> &tags) const;
};
}
void export_adoc(const vector<Database::entry> &entries, ostream &out = cout);
//! Replaces characters in tags that asciidoctor doesn't like.
const string replace_in_tags(string text);
void adoc_print_tags(const std::map<string,vector<Database::entry>> &tags,
ostream &out = cout);
//! Export as Netscape bookmark file.
void export_bookmarks(const vector<Database::entry> &entries,

View File

@ -111,12 +111,12 @@ int main(const int argc, const char *argv[])
{
if (file.is_open())
{
export_adoc(entries, file);
Export::AsciiDoc(entries, file).print();
file.close();
}
else
{
export_adoc(entries);
Export::AsciiDoc(entries).print();
}
break;
}

View File

@ -47,7 +47,7 @@ SCENARIO ("The AsciiDoc export works correctly")
try
{
std::ostringstream output;
export_adoc({ entry }, output);
Export::AsciiDoc({ entry }, output).print();
const string adoc = output.str();
const regex re_header