From fe8bd4d6305e82259eaac94972f1621aa52e89aa Mon Sep 17 00:00:00 2001 From: Shulhan Date: Sun, 12 Feb 2023 12:54:48 +0700 Subject: all: handle empty preamble If the document contains empty preamble do not output the HTML wrapper of it. --- document.go | 16 ++++++++++++++++ document_parser.go | 11 ++++++++--- html_backend.go | 10 ++++++++-- testdata/document_title_test.txt | 8 -------- testdata/header_with_empty_line_test.txt | 2 -- testdata/meta_showtitle_test.txt | 4 ---- testdata/test.exp.html | 2 +- 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
") 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
") + if haveHeader { + fmt.Fprint(out, "\n
") + } 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
%s", metaNameRevRemark, doc.Revision.Remark) } - fmt.Fprint(out, "\n
") + if haveHeader { + fmt.Fprint(out, "\n
") + } 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 >>> Without space after separator @@ -18,8 +16,6 @@ output_call: htmlWriteHeader >>> With multiple separator after separator @@ -29,8 +25,6 @@ output_call: htmlWriteHeader >>> With custom separator @@ -41,6 +35,4 @@ output_call: htmlWriteHeader 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. <<<
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
@@ -24,8 +22,6 @@ output_call: ToHTMLBody <<< With meta showtitle off
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.

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.

-- cgit v1.3