shithub: hugo

Download patch

ref: 8e553dcdefe50ab534f1199c006ae7754e14bee5
parent: d4fc70a3b320a55c4f571eed806d5ad5fdf1ef14
author: Helder Pereira <helfper@gmail.com>
date: Wed Sep 16 19:02:35 EDT 2020

markup/asciidocext: Add preserveTOC option

--- a/docs/content/en/content-management/formats.md
+++ b/docs/content/en/content-management/formats.md
@@ -43,7 +43,7 @@
 
 ## External Helpers
 
-Some of the formats in the table above needs external helpers installed on your PC. For example, for AsciiDoc files, 
+Some of the formats in the table above need external helpers installed on your PC. For example, for AsciiDoc files,
 Hugo will try to call the `asciidoctor` command. This means that you will have to install the associated
 tool on your machine to be able to use these formats.
 
@@ -69,32 +69,47 @@
 [See the Asciidoctor docs for installation instructions](https://asciidoctor.org/docs/install-toolchain/). Make sure that also all
 optional extensions like `asciidoctor-diagram` or `asciidoctor-html5s` are installed if required.
 
-Asciidoctor parameters can be customized in Hugo:
+{{% note %}}
+External `asciidoctor` command requires Hugo rendering to _disk_ to a specific destination directory. It is required to run Hugo with the command option `--destination`.
+{{% /note %}}
 
-Parameter | Default | Comment
---- | --- | --- 
-backend | `html5` | Don't change this unless you know what you are doing.
-doctype | `article` | Currently supported Document type is `article`.
-extensions | | Possible extensions are `asciidoctor-html5s`, `asciidoctor-bibtex`, `asciidoctor-diagram`,	`asciidoctor-interdoc-reftext`, `asciidoctor-katex`, `asciidoctor-latex`, `asciidoctor-mathematical`, `asciidoctor-question`, `asciidoctor-rouge`.
-attributes | | Variables to be referenced in your `adoc` file. This is a list of variable name/value maps. See [Asciidoctor#attributes](https://asciidoctor.org/docs/asciidoc-syntax-quick-reference/#attributes-and-substitutions).
-noheaderorfooter | true | Output an embeddable document, which excludes the header, the footer, and everything outside the body of the document. Don't change this unless you know what you are doing. 
-safemode | `unsafe` | Safe mode level `unsafe`, `safe`, `server` or `secure`. Don't change this unless you know what you are doing.
-sectionnumbers | `false` | Auto-number section titles.
-verbose | `false` | Verbosely print processing information and configuration file checks to stderr.
-trace | `false` | Include backtrace information on errors.
-failurelevel | `fatal` | The minimum logging level that triggers a non-zero exit code (failure).
-workingfoldercurrent | `false` | Set the working folder to the rendered `adoc` file, so [include](https://asciidoctor.org/docs/asciidoc-syntax-quick-reference/#include-files) will work with relative paths. This setting uses the `asciidoctor` cli parameter `--base-dir` and attribute `outdir=`. For rendering [asciidoctor-diagram](https://asciidoctor.org/docs/asciidoctor-diagram/)  `workingfoldercurrent` must be set to `true`.
+Some [Asciidoctor](https://asciidoctor.org/man/asciidoctor/) parameters can be customized in Hugo:
 
+Parameter | Comment
+--- | ---
+backend | Don't change this unless you know what you are doing.
+doctype | Currently, the only document type supported in Hugo is `article`.
+extensions | Possible extensions are `asciidoctor-html5s`, `asciidoctor-bibtex`, `asciidoctor-diagram`, `asciidoctor-interdoc-reftext`, `asciidoctor-katex`, `asciidoctor-latex`, `asciidoctor-mathematical`, `asciidoctor-question`, `asciidoctor-rouge`.
+attributes | Variables to be referenced in your AsciiDoc file. This is a list of variable name/value maps. See [Asciidoctor's attributes](https://asciidoctor.org/docs/asciidoc-syntax-quick-reference/#attributes-and-substitutions).
+noHeaderOrFooter | Output an embeddable document, which excludes the header, the footer, and everything outside the body of the document. Don't change this unless you know what you are doing.
+safeMode | Safe mode level `unsafe`, `safe`, `server` or `secure`. Don't change this unless you know what you are doing.
+sectionNumbers | Auto-number section titles.
+verbose | Verbosely print processing information and configuration file checks to stderr.
+trace | Include backtrace information on errors.
+failureLevel | The minimum logging level that triggers a non-zero exit code (failure).
+
+Hugo provides additional settings that don't map directly to Asciidoctor's CLI options:
+
+workingFolderCurrent
+: Sets the working directory to be the same as that of the AsciiDoc file being processed, so that [include](https://asciidoctor.org/docs/asciidoc-syntax-quick-reference/#include-files) will work with relative paths. This setting uses the `asciidoctor` cli parameter `--base-dir` and attribute `outdir=`. For rendering diagrams with [asciidoctor-diagram](https://asciidoctor.org/docs/asciidoctor-diagram/), `workingFolderCurrent` must be set to `true`.
+
+preserveTOC
+: By default, Hugo removes the table of contents generated by Asciidoctor and provides it through the built-in variable [`.TableOfContents`](/content-management/toc/) to enable further customization and better integration with the various Hugo themes. This option can be set to `true` to preserve Asciidoctor's TOC in the generated page.
+
+Below are all the AsciiDoc related settings in Hugo with their default values:
+
+{{< code-toggle config="markup.asciidocExt" />}}
+
+Example of how to set extensions and attributes:
+
 ```
-[markup.asciidocext]
+[markup.asciidocExt]
     extensions = ["asciidoctor-html5s", "asciidoctor-diagram"]
     workingFolderCurrent = true
-    [markup.asciidocext.attributes]
+    [markup.asciidocExt.attributes]
         my-base-url = "https://example.com/"
         my-attribute-name = "my value"
 ```
-
-Important: External `asciidoctor` requires Hugo rendering to _disk_ to a specific destination folder. It is required to run Hugo with the command option `--destination`!
 
 In a complex Asciidoctor environment it is sometimes helpful to debug the exact call to your external helper with all 
 parameters. Run Hugo with `-v`. You will get an output like
--- a/docs/content/en/content-management/toc.md
+++ b/docs/content/en/content-management/toc.md
@@ -96,14 +96,13 @@
 
 Hugo supports table of contents with AsciiDoc content format.
 
-In the header of your content file, specify the AsciiDoc TOC directives, by using the macro or auto style:
+In the header of your content file, specify the AsciiDoc TOC directives necessary to ensure that the table of contents is generated. Hugo will use the generated TOC to populate the page variable `.TableOfContents` in the same way as described for Markdown. See example below:
 
 ```asciidoc
 // <!-- Your front matter up here -->
-:toc: macro
+:toc:
 // Set toclevels to be at least your hugo [markup.tableOfContents.endLevel] config key
 :toclevels: 4
-toc::[]
 
 == Introduction
 
@@ -117,7 +116,6 @@
 
 A collection of textile samples lay spread out on the table - Samsa was a travelling salesman - and above it there hung a picture that he had recently cut out of an illustrated magazine and housed in a nice, gilded frame. It showed a lady fitted out with a fur hat and fur boa who sat upright, raising a heavy fur muff that covered the whole of her lower arm towards the viewer. Gregor then turned to look out the window at the dull weather. Drops
 ```
-Hugo will take this AsciiDoc and create a table of contents store it in the page variable `.TableOfContents`, in the same as described for Markdown.
 
 [conditionals]: /templates/introduction/#conditionals
 [front matter]: /content-management/front-matter/
--- a/docs/data/docs.json
+++ b/docs/data/docs.json
@@ -1535,7 +1535,8 @@
         "verbose": false,
         "trace": false,
         "failureLevel": "fatal",
-        "workingFolderCurrent": false
+        "workingFolderCurrent": false,
+        "preserveTOC": false
       }
     },
     "minify": {
--- a/markup/asciidocext/asciidocext_config/config.go
+++ b/markup/asciidocext/asciidocext_config/config.go
@@ -27,6 +27,7 @@
 		Trace:                false,
 		FailureLevel:         "fatal",
 		WorkingFolderCurrent: false,
+		PreserveTOC:          false,
 	}
 
 	// CliDefault holds Asciidoctor CLI defaults (see https://asciidoctor.org/docs/user-manual/)
@@ -86,4 +87,5 @@
 	Trace                bool
 	FailureLevel         string
 	WorkingFolderCurrent bool
+	PreserveTOC          bool
 }
--- a/markup/asciidocext/convert.go
+++ b/markup/asciidocext/convert.go
@@ -18,7 +18,6 @@
 
 import (
 	"bytes"
-	"io"
 	"os/exec"
 	"path/filepath"
 
@@ -64,7 +63,7 @@
 }
 
 func (a *asciidocConverter) Convert(ctx converter.RenderContext) (converter.Result, error) {
-	content, toc, err := extractTOC(a.getAsciidocContent(ctx.Src, a.ctx))
+	content, toc, err := a.extractTOC(a.getAsciidocContent(ctx.Src, a.ctx))
 	if err != nil {
 		return nil, err
 	}
@@ -204,7 +203,7 @@
 
 // extractTOC extracts the toc from the given src html.
 // It returns the html without the TOC, and the TOC data
-func extractTOC(src []byte) ([]byte, tableofcontents.Root, error) {
+func (a *asciidocConverter) extractTOC(src []byte) ([]byte, tableofcontents.Root, error) {
 	var buf bytes.Buffer
 	buf.Write(src)
 	node, err := html.Parse(&buf)
@@ -219,7 +218,9 @@
 	f = func(n *html.Node) bool {
 		if n.Type == html.ElementNode && n.Data == "div" && attr(n, "id") == "toc" {
 			toc = parseTOC(n)
-			n.Parent.RemoveChild(n)
+			if !a.cfg.MarkupConfig.AsciidocExt.PreserveTOC {
+				n.Parent.RemoveChild(n)
+			}
 			return true
 		}
 		if n.FirstChild != nil {
@@ -285,7 +286,7 @@
 			f(n.NextSibling, row, level)
 		}
 	}
-	f(doc.FirstChild, 0, 0)
+	f(doc.FirstChild, -1, 0)
 	return toc
 }
 
@@ -300,9 +301,8 @@
 
 func nodeContent(node *html.Node) string {
 	var buf bytes.Buffer
-	w := io.Writer(&buf)
 	for c := node.FirstChild; c != nil; c = c.NextSibling {
-		html.Render(w, c)
+		html.Render(&buf, c)
 	}
 	return buf.String()
 }
--- a/markup/asciidocext/convert_test.go
+++ b/markup/asciidocext/convert_test.go
@@ -277,11 +277,17 @@
 		t.Skip("asciidoctor not installed")
 	}
 	c := qt.New(t)
-	p, err := Provider.New(converter.ProviderConfig{Logger: loggers.NewErrorLogger()})
+	mconf := markup_config.Default
+	p, err := Provider.New(
+		converter.ProviderConfig{
+			MarkupConfig: mconf,
+			Logger:       loggers.NewErrorLogger(),
+		},
+	)
 	c.Assert(err, qt.IsNil)
 	conv, err := p.New(converter.DocumentContext{})
 	c.Assert(err, qt.IsNil)
-	b, err := conv.Convert(converter.RenderContext{Src: []byte(`:toc: macro
+	r, err := conv.Convert(converter.RenderContext{Src: []byte(`:toc: macro
 :toclevels: 4
 toc::[]
 
@@ -300,11 +306,52 @@
 == Section 2
 `)})
 	c.Assert(err, qt.IsNil)
-	toc, ok := b.(converter.TableOfContentsProvider)
+	toc, ok := r.(converter.TableOfContentsProvider)
 	c.Assert(ok, qt.Equals, true)
-	root := toc.TableOfContents()
-	c.Assert(root.ToHTML(2, 4, false), qt.Equals, "<nav id=\"TableOfContents\">\n  <ul>\n    <li><a href=\"#_introduction\">Introduction</a></li>\n    <li><a href=\"#_section_1\">Section 1</a>\n      <ul>\n        <li><a href=\"#_section_1_1\">Section 1.1</a>\n          <ul>\n            <li><a href=\"#_section_1_1_1\">Section 1.1.1</a></li>\n          </ul>\n        </li>\n        <li><a href=\"#_section_1_2\">Section 1.2</a></li>\n      </ul>\n    </li>\n    <li><a href=\"#_section_2\">Section 2</a></li>\n  </ul>\n</nav>")
-	c.Assert(root.ToHTML(2, 3, false), qt.Equals, "<nav id=\"TableOfContents\">\n  <ul>\n    <li><a href=\"#_introduction\">Introduction</a></li>\n    <li><a href=\"#_section_1\">Section 1</a>\n      <ul>\n        <li><a href=\"#_section_1_1\">Section 1.1</a></li>\n        <li><a href=\"#_section_1_2\">Section 1.2</a></li>\n      </ul>\n    </li>\n    <li><a href=\"#_section_2\">Section 2</a></li>\n  </ul>\n</nav>")
+	expected := tableofcontents.Root{
+		Headers: tableofcontents.Headers{
+			{
+				ID:   "",
+				Text: "",
+				Headers: tableofcontents.Headers{
+					{
+						ID:      "_introduction",
+						Text:    "Introduction",
+						Headers: nil,
+					},
+					{
+						ID:   "_section_1",
+						Text: "Section 1",
+						Headers: tableofcontents.Headers{
+							{
+								ID:   "_section_1_1",
+								Text: "Section 1.1",
+								Headers: tableofcontents.Headers{
+									{
+										ID:      "_section_1_1_1",
+										Text:    "Section 1.1.1",
+										Headers: nil,
+									},
+								},
+							},
+							{
+								ID:      "_section_1_2",
+								Text:    "Section 1.2",
+								Headers: nil,
+							},
+						},
+					},
+					{
+						ID:      "_section_2",
+						Text:    "Section 2",
+						Headers: nil,
+					},
+				},
+			},
+		},
+	}
+	c.Assert(toc.TableOfContents(), qt.DeepEquals, expected)
+	c.Assert(string(r.Bytes()), qt.Not(qt.Contains), "<div id=\"toc\" class=\"toc\">")
 }
 
 func TestTableOfContentsWithCode(t *testing.T) {
@@ -322,26 +369,72 @@
 	c.Assert(err, qt.IsNil)
 	conv, err := p.New(converter.DocumentContext{})
 	c.Assert(err, qt.IsNil)
-	b, err := conv.Convert(converter.RenderContext{Src: []byte(`:toc: auto
+	r, err := conv.Convert(converter.RenderContext{Src: []byte(`:toc: auto
 
 == Some ` + "`code`" + ` in the title
 `)})
 	c.Assert(err, qt.IsNil)
-	toc, ok := b.(converter.TableOfContentsProvider)
+	toc, ok := r.(converter.TableOfContentsProvider)
 	c.Assert(ok, qt.Equals, true)
-	expected := tableofcontents.Headers{
-		{},
-		{
-			ID:   "",
-			Text: "",
-			Headers: tableofcontents.Headers{
-				{
-					ID:      "_some_code_in_the_title",
-					Text:    "Some <code>code</code> in the title",
-					Headers: nil,
+	expected := tableofcontents.Root{
+		Headers: tableofcontents.Headers{
+			{
+				ID:   "",
+				Text: "",
+				Headers: tableofcontents.Headers{
+					{
+						ID:      "_some_code_in_the_title",
+						Text:    "Some <code>code</code> in the title",
+						Headers: nil,
+					},
 				},
 			},
 		},
 	}
-	c.Assert(toc.TableOfContents().Headers, qt.DeepEquals, expected)
+	c.Assert(toc.TableOfContents(), qt.DeepEquals, expected)
+	c.Assert(string(r.Bytes()), qt.Not(qt.Contains), "<div id=\"toc\" class=\"toc\">")
+}
+
+func TestTableOfContentsPreserveTOC(t *testing.T) {
+	if !Supports() {
+		t.Skip("asciidoctor not installed")
+	}
+	c := qt.New(t)
+	mconf := markup_config.Default
+	mconf.AsciidocExt.PreserveTOC = true
+	p, err := Provider.New(
+		converter.ProviderConfig{
+			MarkupConfig: mconf,
+			Logger:       loggers.NewErrorLogger(),
+		},
+	)
+	c.Assert(err, qt.IsNil)
+	conv, err := p.New(converter.DocumentContext{})
+	c.Assert(err, qt.IsNil)
+	r, err := conv.Convert(converter.RenderContext{Src: []byte(`:toc:
+:idprefix:
+:idseparator: -
+
+== Some title
+`)})
+	c.Assert(err, qt.IsNil)
+	toc, ok := r.(converter.TableOfContentsProvider)
+	c.Assert(ok, qt.Equals, true)
+	expected := tableofcontents.Root{
+		Headers: tableofcontents.Headers{
+			{
+				ID:   "",
+				Text: "",
+				Headers: tableofcontents.Headers{
+					{
+						ID:      "some-title",
+						Text:    "Some title",
+						Headers: nil,
+					},
+				},
+			},
+		},
+	}
+	c.Assert(toc.TableOfContents(), qt.DeepEquals, expected)
+	c.Assert(string(r.Bytes()), qt.Contains, "<div id=\"toc\" class=\"toc\">")
 }