From 2eae362fd3f50325067fa3d7e13db6882df80e76 Mon Sep 17 00:00:00 2001 From: Aleksey Veresov Date: Fri, 27 Sep 2019 18:09:07 +0300 Subject: Texo initial. --- example/makefile | 24 ++++++++++++++++++ example/plain_to_html.cpp | 23 ++++++++++++++++++ license | 9 +++++++ makefile | 35 ++++++++++++++++++++++++++ src/file.cpp | 9 +++++++ src/file.hpp | 16 ++++++++++++ src/html.cpp | 27 +++++++++++++++++++++ src/html.hpp | 21 ++++++++++++++++ src/plain.cpp | 55 +++++++++++++++++++++++++++++++++++++++++ src/plain.hpp | 25 +++++++++++++++++++ src/texo.cpp | 44 +++++++++++++++++++++++++++++++++ src/texo.hpp | 62 +++++++++++++++++++++++++++++++++++++++++++++++ 12 files changed, 350 insertions(+) create mode 100644 example/makefile create mode 100644 example/plain_to_html.cpp create mode 100644 license create mode 100644 makefile create mode 100644 src/file.cpp create mode 100644 src/file.hpp create mode 100644 src/html.cpp create mode 100644 src/html.hpp create mode 100644 src/plain.cpp create mode 100644 src/plain.hpp create mode 100644 src/texo.cpp create mode 100644 src/texo.hpp 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 +#include +#include +#include +#include +#include + + +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("

"); break; + case Texo::paragraph_end: exporter.PutStr("

"); break; + case Texo::newline: exporter.PutStr("
"); + 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 + + +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 -- cgit v1.2.3