summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAleksey Veresov <aleksey@veresov.pro>2019-09-27 18:09:07 +0300
committerAleksey Veresov <aleksey@veresov.pro>2019-09-27 18:09:07 +0300
commit2eae362fd3f50325067fa3d7e13db6882df80e76 (patch)
treea6eff5f8f12b990e3d256d4b02fdb86d70d81eac /src
downloadtexo-2eae362fd3f50325067fa3d7e13db6882df80e76.tar
texo-2eae362fd3f50325067fa3d7e13db6882df80e76.tar.xz
texo-2eae362fd3f50325067fa3d7e13db6882df80e76.zip
Texo initial.
Diffstat (limited to 'src')
-rw-r--r--src/file.cpp9
-rw-r--r--src/file.hpp16
-rw-r--r--src/html.cpp27
-rw-r--r--src/html.hpp21
-rw-r--r--src/plain.cpp55
-rw-r--r--src/plain.hpp25
-rw-r--r--src/texo.cpp44
-rw-r--r--src/texo.hpp62
8 files changed, 259 insertions, 0 deletions
diff --git a/src/file.cpp b/src/file.cpp
new file mode 100644
index 0000000..845c101
--- /dev/null
+++ b/src/file.cpp
@@ -0,0 +1,9 @@
+#include "file.hpp"
+
+
+TexoFileExporter::TexoFileExporter(FILE *file): file(file) {}
+
+void TexoFileExporter::Put(const char c)
+{
+ fputc(c, file);
+}
diff --git a/src/file.hpp b/src/file.hpp
new file mode 100644
index 0000000..68e31cd
--- /dev/null
+++ b/src/file.hpp
@@ -0,0 +1,16 @@
+#ifndef TEXO_INCLUDED_FILE
+#define TEXO_INCLUDED_FILE
+
+#include "texo.hpp"
+
+
+class TexoFileExporter: public TexoExporter {
+public:
+ TexoFileExporter(FILE *file);
+ void Put(const char c);
+private:
+ FILE *file;
+};
+
+
+#endif
diff --git a/src/html.cpp b/src/html.cpp
new file mode 100644
index 0000000..22209f5
--- /dev/null
+++ b/src/html.cpp
@@ -0,0 +1,27 @@
+#include "html.hpp"
+
+
+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/>");
+ default: break;
+ }
+}
+
+
+TexoHTMLImporter::TexoHTMLImporter(TexoProducer &producer):
+ TexoImporter(producer)
+{}
+
+void TexoHTMLImporter::Put(const char c)
+{
+ producer.Put(Texo(c));
+}
diff --git a/src/html.hpp b/src/html.hpp
new file mode 100644
index 0000000..c5b2aa4
--- /dev/null
+++ b/src/html.hpp
@@ -0,0 +1,21 @@
+#ifndef TEXO_INCLUDED_HTML
+#define TEXO_INCLUDED_HTML
+
+#include "texo.hpp"
+
+
+class TexoHTMLProducer: public TexoProducer {
+public:
+ TexoHTMLProducer(TexoExporter &exporter);
+ void Put(const Texo &piece);
+};
+
+
+class TexoHTMLImporter: public TexoImporter {
+public:
+ TexoHTMLImporter(TexoProducer &producer);
+ void Put(const char c);
+};
+
+
+#endif
diff --git a/src/plain.cpp b/src/plain.cpp
new file mode 100644
index 0000000..d8bc1b1
--- /dev/null
+++ b/src/plain.cpp
@@ -0,0 +1,55 @@
+#include "plain.hpp"
+
+
+TexoPlainProducer::TexoPlainProducer(TexoExporter &exporter):
+ TexoProducer(exporter)
+{}
+
+void TexoPlainProducer::Put(const Texo &piece)
+{
+ switch (piece.type) {
+ case Texo::character: exporter.Put(piece.c);
+ default: break;
+ }
+}
+
+
+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
new file mode 100644
index 0000000..c5c613c
--- /dev/null
+++ b/src/plain.hpp
@@ -0,0 +1,25 @@
+#ifndef TEXO_INCLUDED_PLAIN
+#define TEXO_INCLUDED_PLAIN
+
+#include "texo.hpp"
+
+
+class TexoPlainProducer: public TexoProducer {
+public:
+ TexoPlainProducer(TexoExporter &exporter);
+ void Put(const Texo &piece);
+};
+
+
+class TexoPlainImporter: public TexoImporter {
+public:
+ TexoPlainImporter(TexoProducer &producer);
+ void Put(const char c);
+private:
+ int space;
+ int newline;
+ bool first;
+};
+
+
+#endif
diff --git a/src/texo.cpp b/src/texo.cpp
new file mode 100644
index 0000000..1a2974a
--- /dev/null
+++ b/src/texo.cpp
@@ -0,0 +1,44 @@
+#include "texo.hpp"
+
+
+Texo::Texo(Type type): type(type), c(0) {}
+
+Texo::Texo(const char c): type(character), c(c) {}
+
+
+void TexoExporter::PutStr(const char *str)
+{
+ if (str) {
+ while (*str) {
+ Put(*str);
+ ++str;
+ }
+ }
+}
+
+
+TexoProducer::TexoProducer(TexoExporter &exporter): exporter(exporter) {}
+
+
+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
new file mode 100644
index 0000000..2129f61
--- /dev/null
+++ b/src/texo.hpp
@@ -0,0 +1,62 @@
+#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;
+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