From cf7c5ad9f8e96c3b4bac0bc45be326a6506be908 Mon Sep 17 00:00:00 2001 From: ketmar Date: Sun, 11 Feb 2018 04:04:10 +0000 Subject: [PATCH] unified formatting code (fuck standards!) FossilOrigin-Name: 1574ac95aff7f59509d87c860d1bfd170cf1ba8ae318baab0659883db63cb89a --- xreaderfmt.d | 383 ++++++++++++++++++++++++----------------------------------- 1 file changed, 154 insertions(+), 229 deletions(-) diff --git a/xreaderfmt.d b/xreaderfmt.d index cef8bd5..80a524f 100644 --- a/xreaderfmt.d +++ b/xreaderfmt.d @@ -263,42 +263,156 @@ private: } } - void putParasXX(bool allowBad=false) (Tag tag) { - if (tag is null || tag.name.length == 0) return; + void putOneTag(bool allowBad=false, bool allowText=true) (Tag tag) { + if (tag is null) return; + if (tag.name.length == 0) { + static if (allowText) { + saveTagId(tag); + lay.put(tag.text); + } + return; + } switch (tag.name) { case "p": putTagContents(tag); + lay.endPara(); break; + + case "strong": + lay.pushStyles(); + scope(exit) lay.popStyles; + lay.fontStyle.bold = true; + putTagContents(tag); + break; + case "emphasis": + lay.pushStyles(); + scope(exit) lay.popStyles; + lay.fontStyle.italic = true; + fixFont(); + putTagContents(tag); + break; + case "strikethrough": + lay.pushStyles(); + scope(exit) lay.popStyles; + lay.fontStyle.strike = true; + fixFont(); + putTagContents(tag); + break; + case "empty-line": lay.endLine(); break; case "br": lay.endPara(); break; - case "text-author": + + case "image": + lay.pushStyles(); + scope(exit) lay.popStyles; + lay.endLine; + lay.lineStyle.setCenter; + putImage(tag); + return; + + case "style": + //??? + return; + + case "a": + if (tag.href.length) { + import std.conv : to; + //conwriteln("href found: '", ct.href, "' (", lay.nextWordIndex, ")"); + meta.hrefs[lay.nextWordIndex] = BookMetadata.Anc(tag.href.to!dstring, lay.nextWordIndex); + lay.pushStyles(); + auto c = lay.fontStyle.color; + scope(exit) { lay.popStyles; lay.fontStyle.color = c; } + lay.fontStyle.href = true; + lay.fontStyle.underline = true; + lay.fontStyle.color = colorTextHref.asUint; + fixFont(); + putTagContents(tag); + } else { + putTagContents(tag); + } break; - case "strong": - putTagContents(tag, 1); + + case "sub": // some idiotic files has this + case "sup": // some idiotic files has this + putTagContents(tag); + break; + + case "code": + lay.pushStyles(); + scope(exit) lay.popStyles; + setCodeStyle(); + putTagContents(tag); + lay.endPara; break; + case "poem": + lay.pushStyles(); + scope(exit) lay.popStyles; putPoem(tag); - lay.put(LayText.EndParaCh); + //lay.endPara; break; - case "subtitle": - //FIXME:??? somehow this is invisible + + case "stanza": + putTagContents(tag); + lay.endPara; + break; + + case "v": // for stanzas + putTagContents(tag); + lay.endPara; + break; + + case "date": // for poem + lay.endPara; lay.pushStyles(); scope(exit) lay.popStyles; - setSubtitleStyle(); - if (tag.text.length) { putParaContentInternal(tag); lay.endPara(); } else putTagContents(tag); - break; - case "sub": - case "sup": - putTagContents(tag); // some idiotic files has this + lay.fontStyle.bold = true; + lay.fontStyle.italic = true; + lay.lineStyle.setRight; + fixFont(); + putTagContents(tag); + lay.endPara; break; + + case "cite": + putCite(tag); + return; + case "site": case "annotation": - putParaContentInternal(tag); // some idiotic files has this + lay.pushStyles(); + scope(exit) lay.popStyles; + lay.fontStyle.bold = true; + lay.lineStyle.setCenter; + fixFont(); + putTagContents(tag); + lay.endPara; + break; + + case "text-author": + break; + + case "title": + lay.pushStyles(); + scope(exit) lay.popStyles; + setTitleStyle(); + registerSection(tag); + putTagContents(tag); + if (tag.text.length == 0) lay.endPara(); + break; + + case "subtitle": + lay.pushStyles(); + scope(exit) lay.popStyles; + setSubtitleStyle(); + putTagContents(tag); + if (tag.text.length == 0) lay.endPara(); break; + case "epigraph": lay.pushStyles(); scope(exit) lay.popStyles; @@ -307,14 +421,24 @@ private: setEpigraphStyle(); //lay.lineStyle.leftpad = lay.lineStyle.leftpad+olpad; lay.lineStyle.rightpad = lay.lineStyle.rightpad+orpad; - putEpigraph(tag, false); + putEpigraph(tag); break; - case "emphasis": + + case "section": + putTagContents(tag); + lay.endPara; + break; + + case "table": lay.pushStyles(); scope(exit) lay.popStyles; - lay.fontStyle.italic = true; - putEpigraph(tag, false); + lay.fontStyle.fontsize += 8; + lay.lineStyle.setCenter; + fixFont(); + lay.put("TABLE SKIPPED"); + lay.endPara(); break; + default: static if (allowBad) { break; @@ -324,84 +448,6 @@ private: } } - void putParaContentInternal (Tag ct, int boldc=0, int italicc=0, int underc=0) { - if (ct is null) return; - auto c = lay.fontStyle.color; - scope(exit) lay.fontStyle.color = c; - bool popStyles = false; - bool doFixFont = true; - scope(exit) { - if (popStyles) lay.popStyles; - fixFont(); - } - saveTagId(ct); - switch (ct.name) { - case "strong": ++boldc; lay.fontStyle.bold = true; break; - case "emphasis": ++italicc; lay.fontStyle.italic = true; break; - case "strikethrough": lay.pushStyles(); popStyles = true; lay.fontStyle.strike = true; break; - case "image": putImage(ct); return; - case "style": return; - case "a": - if (ct.href.length) { - import std.conv : to; - //conwriteln("href found: '", ct.href, "' (", lay.nextWordIndex, ")"); - meta.hrefs[lay.nextWordIndex] = BookMetadata.Anc(ct.href.to!dstring, lay.nextWordIndex); - ++underc; - lay.fontStyle.href = true; - lay.fontStyle.underline = true; - lay.fontStyle.color = colorTextHref.asUint; - } - break; - case "sub": break; // some idiotic files has this - case "sup": break; // some idiotic files has this - case "p": - putTagContents(ct, boldc, italicc, underc); - fixFont(); - return; - case "br": - //lay.endLine(); - lay.endPara(); - return; - case "empty-line": - lay.endLine(); - return; - case "code": - lay.pushStyles(); - popStyles = true; - setCodeStyle(); - doFixFont = false; - break; - case "site": - case "annotation": - lay.pushStyles(); - popStyles = true; - ++boldc; - lay.fontStyle.bold = true; - lay.lineStyle.setCenter; - break; - case "poem": - lay.pushStyles(); - popStyles = true; - putPoem(ct); - lay.put(LayText.EndParaCh); - return; - case "": - lay.put(ct.text); - return; - default: - badTag(ct); - break; - } - if (doFixFont) fixFont(); - foreach (Tag tag; ct.children) putParaContentInternal(tag, boldc, italicc, underc); - switch (ct.name) { - case "strong": case "site": case "annotation": if (--boldc == 0) lay.fontStyle.bold = false; break; - case "emphasis": if (--italicc == 0) lay.fontStyle.italic = false; break; - case "a": if (--underc == 0) lay.fontStyle.underline = false; lay.fontStyle.href = false; break; - default: - } - } - bool onlyImagePara (Tag ct) { if (ct is null) return false; int count; @@ -417,29 +463,28 @@ private: return (count == 1); } - void putTagContents/*(bool xdump=false)*/ (Tag ct, int boldc=0, int italicc=0, int underc=0) { + void putTagContents/*(bool xdump=false)*/ (Tag ct) { if (ct is null) return; saveTagId(ct); if (onlyImagePara(ct)) { lay.pushStyles(); scope(exit) lay.popStyles; lay.lineStyle.setCenter; - foreach (Tag tag; ct.children) putParaContentInternal(tag, boldc, italicc, underc); + foreach (Tag tag; ct.children) putOneTag(tag); } else { //if (ct.name == "subtitle") conwriteln("text: [", ct.children[0].text, "]"); foreach (Tag tag; ct.children) { //static if (xdump) conwriteln("text: [", tag.text, "]"); - putParaContentInternal(tag, boldc, italicc, underc); + putOneTag(tag); } } - lay.endPara(); + //lay.endPara(); } void putParas (Tag ct) { - saveTagId(ct); - foreach (Tag tag; ct.children) { - putParasXX(tag); - } + if (ct is null) return; + putTagContents(ct); + lay.endPara(); } void putAuthor (Tag ct) { @@ -461,28 +506,11 @@ private: putAuthor(ct); } - void putEpigraph (Tag ct, bool setstyle=true) { - if (setstyle) lay.pushStyles(); - scope(exit) if (setstyle) lay.popStyles; - if (setstyle) setEpigraphStyle(); + void putEpigraph (Tag ct) { putParas(ct); putAuthor(ct); } - void putStanza (Tag ct) { - saveTagId(ct); - foreach (Tag tag; ct.children) { - if (tag.name.length == 0) continue; - if (tag.name == "text-author") continue; - if (tag.name == "title") badTag(tag, "titles in stanzas are not supported yet"); - if (tag.name == "subtitle") badTag(tag, "subtitles in stanzas are not supported yet"); - if (tag.name == "epigraph") badTag(tag, "epigraphs in poems are not supported yet"); - if (tag.name == "date") continue; - if (tag.name == "v") { putTagContents(tag); continue; } - badTag(tag); - } - } - void putPoem (Tag ct) { saveTagId(ct); lay.pushStyles(); @@ -506,118 +534,15 @@ private: } lay.endPara; foreach (Tag tag; ct.children) { - if (tag.name.length == 0) continue; - if (tag.name == "text-author") continue; - if (tag.name == "title") continue; - if (tag.name == "subtitle") continue; - if (tag.name == "epigraph") badTag(tag, "epigraphs in poems are not supported yet"); - if (tag.name == "date") continue; - if (tag.name == "stanza") { putStanza(tag); lay.put(LayText.EndParaCh); continue; } - badTag(tag); + putOneTag!(false, false)(tag); // skip text without tags } putAuthor(ct); } - void putSection (Tag sc) { - bool sectionRegistered = false; - hasSections = true; - - saveTagId(sc); - foreach (Tag tag; sc.children) { - if (tag.name.length == 0) continue; - if (tag.name == "title") { - lay.pushStyles(); - scope(exit) lay.popStyles; - setTitleStyle(); - if (!sectionRegistered) { sectionRegistered = true; registerSection(tag); } - putParas(tag); - } else if (tag.name == "subtitle") { - lay.pushStyles(); - scope(exit) lay.popStyles; - setSubtitleStyle(); - if (!sectionRegistered) { sectionRegistered = true; registerSection(tag); } - putParas(tag); - } else if (tag.name == "epigraph") { - putEpigraph(tag); - } else if (tag.name == "section") { - lay.pushStyles(); - scope(exit) lay.popStyles; - setNormalStyle(); - putSection(tag); - } else if (tag.name == "p") { - putTagContents(tag); - } else if (tag.name == "image") { - lay.pushStyles(); - scope(exit) lay.popStyles; - lay.lineStyle.leftpad = 0; - lay.lineStyle.rightpad = 0; - lay.lineStyle.paraIndent = 0; - lay.lineStyle.setCenter; - putImage(tag); - lay.endPara(); - } else if (tag.name == "empty-line") { - lay.endPara(); - } else if (tag.name == "cite") { - putCite(tag); - } else if (tag.name == "table") { - lay.pushStyles(); - scope(exit) lay.popStyles; - lay.fontStyle.fontsize += 8; - lay.lineStyle.setCenter; - lay.put("TABLE SKIPPED"); - lay.endPara(); - } else if (tag.name == "poem") { - putPoem(tag); - } else if (tag.name == "image") { - } else if (tag.name == "style") { - } else if (tag.name == "site" || tag.name == "annotation") { - } else { - badTag(tag); - } - } - lay.endPara; - } - void putMain () { + setNormalStyle(); foreach (Tag tag; book.content.children) { - if (tag.name == "title") { - lay.pushStyles(); - scope(exit) lay.popStyles; - setTitleStyle(); - lay.endPara(); - if (!hasSections) registerSection(tag); - putParas(tag); - lay.endPara(); - } else if (tag.name == "subtitle") { - lay.pushStyles(); - scope(exit) lay.popStyles; - setTitleStyle(); - putParas(tag); - lay.endPara(); - } else if (tag.name == "epigraph") { - putEpigraph(tag); - } else if (tag.name == "section") { - lay.pushStyles(); - scope(exit) lay.popStyles; - setNormalStyle(); - putSection(tag); - } else if (tag.name == "image") { - putImage(tag); - } else /*if (tag.name == "empty-line") { - lay.endLine(); - } else if (tag.name == "p") { - lay.pushStyles(); - scope(exit) lay.popStyles; - setNormalStyle(lay); - putParas(tag); - lay.endPara(); - putTagContents(tag); - }*/{ - lay.pushStyles(); - scope(exit) lay.popStyles; - setNormalStyle(); - putParasXX!true(tag); - } + putOneTag(tag); } } -- 2.11.4.GIT