summaryrefslogtreecommitdiff
path: root/src/plain.cpp
diff options
context:
space:
mode:
authorAleksey Veresov <aleksey@veresov.pro>2019-11-08 19:49:40 +0300
committerAleksey Veresov <aleksey@veresov.pro>2019-11-08 19:56:11 +0300
commit3047ed6100a56a529f26e43ada0a3fc3c3815c3e (patch)
tree9648d5a74bae9cbd90670a8bfa0af47189e4536f /src/plain.cpp
parentc6419eed96f2832b1de2b94d711552efaa9b172d (diff)
downloadtexo-3047ed6100a56a529f26e43ada0a3fc3c3815c3e.tar
texo-3047ed6100a56a529f26e43ada0a3fc3c3815c3e.tar.xz
texo-3047ed6100a56a529f26e43ada0a3fc3c3815c3e.zip
[texo] Full rewrite of inner representation.
Diffstat (limited to 'src/plain.cpp')
-rw-r--r--src/plain.cpp132
1 files changed, 76 insertions, 56 deletions
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);
}