summaryrefslogtreecommitdiff
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
downloadtexo-2eae362fd3f50325067fa3d7e13db6882df80e76.tar
texo-2eae362fd3f50325067fa3d7e13db6882df80e76.tar.xz
texo-2eae362fd3f50325067fa3d7e13db6882df80e76.zip
Texo initial.
-rw-r--r--example/makefile24
-rw-r--r--example/plain_to_html.cpp23
-rw-r--r--license9
-rw-r--r--makefile35
-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
12 files changed, 350 insertions, 0 deletions
diff --git a/example/makefile b/example/makefile
new file mode 100644
index 0000000..5255f5b
--- /dev/null
+++ b/example/makefile
@@ -0,0 +1,24 @@
+# Uncomment following to enable debug mode:
+# DEBUG = yes
+
+CC = g++
+EXAMPLES = plain_to_html
+
+CFLAGS = -Wall
+ifeq '$(DEBUG)' 'yes'
+CFLAGS += -g -O0
+else
+CFLAGS += -O3
+endif
+
+INCLUDE = -I ../src
+LFLAGS = -L.. -ltexo
+
+
+default: $(EXAMPLES)
+
+%: %.cpp
+ $(CC) $(CFLAGS) $(INCLUDE) $< $(LFLAGS) -o $@
+
+clean:
+ rm -f $(EXAMPLES)
diff --git a/example/plain_to_html.cpp b/example/plain_to_html.cpp
new file mode 100644
index 0000000..86171a6
--- /dev/null
+++ b/example/plain_to_html.cpp
@@ -0,0 +1,23 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <html.hpp>
+#include <plain.hpp>
+#include <file.hpp>
+
+
+int main()
+{
+ TexoFileExporter exporter(stdout);
+ TexoHTMLProducer producer(exporter);
+ TexoPlainImporter importer(producer);
+ importer.PutStr(
+ "I am a little cute line. \n"
+ "I am another and that's fine. \n"
+ "But I am as long as a pine,\n"
+ "so lets drink a glass of wine.\n\n"
+ "Second paragraph was started here. \n"
+ "Take your bananas and go home, seer.\n"
+ );
+ return 0;
+}
diff --git a/license b/license
new file mode 100644
index 0000000..8f83b9c
--- /dev/null
+++ b/license
@@ -0,0 +1,9 @@
+Copyright 2019 Aleksey Veresov
+
+This software is provided 'as-is', without any express or implied warranty.
+In no event will the authors be held liable for any damages arising from
+the use of this software.
+
+Permission to use, copy, modify, and distribute this software for any
+purpose with or without fee is hereby granted, provided that the above
+copyright notice and this permission notice appear in all copies.
diff --git a/makefile b/makefile
new file mode 100644
index 0000000..68c986c
--- /dev/null
+++ b/makefile
@@ -0,0 +1,35 @@
+# Debug mode:
+# DEBUG = yes
+
+CC = g++
+LIB = libtexo.a
+
+CFLAGS = -Wall
+ifeq '$(DEBUG)' 'yes'
+CFLAGS += -g -O0
+else
+CFLAGS += -O3
+endif
+
+SRC_DIR = src
+SRC = $(wildcard $(SRC_DIR)/*.cpp)
+OBJ = $(SRC:.cpp=.o)
+
+
+default: $(LIB)
+
+ifneq "clean" "$(MAKECMDGOALS)"
+-include deps.mk
+endif
+
+deps.mk: $(SRC)
+ $(CC) -MM $^ > $@
+
+%.o: %.cpp %.h
+ $(CC) $(CFLAGS) -c $< -o $@
+
+$(LIB): $(OBJ)
+ ar rcs $@ $^
+
+clean:
+ rm -f $(OBJ) $(LIB) deps.mk
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