summaryrefslogtreecommitdiffstats
path: root/markup/asciidocext
diff options
context:
space:
mode:
Diffstat (limited to 'markup/asciidocext')
-rw-r--r--markup/asciidocext/asciidocext_config/config.go83
-rw-r--r--markup/asciidocext/convert.go184
-rw-r--r--markup/asciidocext/convert_test.go210
3 files changed, 477 insertions, 0 deletions
diff --git a/markup/asciidocext/asciidocext_config/config.go b/markup/asciidocext/asciidocext_config/config.go
new file mode 100644
index 000000000..8cc3e79e6
--- /dev/null
+++ b/markup/asciidocext/asciidocext_config/config.go
@@ -0,0 +1,83 @@
+// Copyright 2020 The Hugo Authors. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+// Package asciidoc_config holds asciidoc related configuration.
+package asciidocext_config
+
+// DefaultConfig holds the default asciidoc configuration.
+// These values are asciidoctor cli defaults (see https://asciidoctor.org/docs/user-manual/)
+var (
+ Default = Config{
+ Backend: "html5",
+ DocType: "article",
+ Extensions: []string{},
+ Attributes: map[string]string{},
+ NoHeaderOrFooter: true,
+ SafeMode: "unsafe",
+ SectionNumbers: false,
+ Verbose: true,
+ Trace: false,
+ FailureLevel: "fatal",
+ WorkingFolderCurrent: false,
+ }
+
+ AllowedExtensions = map[string]bool{
+ "asciidoctor-html5s": true,
+ "asciidoctor-diagram": true,
+ "asciidoctor-interdoc-reftext": true,
+ "asciidoctor-katex": true,
+ "asciidoctor-latex": true,
+ "asciidoctor-question": true,
+ "asciidoctor-rouge": true,
+ }
+
+ AllowedSafeMode = map[string]bool{
+ "unsafe": true,
+ "safe": true,
+ "server": true,
+ "secure": true,
+ }
+
+ AllowedFailureLevel = map[string]bool{
+ "fatal": true,
+ "warn": true,
+ }
+
+ AllowedBackend = map[string]bool{
+ "html5": true,
+ "html5s": true,
+ "xhtml5": true,
+ "docbook5": true,
+ "docbook45": true,
+ "manpage": true,
+ }
+
+ DisallowedAttributes = map[string]bool{
+ "outdir": true,
+ }
+)
+
+// Config configures asciidoc.
+type Config struct {
+ Backend string
+ DocType string
+ Extensions []string
+ Attributes map[string]string
+ NoHeaderOrFooter bool
+ SafeMode string
+ SectionNumbers bool
+ Verbose bool
+ Trace bool
+ FailureLevel string
+ WorkingFolderCurrent bool
+}
diff --git a/markup/asciidocext/convert.go b/markup/asciidocext/convert.go
new file mode 100644
index 000000000..5c794819b
--- /dev/null
+++ b/markup/asciidocext/convert.go
@@ -0,0 +1,184 @@
+// Copyright 2020 The Hugo Authors. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+// Package asciidocext converts Asciidoc to HTML using Asciidoc or Asciidoctor
+// external binaries. The `asciidoc` module is reserved for a future golang
+// implementation.
+package asciidocext
+
+import (
+ "os/exec"
+ "path/filepath"
+
+ "github.com/gohugoio/hugo/identity"
+ "github.com/gohugoio/hugo/markup/asciidocext/asciidocext_config"
+ "github.com/gohugoio/hugo/markup/converter"
+ "github.com/gohugoio/hugo/markup/internal"
+)
+
+/* ToDo: RelPermalink patch for svg posts not working*/
+type pageSubset interface {
+ RelPermalink() string
+}
+
+// Provider is the package entry point.
+var Provider converter.ProviderProvider = provider{}
+
+type provider struct{}
+
+func (p provider) New(cfg converter.ProviderConfig) (converter.Provider, error) {
+ return converter.NewProvider("asciidocext", func(ctx converter.DocumentContext) (converter.Converter, error) {
+ return &asciidocConverter{
+ ctx: ctx,
+ cfg: cfg,
+ }, nil
+ }), nil
+}
+
+type asciidocConverter struct {
+ ctx converter.DocumentContext
+ cfg converter.ProviderConfig
+}
+
+func (a *asciidocConverter) Convert(ctx converter.RenderContext) (converter.Result, error) {
+ return converter.Bytes(a.getAsciidocContent(ctx.Src, a.ctx)), nil
+}
+
+func (c *asciidocConverter) Supports(feature identity.Identity) bool {
+ return false
+}
+
+// getAsciidocContent calls asciidoctor or asciidoc as an external helper
+// to convert AsciiDoc content to HTML.
+func (a *asciidocConverter) getAsciidocContent(src []byte, ctx converter.DocumentContext) []byte {
+ path := getAsciidoctorExecPath()
+ if path == "" {
+ a.cfg.Logger.ERROR.Println("asciidoctor / asciidoc not found in $PATH: Please install.\n",
+ " Leaving AsciiDoc content unrendered.")
+ return src
+ }
+
+ args := a.parseArgs(ctx)
+ args = append(args, "--trace")
+ args = append(args, "-")
+
+ a.cfg.Logger.INFO.Println("Rendering", ctx.DocumentName, "with", path, "using asciidoctor args", args, "...")
+
+ return internal.ExternallyRenderContent(a.cfg, ctx, src, path, args)
+}
+
+func (a *asciidocConverter) parseArgs(ctx converter.DocumentContext) []string {
+ var cfg = a.cfg.MarkupConfig.AsciidocExt
+ args := []string{}
+
+ if asciidocext_config.AllowedBackend[cfg.Backend] && cfg.Backend != asciidocext_config.Default.Backend {
+ args = append(args, "-b", cfg.Backend)
+ }
+
+ for _, extension := range cfg.Extensions {
+ if !asciidocext_config.AllowedExtensions[extension] {
+ a.cfg.Logger.ERROR.Println("Unsupported asciidoctor extension was passed in. Extension `" + extension + "` ignored.")
+ continue
+ }
+
+ args = append(args, "-r", extension)
+ }
+
+ for attributeKey, attributeValue := range cfg.Attributes {
+ if asciidocext_config.DisallowedAttributes[attributeKey] {
+ a.cfg.Logger.ERROR.Println("Unsupported asciidoctor attribute was passed in. Attribute `" + attributeKey + "` ignored.")
+ continue
+ }
+
+ args = append(args, "-a", attributeKey+"="+attributeValue)
+ }
+
+ if cfg.WorkingFolderCurrent {
+ contentDir := filepath.Dir(ctx.Filename)
+ sourceDir := a.cfg.Cfg.GetString("source")
+ destinationDir := a.cfg.Cfg.GetString("destination")
+
+ if destinationDir == "" {
+ a.cfg.Logger.ERROR.Println("markup.asciidocext.workingFolderCurrent requires hugo command option --destination to be set")
+ }
+ if !filepath.IsAbs(destinationDir) && sourceDir != "" {
+ destinationDir = filepath.Join(sourceDir, destinationDir)
+ }
+
+ var outDir string
+ var err error
+
+ file := filepath.Base(ctx.Filename)
+ if a.cfg.Cfg.GetBool("uglyUrls") || file == "_index.adoc" || file == "index.adoc" {
+ outDir, err = filepath.Abs(filepath.Dir(filepath.Join(destinationDir, ctx.DocumentName)))
+
+ } else {
+ postDir := ""
+ page, ok := ctx.Document.(pageSubset)
+ if ok {
+ postDir = filepath.Base(page.RelPermalink())
+ } else {
+ a.cfg.Logger.ERROR.Println("unable to cast interface to pageSubset")
+ }
+
+ outDir, err = filepath.Abs(filepath.Join(destinationDir, filepath.Dir(ctx.DocumentName), postDir))
+ }
+
+ if err != nil {
+ a.cfg.Logger.ERROR.Println("asciidoctor outDir: ", err)
+ }
+
+ args = append(args, "--base-dir", contentDir, "-a", "outdir="+outDir)
+ }
+
+ if cfg.NoHeaderOrFooter {
+ args = append(args, "--no-header-footer")
+ } else {
+ a.cfg.Logger.WARN.Println("asciidoctor parameter NoHeaderOrFooter is expected for correct html rendering")
+ }
+
+ if cfg.SectionNumbers != asciidocext_config.Default.SectionNumbers {
+ args = append(args, "--section-numbers")
+ }
+
+ if cfg.Verbose != asciidocext_config.Default.Verbose {
+ args = append(args, "-v")
+ }
+
+ if cfg.Trace != asciidocext_config.Default.Trace {
+ args = append(args, "--trace")
+ }
+
+ if asciidocext_config.AllowedFailureLevel[cfg.FailureLevel] && cfg.FailureLevel != asciidocext_config.Default.FailureLevel {
+ args = append(args, "--failure-level", cfg.FailureLevel)
+ }
+
+ if asciidocext_config.AllowedSafeMode[cfg.SafeMode] && cfg.SafeMode != asciidocext_config.Default.SafeMode {
+ args = append(args, "--safe-mode", cfg.SafeMode)
+ }
+
+ return args
+}
+
+func getAsciidoctorExecPath() string {
+ path, err := exec.LookPath("asciidoctor")
+ if err != nil {
+ return ""
+ }
+ return path
+}
+
+// Supports returns whether Asciidoctor is installed on this computer.
+func Supports() bool {
+ return getAsciidoctorExecPath() != ""
+}
diff --git a/markup/asciidocext/convert_test.go b/markup/asciidocext/convert_test.go
new file mode 100644
index 000000000..7fa1035c3
--- /dev/null
+++ b/markup/asciidocext/convert_test.go
@@ -0,0 +1,210 @@
+// Copyright 2020 The Hugo Authors. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+// Package asciidocext converts Asciidoc to HTML using Asciidoc or Asciidoctor
+// external binaries. The `asciidoc` module is reserved for a future golang
+// implementation.
+
+package asciidocext
+
+import (
+ "github.com/gohugoio/hugo/markup/markup_config"
+ "path/filepath"
+ "strings"
+ "testing"
+
+ "github.com/gohugoio/hugo/common/loggers"
+ "github.com/gohugoio/hugo/markup/converter"
+ "github.com/spf13/viper"
+
+ qt "github.com/frankban/quicktest"
+)
+
+func TestAsciidoctorDefaultArgs(t *testing.T) {
+ c := qt.New(t)
+ cfg := viper.New()
+ mconf := markup_config.Default
+
+ 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{})
+ c.Assert(args, qt.Not(qt.IsNil))
+ c.Assert(strings.Join(args, " "), qt.Equals, "--no-header-footer")
+}
+
+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"
+
+ 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{})
+ c.Assert(len(args), qt.Equals, 7)
+ c.Assert(strings.Join(args, " "), qt.Equals, "-b html5s -r asciidoctor-html5s -r asciidoctor-diagram --no-header-footer")
+}
+
+func TestAsciidoctorWorkingFolderCurrent(t *testing.T) {
+ c := qt.New(t)
+ cfg := viper.New()
+ mconf := markup_config.Default
+ mconf.AsciidocExt.WorkingFolderCurrent = true
+ p, err := Provider.New(
+ converter.ProviderConfig{
+ Cfg: cfg,
+ MarkupConfig: mconf,
+ Logger: loggers.NewErrorLogger(),
+ },
+ )
+ c.Assert(err, qt.IsNil)
+
+ ctx := converter.DocumentContext{Filename: "/tmp/hugo_asciidoc_ddd/docs/chapter2/index.adoc", DocumentName: "chapter2/index.adoc"}
+ conv, err := p.New(ctx)
+ c.Assert(err, qt.IsNil)
+
+ ac := conv.(*asciidocConverter)
+ c.Assert(ac, qt.Not(qt.IsNil))
+
+ args := ac.parseArgs(ctx)
+ c.Assert(len(args), qt.Equals, 5)
+ c.Assert(args[0], qt.Equals, "--base-dir")
+ c.Assert(filepath.ToSlash(args[1]), qt.Matches, "/tmp/hugo_asciidoc_ddd/docs/chapter2")
+ c.Assert(args[2], qt.Equals, "-a")
+ c.Assert(args[3], qt.Matches, `outdir=.*[/\\]{1,2}asciidocext[/\\]{1,2}chapter2`)
+ c.Assert(args[4], qt.Equals, "--no-header-footer")
+}
+
+func TestAsciidoctorWorkingFolderCurrentAndExtensions(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.WorkingFolderCurrent = true
+ 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{})
+ c.Assert(len(args), qt.Equals, 11)
+ c.Assert(args[0], qt.Equals, "-b")
+ c.Assert(args[1], qt.Equals, "html5s")
+ c.Assert(args[2], qt.Equals, "-r")
+ c.Assert(args[3], qt.Equals, "asciidoctor-html5s")
+ c.Assert(args[4], qt.Equals, "-r")
+ c.Assert(args[5], qt.Equals, "asciidoctor-diagram")
+ c.Assert(args[6], qt.Equals, "--base-dir")
+ c.Assert(args[7], qt.Equals, ".")
+ c.Assert(args[8], qt.Equals, "-a")
+ c.Assert(args[9], qt.Contains, "outdir=")
+ c.Assert(args[10], qt.Equals, "--no-header-footer")
+}
+
+func TestAsciidoctorAttributes(t *testing.T) {
+ c := qt.New(t)
+ cfg := viper.New()
+ mconf := markup_config.Default
+ mconf.AsciidocExt.Attributes = map[string]string{"my-base-url": "https://gohugo.io/", "my-attribute-name": "my value"}
+ 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))
+
+ expectedValues := map[string]bool{
+ "my-base-url=https://gohugo.io/": true,
+ "my-attribute-name=my value": true,
+ }
+
+ args := ac.parseArgs(converter.DocumentContext{})
+ c.Assert(len(args), qt.Equals, 5)
+ c.Assert(args[0], qt.Equals, "-a")
+ c.Assert(expectedValues[args[1]], qt.Equals, true)
+ c.Assert(args[2], qt.Equals, "-a")
+ c.Assert(expectedValues[args[3]], qt.Equals, true)
+ c.Assert(args[4], qt.Equals, "--no-header-footer")
+
+}
+
+func TestConvert(t *testing.T) {
+ if !Supports() {
+ t.Skip("asciidoc/asciidoctor not installed")
+ }
+ c := qt.New(t)
+
+ 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("testContent")})
+ c.Assert(err, qt.IsNil)
+ c.Assert(string(b.Bytes()), qt.Equals, "<div class=\"paragraph\">\n<p>testContent</p>\n</div>\n")
+}