From 9655c07d2764f2f0ab791761dc0da388e2bc4666 Mon Sep 17 00:00:00 2001 From: ketmar Date: Sun, 11 Feb 2018 03:28:57 +0000 Subject: [PATCH] more changes FossilOrigin-Name: 2aa03902b57bcdcb8b18952bbadceb162653d57910881e2de29e498547ee8d9a --- xreaderfmt.d | 63 +++++++++++++++++++++++++++++++----------------------------- 1 file changed, 33 insertions(+), 30 deletions(-) diff --git a/xreaderfmt.d b/xreaderfmt.d index 9c2ab5e..cef8bd5 100644 --- a/xreaderfmt.d +++ b/xreaderfmt.d @@ -171,6 +171,7 @@ class BookMetadata { final class FBFormatter { private: BookText book; + bool hasSections; public: LayText lay; @@ -189,6 +190,7 @@ public: book = abook; scope(exit) book = null; + hasSections = false; putMain(); @@ -222,6 +224,32 @@ private: } } + void registerSection (Tag tag) { + import std.conv : to; + string text = tag.textContent.xstrip; + //lay.sections ~= lay.curWordIndex; + string name; + while (text.length) { + char ch = text.ptr[0]; + text = text[1..$]; + if (ch <= ' ' || ch == 127) { + if (name.length == 0) continue; + if (ch != '\n') ch = ' '; + if (ch == '\n') { + if (name[$-1] > ' ') name ~= "\n..."; + text = text.xstrip; + } else { + if (name[$-1] > ' ') name ~= ' '; + } + } else { + name ~= ch; + } + } + name = xstrip(name); + if (name.length == 0) name = "* * *"; + meta.sections ~= BookMetadata.Anc(name.to!dstring, lay.nextWordIndex); + } + void putImage (Tag tag) { if (tag is null || tag.href.length < 2 || tag.href[0] != '#') return; string iid = tag.href[1..$]; @@ -466,6 +494,7 @@ private: if (tag.name == "title") { lay.pushStyles(); scope(exit) lay.popStyles; + if (!hasSections) registerSection(tag); putParas(tag); lay.endPara(); // space } else if (tag.name == "subtitle") { @@ -491,34 +520,7 @@ private: void putSection (Tag sc) { bool sectionRegistered = false; - - void registerSection (Tag tag) { - import std.conv : to; - if (sectionRegistered) return; - sectionRegistered = true; - string text = tag.textContent.xstrip; - //lay.sections ~= lay.curWordIndex; - string name; - while (text.length) { - char ch = text.ptr[0]; - text = text[1..$]; - if (ch <= ' ' || ch == 127) { - if (name.length == 0) continue; - if (ch != '\n') ch = ' '; - if (ch == '\n') { - if (name[$-1] > ' ') name ~= "\n..."; - text = text.xstrip; - } else { - if (name[$-1] > ' ') name ~= ' '; - } - } else { - name ~= ch; - } - } - name = xstrip(name); - if (name.length == 0) name = "* * *"; - meta.sections ~= BookMetadata.Anc(name.to!dstring, lay.nextWordIndex); - } + hasSections = true; saveTagId(sc); foreach (Tag tag; sc.children) { @@ -527,13 +529,13 @@ private: lay.pushStyles(); scope(exit) lay.popStyles; setTitleStyle(); - registerSection(tag); + if (!sectionRegistered) { sectionRegistered = true; registerSection(tag); } putParas(tag); } else if (tag.name == "subtitle") { lay.pushStyles(); scope(exit) lay.popStyles; setSubtitleStyle(); - registerSection(tag); + if (!sectionRegistered) { sectionRegistered = true; registerSection(tag); } putParas(tag); } else if (tag.name == "epigraph") { putEpigraph(tag); @@ -583,6 +585,7 @@ private: scope(exit) lay.popStyles; setTitleStyle(); lay.endPara(); + if (!hasSections) registerSection(tag); putParas(tag); lay.endPara(); } else if (tag.name == "subtitle") { -- 2.11.4.GIT