From 56a1e4c4bfa2adb882bd9f0fc33b16110a2d09bd Mon Sep 17 00:00:00 2001 From: Shulhan Date: Thu, 13 Feb 2025 19:20:19 +0700 Subject: all: add default HTML stylesheet The generated HTML now contains the default stylesheet. The stylesheet is copied from HTML file generated by Asciidoctor v2.0.23. --- Makefile | 2 +- README.md | 1 + document.go | 14 +- document_attribute.go | 2 + html_template.go | 431 ++++++++++++++++++++++++++++++++++++++ testdata/document_tohtml_test.txt | 2 + testdata/test.exp.html | 429 ++++++++++++++++++++++++++++++++++++- testdata/test.got.html | 430 ++++++++++++++++++++++++++++++++++++- 8 files changed, 1304 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index 315670e..8ef844a 100644 --- a/Makefile +++ b/Makefile @@ -16,7 +16,7 @@ test: .PHONY: test-parser test-parser: - asciidoctor --attribute stylesheet! \ + asciidoctor \ --out-file=testdata/test.exp.html \ testdata/test.adoc go test -v -run=Open . diff --git a/README.md b/README.md index c5946e1..5cdafdc 100644 --- a/README.md +++ b/README.md @@ -142,6 +142,7 @@ Only on document attributes, not on include directive. * `sectnumlevels` * `sectnums` * `showtitle` +* `stylesheet` * `table-caption` * `title-separator` * `version-label` diff --git a/document.go b/document.go index cfa3eeb..29d589e 100644 --- a/document.go +++ b/document.go @@ -203,12 +203,18 @@ func (doc *Document) ToHTML(out io.Writer) (err error) { fmt.Fprintf(buf, "\n%s", title) } + var ok bool + + _, ok = doc.Attributes.Entry[docAttrStylesheet] + if ok { + buf.WriteByte('\n') + buf.WriteString(_defaultCSS) + } + fmt.Fprintf(buf, "\n\n\n", doc.classes.String()) - var ( - isWithHeaderFooter = true - ok bool - ) + var isWithHeaderFooter = true + _, ok = doc.Attributes.Entry[docAttrNoHeaderFooter] if ok { isWithHeaderFooter = false diff --git a/document_attribute.go b/document_attribute.go index 7d40a50..ec86de1 100644 --- a/document_attribute.go +++ b/document_attribute.go @@ -41,6 +41,7 @@ const ( docAttrSectNumLevel = `sectnumlevels` docAttrSectNums = `sectnums` docAttrShowTitle = `showtitle` + docAttrStylesheet = `stylesheet` docAttrTOC = `toc` docAttrTOCLevels = `toclevels` docAttrTOCTitle = `toc-title` @@ -74,6 +75,7 @@ func newDocumentAttribute() DocumentAttribute { docAttrLastUpdateValue: ``, docAttrSectIDs: ``, docAttrShowTitle: ``, + docAttrStylesheet: ``, docAttrTableCaption: ``, docAttrVersionLabel: ``, }, diff --git a/html_template.go b/html_template.go index c846fab..822c467 100644 --- a/html_template.go +++ b/html_template.go @@ -98,3 +98,434 @@ const ( ` ) + +//---- Default stylesheet. + +const _defaultCSS = ` + +` diff --git a/testdata/document_tohtml_test.txt b/testdata/document_tohtml_test.txt index 01f9a51..65b1d7a 100644 --- a/testdata/document_tohtml_test.txt +++ b/testdata/document_tohtml_test.txt @@ -11,6 +11,7 @@ with backslash key, \ words :last-update-label!: +:stylesheet!: Document body. @@ -56,6 +57,7 @@ John Doe v1.0, 15 Dec 2022 :last-update-label!: :idprefix: +:stylesheet!: This is preamble. diff --git a/testdata/test.exp.html b/testdata/test.exp.html index b4c6eee..309d46f 100644 --- a/testdata/test.exp.html +++ b/testdata/test.exp.html @@ -9,6 +9,433 @@ Example Document title + +