162 lines
5.2 KiB
C++
162 lines
5.2 KiB
C++
#include <QtCore/QCoreApplication>
|
||
#include <QCoreApplication>
|
||
#include <QDebug>
|
||
#include <QFileInfoList>
|
||
#include <ast_gen.h>
|
||
#include <ast_novel.h>
|
||
#include <libtoken.h>
|
||
#include <syntax_novel.h>
|
||
#include <tokens_novel.h>
|
||
#include <QDir>
|
||
#include <QTextStream>
|
||
|
||
#include "novelparser.h"
|
||
#include "htmlprint.h"
|
||
|
||
using namespace example_novel;
|
||
|
||
int main(int argc, char* argv[]) {
|
||
QCoreApplication a(argc, argv);
|
||
|
||
auto sdir = QDir::current();
|
||
if (argc >= 3 && !strcmp(argv[1], "-path")) {
|
||
auto tdir = QDir(QString::fromLocal8Bit(argv[2]));
|
||
if (tdir.exists())
|
||
sdir = tdir;
|
||
}
|
||
|
||
auto files = sdir.entryInfoList(QStringList() << "*.story");
|
||
if (files.size()) {
|
||
try {
|
||
auto parser = std::make_shared<NovelParser>();
|
||
auto novel_accesstree = parser->parse(files);
|
||
|
||
printer::tools_printer tool;
|
||
tool.build_fragments(novel_accesstree);
|
||
tool.build_refers_network(novel_accesstree);
|
||
|
||
tool.fragments_anchors_define(tool.fragment_defines.values(), QDir::current());
|
||
tool.storylines_anchors_define(tool.storyline_defines.values(), QDir::current());
|
||
tool.volumes_anchors_define(tool.volume_defines.values(), QDir::current());
|
||
|
||
std::function<void(std::shared_ptr<const printer::Access>)> html_output =
|
||
[](std::shared_ptr<const printer::Access> inst) {
|
||
auto target_path = inst->pageRefers();
|
||
QFile tfile(target_path);
|
||
if (tfile.open(QIODevice::WriteOnly | QIODevice::Text)) {
|
||
QDomDocument doc_inst(QDomImplementation().createDocumentType(u8"html", QString(), QString()));
|
||
auto html = doc_inst.createElement(u8"html");
|
||
doc_inst.appendChild(html);
|
||
|
||
auto body = doc_inst.createElement(u8"body");
|
||
html.appendChild(body);
|
||
|
||
inst->buildPageHTML(body);
|
||
|
||
QTextStream tout(&tfile);
|
||
doc_inst.save(tout, 2);
|
||
tout.flush();
|
||
}
|
||
};
|
||
|
||
for (auto& node : tool.fragment_defines)
|
||
html_output(node);
|
||
for (auto& node : tool.storyline_defines)
|
||
html_output(node);
|
||
for (auto& node : tool.volume_defines)
|
||
html_output(node);
|
||
|
||
|
||
|
||
std::function<void(std::shared_ptr<const ast_gen::ElementAccess>, int)> tnode_print =
|
||
[&](std::shared_ptr<const ast_gen::ElementAccess> node, int intend) {
|
||
auto name = node->element()->signature();
|
||
auto text = QString(intend * 2, ' ') + name;
|
||
/*for (auto& t : node->tokens()) {
|
||
text += " " + t->token()->content();
|
||
}*/
|
||
qDebug() << text;
|
||
|
||
for (auto& c_n : node->children()) {
|
||
tnode_print(c_n, intend + 1);
|
||
}
|
||
};
|
||
|
||
{
|
||
QFile tfile("./index.html");
|
||
if (tfile.open(QIODevice::WriteOnly | QIODevice::Text)) {
|
||
QDomDocument doc_inst(QDomImplementation().createDocumentType(u8"html", QString(), QString()));
|
||
auto html = doc_inst.createElement(u8"html");
|
||
doc_inst.appendChild(html);
|
||
|
||
auto body = doc_inst.createElement(u8"body");
|
||
html.appendChild(body);
|
||
|
||
auto dom_storyline = doc_inst.createElement("div");
|
||
auto dom_storyline_title = doc_inst.createElement("h2");
|
||
dom_storyline_title.appendChild(doc_inst.createTextNode(u8"<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>"));
|
||
dom_storyline.appendChild(dom_storyline_title);
|
||
body.appendChild(dom_storyline);
|
||
|
||
for (auto& inst_line : tool.storyline_defines) {
|
||
auto dom_line = doc_inst.createElement("p");
|
||
dom_storyline.appendChild(dom_line);
|
||
|
||
auto line_href = doc_inst.createElement("a");
|
||
line_href.setAttribute(u8"href", u8"file:///"+inst_line->pageRefers());
|
||
line_href.appendChild(doc_inst.createTextNode(inst_line->accessPeers()->element()->signature()));
|
||
dom_line.appendChild(line_href);
|
||
}
|
||
|
||
auto dom_volume = doc_inst.createElement("div");
|
||
auto dom_volume_title = doc_inst.createElement("h2");
|
||
dom_volume_title.appendChild(doc_inst.createTextNode(u8"<EFBFBD>־<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>"));
|
||
dom_volume.appendChild(dom_volume_title);
|
||
body.appendChild(dom_volume);
|
||
|
||
for (auto& inst_volume : tool.volume_defines) {
|
||
auto dom_volume_ref = doc_inst.createElement("p");
|
||
dom_volume.appendChild(dom_volume_ref);
|
||
|
||
auto volume_href = doc_inst.createElement("a");
|
||
volume_href.setAttribute(u8"href", u8"file:///" + inst_volume->pageRefers());
|
||
volume_href.appendChild(doc_inst.createTextNode(inst_volume->accessPeers()->element()->signature()));
|
||
dom_volume_ref.appendChild(volume_href);
|
||
}
|
||
|
||
auto dom_fragment = doc_inst.createElement("div");
|
||
auto dom_fragment_title = doc_inst.createElement("h2");
|
||
dom_fragment_title.appendChild(doc_inst.createTextNode(u8"<EFBFBD><EFBFBD><EFBFBD>ڼ<EFBFBD><EFBFBD><EFBFBD>"));
|
||
dom_fragment.appendChild(dom_fragment_title);
|
||
body.appendChild(dom_fragment);
|
||
|
||
for (auto &inst_frag : tool.fragment_defines) {
|
||
auto dom_fragment_ref = doc_inst.createElement("p");
|
||
dom_fragment.appendChild(dom_fragment_ref);
|
||
|
||
auto frag_href = doc_inst.createElement("a");
|
||
frag_href.setAttribute(u8"href", u8"file:///" + inst_frag->pageRefers());
|
||
frag_href.appendChild(doc_inst.createTextNode(inst_frag->accessPeers()->element()->signature()));
|
||
dom_fragment_ref.appendChild(frag_href);
|
||
}
|
||
|
||
QTextStream tout(&tfile);
|
||
doc_inst.save(tout, 2);
|
||
tout.flush();
|
||
}
|
||
}
|
||
|
||
tnode_print(novel_accesstree, 0);
|
||
}
|
||
catch (lib_syntax::SyntaxException* e) {
|
||
qDebug().noquote() << e->message();
|
||
delete e;
|
||
}
|
||
catch (lib_parse::CheckException* e) {
|
||
qDebug().noquote() << e->message();
|
||
delete e;
|
||
}
|
||
}
|
||
return a.exec();
|
||
}
|