From 2dd794265dfda767e12a009b26296acc121ca458 Mon Sep 17 00:00:00 2001 From: Shulhan Date: Fri, 16 Dec 2022 02:39:55 +0700 Subject: all: detach parsing preamble from content This is to prevent empty preamble being rendered in HTML content. --- asciidoctor.go | 2 ++ document.go | 20 +++++++++---------- document_parser.go | 33 +++++++++++++++++++++++++++++--- element.go | 16 ---------------- html_backend.go | 15 +++++++++++++++ html_template.go | 6 ------ testdata/header_with_empty_line_test.txt | 4 ---- testdata/meta_showtitle_test.txt | 8 -------- 8 files changed, 57 insertions(+), 47 deletions(-) diff --git a/asciidoctor.go b/asciidoctor.go index c6888ac..12942c0 100644 --- a/asciidoctor.go +++ b/asciidoctor.go @@ -12,6 +12,8 @@ import "github.com/shuLhan/share/lib/math/big" const ( // Version of this module. Version = `0.3.3-dev` + + _lf = "\n" ) func init() { diff --git a/document.go b/document.go index 653a3e1..2b1cd91 100644 --- a/document.go +++ b/document.go @@ -85,19 +85,10 @@ func newDocument() (doc *Document) { header: &element{ kind: elKindDocHeader, }, - preamble: &element{ - elementAttribute: elementAttribute{ - Attrs: make(map[string]string), - }, - kind: elKindPreamble, - }, content: &element{ kind: elKindDocContent, }, } - - doc.content.addChild(doc.preamble) - return doc } @@ -157,7 +148,16 @@ func parse(doc *Document, content []byte) { doc.sectLevel, _ = strconv.Atoi(sectLevel) } - docp.parseBlock(doc.preamble, 0) + if docp.hasPreamble() { + doc.preamble = &element{ + elementAttribute: elementAttribute{ + Attrs: make(map[string]string), + }, + kind: elKindPreamble, + } + docp.parseBlock(doc.preamble, 0) + } + docp.parseBlock(doc.content, 0) } // ToHTMLEmbedded convert the Document object into HTML with content only, diff --git a/document_parser.go b/document_parser.go index 2a29152..45b7597 100644 --- a/document_parser.go +++ b/document_parser.go @@ -118,6 +118,27 @@ func (docp *documentParser) consumeLinesUntil(el *element, term int, terms []int return line } +// hasPreamble will return true if the contents contains preamble, indicated +// by the first section that found after current line. +func (docp *documentParser) hasPreamble() bool { + var ( + start = docp.lineNum + + line []byte + kind int + ) + for ; start < len(docp.lines); start++ { + line = docp.lines[start] + kind, _, _ = whatKindOfLine(line) + if kind == elKindSectionL1 || kind == elKindSectionL2 || + kind == elKindSectionL3 || kind == elKindSectionL4 || + kind == elKindSectionL5 { + return true + } + } + return false +} + func (docp *documentParser) include(el *elementInclude) { var ( content []byte = bytes.ReplaceAll(el.content, []byte("\r\n"), []byte("\n")) @@ -336,9 +357,15 @@ func (docp *documentParser) parseBlock(parent *element, term int) { el = new(element) continue - case elKindSectionL1, elKindSectionL2, - elKindSectionL3, elKindSectionL4, - elKindSectionL5: + case elKindSectionL1, elKindSectionL2, elKindSectionL3, elKindSectionL4, elKindSectionL5: + if parent.kind == elKindPreamble { + docp.kind = lineKindEmpty + docp.prevKind = lineKindEmpty + docp.lineNum-- + isTerm = true + continue + } + if term == elKindBlockOpen { line = docp.parseParagraph(parent, el, line, term) parent.addChild(el) diff --git a/element.go b/element.go index c0e94c1..9609570 100644 --- a/element.go +++ b/element.go @@ -843,11 +843,6 @@ func (el *element) toHTML(doc *Document, w io.Writer) { doc.tocHTML(w) } - case elKindPreamble: - if !doc.isEmbedded { - fmt.Fprint(w, _htmlPreambleBegin) - } - case elKindSectionDiscrete: hmltWriteSectionDiscrete(doc, el, w) @@ -1051,17 +1046,6 @@ func (el *element) toHTML(doc *Document, w io.Writer) { } switch el.kind { - case elKindPreamble: - if !doc.isEmbedded { - fmt.Fprint(w, "\n") - } - if doc.tocIsEnabled && doc.tocPosition == metaValuePreamble { - doc.tocHTML(w) - } - if !doc.isEmbedded { - fmt.Fprint(w, "\n") - } - case elKindSectionL1, elKindSectionL2, elKindSectionL3, elKindSectionL4, elKindSectionL5: if el.kind == elKindSectionL1 { diff --git a/html_backend.go b/html_backend.go index 1635328..b3f5adb 100644 --- a/html_backend.go +++ b/html_backend.go @@ -865,6 +865,21 @@ func htmlWriteBlockVideo(el *element, out io.Writer) { func htmlWriteBody(doc *Document, out *bytes.Buffer) { if !doc.isEmbedded { fmt.Fprint(out, "\n
") + + if doc.preamble != nil { + fmt.Fprint(out, _lf+`
`) + fmt.Fprint(out, _lf+`
`) + + if doc.preamble.child != nil { + doc.preamble.child.toHTML(doc, out) + } + + fmt.Fprint(out, _lf+`
`) + if doc.tocIsEnabled && doc.tocPosition == metaValuePreamble { + doc.tocHTML(out) + } + fmt.Fprint(out, _lf+`
`) + } } if doc.content.child != nil { diff --git a/html_template.go b/html_template.go index cd173ab..c846fab 100644 --- a/html_template.go +++ b/html_template.go @@ -13,12 +13,6 @@ const ( ` ) -const ( - _htmlPreambleBegin = ` -
-
` -) - // HTML templates for table of contents. const ( _htmlToCBegin = ` diff --git a/testdata/header_with_empty_line_test.txt b/testdata/header_with_empty_line_test.txt index b2810db..f36f99b 100644 --- a/testdata/header_with_empty_line_test.txt +++ b/testdata/header_with_empty_line_test.txt @@ -17,14 +17,10 @@ Below is empty line with spaces.
-
-

= Title

-
-
-
-
-
-
-
-
-
-