summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAleksey Veresov <aleksey@veresov.pro>2019-10-18 17:33:06 +0300
committerAleksey Veresov <aleksey@veresov.pro>2019-10-18 17:33:06 +0300
commit2c12c0652d2b8c8440e1e908f004826840ed14ab (patch)
tree881dd649f2b15ba5a8cd3aa285d073eee7c73386 /src
parent9a23fd146be4ba64b0cb720993bdc73a514fecf2 (diff)
downloadtexo-2c12c0652d2b8c8440e1e908f004826840ed14ab.tar
texo-2c12c0652d2b8c8440e1e908f004826840ed14ab.tar.xz
texo-2c12c0652d2b8c8440e1e908f004826840ed14ab.zip
[texo] Semi-working.
Diffstat (limited to 'src')
-rw-r--r--src/html.cpp173
-rw-r--r--src/html.hpp21
-rw-r--r--src/importer.cpp21
-rw-r--r--src/importer.hpp16
-rw-r--r--src/plain.cpp174
-rw-r--r--src/plain.hpp36
-rw-r--r--src/producer.cpp25
-rw-r--r--src/producer.hpp30
-rw-r--r--src/texo.cpp31
-rw-r--r--src/texo.hpp82
10 files changed, 607 insertions, 2 deletions
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("&lt;"); break;
+ case '>': exporter.Put("&gt;"); 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("</p>");
+ } else {
+ exporter.Put("<p>");
+ }
+}
+
+void TexoProducerHTML::Put(const TexoCode &piece)
+{
+ if (piece.closing) {
+ exporter.Put("</pre>");
+ } else {
+ exporter.Put("<pre>");
+ }
+}
+
+void TexoProducerHTML::Put(const TexoQuote &piece)
+{
+ if (piece.closing) {
+ exporter.Put("</p></blockquote>");
+ } else {
+ exporter.Put("<blockquote><p>");
+ }
+}
+
+void TexoProducerHTML::Put(const TexoMono &piece)
+{
+ if (piece.closing) {
+ exporter.Put("</code>");
+ } else {
+ exporter.Put("<code>");
+ }
+}
+
+void TexoProducerHTML::Put(const TexoBold &piece)
+{
+ if (piece.closing) {
+ exporter.Put("</b>");
+ } else {
+ exporter.Put("<b>");
+ }
+}
+
+void TexoProducerHTML::Put(const TexoItalic &piece)
+{
+ if (piece.closing) {
+ exporter.Put("</i>");
+ } else {
+ exporter.Put("<i>");
+ }
+}
+
+void TexoProducerHTML::Put(const TexoUnderline &piece)
+{
+ if (piece.closing) {
+ exporter.Put("</ins>");
+ } else {
+ exporter.Put("<ins>");
+ }
+}
+
+void TexoProducerHTML::Put(const TexoStrike &piece)
+{
+ if (piece.closing) {
+ exporter.Put("</del>");
+ } else {
+ exporter.Put("<del>");
+ }
+}
+
+void TexoProducerHTML::Put(const TexoImage &piece)
+{
+ if (piece.path != "") {
+ bool link = piece.link != "";
+ bool title = piece.title != "";
+ if (link) {
+ exporter.Put("<a href='");
+ exporter.Put(piece.link);
+ if (title) {
+ exporter.Put("' title='");
+ exporter.Put(piece.title);
+ }
+ exporter.Put("'>");
+ }
+ exporter.Put("<img src='");
+ exporter.Put(piece.path);
+ if (piece.alt != "") {
+ exporter.Put("' alt='");
+ exporter.Put(piece.alt);
+ }
+ if (title) {
+ exporter.Put("' title='");
+ exporter.Put(piece.title);
+ }
+ exporter.Put("'/>");
+ if (link) {
+ exporter.Put("</a>");
+ }
+ }
+}
+
+void TexoProducerHTML::Put(const TexoLink &piece)
+{
+ if (piece.text != "" && piece.link != "") {
+ exporter.Put("<a href='");
+ exporter.Put(piece.link);
+ if (piece.title != "") {
+ exporter.Put("' title='");
+ exporter.Put(piece.title);
+ }
+ exporter.Put("'>");
+ exporter.Put(piece.text);
+ exporter.Put("</a>");
+ }
+}
+
+void TexoProducerHTML::Put(const TexoLineBreak &piece)
+{
+ exporter.Put("<br/>");
+}
+
+void TexoProducerHTML::Put(const TexoHorizontalRule &piece)
+{
+ exporter.Put("<hr/>");
+}
+
+
+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 <stdio.h>
+
+
+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 <scrvar.hpp>
+
+
+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