WsParser_VS/WsNovelParser/main.cpp

249 lines
8.3 KiB
C++
Raw Normal View History

2024-03-17 07:58:28 +00:00
#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>
2024-03-31 05:59:17 +00:00
#include <QDir>
2024-04-04 05:18:44 +00:00
#include <QTextStream>
2024-04-20 03:34:33 +00:00
#include <iostream>
2024-03-17 07:58:28 +00:00
#include "novelparser.h"
2024-03-31 05:59:17 +00:00
#include "htmlprint.h"
2024-03-17 07:58:28 +00:00
using namespace example_novel;
2024-06-04 14:17:40 +00:00
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);
}
};
2024-06-05 00:27:34 +00:00
/*
* nsc --help
* nsc [opts] --path path-to-dir
* opts:
* -p print-struct <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2024-06-05 04:12:31 +00:00
* -s service <EFBFBD><EFBFBD>פ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2024-06-05 00:27:34 +00:00
*/
2024-03-17 07:58:28 +00:00
int main(int argc, char* argv[]) {
2024-04-05 12:51:20 +00:00
QCoreApplication a(argc, argv);
2024-03-17 07:58:28 +00:00
2024-06-05 04:12:31 +00:00
QStringList args;
for (int idx = 0; idx < argc; idx++) {
args += QString::fromLocal8Bit(argv[idx]);
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>÷<EFBFBD>
if (args.contains(u8"--help")) {
2024-04-20 03:34:33 +00:00
std::cout << "nsc(WsNovelStoryCompiler<65><72><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߱<EFBFBD><DFB1><EFBFBD><EFBFBD><EFBFBD>)" << std::endl;
2024-06-05 00:27:34 +00:00
std::cout << "<EFBFBD><EFBFBD><EFBFBD>V1.0.0" << std::endl;
2024-04-20 03:34:33 +00:00
std::cout << "nsc -path path-to-dir" << std::endl;
exit(0);
}
2024-06-05 04:12:31 +00:00
auto source_dir = QDir::current();
auto index = args.indexOf(u8"--path");
if (index < 0) {
std::cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>󣺱<EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD> --path <20><><EFBFBD><EFBFBD>" << endl;
exit(1);
}
if (index + 1 >= args.size()) {
std::cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>--path <20><><EFBFBD><EFBFBD>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>story<72>ļ<EFBFBD><C4BC>ĸ<EFBFBD><C4B8>ļ<EFBFBD><C4BC><EFBFBD>·<EFBFBD><C2B7><EFBFBD><EFBFBD>" << std::endl;
exit(1);
2024-04-05 12:51:20 +00:00
}
2024-06-05 04:12:31 +00:00
else {
auto tdir = QDir(args[index+1]);
if (tdir.exists()) {
source_dir = tdir;
}
else {
std::cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>󣺴<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>story<EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD>·<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ч<EFBFBD><EFBFBD>"<< std::endl;
exit(1);
}
}
2024-03-17 07:58:28 +00:00
2024-06-05 04:12:31 +00:00
auto files = source_dir.entryInfoList(QStringList() << "*.story");
2024-04-05 12:51:20 +00:00
if (files.size()) {
try {
auto parser = std::make_shared<NovelParser>();
auto novel_accesstree = parser->parse(files);
2024-03-17 07:58:28 +00:00
2024-04-05 12:51:20 +00:00
printer::tools_printer tool;
tool.build_fragments(novel_accesstree);
tool.build_refers_network(novel_accesstree);
2024-03-31 05:59:17 +00:00
2024-04-05 12:51:20 +00:00
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());
2024-04-04 05:18:44 +00:00
2024-04-05 12:51:20 +00:00
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);
2024-04-04 05:18:44 +00:00
2024-04-05 12:51:20 +00:00
auto body = doc_inst.createElement(u8"body");
html.appendChild(body);
2024-04-04 05:18:44 +00:00
2024-04-05 12:51:20 +00:00
inst->buildPageHTML(body);
2024-04-04 05:18:44 +00:00
2024-04-05 12:51:20 +00:00
QTextStream tout(&tfile);
doc_inst.save(tout, 2);
tout.flush();
}
};
2024-04-04 05:18:44 +00:00
2024-04-05 12:51:20 +00:00
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);
2024-03-17 07:58:28 +00:00
2024-04-16 15:40:13 +00:00
auto dot_src = tool.storylines_paint(tool.storyline_defines.values());
QFile dot_file(QDir::current().filePath(u8"relates.dot"));
if (dot_file.open(QIODevice::WriteOnly | QIODevice::Text)) {
QTextStream txt(&dot_file);
txt.setCodec(u8"UTF-8");
txt << dot_src;
txt.flush();
}
2024-04-17 17:56:44 +00:00
system("dot -Tsvg relates.dot -o relates.svg");
auto vols_src = tool.volumes_paint(tool.volume_defines.values(), tool.storyline_defines.values());
QFile vols_file(QDir::current().filePath(u8"volumes_group.dot"));
if (vols_file.open(QIODevice::WriteOnly | QIODevice::Text)) {
QTextStream txt(&vols_file);
txt.setCodec(u8"UTF-8");
txt << vols_src;
txt.flush();
}
system("fdp -Tsvg volumes_group.dot -o volumes_group.svg");
2024-04-05 13:39:29 +00:00
2024-04-05 12:51:20 +00:00
2024-04-05 13:39:29 +00:00
{
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);
2024-05-26 04:11:45 +00:00
auto lines = tool.storyline_defines.values();
2024-06-05 04:12:31 +00:00
std::sort(lines.begin(), lines.end(), [](std::shared_ptr<printer::StoryLine> a, std::shared_ptr<printer::StoryLine> b)->bool {
2024-05-26 04:11:45 +00:00
auto a_elm = std::dynamic_pointer_cast<const example_novel::StoryDefine>(a->accessPeers()->element());
auto b_elm = std::dynamic_pointer_cast<const example_novel::StoryDefine>(b->accessPeers()->element());
return a_elm->sort() < b_elm->sort();
});
for (auto& inst_line : lines) {
2024-04-05 13:39:29 +00:00
auto dom_line = doc_inst.createElement("p");
dom_storyline.appendChild(dom_line);
auto line_href = doc_inst.createElement("a");
2024-04-16 15:40:13 +00:00
line_href.setAttribute(u8"href", u8"file:///" + inst_line->pageRefers());
2024-05-26 04:11:45 +00:00
auto a_elm = std::dynamic_pointer_cast<const example_novel::StoryDefine>(inst_line->accessPeers()->element());
line_href.appendChild(doc_inst.createTextNode(QString(u8"%1.%2").arg(a_elm->sort()).arg(a_elm->signature())));
2024-04-05 13:39:29 +00:00
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);
2024-05-27 11:53:36 +00:00
auto table_cube = doc_inst.createElement(u8"table");
dom_fragment.appendChild(table_cube);
int row_ctrl = 0;
QDomElement elm_row;
2024-04-16 15:40:13 +00:00
for (auto& inst_frag : tool.fragment_defines) {
2024-05-27 11:53:36 +00:00
if(row_ctrl++ % 4 == 0){
elm_row = doc_inst.createElement(u8"tr");
table_cube.appendChild(elm_row);
}
auto dom_fragment_ref = doc_inst.createElement("td");
elm_row.appendChild(dom_fragment_ref);
2024-04-05 13:39:29 +00:00
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);
}
2024-04-16 15:40:13 +00:00
auto dom_relate = doc_inst.createElement(u8"div");
auto dom_relate_title = doc_inst.createElement(u8"h2");
dom_relate_title.appendChild(doc_inst.createTextNode(u8"<EFBFBD><EFBFBD>ϵͼ"));
2024-04-16 15:47:58 +00:00
dom_relate.appendChild(dom_relate_title);
2024-04-16 15:40:13 +00:00
body.appendChild(dom_relate);
auto img = doc_inst.createElement(u8"img");
img.setAttribute(u8"src", u8"file:///" + QDir::current().filePath(u8"relates.svg"));
dom_relate.appendChild(img);
2024-04-17 17:56:44 +00:00
dom_relate.appendChild(doc_inst.createElement("br"));
img = doc_inst.createElement(u8"img");
img.setAttribute(u8"src", u8"file:///" + QDir::current().filePath(u8"volumes_group.svg"));
dom_relate.appendChild(img);
2024-04-16 15:40:13 +00:00
2024-04-05 13:39:29 +00:00
QTextStream tout(&tfile);
doc_inst.save(tout, 2);
tout.flush();
}
}
2024-06-04 14:17:40 +00:00
//tnode_print(novel_accesstree, 0);
qDebug() << u8"<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɹ<EFBFBD><EFBFBD><EFBFBD>" << QDir::current().absoluteFilePath(u8"index.html");
2024-04-05 12:51:20 +00:00
}
catch (lib_syntax::SyntaxException* e) {
qDebug().noquote() << e->message();
delete e;
}
catch (lib_parse::CheckException* e) {
qDebug().noquote() << e->message();
delete e;
}
}
2024-05-26 04:11:45 +00:00
return 0;
//return a.exec();
2024-03-17 07:58:28 +00:00
}