diff options
| author | Shulhan <ms@kilabit.info> | 2023-02-12 12:54:48 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2023-03-02 23:25:12 +0700 |
| commit | fe8bd4d6305e82259eaac94972f1621aa52e89aa (patch) | |
| tree | 3e0036fe349e41564c7596a23d80c0ec06ff3dfa | |
| parent | 7059665463bd648321fbd8c4b9227ead65767975 (diff) | |
| download | asciidoctor-go-fe8bd4d6305e82259eaac94972f1621aa52e89aa.tar.xz | |
all: handle empty preamble
If the document contains empty preamble do not output the HTML wrapper of
it.
| -rw-r--r-- | document.go | 16 | ||||
| -rw-r--r-- | document_parser.go | 11 | ||||
| -rw-r--r-- | html_backend.go | 10 | ||||
| -rw-r--r-- | testdata/document_title_test.txt | 8 | ||||
| -rw-r--r-- | testdata/header_with_empty_line_test.txt | 2 | ||||
| -rw-r--r-- | testdata/meta_showtitle_test.txt | 4 | ||||
| -rw-r--r-- | testdata/test.exp.html | 2 | ||||
| -rw-r--r-- | testdata/test.got.html | 2 |
8 files changed, 34 insertions, 21 deletions
diff --git a/document.go b/document.go index 2b1cd91..97c8171 100644 --- a/document.go +++ b/document.go @@ -260,6 +260,22 @@ func (doc *Document) generateClasses() { } } +func (doc *Document) haveHeader() bool { + if len(doc.Authors) > 0 { + return true + } + if len(doc.Revision.Number) > 0 { + return true + } + if len(doc.Revision.Date) > 0 { + return true + } + if len(doc.Revision.Remark) > 0 { + return true + } + return false +} + func (doc *Document) toHTMLBody(buf *bytes.Buffer, withHeaderFooter bool) { var ( ok bool diff --git a/document_parser.go b/document_parser.go index 1117b99..3ccfa0f 100644 --- a/document_parser.go +++ b/document_parser.go @@ -136,17 +136,22 @@ func (docp *documentParser) hasPreamble() bool { var ( start = docp.lineNum - line []byte - kind int + notEmtpy int + line []byte + kind int ) for ; start < len(docp.lines); start++ { line = docp.lines[start] + if len(line) == 0 { + continue + } kind, _, _ = whatKindOfLine(line) if kind == elKindSectionL1 || kind == elKindSectionL2 || kind == elKindSectionL3 || kind == elKindSectionL4 || kind == elKindSectionL5 { - return true + return notEmtpy > 0 } + notEmtpy++ } return false } diff --git a/html_backend.go b/html_backend.go index b3f5adb..100fab1 100644 --- a/html_backend.go +++ b/html_backend.go @@ -979,6 +979,8 @@ func htmlWriteHeader(doc *Document, out io.Writer) { fmt.Fprint(out, "\n<div id=\"header\">") var ( + haveHeader = doc.haveHeader() + author *Author prefix string sep string @@ -996,7 +998,9 @@ func htmlWriteHeader(doc *Document, out io.Writer) { } } - fmt.Fprint(out, "\n<div class=\"details\">") + if haveHeader { + fmt.Fprint(out, "\n<div class=\"details\">") + } var authorID, emailID string for x, author = range doc.Authors { @@ -1042,7 +1046,9 @@ func htmlWriteHeader(doc *Document, out io.Writer) { fmt.Fprintf(out, "\n<br><span id=%q>%s</span>", metaNameRevRemark, doc.Revision.Remark) } - fmt.Fprint(out, "\n</div>") + if haveHeader { + fmt.Fprint(out, "\n</div>") + } if doc.tocIsEnabled && (doc.tocPosition == `` || doc.tocPosition == metaValueAuto || diff --git a/testdata/document_title_test.txt b/testdata/document_title_test.txt index f5dbdf9..14098ca 100644 --- a/testdata/document_title_test.txt +++ b/testdata/document_title_test.txt @@ -7,8 +7,6 @@ output_call: htmlWriteHeader <div id="header"> <h1>Main: sub</h1> -<div class="details"> -</div> </div> >>> Without space after separator @@ -18,8 +16,6 @@ output_call: htmlWriteHeader <div id="header"> <h1>Main:sub</h1> -<div class="details"> -</div> </div> >>> With multiple separator after separator @@ -29,8 +25,6 @@ output_call: htmlWriteHeader <div id="header"> <h1>a: b: c</h1> -<div class="details"> -</div> </div> >>> With custom separator @@ -41,6 +35,4 @@ output_call: htmlWriteHeader <div id="header"> <h1>Mainx sub</h1> -<div class="details"> -</div> </div> diff --git a/testdata/header_with_empty_line_test.txt b/testdata/header_with_empty_line_test.txt index f36f99b..c4a4a63 100644 --- a/testdata/header_with_empty_line_test.txt +++ b/testdata/header_with_empty_line_test.txt @@ -13,8 +13,6 @@ Below is empty line with spaces. <<< <div id="header"> -<div class="details"> -</div> </div> <div id="content"> <div class="paragraph"> diff --git a/testdata/meta_showtitle_test.txt b/testdata/meta_showtitle_test.txt index e534c1c..163f264 100644 --- a/testdata/meta_showtitle_test.txt +++ b/testdata/meta_showtitle_test.txt @@ -7,8 +7,6 @@ output_call: ToHTMLBody <div id="header"> <h1>Abc</h1> -<div class="details"> -</div> </div> <div id="content"> </div> @@ -24,8 +22,6 @@ output_call: ToHTMLBody <<< With meta showtitle off <div id="header"> -<div class="details"> -</div> </div> <div id="content"> </div> diff --git a/testdata/test.exp.html b/testdata/test.exp.html index 6af6df4..f0a36d0 100644 --- a/testdata/test.exp.html +++ b/testdata/test.exp.html @@ -2995,7 +2995,7 @@ this sidebar.</p> <div id="footer"> <div id="footer-text"> 1.1.1<br> -Last updated 2022-10-18 00:21:57 +0700 +Last updated 2023-03-02 20:49:28 +0700 </div> </div> </body> diff --git a/testdata/test.got.html b/testdata/test.got.html index 512f9a7..28cba38 100644 --- a/testdata/test.got.html +++ b/testdata/test.got.html @@ -2997,7 +2997,7 @@ this sidebar.</p> <div id="footer"> <div id="footer-text"> 1.1.1<br> -Last updated 2022-10-18 00:21:58 +0700 +Last updated 2023-03-02 20:49:28 +0700 </div> </div> </body> |
