From 1de3a9aae84ec71c4fd83604ea9c45204efb9baf Mon Sep 17 00:00:00 2001 From: Aleksey Veresov Date: Fri, 25 Oct 2019 14:10:45 +0300 Subject: . --- src/plain.cpp | 130 +++++++++++++++++++++++++++++++++------------------------- 1 file changed, 75 insertions(+), 55 deletions(-) (limited to 'src/plain.cpp') diff --git a/src/plain.cpp b/src/plain.cpp index bff9398..d6d262d 100644 --- a/src/plain.cpp +++ b/src/plain.cpp @@ -2,22 +2,23 @@ TexoProducerPlain::TexoProducerPlain(TexoExporter &exporter): - TexoProducer(exporter) + TexoProducer(exporter), quoted(false), newline(false) {} void TexoProducerPlain::Put(const Texo &piece) { - if (quoted && piece.c == '\n') { - exporter.Put("\n> "); + if (piece.c == '\n') { + if (quoted) { + exporter.Put("\n>"); + } else if (newline) { + exporter.Put(' '); + } else { + exporter.Put('\n'); + newline = true; + } } else { exporter.Put(piece.c); - } -} - -void TexoProducerPlain::Put(const TexoHeader &piece) -{ - if (!piece.closing) { - exporter.Put("\n\n# "); + newline = false; } } @@ -25,28 +26,38 @@ void TexoProducerPlain::Put(const TexoParagraph &piece) { if (piece.closing) { exporter.Put("\n\n"); + newline = true; } } void TexoProducerPlain::Put(const TexoQuote &piece) { if (piece.closing) { - quoted = false; exporter.Put("\n\n"); + quoted = false; + newline = true; } else { + if (!newline) { + exporter.Put('\n'); + } + exporter.Put(">"); quoted = true; - exporter.Put("\n\n> "); } } void TexoProducerPlain::Put(const TexoLineBreak &piece) { - exporter.Put(" \n"); + exporter.Put("\n\n"); + newline = true; } void TexoProducerPlain::Put(const TexoHorizontalRule &piece) { - exporter.Put("\n--------------------------------------------------\n"); + if (!newline) { + exporter.Put('\n'); + } + exporter.Put("--------------------------------------------------\n"); + newline = false; } @@ -59,15 +70,17 @@ TexoImporterPlain::TexoImporterPlain(TexoProducer &producer): TexoImporterPlain::~TexoImporterPlain() { switch (state) { - case text: case space: case newline: + case rule: + producer.Put('\n'); + for (;dash_count > 0; --dash_count) { + producer.Put('-'); + } + case text: case newline: producer.Put(TexoParagraph(true)); break; case quote: case quote_newline: producer.Put(TexoQuote(true)); break; - case header: - producer.Put(TexoHeader(true)); - break; case paragraph: break; } @@ -76,13 +89,12 @@ TexoImporterPlain::~TexoImporterPlain() void TexoImporterPlain::Put(char c) { switch (state) { - case text: Text(c); break; - case space: Space(c); break; - case newline: Newline(c); break; - case paragraph: Paragraph(c); break; - case quote: Quote(c); break; - case quote_newline: QuoteNewline(c); break; - case header: Header(c); break; + case text: Text(c); break; + case newline: Newline(c); break; + case paragraph: Paragraph(c); break; + case quote: Quote(c); break; + case quote_newline: QuoteNewline(c); break; + case rule: Rule(c); break; } } @@ -91,53 +103,47 @@ void TexoImporterPlain::Put(const ScriptVariable &str) TexoImporter::Put(str); } -void TexoImporterPlain::Text(char c) +void TexoImporterPlain::Put(FILE *file) { - switch (c) { - case ' ': state = space; break; - case '\n': state = newline; break; - default: producer.Put(Texo(c)); break; - } + TexoImporter::Put(file); } -void TexoImporterPlain::Space(char c) +void TexoImporterPlain::Text(char c) { - if (c == '\n') { - producer.Put(TexoLineBreak()); - state = text; - } else if (c == ' ') { - producer.Put(Texo(' ')); - } else { - producer.Put(Texo(' ')); - producer.Put(Texo(c)); - state = text; + switch (c) { + case '\n': state = newline; break; + default: producer.Put(c); break; } } void TexoImporterPlain::Newline(char c) { if (c == '\n') { - producer.Put(TexoParagraph(true)); state = paragraph; + } else if (c == '-') { + dash_count = 1; + state = rule; } else { - producer.Put(Texo('\n')); - producer.Put(Texo(c)); + producer.Put('\n'); state = text; + Text(c); } } void TexoImporterPlain::Paragraph(char c) { if (c == '>') { + producer.Put(TexoParagraph(true)); producer.Put(TexoQuote()); state = quote; - } else if (c == '#') { - producer.Put(TexoHeader(1)); - state = header; - } else { + } else if (c == '-') { + dash_count = 1; + state = rule; + } else if (c != '\n') { + producer.Put(TexoParagraph(true)); producer.Put(TexoParagraph()); - producer.Put(Texo(c)); state = text; + Text(c); } } @@ -145,31 +151,45 @@ void TexoImporterPlain::Quote(char c) { switch (c) { case '\n': state = quote_newline; break; - default: producer.Put(Texo(c)); break; + default: producer.Put(c); break; } } void TexoImporterPlain::QuoteNewline(char c) { if (c == '>') { + producer.Put('\n'); state = quote; } else if (c == '\n') { producer.Put(TexoQuote(true)); + producer.Put(TexoParagraph()); state = paragraph; + } else if (c == '-') { + producer.Put(TexoQuote(true)); + producer.Put(TexoParagraph()); + dash_count = 1; + state = rule; } else { producer.Put(TexoQuote(true)); producer.Put(TexoParagraph()); - producer.Put(Texo(c)); state = text; + Text(c); } } -void TexoImporterPlain::Header(char c) +void TexoImporterPlain::Rule(char c) { if (c == '\n') { - producer.Put(TexoHeader(1, true)); - state = paragraph; + producer.Put(TexoHorizontalRule()); + state = text; + } else if (c == '-') { + ++dash_count; } else { - producer.Put(Texo(c)); + producer.Put('\n'); + for (;dash_count > 0; --dash_count) { + producer.Put('-'); + } + state = text; + Text(c); } } -- cgit v1.2.3