From ed7a63015c1583d5544ac18bbaccaedc95f3e5e3 Mon Sep 17 00:00:00 2001 From: Aleksey Veresov Date: Fri, 8 Nov 2019 19:49:40 +0300 Subject: [texo] Full rewrite of inner representation. --- src/plain.cpp | 132 +++++++++++++++++++++++++++++++++------------------------- 1 file changed, 76 insertions(+), 56 deletions(-) (limited to 'src/plain.cpp') diff --git a/src/plain.cpp b/src/plain.cpp index 8b3d470..9e9280e 100644 --- a/src/plain.cpp +++ b/src/plain.cpp @@ -1,82 +1,100 @@ #include "plain.hpp" +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * Texo Plain Producer + */ TexoProducerPlain::TexoProducerPlain(TexoExporter &exporter): TexoProducer(exporter), quoted(false), newline(false), nospace(true) {} -void TexoProducerPlain::End() + +bool TexoProducerPlain::End() { if (!newline) { exporter.Put('\n'); newline = true; } + return true; } -void TexoProducerPlain::Put(const Texo &piece) + +bool TexoProducerPlain::Put(char c) { - if (piece.c == '\n') { + if (c == '\n') { if (quoted) { - exporter.Put("\n> "); + return exporter.Put("\n> "); } else if (!nospace) { if (newline) { - exporter.Put(' '); + return exporter.Put(' '); } else { - exporter.Put('\n'); newline = true; + return exporter.Put('\n'); } + } else { + return true; } } else { - exporter.Put(piece.c); newline = false; nospace = false; + return exporter.Put(c); } } -void TexoProducerPlain::Put(const TexoParagraph &piece) + +bool TexoProducerPlain::Paragraph() { + quoted = false; if (!nospace) { - exporter.Put("\n\n"); + nospace = true; + return exporter.Put("\n\n"); + } else { + return true; } - quoted = false; - nospace = true; } -void TexoProducerPlain::Put(const TexoQuote &piece) +bool TexoProducerPlain::Quote() { + quoted = true; if (!nospace) { - exporter.Put("\n\n"); + return exporter.Put("\n\n> "); + } else { + return exporter.Put("> "); } - exporter.Put("> "); - quoted = true; } -void TexoProducerPlain::Put(const TexoHorizontalRule &piece) +bool TexoProducerPlain::PutHorizontalRule() { + bool ok = true; if (!newline && !nospace) { - exporter.Put('\n'); + ok = exporter.Put('\n'); } - exporter.Put("--------------------------------------------------\n"); nospace = true; + return ok && exporter.Put( + "--------------------------------------------------\n" + ); } +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * Texo Plain Importer + */ TexoImporterPlain::TexoImporterPlain(TexoProducer &producer): TexoImporter(producer), state(text) -{ - producer.Put(TexoParagraph()); -} +{} + -void TexoImporterPlain::End() +bool TexoImporterPlain::TrueEnd() { if (state == rule) { - producer.Put(TexoHorizontalRule()); + ok = producer.PutHorizontalRule(); state = text; } - producer.End(); + return ok && producer.End(); } -void TexoImporterPlain::Put(char c) + +bool TexoImporterPlain::TruePut(char c) { switch (state) { case text: Text(c); break; @@ -88,23 +106,15 @@ void TexoImporterPlain::Put(char c) case rule: Rule(c); break; case paragraph_rule: ParagraphRule(c); break; } + return ok; } -void TexoImporterPlain::Put(const ScriptVariable &str) -{ - TexoImporter::Put(str); -} - -void TexoImporterPlain::Put(FILE *file) -{ - TexoImporter::Put(file); -} void TexoImporterPlain::Text(char c) { switch (c) { - case '\n': state = newline; break; - default: producer.Put(c); break; + case '\n': state = newline; break; + default: ok = producer.Put(c); break; } } @@ -114,26 +124,30 @@ void TexoImporterPlain::Newline(char c) state = paragraph; } else if (c == '-') { dash_count = 1; - state = rule; + state = rule; } else { - producer.Put('\n'); state = text; - Text(c); + ok = producer.Put('\n'); + if (ok) { + Text(c); + } } } void TexoImporterPlain::Paragraph(char c) { if (c == '>') { - producer.Put(TexoQuote()); + ok = producer.Quote(); state = quote_pre; } else if (c == '-') { dash_count = 1; - state = paragraph_rule; + state = paragraph_rule; } else if (c != '\n') { - producer.Put(TexoParagraph()); state = text; - Text(c); + ok = producer.Paragraph(); + if (ok) { + Text(c); + } } } @@ -148,49 +162,55 @@ void TexoImporterPlain::QuotePre(char c) void TexoImporterPlain::Quote(char c) { switch (c) { - case '\n': state = quote_newline; break; - default: producer.Put(c); break; + case '\n': state = quote_newline; break; + default: ok = producer.Put(c); break; } } void TexoImporterPlain::QuoteNewline(char c) { if (c == '>') { - producer.Put('\n'); + ok = producer.Put('\n'); state = quote_pre; } else if (c == '\n') { state = paragraph; } else if (c == '-') { dash_count = 1; - state = paragraph_rule; + state = paragraph_rule; } else { - producer.Put(TexoParagraph()); state = text; - Text(c); + ok = producer.Paragraph(); + if (ok) { + Text(c); + } } } void TexoImporterPlain::Rule(char c) { if (c == '\n') { - producer.Put(TexoHorizontalRule()); + ok = producer.PutHorizontalRule(); state = text; } else if (c == '-') { ++dash_count; } else { - producer.Put('\n'); - for (;dash_count > 0; --dash_count) { - producer.Put('-'); + ok = producer.Put('\n'); + for (; ok && dash_count > 0; --dash_count) { + ok = producer.Put('-'); } state = text; - Text(c); + if (ok) { + Text(c); + } } } void TexoImporterPlain::ParagraphRule(char c) { if (c == '\n') { - producer.Put(TexoParagraph()); + ok = producer.Paragraph(); + } + if (ok) { + Rule(c); } - Rule(c); } -- cgit v1.2.3