summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAleksey Veresov <aleksey@veresov.pro>2019-10-18 12:37:46 +0300
committerAleksey Veresov <aleksey@veresov.pro>2019-10-18 12:37:46 +0300
commit9a23fd146be4ba64b0cb720993bdc73a514fecf2 (patch)
tree9c8d9554d37f7ab25087e1468c012fb41a6e39ee /src
parent7b09f265c1deb12c9e6778c4d92473d66fc5406b (diff)
downloadtexo-9a23fd146be4ba64b0cb720993bdc73a514fecf2.tar
texo-9a23fd146be4ba64b0cb720993bdc73a514fecf2.tar.xz
texo-9a23fd146be4ba64b0cb720993bdc73a514fecf2.zip
.
Diffstat (limited to 'src')
-rw-r--r--src/exporter.cpp10
-rw-r--r--src/exporter.hpp14
-rw-r--r--src/file.cpp10
-rw-r--r--src/file.hpp11
-rw-r--r--src/html.cpp352
-rw-r--r--src/html.hpp73
-rw-r--r--src/importer.cpp1
-rw-r--r--src/importer.hpp5
-rw-r--r--src/plain.cpp41
-rw-r--r--src/plain.hpp17
-rw-r--r--src/producer.cpp1
-rw-r--r--src/producer.hpp5
-rw-r--r--src/str.cpp15
-rw-r--r--src/str.hpp19
-rw-r--r--src/texo.cpp57
-rw-r--r--src/texo.hpp60
16 files changed, 102 insertions, 589 deletions
diff --git a/src/exporter.cpp b/src/exporter.cpp
new file mode 100644
index 0000000..3baa99e
--- /dev/null
+++ b/src/exporter.cpp
@@ -0,0 +1,10 @@
+#include "exporter.hpp"
+
+
+void TexoExporter::Put(const ScriptVariable &str)
+{
+ const int len = str.Length();
+ for (int i = 0; i < len; ++i) {
+ Put(str[i]);
+ }
+}
diff --git a/src/exporter.hpp b/src/exporter.hpp
new file mode 100644
index 0000000..6a0be93
--- /dev/null
+++ b/src/exporter.hpp
@@ -0,0 +1,14 @@
+#ifndef TEXO_INCLUDED_EXPORTER
+#define TEXO_INCLUDED_EXPORTER
+
+#include <scrvar.hpp>
+
+
+class TexoExporter {
+public:
+ virtual void Put(char c) = 0;
+ virtual void Put(const ScriptVariable &str);
+};
+
+
+#endif
diff --git a/src/file.cpp b/src/file.cpp
index 845c101..ae41bde 100644
--- a/src/file.cpp
+++ b/src/file.cpp
@@ -1,9 +1,15 @@
#include "file.hpp"
-TexoFileExporter::TexoFileExporter(FILE *file): file(file) {}
+TexoExporterFile::TexoExporterFile(FILE *file): file(file)
+{}
-void TexoFileExporter::Put(const char c)
+void TexoExporterFile::Put(char c)
{
fputc(c, file);
}
+
+void TexoExporterFile::Put(const ScriptVariable &str)
+{
+ fwrite(str.c_str(), 1, str.Length(), file);
+}
diff --git a/src/file.hpp b/src/file.hpp
index c867bcc..1cfea0b 100644
--- a/src/file.hpp
+++ b/src/file.hpp
@@ -1,13 +1,16 @@
#ifndef TEXO_INCLUDED_FILE
#define TEXO_INCLUDED_FILE
-#include "texo.hpp"
+#include "exporter.hpp"
+#include <stdio.h>
-class TexoFileExporter: public TexoExporter {
+class TexoExporterFile: public TexoExporter {
public:
- TexoFileExporter(FILE *file);
- void Put(const char c);
+ TexoExporterFile(FILE *file);
+
+ void Put(char c);
+ void Put(const ScriptVariable &str);
private:
FILE *file;
diff --git a/src/html.cpp b/src/html.cpp
index 76e9fb9..290891e 100644
--- a/src/html.cpp
+++ b/src/html.cpp
@@ -1,353 +1 @@
#include "html.hpp"
-
-#include <string.h>
-
-
-TexoHTMLProducer::TexoHTMLProducer(TexoExporter &exporter):
- TexoProducer(exporter)
-{}
-
-void TexoHTMLProducer::Put(const Texo &piece)
-{
- switch (piece.type) {
- case Texo::character: exporter.Put(piece.c); break;
- case Texo::paragraph_begin: exporter.PutStr("<p>"); break;
- case Texo::paragraph_end: exporter.PutStr("</p>"); break;
- case Texo::newline: exporter.PutStr("<br/>"); break;
- case Texo::bold_begin: exporter.PutStr("<b>"); break;
- case Texo::bold_end: exporter.PutStr("</b>"); break;
- case Texo::italic_begin: exporter.PutStr("<i>"); break;
- case Texo::italic_end: exporter.PutStr("</i>"); break;
- case Texo::strike_begin: exporter.PutStr("<del>"); break;
- case Texo::strike_end: exporter.PutStr("</del>"); break;
- case Texo::underline_begin: exporter.PutStr("<ins>"); break;
- case Texo::underline_end: exporter.PutStr("</ins>"); break;
- case Texo::link_begin: BeginLink(piece); break;
- case Texo::link_end: exporter.PutStr("</a>"); break;
- case Texo::image: Image(piece); break;
- default: break;
- }
-}
-
-void TexoHTMLProducer::BeginLink(const Texo &piece)
-{
- exporter.PutStr("<a");
- if (piece.link_url) {
- exporter.PutStr(" href='");
- exporter.PutStr(piece.link_url);
- exporter.Put('\'');
- }
- exporter.Put('>');
-}
-
-void TexoHTMLProducer::Image(const Texo &piece)
-{
- exporter.PutStr("<img");
- if (piece.image_src) {
- exporter.PutStr(" src='");
- exporter.PutStr(piece.image_src);
- exporter.Put('\'');
- }
- if (piece.image_alt) {
- exporter.PutStr(" alt='");
- exporter.PutStr(piece.image_alt);
- exporter.Put('\'');
- }
- exporter.PutStr("/>");
-}
-
-
-TexoHTMLStack::TexoHTMLStack(): next(0)
-{}
-
-TexoHTMLStack::~TexoHTMLStack()
-{
- if (next) {
- while (next != this) {
- Pop();
- }
- }
-}
-
-void TexoHTMLStack::Push(Texo::Type tag)
-{
- if (next) {
- TexoHTMLStack *buf = next;
- next = new TexoHTMLStack;
- next->next = buf;
- next->tag = this->tag;
- } else {
- next = this;
- }
- this->tag = tag;
-}
-
-Texo::Type TexoHTMLStack::Pop()
-{
- if (next) {
- Texo::Type res = tag;
- if (next != this) {
- TexoHTMLStack *buf = next;
- tag = next->tag;
- next = next->next;
- delete buf;
- } else {
- next = 0;
- }
- return res;
- } else {
- return Texo::character;
- }
-}
-
-
-TexoHTMLImporter::TexoHTMLImporter(TexoProducer &producer, bool is_autoclose):
- TexoImporter(producer), state(text), is_closing(0), tag(Texo::character),
- readed(0), readed_len(0), readed_size(0), is_autoclose(is_autoclose)
-{}
-
-TexoHTMLImporter::~TexoHTMLImporter()
-{
- ReadedClear();
-}
-
-void TexoHTMLImporter::Put(const char c)
-{
- if (c) {
- switch (state) {
- case text: state = Text(c); break;
- case tag_open: state = TagOpen(c); break;
- case tag_name: state = TagName(c); break;
- case tag_param_skip: state = TagParamSkip(c); break;
- case tag_param_pre: state = TagParamPre(c); break;
- case tag_param: state = TagParam(c); break;
- case tag_param_post: state = TagParamPost(c); break;
- case tag_param_value_pre: state = TagParamValuePre(c); break;
- case tag_param_value: state = TagParamValue(c); break;
- case tag_param_value_quoted: state = TagParamValueQuoted(c); break;
- default: break;
- }
- } else {
- tag = Texo::character;
- Close();
- }
-}
-
-TexoHTMLImporter::State TexoHTMLImporter::Text(const char c)
-{
- State st = text;
- switch (c) {
- case '<': st = tag_open; break;
- case '>': producer.PutStr("&gt;"); break;
- default: producer.Put(Texo(c)); break;
- }
- return st;
-}
-
-TexoHTMLImporter::State TexoHTMLImporter::TagOpen(const char c)
-{
- State st = tag_open;
- switch (c) {
- case '/':
- is_closing = 1;
- break;
- case ' ':
- st = text;
- producer.PutStr("&lt;");
- if (is_closing) {
- producer.Put(Texo('/'));
- }
- producer.Put(Texo(' '));
- break;
- case '>':
- st = text;
- producer.PutStr("&lt;");
- if (is_closing) {
- producer.Put(Texo('/'));
- }
- producer.PutStr("&gt;");
- break;
- default:
- st = tag_name;
- ReadedAdd(c);
- break;
- }
- return st;
-}
-
-TexoHTMLImporter::State TexoHTMLImporter::TagName(const char c)
-{
- State st = tag_name;
- switch (c) {
- case ' ':
- st = tag_param_skip;
- SetTag();
- break;
- case '/':
- st = tag_param_skip;
- SetTag();
- break;
- case '>':
- st = text;
- SetTag();
- if (tag) {
- if (is_closing) {
- Close();
- } else {
- producer.Put(Texo(tag));
- }
- }
- is_closing = false;
- break;
- default:
- ReadedAdd(c);
- break;
- }
- return st;
-}
-
-TexoHTMLImporter::State TexoHTMLImporter::TagParamSkip(const char c)
-{
- State st = tag_param_skip;
- switch (c) {
- case '>':
- st = text;
- if (tag) {
- if (is_closing) {
- Close();
- } else {
- producer.Put(Texo(tag));
- }
- }
- is_closing = false;
- break;
- default:
- break;
- }
- return st;
-}
-
-TexoHTMLImporter::State TexoHTMLImporter::TagParamPre(const char c)
-{
- return text;
-}
-
-TexoHTMLImporter::State TexoHTMLImporter::TagParam(const char c)
-{
- return text;
-}
-
-TexoHTMLImporter::State TexoHTMLImporter::TagParamPost(const char c)
-{
- return text;
-}
-
-TexoHTMLImporter::State TexoHTMLImporter::TagParamValuePre(const char c)
-{
- return text;
-}
-
-TexoHTMLImporter::State TexoHTMLImporter::TagParamValue(const char c)
-{
- return text;
-}
-
-TexoHTMLImporter::State TexoHTMLImporter::TagParamValueQuoted(const char c)
-{
- return text;
-}
-
-void TexoHTMLImporter::Close()
-{
- Texo::Type current = opened.Pop();
- while ((current != tag) && current) {
- producer.Put(Texo(current));
- current = opened.Pop();
- }
- if (current) {
- producer.Put(Texo(current));
- }
-}
-
-void TexoHTMLImporter::SetTag()
-{
- if (!strcmp(readed, "a")) {
- if (is_closing) {
- tag = Texo::link_end;
- } else {
- tag = Texo::link_begin;
- opened.Push(Texo::link_end);
- }
- } else if (!strcmp(readed, "img")) {
- tag = Texo::image;
- } else if (!strcmp(readed, "b")) {
- if (is_closing) {
- tag = Texo::bold_end;
- } else {
- tag = Texo::bold_begin;
- opened.Push(Texo::bold_end);
- }
- } else if (!strcmp(readed, "i")) {
- if (is_closing) {
- tag = Texo::italic_end;
- } else {
- tag = Texo::italic_begin;
- opened.Push(Texo::italic_end);
- }
- } else if (!strcmp(readed, "del")) {
- if (is_closing) {
- tag = Texo::strike_end;
- } else {
- tag = Texo::strike_begin;
- opened.Push(Texo::strike_end);
- }
- } else if (!strcmp(readed, "ins")) {
- if (is_closing) {
- tag = Texo::underline_end;
- } else {
- tag = Texo::underline_begin;
- opened.Push(Texo::underline_end);
- }
- } else if (!strcmp(readed, "p")) {
- if (is_closing) {
- tag = Texo::paragraph_end;
- } else {
- tag = Texo::paragraph_begin;
- opened.Push(Texo::paragraph_end);
- }
- } else if (!strcmp(readed, "br")) {
- tag = Texo::newline;
- } else {
- tag = Texo::character;
- }
- ReadedClear();
-}
-
-void TexoHTMLImporter::ReadedAdd(const char c)
-{
- if (readed_size == 0) {
- readed_size = 2;
- readed = new char[readed_size];
- } else if (readed_size - 1 == readed_len) {
- char *buf = readed;
- readed_size = readed_size << 1;
- readed = new char[readed_size];
- for (int i = 0; i < readed_len; ++i) {
- readed[i] = buf[i];
- }
- delete buf;
- }
- ++readed_len;
- readed[readed_len - 1] = c;
- readed[readed_len] = 0;
-}
-
-void TexoHTMLImporter::ReadedClear()
-{
- readed_len = 0;
- readed_size = 0;
- if (readed) {
- delete readed;
- readed = 0;
- }
-}
diff --git a/src/html.hpp b/src/html.hpp
index 7a5a511..a904d56 100644
--- a/src/html.hpp
+++ b/src/html.hpp
@@ -1,80 +1,21 @@
#ifndef TEXO_INCLUDED_HTML
#define TEXO_INCLUDED_HTML
+#include "exporter.hpp"
+#include "producer.hpp"
+#include "importer.hpp"
#include "texo.hpp"
-class TexoHTMLProducer: public TexoProducer {
+class TexoProducerHTML: public TexoProducer {
public:
- TexoHTMLProducer(TexoExporter &exporter);
- void Put(const Texo &piece);
-
-private:
- void BeginLink(const Texo &piece);
- void Image(const Texo &piece);
+ TexoProducerHTML(TexoExporter &exporter);
};
-class TexoHTMLStack {
+class TexoImporterHTML: public TexoImporter {
public:
- TexoHTMLStack();
- ~TexoHTMLStack();
- void Push(Texo::Type tag);
- Texo::Type Pop();
-
-private:
- TexoHTMLStack *next;
- Texo::Type tag;
-};
-
-class TexoHTMLImporter: public TexoImporter {
-public:
- TexoHTMLImporter(TexoProducer &producer, bool is_autoclose = true);
- ~TexoHTMLImporter();
- void Put(const char c);
-
-private:
- bool is_autoclose;
-
- enum State {
- text = 0,
- tag_open,
- tag_name,
- tag_param_skip,
- tag_param_pre,
- tag_param,
- tag_param_post,
- tag_param_value_pre,
- tag_param_value,
- tag_param_value_quoted
- } state;
-
- State Text(const char c);
- State TagOpen(const char c);
- State TagName(const char c);
- State TagParamSkip(const char c);
- State TagParamPre(const char c);
- State TagParam(const char c);
- State TagParamPost(const char c);
- State TagParamValuePre(const char c);
- State TagParamValue(const char c);
- State TagParamValueQuoted(const char c);
-
- TexoHTMLStack opened;
-
- void Close();
-
- bool is_closing;
- Texo::Type tag;
-
- void SetTag();
-
- char *readed;
- int readed_len;
- int readed_size;
-
- void ReadedAdd(const char c);
- void ReadedClear();
+ TexoImporterHTML(TexoProducer &producer);
};
diff --git a/src/importer.cpp b/src/importer.cpp
new file mode 100644
index 0000000..cb8d62e
--- /dev/null
+++ b/src/importer.cpp
@@ -0,0 +1 @@
+#include "importer.hpp"
diff --git a/src/importer.hpp b/src/importer.hpp
new file mode 100644
index 0000000..81a3b59
--- /dev/null
+++ b/src/importer.hpp
@@ -0,0 +1,5 @@
+#ifndef TEXO_INCLUDED_IMPORTER
+#define TEXO_INCLUDED_IMPORTER
+
+
+#endif
diff --git a/src/plain.cpp b/src/plain.cpp
index db5c63b..b510d58 100644
--- a/src/plain.cpp
+++ b/src/plain.cpp
@@ -1,42 +1 @@
#include "plain.hpp"
-
-
-TexoPlainImporter::TexoPlainImporter(TexoProducer &producer):
- TexoImporter(producer), first(true), space(0), newline(false)
-{}
-
-void TexoPlainImporter::Put(const char c)
-{
- if (first) {
- first = false;
- producer.Put(Texo(Texo::paragraph_begin));
- }
- if (c == ' ') {
- ++space;
- } else if (c == '\n') {
- if (newline) {
- newline = false;
- producer.Put(Texo(Texo::paragraph_end));
- producer.Put(Texo(Texo::paragraph_begin));
- } else {
- newline = true;
- }
- } else if (!c) {
- producer.Put(Texo(Texo::paragraph_end));
- } else {
- if (newline) {
- newline = false;
- if (space) {
- space = 0;
- producer.Put(Texo(Texo::newline));
- } else {
- producer.Put(Texo(' '));
- }
- }
- while (space) {
- producer.Put(Texo(' '));
- --space;
- }
- producer.Put(Texo(c));
- }
-}
diff --git a/src/plain.hpp b/src/plain.hpp
index c88f8ef..d1a9efb 100644
--- a/src/plain.hpp
+++ b/src/plain.hpp
@@ -1,18 +1,21 @@
#ifndef TEXO_INCLUDED_PLAIN
#define TEXO_INCLUDED_PLAIN
+#include "exporter.hpp"
+#include "producer.hpp"
+#include "importer.hpp"
#include "texo.hpp"
-class TexoPlainImporter: public TexoImporter {
+class TexoProducerPlain: public TexoProducer {
public:
- TexoPlainImporter(TexoProducer &producer);
- void Put(const char c);
+ TexoProducerPlain(TexoExporter &exporter);
+};
+
-private:
- int space;
- int newline;
- bool first;
+class TexoImporterPlain: public TexoImporter {
+public:
+ TexoImporterPlain(TexoProducer &producer);
};
diff --git a/src/producer.cpp b/src/producer.cpp
new file mode 100644
index 0000000..959e210
--- /dev/null
+++ b/src/producer.cpp
@@ -0,0 +1 @@
+#include "producer.hpp"
diff --git a/src/producer.hpp b/src/producer.hpp
new file mode 100644
index 0000000..65f6ec3
--- /dev/null
+++ b/src/producer.hpp
@@ -0,0 +1,5 @@
+#ifndef TEXO_INCLUDED_PRODUCER
+#define TEXO_INCLUDED_PRODUCER
+
+
+#endif
diff --git a/src/str.cpp b/src/str.cpp
new file mode 100644
index 0000000..463a8dc
--- /dev/null
+++ b/src/str.cpp
@@ -0,0 +1,15 @@
+#include "str.hpp"
+
+
+TexoExporterString::TexoExporterString(ScriptVariable &str): str(str)
+{}
+
+void TexoExporterString::Put(char c)
+{
+ str += c;
+}
+
+void TexoExporterString::Put(const ScriptVariable &addon)
+{
+ str += addon;
+}
diff --git a/src/str.hpp b/src/str.hpp
new file mode 100644
index 0000000..c7cecfc
--- /dev/null
+++ b/src/str.hpp
@@ -0,0 +1,19 @@
+#ifndef TEXO_INCLUDED_STR
+#define TEXO_INCLUDED_STR
+
+#include "exporter.hpp"
+
+
+class TexoExporterString: public TexoExporter {
+public:
+ TexoExporterString(ScriptVariable &str);
+
+ void Put(char c);
+ void Put(const ScriptVariable &addon);
+
+private:
+ ScriptVariable &str;
+};
+
+
+#endif
diff --git a/src/texo.cpp b/src/texo.cpp
index 3610184..aefff4c 100644
--- a/src/texo.cpp
+++ b/src/texo.cpp
@@ -1,58 +1 @@
#include "texo.hpp"
-
-
-Texo::Texo(Type type):
- type(type), c(0), image_src(0), image_alt(0), link_url(0)
-{}
-
-Texo::Texo(const char c):
- type(character), c(c), image_src(0), image_alt(0), link_url(0)
-{}
-
-
-void TexoExporter::PutStr(const char *str)
-{
- if (str) {
- while (*str) {
- Put(*str);
- ++str;
- }
- }
-}
-
-
-TexoProducer::TexoProducer(TexoExporter &exporter): exporter(exporter) {}
-
-void TexoProducer::PutStr(const char *str)
-{
- if (str) {
- while (*str) {
- Put(Texo(*str));
- ++str;
- }
- }
-}
-
-
-TexoImporter::TexoImporter(TexoProducer &producer): producer(producer) {}
-
-void TexoImporter::PutStr(const char *str)
-{
- if (str) {
- while (*str) {
- Put(*str);
- ++str;
- }
- Put(0);
- }
-}
-
-void TexoImporter::PutFile(FILE *file)
-{
- if (file) {
- for (int c = fgetc(file); c != EOF; c = fgetc(file)) {
- Put(c);
- }
- Put(0);
- }
-}
diff --git a/src/texo.hpp b/src/texo.hpp
index bfea181..93cad3a 100644
--- a/src/texo.hpp
+++ b/src/texo.hpp
@@ -1,65 +1,5 @@
#ifndef TEXO_INCLUDED_TEXO
#define TEXO_INCLUDED_TEXO
-#include <stdio.h>
-
-
-class Texo {
-public:
- enum Type {
- character = 0,
- link_begin,
- link_end,
- image,
- bold_begin,
- bold_end,
- italic_begin,
- italic_end,
- strike_begin,
- strike_end,
- underline_begin,
- underline_end,
- paragraph_begin,
- paragraph_end,
- newline
- } type;
- Texo(Type type);
- Texo(const char c);
- const char c;
- const char *link_url;
- const char *image_src;
- const char *image_alt;
-};
-
-
-class TexoExporter {
-public:
- virtual void Put(const char c) = 0;
- virtual void PutStr(const char *str);
-};
-
-
-class TexoProducer {
-public:
- TexoProducer(TexoExporter &exporter);
- virtual void Put(const Texo &piece) = 0;
- virtual void PutStr(const char *str);
-
-protected:
- TexoExporter &exporter;
-};
-
-
-class TexoImporter {
-public:
- TexoImporter(TexoProducer &producer);
- virtual void Put(const char c) = 0;
- virtual void PutStr(const char *str);
- virtual void PutFile(FILE *file);
-
-protected:
- TexoProducer &producer;
-};
-
#endif