summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2022-12-16 02:39:55 +0700
committerShulhan <ms@kilabit.info>2022-12-16 02:39:55 +0700
commit2dd794265dfda767e12a009b26296acc121ca458 (patch)
treeb75ac94f42b4f9322ced45e4f6a5433f4fe5584c
parent60a55a32252e349f2bdfeace983c251ee5865fb6 (diff)
downloadasciidoctor-go-2dd794265dfda767e12a009b26296acc121ca458.tar.xz
all: detach parsing preamble from content
This is to prevent empty preamble being rendered in HTML content.
-rw-r--r--asciidoctor.go2
-rw-r--r--document.go20
-rw-r--r--document_parser.go33
-rw-r--r--element.go16
-rw-r--r--html_backend.go15
-rw-r--r--html_template.go6
-rw-r--r--testdata/header_with_empty_line_test.txt4
-rw-r--r--testdata/meta_showtitle_test.txt8
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</div>")
- }
- if doc.tocIsEnabled && doc.tocPosition == metaValuePreamble {
- doc.tocHTML(w)
- }
- if !doc.isEmbedded {
- fmt.Fprint(w, "\n</div>")
- }
-
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<div id=\"content\">")
+
+ if doc.preamble != nil {
+ fmt.Fprint(out, _lf+`<div id="preamble">`)
+ fmt.Fprint(out, _lf+`<div class="sectionbody">`)
+
+ if doc.preamble.child != nil {
+ doc.preamble.child.toHTML(doc, out)
+ }
+
+ fmt.Fprint(out, _lf+`</div>`)
+ if doc.tocIsEnabled && doc.tocPosition == metaValuePreamble {
+ doc.tocHTML(out)
+ }
+ fmt.Fprint(out, _lf+`</div>`)
+ }
}
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 (
<meta name="viewport" content="width=device-width, initial-scale=1.0">`
)
-const (
- _htmlPreambleBegin = `
-<div id="preamble">
-<div class="sectionbody">`
-)
-
// 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.
</div>
</div>
<div id="content">
-<div id="preamble">
-<div class="sectionbody">
<div class="paragraph">
<p>= Title</p>
</div>
</div>
-</div>
-</div>
<div id="footer">
<div id="footer-text">
</div>
diff --git a/testdata/meta_showtitle_test.txt b/testdata/meta_showtitle_test.txt
index 7c5c047..e534c1c 100644
--- a/testdata/meta_showtitle_test.txt
+++ b/testdata/meta_showtitle_test.txt
@@ -11,10 +11,6 @@ output_call: ToHTMLBody
</div>
</div>
<div id="content">
-<div id="preamble">
-<div class="sectionbody">
-</div>
-</div>
</div>
<div id="footer">
<div id="footer-text">
@@ -32,10 +28,6 @@ output_call: ToHTMLBody
</div>
</div>
<div id="content">
-<div id="preamble">
-<div class="sectionbody">
-</div>
-</div>
</div>
<div id="footer">
<div id="footer-text">