Fix Asciidoctor args

* Fix Asciidoctor args

* Fix Asciidoctor args documentation

* Update AsciiDoc documentation

Co-authored-by: Derk Muenchhausen <derk@muenchhausen.de>

Fixes #7493
This commit is contained in:
Helder Pereira 2020-07-23 14:59:48 +01:00 committed by GitHub
parent a06c06a5c2
commit 45c665d396
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 108 additions and 39 deletions

View file

@ -33,7 +33,7 @@ The current list of content formats in Hugo:
| Blackfriday | blackfriday |Blackfriday will eventually be deprecated.| | Blackfriday | blackfriday |Blackfriday will eventually be deprecated.|
|MMark|mmark|Mmark is deprecated and will be removed in a future release.| |MMark|mmark|Mmark is deprecated and will be removed in a future release.|
|Emacs Org-Mode|org|See [go-org](https://github.com/niklasfasching/go-org).| |Emacs Org-Mode|org|See [go-org](https://github.com/niklasfasching/go-org).|
|AsciiDoc|asciidoc, adoc, ad|Needs AsciiDoc or [Asciidoctor][ascii] installed.| |AsciiDoc|asciidocext, adoc, ad|Needs [Asciidoctor][ascii] installed.|
|RST|rst|Needs [RST](http://docutils.sourceforge.net/rst.html) installed.| |RST|rst|Needs [RST](http://docutils.sourceforge.net/rst.html) installed.|
|Pandoc|pandoc, pdc|Needs [Pandoc](https://www.pandoc.org/) installed.| |Pandoc|pandoc, pdc|Needs [Pandoc](https://www.pandoc.org/) installed.|
|HTML|html, htm|To be treated as a content file, with layout, shortcodes etc., it must have front matter. If not, it will be copied as-is.| |HTML|html, htm|To be treated as a content file, with layout, shortcodes etc., it must have front matter. If not, it will be copied as-is.|
@ -44,17 +44,17 @@ The `markup identifier` is fetched from either the `markup` variable in front ma
## External Helpers ## 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 needs external helpers installed on your PC. For example, for AsciiDoc files,
Hugo will try to call the `asciidoctor` or `asciidoc` command. This means that you will have to install the associated 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. tool on your machine to be able to use these formats.
Hugo passes reasonable default arguments to these external helpers by default: Hugo passes reasonable default arguments to these external helpers by default:
- `asciidoctor`: `--no-header-footer -v -` - `asciidoctor`: `--no-header-footer --trace -`
- `rst2html`: `--leave-comments --initial-header-level=2` - `rst2html`: `--leave-comments --initial-header-level=2`
- `pandoc`: `--mathjax` - `pandoc`: `--mathjax`
{{% warning "Performance of External Helpers" %}} {{% warning "Performance of External Helpers" %}}
Because additional formats are external commands generation performance will rely heavily on the performance of the external tool you are using. As this feature is still in its infancy, feedback is welcome. Because additional formats are external commands, generation performance will rely heavily on the performance of the external tool you are using. As this feature is still in its infancy, feedback is welcome.
{{% /warning %}} {{% /warning %}}
### External Helper AsciiDoc ### External Helper AsciiDoc
@ -74,14 +74,14 @@ Asciidoctor parameters can be customized in Hugo:
Parameter | Default | Comment Parameter | Default | Comment
--- | --- | --- --- | --- | ---
backend | `html5` | Don't change this unless you know what you are doing. backend | `html5` | Don't change this unless you know what you are doing.
doctype | `article` | Document type (article, book or manpage). 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`. 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). 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. 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. 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. sectionnumbers | `false` | Auto-number section titles.
verbose | `true` | Verbosely print processing information and configuration file checks to stderr. verbose | `false` | Verbosely print processing information and configuration file checks to stderr.
trace | `false` | Include backtrace information on errors. trace | `true` | Include backtrace information on errors.
failurelevel | `fatal` | The minimum logging level that triggers a non-zero exit code (failure). 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`. 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`.
@ -89,7 +89,6 @@ workingfoldercurrent | `false` | Set the working folder to the rendered `adoc` f
[markup.asciidocext] [markup.asciidocext]
extensions = ["asciidoctor-html5s", "asciidoctor-diagram"] extensions = ["asciidoctor-html5s", "asciidoctor-diagram"]
workingFolderCurrent = true workingFolderCurrent = true
trace = true
[markup.asciidocext.attributes] [markup.asciidocext.attributes]
my-base-url = "https://example.com/" my-base-url = "https://example.com/"
my-attribute-name = "my value" my-attribute-name = "my value"

View file

@ -14,23 +14,28 @@
// Package asciidoc_config holds asciidoc related configuration. // Package asciidoc_config holds asciidoc related configuration.
package asciidocext_config package asciidocext_config
// DefaultConfig holds the default asciidoc configuration.
// These values are asciidoctor cli defaults (see https://asciidoctor.org/docs/user-manual/)
var ( var (
// Default holds Hugo's default asciidoc configuration.
Default = Config{ Default = Config{
Backend: "html5", Backend: "html5",
DocType: "article",
Extensions: []string{}, Extensions: []string{},
Attributes: map[string]string{}, Attributes: map[string]string{},
NoHeaderOrFooter: true, NoHeaderOrFooter: true,
SafeMode: "unsafe", SafeMode: "unsafe",
SectionNumbers: false, SectionNumbers: false,
Verbose: true, Verbose: false,
Trace: false, Trace: true,
FailureLevel: "fatal", FailureLevel: "fatal",
WorkingFolderCurrent: false, WorkingFolderCurrent: false,
} }
// CliDefault holds Asciidoctor CLI defaults (see https://asciidoctor.org/docs/user-manual/)
CliDefault = Config{
Backend: "html5",
SafeMode: "unsafe",
FailureLevel: "fatal",
}
AllowedExtensions = map[string]bool{ AllowedExtensions = map[string]bool{
"asciidoctor-html5s": true, "asciidoctor-html5s": true,
"asciidoctor-bibtex": true, "asciidoctor-bibtex": true,
@ -72,7 +77,6 @@ var (
// Config configures asciidoc. // Config configures asciidoc.
type Config struct { type Config struct {
Backend string Backend string
DocType string
Extensions []string Extensions []string
Attributes map[string]string Attributes map[string]string
NoHeaderOrFooter bool NoHeaderOrFooter bool

View file

@ -69,7 +69,6 @@ func (a *asciidocConverter) getAsciidocContent(src []byte, ctx converter.Documen
} }
args := a.parseArgs(ctx) args := a.parseArgs(ctx)
args = append(args, "--trace")
args = append(args, "-") args = append(args, "-")
a.cfg.Logger.INFO.Println("Rendering", ctx.DocumentName, "with", path, "using asciidoctor args", args, "...") a.cfg.Logger.INFO.Println("Rendering", ctx.DocumentName, "with", path, "using asciidoctor args", args, "...")
@ -81,9 +80,7 @@ func (a *asciidocConverter) parseArgs(ctx converter.DocumentContext) []string {
var cfg = a.cfg.MarkupConfig.AsciidocExt var cfg = a.cfg.MarkupConfig.AsciidocExt
args := []string{} args := []string{}
if asciidocext_config.AllowedBackend[cfg.Backend] && cfg.Backend != asciidocext_config.Default.Backend { args = a.appendArg(args, "-b", cfg.Backend, asciidocext_config.CliDefault.Backend, asciidocext_config.AllowedBackend)
args = append(args, "-b", cfg.Backend)
}
for _, extension := range cfg.Extensions { for _, extension := range cfg.Extensions {
if !asciidocext_config.AllowedExtensions[extension] { if !asciidocext_config.AllowedExtensions[extension] {
@ -147,29 +144,36 @@ func (a *asciidocConverter) parseArgs(ctx converter.DocumentContext) []string {
a.cfg.Logger.WARN.Println("asciidoctor parameter NoHeaderOrFooter is expected for correct html rendering") a.cfg.Logger.WARN.Println("asciidoctor parameter NoHeaderOrFooter is expected for correct html rendering")
} }
if cfg.SectionNumbers != asciidocext_config.Default.SectionNumbers { if cfg.SectionNumbers {
args = append(args, "--section-numbers") args = append(args, "--section-numbers")
} }
if cfg.Verbose != asciidocext_config.Default.Verbose { if cfg.Verbose {
args = append(args, "-v") args = append(args, "--verbose")
} }
if cfg.Trace != asciidocext_config.Default.Trace { if cfg.Trace {
args = append(args, "--trace") args = append(args, "--trace")
} }
if asciidocext_config.AllowedFailureLevel[cfg.FailureLevel] && cfg.FailureLevel != asciidocext_config.Default.FailureLevel { args = a.appendArg(args, "--failure-level", cfg.FailureLevel, asciidocext_config.CliDefault.FailureLevel, asciidocext_config.AllowedFailureLevel)
args = append(args, "--failure-level", cfg.FailureLevel)
}
if asciidocext_config.AllowedSafeMode[cfg.SafeMode] && cfg.SafeMode != asciidocext_config.Default.SafeMode { args = a.appendArg(args, "--safe-mode", cfg.SafeMode, asciidocext_config.CliDefault.SafeMode, asciidocext_config.AllowedSafeMode)
args = append(args, "--safe-mode", cfg.SafeMode)
}
return args return args
} }
func (a *asciidocConverter) appendArg(args []string, option, value, defaultValue string, allowedValues map[string]bool) []string {
if value != defaultValue {
if allowedValues[value] {
args = append(args, option, value)
} else {
a.cfg.Logger.ERROR.Println("Unsupported asciidoctor value `" + value + "` for option " + option + " was passed in and will be ignored.")
}
}
return args
}
func getAsciidoctorExecPath() string { func getAsciidoctorExecPath() string {
path, err := exec.LookPath("asciidoctor") path, err := exec.LookPath("asciidoctor")
if err != nil { if err != nil {

View file

@ -18,13 +18,12 @@
package asciidocext package asciidocext
import ( import (
"github.com/gohugoio/hugo/markup/markup_config"
"path/filepath" "path/filepath"
"strings"
"testing" "testing"
"github.com/gohugoio/hugo/common/loggers" "github.com/gohugoio/hugo/common/loggers"
"github.com/gohugoio/hugo/markup/converter" "github.com/gohugoio/hugo/markup/converter"
"github.com/gohugoio/hugo/markup/markup_config"
"github.com/spf13/viper" "github.com/spf13/viper"
qt "github.com/frankban/quicktest" qt "github.com/frankban/quicktest"
@ -51,18 +50,21 @@ func TestAsciidoctorDefaultArgs(t *testing.T) {
c.Assert(ac, qt.Not(qt.IsNil)) c.Assert(ac, qt.Not(qt.IsNil))
args := ac.parseArgs(converter.DocumentContext{}) args := ac.parseArgs(converter.DocumentContext{})
c.Assert(args, qt.Not(qt.IsNil)) expected := []string{"--no-header-footer", "--trace"}
c.Assert(strings.Join(args, " "), qt.Equals, "--no-header-footer") c.Assert(args, qt.DeepEquals, expected)
} }
func TestAsciidoctorDiagramArgs(t *testing.T) { func TestAsciidoctorNonDefaultArgs(t *testing.T) {
c := qt.New(t) c := qt.New(t)
cfg := viper.New() cfg := viper.New()
mconf := markup_config.Default mconf := markup_config.Default
mconf.AsciidocExt.NoHeaderOrFooter = true mconf.AsciidocExt.Backend = "manpage"
mconf.AsciidocExt.Extensions = []string{"asciidoctor-html5s", "asciidoctor-diagram"} mconf.AsciidocExt.NoHeaderOrFooter = false
mconf.AsciidocExt.Backend = "html5s" mconf.AsciidocExt.SafeMode = "safe"
mconf.AsciidocExt.SectionNumbers = true
mconf.AsciidocExt.Verbose = true
mconf.AsciidocExt.Trace = false
mconf.AsciidocExt.FailureLevel = "warn"
p, err := Provider.New( p, err := Provider.New(
converter.ProviderConfig{ converter.ProviderConfig{
Cfg: cfg, Cfg: cfg,
@ -79,8 +81,65 @@ func TestAsciidoctorDiagramArgs(t *testing.T) {
c.Assert(ac, qt.Not(qt.IsNil)) c.Assert(ac, qt.Not(qt.IsNil))
args := ac.parseArgs(converter.DocumentContext{}) args := ac.parseArgs(converter.DocumentContext{})
c.Assert(len(args), qt.Equals, 7) expected := []string{"-b", "manpage", "--section-numbers", "--verbose", "--failure-level", "warn", "--safe-mode", "safe"}
c.Assert(strings.Join(args, " "), qt.Equals, "-b html5s -r asciidoctor-html5s -r asciidoctor-diagram --no-header-footer") c.Assert(args, qt.DeepEquals, expected)
}
func TestAsciidoctorDisallowedArgs(t *testing.T) {
c := qt.New(t)
cfg := viper.New()
mconf := markup_config.Default
mconf.AsciidocExt.Backend = "disallowed-backend"
mconf.AsciidocExt.Extensions = []string{"disallowed-extention"}
mconf.AsciidocExt.Attributes = map[string]string{"outdir": "disallowed-attribute"}
mconf.AsciidocExt.SafeMode = "disallowed-safemode"
mconf.AsciidocExt.FailureLevel = "disallowed-failurelevel"
p, err := Provider.New(
converter.ProviderConfig{
Cfg: cfg,
MarkupConfig: mconf,
Logger: loggers.NewErrorLogger(),
},
)
c.Assert(err, qt.IsNil)
conv, err := p.New(converter.DocumentContext{})
c.Assert(err, qt.IsNil)
ac := conv.(*asciidocConverter)
c.Assert(ac, qt.Not(qt.IsNil))
args := ac.parseArgs(converter.DocumentContext{})
expected := []string{"--no-header-footer", "--trace"}
c.Assert(args, qt.DeepEquals, expected)
}
func TestAsciidoctorDiagramArgs(t *testing.T) {
c := qt.New(t)
cfg := viper.New()
mconf := markup_config.Default
mconf.AsciidocExt.NoHeaderOrFooter = true
mconf.AsciidocExt.Extensions = []string{"asciidoctor-html5s", "asciidoctor-diagram"}
mconf.AsciidocExt.Backend = "html5s"
mconf.AsciidocExt.Trace = false
p, err := Provider.New(
converter.ProviderConfig{
Cfg: cfg,
MarkupConfig: mconf,
Logger: loggers.NewErrorLogger(),
},
)
c.Assert(err, qt.IsNil)
conv, err := p.New(converter.DocumentContext{})
c.Assert(err, qt.IsNil)
ac := conv.(*asciidocConverter)
c.Assert(ac, qt.Not(qt.IsNil))
args := ac.parseArgs(converter.DocumentContext{})
expected := []string{"-b", "html5s", "-r", "asciidoctor-html5s", "-r", "asciidoctor-diagram", "--no-header-footer"}
c.Assert(args, qt.DeepEquals, expected)
} }
func TestAsciidoctorWorkingFolderCurrent(t *testing.T) { func TestAsciidoctorWorkingFolderCurrent(t *testing.T) {
@ -88,6 +147,7 @@ func TestAsciidoctorWorkingFolderCurrent(t *testing.T) {
cfg := viper.New() cfg := viper.New()
mconf := markup_config.Default mconf := markup_config.Default
mconf.AsciidocExt.WorkingFolderCurrent = true mconf.AsciidocExt.WorkingFolderCurrent = true
mconf.AsciidocExt.Trace = false
p, err := Provider.New( p, err := Provider.New(
converter.ProviderConfig{ converter.ProviderConfig{
Cfg: cfg, Cfg: cfg,
@ -121,6 +181,7 @@ func TestAsciidoctorWorkingFolderCurrentAndExtensions(t *testing.T) {
mconf.AsciidocExt.Extensions = []string{"asciidoctor-html5s", "asciidoctor-diagram"} mconf.AsciidocExt.Extensions = []string{"asciidoctor-html5s", "asciidoctor-diagram"}
mconf.AsciidocExt.Backend = "html5s" mconf.AsciidocExt.Backend = "html5s"
mconf.AsciidocExt.WorkingFolderCurrent = true mconf.AsciidocExt.WorkingFolderCurrent = true
mconf.AsciidocExt.Trace = false
p, err := Provider.New( p, err := Provider.New(
converter.ProviderConfig{ converter.ProviderConfig{
Cfg: cfg, Cfg: cfg,
@ -156,6 +217,7 @@ func TestAsciidoctorAttributes(t *testing.T) {
cfg := viper.New() cfg := viper.New()
mconf := markup_config.Default mconf := markup_config.Default
mconf.AsciidocExt.Attributes = map[string]string{"my-base-url": "https://gohugo.io/", "my-attribute-name": "my value"} mconf.AsciidocExt.Attributes = map[string]string{"my-base-url": "https://gohugo.io/", "my-attribute-name": "my value"}
mconf.AsciidocExt.Trace = false
p, err := Provider.New( p, err := Provider.New(
converter.ProviderConfig{ converter.ProviderConfig{
Cfg: cfg, Cfg: cfg,