From 2c12c0652d2b8c8440e1e908f004826840ed14ab Mon Sep 17 00:00:00 2001 From: Aleksey Veresov Date: Fri, 18 Oct 2019 17:33:06 +0300 Subject: [texo] Semi-working. --- Makefile | 2 +- examples/Makefile | 2 +- examples/plain_to_html.cpp | 9 +-- src/html.cpp | 173 ++++++++++++++++++++++++++++++++++++++++++++ src/html.hpp | 21 +++++- src/importer.cpp | 21 ++++++ src/importer.hpp | 16 +++++ src/plain.cpp | 174 +++++++++++++++++++++++++++++++++++++++++++++ src/plain.hpp | 36 +++++++++- src/producer.cpp | 25 +++++++ src/producer.hpp | 30 ++++++++ src/texo.cpp | 31 ++++++++ src/texo.hpp | 82 +++++++++++++++++++++ 13 files changed, 614 insertions(+), 8 deletions(-) diff --git a/Makefile b/Makefile index 7a0491a..0d85446 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ # Debug mode: -# DEBUG = yes +DEBUG = yes CC = g++ LIB = libtexo.a diff --git a/examples/Makefile b/examples/Makefile index d288391..1959b4b 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -1,5 +1,5 @@ # Uncomment following to enable debug mode: -# DEBUG = yes +DEBUG = yes CC = g++ EXAMPLES = plain_to_html diff --git a/examples/plain_to_html.cpp b/examples/plain_to_html.cpp index 9a81652..d123a84 100644 --- a/examples/plain_to_html.cpp +++ b/examples/plain_to_html.cpp @@ -2,14 +2,15 @@ #include #include #include +#include int main() { - TexoFileExporter exporter(stdout); - TexoHTMLProducer producer(exporter); - TexoPlainImporter importer(producer); - importer.PutStr( + TexoExporterFile exporter(stdout); + TexoProducerHTML producer(exporter); + TexoImporterPlain importer(producer); + importer.Put( "I am a little cute line. \n" "I am another and that's fine. \n" "But I am as long as a pine,\n" diff --git a/src/html.cpp b/src/html.cpp index 290891e..5155661 100644 --- a/src/html.cpp +++ b/src/html.cpp @@ -1 +1,174 @@ #include "html.hpp" + + +TexoProducerHTML::TexoProducerHTML(TexoExporter &exporter): + TexoProducer(exporter) +{} + +void TexoProducerHTML::Put(const Texo &piece) +{ + switch (piece.c) { + case '<': exporter.Put("<"); break; + case '>': exporter.Put(">"); break; + default: exporter.Put(piece.c); break; + } +} + +void TexoProducerHTML::Put(const TexoHeader &piece) +{ + exporter.Put('<'); + if (piece.closing) { + exporter.Put('/'); + } + if (piece.level <= 1) { + exporter.Put("h6>"); + } else if (piece.level == 2) { + exporter.Put("h5>"); + } else if (piece.level == 3) { + exporter.Put("h4>"); + } else if (piece.level == 4) { + exporter.Put("h3>"); + } else if (piece.level == 5) { + exporter.Put("h2>"); + } else { + exporter.Put("h1>"); + } +} + +void TexoProducerHTML::Put(const TexoParagraph &piece) +{ + if (piece.closing) { + exporter.Put("

"); + } else { + exporter.Put("

"); + } +} + +void TexoProducerHTML::Put(const TexoCode &piece) +{ + if (piece.closing) { + exporter.Put(""); + } else { + exporter.Put("

");
+    }
+}
+
+void TexoProducerHTML::Put(const TexoQuote &piece)
+{
+    if (piece.closing) {
+        exporter.Put("

"); + } else { + exporter.Put("

"); + } +} + +void TexoProducerHTML::Put(const TexoMono &piece) +{ + if (piece.closing) { + exporter.Put(""); + } else { + exporter.Put(""); + } +} + +void TexoProducerHTML::Put(const TexoBold &piece) +{ + if (piece.closing) { + exporter.Put(""); + } else { + exporter.Put(""); + } +} + +void TexoProducerHTML::Put(const TexoItalic &piece) +{ + if (piece.closing) { + exporter.Put(""); + } else { + exporter.Put(""); + } +} + +void TexoProducerHTML::Put(const TexoUnderline &piece) +{ + if (piece.closing) { + exporter.Put(""); + } else { + exporter.Put(""); + } +} + +void TexoProducerHTML::Put(const TexoStrike &piece) +{ + if (piece.closing) { + exporter.Put(""); + } else { + exporter.Put(""); + } +} + +void TexoProducerHTML::Put(const TexoImage &piece) +{ + if (piece.path != "") { + bool link = piece.link != ""; + bool title = piece.title != ""; + if (link) { + exporter.Put(""); + } + exporter.Put("");
+            exporter.Put(piece.alt);
+        }
+        if (title) {
+            exporter.Put(""); + if (link) { + exporter.Put(""); + } + } +} + +void TexoProducerHTML::Put(const TexoLink &piece) +{ + if (piece.text != "" && piece.link != "") { + exporter.Put(""); + exporter.Put(piece.text); + exporter.Put(""); + } +} + +void TexoProducerHTML::Put(const TexoLineBreak &piece) +{ + exporter.Put("
"); +} + +void TexoProducerHTML::Put(const TexoHorizontalRule &piece) +{ + exporter.Put("


"); +} + + +TexoImporterHTML::TexoImporterHTML(TexoProducer &producer): + TexoImporter(producer) +{} + +void TexoImporterHTML::Put(char c) +{ // TODO + producer.Put(Texo(c)); +} diff --git a/src/html.hpp b/src/html.hpp index a904d56..dc291f6 100644 --- a/src/html.hpp +++ b/src/html.hpp @@ -4,18 +4,37 @@ #include "exporter.hpp" #include "producer.hpp" #include "importer.hpp" -#include "texo.hpp" class TexoProducerHTML: public TexoProducer { public: TexoProducerHTML(TexoExporter &exporter); + + void Put(const Texo &piece); + + void Put(const TexoHeader &piece); + void Put(const TexoParagraph &piece); + void Put(const TexoCode &piece); + void Put(const TexoQuote &piece); + + void Put(const TexoMono &piece); + void Put(const TexoBold &piece); + void Put(const TexoItalic &piece); + void Put(const TexoUnderline &piece); + void Put(const TexoStrike &piece); + + void Put(const TexoImage &piece); + void Put(const TexoLink &piece); + void Put(const TexoLineBreak &piece); + void Put(const TexoHorizontalRule &piece); }; class TexoImporterHTML: public TexoImporter { public: TexoImporterHTML(TexoProducer &producer); + + void Put(char c); }; diff --git a/src/importer.cpp b/src/importer.cpp index cb8d62e..73f23e6 100644 --- a/src/importer.cpp +++ b/src/importer.cpp @@ -1 +1,22 @@ #include "importer.hpp" + + +TexoImporter::TexoImporter(TexoProducer &producer): producer(producer) +{} + +void TexoImporter::Put(const ScriptVariable &str) +{ + const int len = str.Length(); + for (int i = 0; i < len; ++i) { + Put(str[i]); + } +} + +void TexoImporter::Put(FILE *file) +{ + if (file) { + for (int c = fgetc(file); c != EOF; c = fgetc(file)) { + Put(c); + } + } +} diff --git a/src/importer.hpp b/src/importer.hpp index 81a3b59..915416c 100644 --- a/src/importer.hpp +++ b/src/importer.hpp @@ -1,5 +1,21 @@ #ifndef TEXO_INCLUDED_IMPORTER #define TEXO_INCLUDED_IMPORTER +#include "producer.hpp" +#include + + +class TexoImporter { +public: + TexoImporter(TexoProducer &producer); + + virtual void Put(char c) = 0; + virtual void Put(const ScriptVariable &str); + virtual void Put(FILE *file); + +protected: + TexoProducer &producer; +}; + #endif diff --git a/src/plain.cpp b/src/plain.cpp index b510d58..bff9398 100644 --- a/src/plain.cpp +++ b/src/plain.cpp @@ -1 +1,175 @@ #include "plain.hpp" + + +TexoProducerPlain::TexoProducerPlain(TexoExporter &exporter): + TexoProducer(exporter) +{} + +void TexoProducerPlain::Put(const Texo &piece) +{ + if (quoted && piece.c == '\n') { + exporter.Put("\n> "); + } else { + exporter.Put(piece.c); + } +} + +void TexoProducerPlain::Put(const TexoHeader &piece) +{ + if (!piece.closing) { + exporter.Put("\n\n# "); + } +} + +void TexoProducerPlain::Put(const TexoParagraph &piece) +{ + if (piece.closing) { + exporter.Put("\n\n"); + } +} + +void TexoProducerPlain::Put(const TexoQuote &piece) +{ + if (piece.closing) { + quoted = false; + exporter.Put("\n\n"); + } else { + quoted = true; + exporter.Put("\n\n> "); + } +} + +void TexoProducerPlain::Put(const TexoLineBreak &piece) +{ + exporter.Put(" \n"); +} + +void TexoProducerPlain::Put(const TexoHorizontalRule &piece) +{ + exporter.Put("\n--------------------------------------------------\n"); +} + + +TexoImporterPlain::TexoImporterPlain(TexoProducer &producer): + TexoImporter(producer), state(text) +{ + producer.Put(TexoParagraph()); +} + +TexoImporterPlain::~TexoImporterPlain() +{ + switch (state) { + case text: case space: case newline: + producer.Put(TexoParagraph(true)); + break; + case quote: case quote_newline: + producer.Put(TexoQuote(true)); + break; + case header: + producer.Put(TexoHeader(true)); + break; + case paragraph: + break; + } +} + +void TexoImporterPlain::Put(char c) +{ + switch (state) { + case text: Text(c); break; + case space: Space(c); break; + case newline: Newline(c); break; + case paragraph: Paragraph(c); break; + case quote: Quote(c); break; + case quote_newline: QuoteNewline(c); break; + case header: Header(c); break; + } +} + +void TexoImporterPlain::Put(const ScriptVariable &str) +{ + TexoImporter::Put(str); +} + +void TexoImporterPlain::Text(char c) +{ + switch (c) { + case ' ': state = space; break; + case '\n': state = newline; break; + default: producer.Put(Texo(c)); break; + } +} + +void TexoImporterPlain::Space(char c) +{ + if (c == '\n') { + producer.Put(TexoLineBreak()); + state = text; + } else if (c == ' ') { + producer.Put(Texo(' ')); + } else { + producer.Put(Texo(' ')); + producer.Put(Texo(c)); + state = text; + } +} + +void TexoImporterPlain::Newline(char c) +{ + if (c == '\n') { + producer.Put(TexoParagraph(true)); + state = paragraph; + } else { + producer.Put(Texo('\n')); + producer.Put(Texo(c)); + state = text; + } +} + +void TexoImporterPlain::Paragraph(char c) +{ + if (c == '>') { + producer.Put(TexoQuote()); + state = quote; + } else if (c == '#') { + producer.Put(TexoHeader(1)); + state = header; + } else { + producer.Put(TexoParagraph()); + producer.Put(Texo(c)); + state = text; + } +} + +void TexoImporterPlain::Quote(char c) +{ + switch (c) { + case '\n': state = quote_newline; break; + default: producer.Put(Texo(c)); break; + } +} + +void TexoImporterPlain::QuoteNewline(char c) +{ + if (c == '>') { + state = quote; + } else if (c == '\n') { + producer.Put(TexoQuote(true)); + state = paragraph; + } else { + producer.Put(TexoQuote(true)); + producer.Put(TexoParagraph()); + producer.Put(Texo(c)); + state = text; + } +} + +void TexoImporterPlain::Header(char c) +{ + if (c == '\n') { + producer.Put(TexoHeader(1, true)); + state = paragraph; + } else { + producer.Put(Texo(c)); + } +} diff --git a/src/plain.hpp b/src/plain.hpp index d1a9efb..6fa086f 100644 --- a/src/plain.hpp +++ b/src/plain.hpp @@ -4,18 +4,52 @@ #include "exporter.hpp" #include "producer.hpp" #include "importer.hpp" -#include "texo.hpp" class TexoProducerPlain: public TexoProducer { public: TexoProducerPlain(TexoExporter &exporter); + + void Put(const Texo &piece); + + void Put(const TexoHeader &piece); + void Put(const TexoParagraph &piece); + void Put(const TexoQuote &piece); + + void Put(const TexoLineBreak &piece); + void Put(const TexoHorizontalRule &piece); + +private: + bool quoted; }; class TexoImporterPlain: public TexoImporter { public: TexoImporterPlain(TexoProducer &producer); + ~TexoImporterPlain(); + + void Put(char c); + void Put(const ScriptVariable &str); + +private: + enum State { + text, + space, + newline, + paragraph, + quote, + quote_newline, + header + } state; + + void Text(char c); + void Space(char c); + void Newline(char c); + void Paragraph(char c); + void Quote(char c); + void QuoteNewline(char c); + void Header(char c); }; diff --git a/src/producer.cpp b/src/producer.cpp index 959e210..c3ef801 100644 --- a/src/producer.cpp +++ b/src/producer.cpp @@ -1 +1,26 @@ #include "producer.hpp" + + +TexoProducer::TexoProducer(TexoExporter &exporter): exporter(exporter) +{} + +void TexoProducer::Put(const Texo &piece) +{ + exporter.Put(piece.c); +} + +void TexoProducer::Put(const TexoHeader &piece) {} +void TexoProducer::Put(const TexoParagraph &piece) {} +void TexoProducer::Put(const TexoCode &piece) {} +void TexoProducer::Put(const TexoQuote &piece) {} + +void TexoProducer::Put(const TexoMono &piece) {} +void TexoProducer::Put(const TexoBold &piece) {} +void TexoProducer::Put(const TexoItalic &piece) {} +void TexoProducer::Put(const TexoUnderline &piece) {} +void TexoProducer::Put(const TexoStrike &piece) {} +void TexoProducer::Put(const TexoLink &piece) {} + +void TexoProducer::Put(const TexoImage &piece) {} +void TexoProducer::Put(const TexoLineBreak &piece) {} +void TexoProducer::Put(const TexoHorizontalRule &piece) {} diff --git a/src/producer.hpp b/src/producer.hpp index 65f6ec3..a2f9ecc 100644 --- a/src/producer.hpp +++ b/src/producer.hpp @@ -1,5 +1,35 @@ #ifndef TEXO_INCLUDED_PRODUCER #define TEXO_INCLUDED_PRODUCER +#include "texo.hpp" +#include "exporter.hpp" + + +class TexoProducer { +public: + TexoProducer(TexoExporter &exporter); + + virtual void Put(const Texo &piece); + + virtual void Put(const TexoHeader &piece); + virtual void Put(const TexoParagraph &piece); + virtual void Put(const TexoCode &piece); + virtual void Put(const TexoQuote &piece); + + virtual void Put(const TexoMono &piece); + virtual void Put(const TexoBold &piece); + virtual void Put(const TexoItalic &piece); + virtual void Put(const TexoUnderline &piece); + virtual void Put(const TexoStrike &piece); + + virtual void Put(const TexoImage &piece); + virtual void Put(const TexoLink &piece); + virtual void Put(const TexoLineBreak &piece); + virtual void Put(const TexoHorizontalRule &piece); + +protected: + TexoExporter &exporter; +}; + #endif diff --git a/src/texo.cpp b/src/texo.cpp index aefff4c..fa4b4be 100644 --- a/src/texo.cpp +++ b/src/texo.cpp @@ -1 +1,32 @@ #include "texo.hpp" + + +Texo::Texo(char c): c(c) +{} + +TexoHeader::TexoHeader(int level, bool closing): level(level), closing(closing) +{} + +TexoParagraph::TexoParagraph(bool closing): closing(closing) +{} + +TexoCode::TexoCode(bool closing): closing(closing) +{} + +TexoQuote::TexoQuote(bool closing): closing(closing) +{} + +TexoMono::TexoMono(bool closing): closing(closing) +{} + +TexoBold::TexoBold(bool closing): closing(closing) +{} + +TexoItalic::TexoItalic(bool closing): closing(closing) +{} + +TexoUnderline::TexoUnderline(bool closing): closing(closing) +{} + +TexoStrike::TexoStrike(bool closing): closing(closing) +{} diff --git a/src/texo.hpp b/src/texo.hpp index 93cad3a..e2a7d86 100644 --- a/src/texo.hpp +++ b/src/texo.hpp @@ -1,5 +1,87 @@ #ifndef TEXO_INCLUDED_TEXO #define TEXO_INCLUDED_TEXO +#include + + +struct Texo { + Texo(char c); + char c; +}; + +/* + * Texo Blocks + */ +struct TexoHeader { + TexoHeader(int level, bool closing = false); + int level; // Number >= 1, bigger for bigger. + bool closing; +}; + +struct TexoParagraph { + TexoParagraph(bool closing = false); + bool closing; +}; + +struct TexoCode { + TexoCode(bool closing = false); + bool closing; +}; + +struct TexoQuote { + TexoQuote(bool closing = false); + bool closing; +}; + +/* + * Texo Decorators + */ +struct TexoMono { + TexoMono(bool closing = false); + bool closing; +}; + +struct TexoBold { + TexoBold(bool closing = false); + bool closing; +}; + +struct TexoItalic { + TexoItalic(bool closing = false); + bool closing; +}; + +struct TexoUnderline { + TexoUnderline(bool closing = false); + bool closing; +}; + +struct TexoStrike { + TexoStrike(bool closing = false); + bool closing; +}; + +/* + * Texo Signals + */ +struct TexoImage { + ScriptVariable path; + ScriptVariable title; + ScriptVariable alt; + ScriptVariable link; +}; + +struct TexoLink { + ScriptVariable text; + ScriptVariable link; + ScriptVariable title; +}; + +struct TexoLineBreak { +}; + +struct TexoHorizontalRule { +}; + #endif -- cgit v1.2.3