2019-05-16 08:37:50 +02:00
|
|
|
/* This file is part of remwharead.
|
|
|
|
* Copyright © 2019 tastytea <tastytea@tastytea.de>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, version 3.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <map>
|
|
|
|
#include <regex>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <utility>
|
2019-05-19 19:04:18 +02:00
|
|
|
#include <locale>
|
2019-05-16 08:37:50 +02:00
|
|
|
#include <curlpp/cURLpp.hpp>
|
|
|
|
#include "version.hpp"
|
|
|
|
#include "time.hpp"
|
|
|
|
#include "export.hpp"
|
|
|
|
|
|
|
|
using std::cerr;
|
|
|
|
using std::endl;
|
|
|
|
using std::regex;
|
|
|
|
using std::regex_replace;
|
2019-05-16 12:39:22 +02:00
|
|
|
using tagpair = std::pair<string,vector<Database::entry>>;
|
2019-05-16 08:37:50 +02:00
|
|
|
|
|
|
|
void export_adoc(const vector<Database::entry> &entries, ostream &out)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
out << "= Visited things\n"
|
|
|
|
<< ":Author: remwharead " << global::version << endl
|
2019-05-16 12:53:32 +02:00
|
|
|
<< ":Date: " << timepoint_to_string(system_clock::now()) << endl
|
2019-05-17 01:42:49 +02:00
|
|
|
<< ":TOC: right" << endl
|
2019-05-16 08:37:50 +02:00
|
|
|
// << ":toc-title:" << endl
|
|
|
|
<< endl;
|
|
|
|
|
2019-05-16 12:39:22 +02:00
|
|
|
std::map<string,vector<Database::entry>> alltags;
|
2019-05-16 08:37:50 +02:00
|
|
|
string day;
|
|
|
|
for (const Database::entry &entry : entries)
|
|
|
|
{
|
|
|
|
const string datetime = timepoint_to_string(entry.datetime);
|
|
|
|
const string newday = datetime.substr(0, datetime.find('T'));
|
|
|
|
const string time = datetime.substr(datetime.find('T') + 1);
|
|
|
|
if (newday != day)
|
|
|
|
{
|
|
|
|
day = newday;
|
|
|
|
out << "== " << day << endl << endl;
|
|
|
|
}
|
2019-05-16 12:53:32 +02:00
|
|
|
|
2019-05-20 12:45:58 +02:00
|
|
|
out << "[[dt_" << datetime << "]]\n";
|
2019-05-23 12:06:27 +02:00
|
|
|
out << "* link:" << entry.uri;
|
2019-05-16 08:37:50 +02:00
|
|
|
if (!entry.title.empty())
|
|
|
|
{
|
|
|
|
out << '[' << entry.title << ']';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
out << "[]";
|
|
|
|
}
|
2019-05-23 12:06:27 +02:00
|
|
|
out << " +" << endl;
|
2019-05-16 12:53:32 +02:00
|
|
|
|
2019-05-17 11:13:39 +02:00
|
|
|
out << '_' << time.substr(0, 5) << '_';
|
|
|
|
if (!entry.archive_uri.empty())
|
|
|
|
{
|
|
|
|
out << " (link:" << entry.archive_uri << "[archived version])";
|
|
|
|
}
|
|
|
|
|
|
|
|
bool separator = false;
|
2019-05-16 08:37:50 +02:00
|
|
|
for (const string &tag : entry.tags)
|
|
|
|
{
|
|
|
|
if (tag.empty())
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2019-05-17 11:13:39 +02:00
|
|
|
if (!separator)
|
|
|
|
{
|
2019-05-18 03:03:37 +02:00
|
|
|
out << "\n| ";
|
2019-05-17 11:13:39 +02:00
|
|
|
separator = true;
|
|
|
|
}
|
2019-05-16 08:37:50 +02:00
|
|
|
|
|
|
|
auto globaltag = alltags.find(tag);
|
|
|
|
if (globaltag != alltags.end())
|
|
|
|
{
|
2019-05-16 12:39:22 +02:00
|
|
|
globaltag->second.push_back(entry);
|
2019-05-16 08:37:50 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-05-16 12:39:22 +02:00
|
|
|
alltags.insert({ tag, { entry } });
|
2019-05-16 08:37:50 +02:00
|
|
|
}
|
|
|
|
|
2019-05-20 12:45:58 +02:00
|
|
|
out << "xref:t_" << replace_in_tags(tag) << "[" << tag << ']';
|
2019-05-16 08:37:50 +02:00
|
|
|
if (tag != *(entry.tags.rbegin()))
|
|
|
|
{
|
2019-05-17 11:13:39 +02:00
|
|
|
out << ", ";
|
2019-05-16 08:37:50 +02:00
|
|
|
}
|
|
|
|
}
|
2019-05-16 12:53:32 +02:00
|
|
|
|
2019-05-16 08:37:50 +02:00
|
|
|
if (!entry.description.empty())
|
|
|
|
{
|
2019-05-23 12:06:27 +02:00
|
|
|
out << " +" << endl << entry.description;
|
2019-05-16 08:37:50 +02:00
|
|
|
}
|
2019-05-23 12:06:27 +02:00
|
|
|
out << endl << endl;
|
2019-05-16 08:37:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!alltags.empty())
|
|
|
|
{
|
|
|
|
out << "== Tags\n\n";
|
2019-05-16 12:39:22 +02:00
|
|
|
vector<tagpair> sortedtags(alltags.size());
|
2019-05-16 08:37:50 +02:00
|
|
|
std::move(alltags.begin(), alltags.end(), sortedtags.begin());
|
|
|
|
std::sort(sortedtags.begin(), sortedtags.end(),
|
2019-05-16 12:39:22 +02:00
|
|
|
[](const tagpair &a, tagpair &b)
|
2019-05-16 08:37:50 +02:00
|
|
|
{
|
2019-05-18 15:13:27 +02:00
|
|
|
if (a.second.size() != b.second.size())
|
|
|
|
{ // Sort by number of occurrences if they are
|
|
|
|
// different.
|
|
|
|
return a.second.size() > b.second.size();
|
|
|
|
}
|
|
|
|
else
|
2019-05-19 19:04:18 +02:00
|
|
|
{ // Sort by tag names otherwise.
|
2019-05-22 12:25:06 +02:00
|
|
|
std::locale loc;
|
2019-05-19 19:04:18 +02:00
|
|
|
const std::collate<char> &coll =
|
|
|
|
std::use_facet<std::collate<char>>(loc);
|
2019-05-22 12:25:06 +02:00
|
|
|
return (coll.compare(
|
|
|
|
a.first.data(), a.first.data()
|
|
|
|
+ a.first.length(),
|
|
|
|
b.first.data(), b.first.data()
|
|
|
|
+ b.first.length()) == -1);
|
2019-05-18 15:13:27 +02:00
|
|
|
}
|
2019-05-16 08:37:50 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
for (const auto &tag : sortedtags)
|
|
|
|
{
|
2019-05-20 12:45:58 +02:00
|
|
|
out << "=== [[t_" << replace_in_tags(tag.first) << "]]"
|
2019-05-16 12:39:22 +02:00
|
|
|
<< tag.first << endl;
|
|
|
|
for (const Database::entry &entry : tag.second)
|
2019-05-16 08:37:50 +02:00
|
|
|
{
|
2019-05-24 23:53:01 +02:00
|
|
|
const string datetime = timepoint_to_string(entry.datetime);
|
2019-05-25 16:06:16 +02:00
|
|
|
const string date = datetime.substr(0, datetime.find('T'));
|
2019-05-21 15:26:20 +02:00
|
|
|
string title = entry.title;
|
|
|
|
if (title.empty())
|
|
|
|
{
|
|
|
|
title = "++" + entry.uri + "++";
|
|
|
|
}
|
2019-05-24 23:53:01 +02:00
|
|
|
out << endl << "* xref:dt_" << datetime
|
2019-05-25 16:06:16 +02:00
|
|
|
<< '[' << title << "] _(" << date << ")_" << endl;
|
2019-05-16 08:37:50 +02:00
|
|
|
}
|
2019-05-16 12:39:22 +02:00
|
|
|
out << endl;
|
2019-05-16 08:37:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (std::exception &e)
|
|
|
|
{
|
|
|
|
cerr << "Error in " << __func__ << ": " << e.what() << endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-20 10:44:27 +02:00
|
|
|
const string replace_in_tags(string text)
|
2019-05-16 08:37:50 +02:00
|
|
|
{
|
2019-05-20 10:44:27 +02:00
|
|
|
// TODO: Find a better solution.
|
|
|
|
const std::map<const string, const string> searchreplace =
|
|
|
|
{
|
2019-05-20 12:45:58 +02:00
|
|
|
{ " ", "-" }, { "§", "-" },
|
|
|
|
{ "$", "-" }, { "%", "-" },
|
|
|
|
{ "&", "-" }, { "/", "-" },
|
|
|
|
{ "=", "-" }, { "^", "-" },
|
|
|
|
{ "!", "-" }, { "?", "-" },
|
2019-05-20 10:44:27 +02:00
|
|
|
{ "₀", "0" }, { "⁰", "0" },
|
|
|
|
{ "₁", "1" }, { "¹", "1" },
|
|
|
|
{ "₂", "2" }, { "²", "2" },
|
|
|
|
{ "₃", "3" }, { "³", "3" },
|
|
|
|
{ "₄", "4" }, { "⁴", "4" },
|
|
|
|
{ "₅", "5" }, { "⁵", "5" },
|
|
|
|
{ "₆", "6" }, { "⁶", "6" },
|
|
|
|
{ "₇", "7" }, { "⁷", "7" },
|
|
|
|
{ "₈", "8" }, { "⁸", "8" },
|
|
|
|
{ "₉", "9" }, { "⁹", "9" }
|
|
|
|
};
|
|
|
|
for (const std::pair<const string, const string> &sr : searchreplace)
|
|
|
|
{
|
|
|
|
size_t pos = 0;
|
|
|
|
while ((pos = text.find(sr.first, pos)) != std::string::npos)
|
|
|
|
{
|
|
|
|
text.replace(pos, sr.first.length(), sr.second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return text;
|
2019-05-16 08:37:50 +02:00
|
|
|
}
|