mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-07 20:30:36 -05:00
Rework external asciidoctor integration
This commit solves the relative path problem with asciidoctor tooling. An include will resolve relatively, so you can refer easily to files in the same folder. Also `asciidoctor-diagram` and PlantUML rendering works now, because the created temporary files will be placed in the correct folder. This patch covers just the Ruby version of asciidoctor. The old AsciiDoc CLI EOLs in Jan 2020, so this variant is removed from code. The configuration is completely rewritten and now available in `config.toml` under the key `[markup.asciidocext]`: ```toml [markup.asciidocext] extensions = ["asciidoctor-html5s", "asciidoctor-diagram"] workingFolderCurrent = true trace = true [markup.asciidocext.attributes] my-base-url = "https://example.com/" my-attribute-name = "my value" ``` - backends, safe-modes, and extensions are now whitelisted to the popular (ruby) extensions and valid values. - the default for extensions is to not enable any, because they're all external dependencies so the build would break if the user didn't install them beforehand. - the default backend is html5 because html5s is an external gem dependency. - the default safe-mode is safe, explanations of the modes: https://asciidoctor.org/man/asciidoctor/ - the config is namespaced under asciidocext_config and the parser looks at asciidocext to allow a future native Go asciidoc. - `uglyUrls=true` option and `--source` flag are supported - `--destination` flag is required Follow the updated documentation under `docs/content/en/content-management/formats.md`. This patch would be a breaking change, because you need to correct all your absolute include pathes to relative paths, so using relative paths must be configured explicitly by setting `workingFolderCurrent = true`.
This commit is contained in:
parent
77aa385b84
commit
f0266e2ef3
16 changed files with 555 additions and 155 deletions
|
@ -33,8 +33,8 @@ The current list of content formats in Hugo:
|
|||
| Blackfriday | blackfriday |Blackfriday will eventually be deprecated.|
|
||||
|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).|
|
||||
|Asciidoc|asciidoc, adoc, ad|Needs Asciidoc or [Asciidoctor][ascii] installed.|
|
||||
|RST|rst|Needs [RST](https://docutils.sourceforge.io/rst.html) installed.|
|
||||
|AsciiDoc|asciidoc, adoc, ad|Needs AsciiDoc or [Asciidoctor][ascii] installed.|
|
||||
|RST|rst|Needs [RST](http://docutils.sourceforge.net/rst.html) 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.|
|
||||
|
||||
|
@ -43,12 +43,13 @@ The `markup identifier` is fetched from either the `markup` variable in front ma
|
|||
|
||||
## External Helpers
|
||||
|
||||
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 tool on your machine to be able to use these formats. ([See the Asciidoctor docs for installation instructions](https://asciidoctor.org/docs/install-toolchain/)).
|
||||
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
|
||||
tool on your machine to be able to use these formats.
|
||||
|
||||
Hugo passes reasonable default arguments to these external helpers by default:
|
||||
|
||||
- `asciidoc`: `--no-header-footer --safe -`
|
||||
- `asciidoctor`: `--no-header-footer --safe --trace -`
|
||||
- `asciidoctor`: `--no-header-footer -v -`
|
||||
- `rst2html`: `--leave-comments --initial-header-level=2`
|
||||
- `pandoc`: `--mathjax`
|
||||
|
||||
|
@ -56,6 +57,53 @@ Hugo passes reasonable default arguments to these external helpers by default:
|
|||
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 %}}
|
||||
|
||||
### External Helper AsciiDoc
|
||||
|
||||
[AsciiDoc](https://github.com/asciidoc/asciidoc) implementation EOLs in Jan 2020 and is no longer supported.
|
||||
AsciiDoc development is being continued under [Asciidoctor](https://github.com/asciidoctor). The format AsciiDoc
|
||||
remains of course. Please continue with the implementation Asciidoctor.
|
||||
|
||||
### External Helper Asciidoctor
|
||||
|
||||
The Asciidoctor community offers a wide set of tools for the AsciiDoc format that can be installed additionally to Hugo.
|
||||
[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:
|
||||
|
||||
Parameter | Default | Comment
|
||||
--- | --- | ---
|
||||
backend | `html5` | Don't change this unless you know what you are doing.
|
||||
doctype | `article` | Document type (article, book or manpage).
|
||||
extensions | | Possible extensions are `asciidoctor-html5s`, `asciidoctor-diagram`, `asciidoctor-interdoc-reftext`, `asciidoctor-katex`, `asciidoctor-latex`, `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 | `true` | 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`.
|
||||
|
||||
```
|
||||
[markup.asciidocext]
|
||||
extensions = ["asciidoctor-html5s", "asciidoctor-diagram"]
|
||||
workingFolderCurrent = true
|
||||
trace = true
|
||||
[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
|
||||
|
||||
```
|
||||
INFO 2019/12/22 09:08:48 Rendering book-as-pdf.adoc with C:\Ruby26-x64\bin\asciidoctor.bat using asciidoc args [--no-header-footer -r asciidoctor-html5s -b html5s -r asciidoctor-diagram --base-dir D:\prototypes\hugo_asciidoc_ddd\docs -a outdir=D:\prototypes\hugo_asciidoc_ddd\build -] ...
|
||||
```
|
||||
|
||||
## Learn Markdown
|
||||
|
||||
Markdown syntax is simple enough to learn in a single sitting. The following are excellent resources to get you up and running:
|
||||
|
|
|
@ -40,9 +40,9 @@ func TestResolveMarkup(t *testing.T) {
|
|||
{"md", "markdown"},
|
||||
{"markdown", "markdown"},
|
||||
{"mdown", "markdown"},
|
||||
{"asciidoc", "asciidoc"},
|
||||
{"adoc", "asciidoc"},
|
||||
{"ad", "asciidoc"},
|
||||
{"asciidocext", "asciidocext"},
|
||||
{"adoc", "asciidocext"},
|
||||
{"ad", "asciidocext"},
|
||||
{"rst", "rst"},
|
||||
{"pandoc", "pandoc"},
|
||||
{"pdc", "pandoc"},
|
||||
|
|
|
@ -737,6 +737,7 @@ func (p *pageMeta) newContentConverter(ps *pageState, markup string, renderingCo
|
|||
Document: newPageForRenderHook(ps),
|
||||
DocumentID: id,
|
||||
DocumentName: p.Path(),
|
||||
Filename: p.f.Filename(),
|
||||
ConfigOverrides: renderingConfigOverrides,
|
||||
},
|
||||
)
|
||||
|
|
|
@ -485,7 +485,7 @@ func splitUserDefinedSummaryAndContent(markup string, c []byte) (summary []byte,
|
|||
|
||||
startTag := "p"
|
||||
switch markup {
|
||||
case "asciidoc":
|
||||
case "asciidocext":
|
||||
startTag = "div"
|
||||
|
||||
}
|
||||
|
|
|
@ -20,7 +20,7 @@ import (
|
|||
|
||||
"github.com/gohugoio/hugo/markup/rst"
|
||||
|
||||
"github.com/gohugoio/hugo/markup/asciidoc"
|
||||
"github.com/gohugoio/hugo/markup/asciidocext"
|
||||
|
||||
"github.com/gohugoio/hugo/config"
|
||||
|
||||
|
@ -377,7 +377,7 @@ func testAllMarkdownEnginesForPages(t *testing.T,
|
|||
}{
|
||||
{"md", func() bool { return true }},
|
||||
{"mmark", func() bool { return true }},
|
||||
{"ad", func() bool { return asciidoc.Supports() }},
|
||||
{"ad", func() bool { return asciidocext.Supports() }},
|
||||
{"rst", func() bool { return rst.Supports() }},
|
||||
}
|
||||
|
||||
|
|
|
@ -18,7 +18,7 @@ import (
|
|||
"path/filepath"
|
||||
"reflect"
|
||||
|
||||
"github.com/gohugoio/hugo/markup/asciidoc"
|
||||
"github.com/gohugoio/hugo/markup/asciidocext"
|
||||
"github.com/gohugoio/hugo/markup/rst"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
|
@ -552,7 +552,7 @@ title: "Foo"
|
|||
|
||||
temp := tests[:0]
|
||||
for _, test := range tests {
|
||||
if strings.HasSuffix(test.contentPath, ".ad") && !asciidoc.Supports() {
|
||||
if strings.HasSuffix(test.contentPath, ".ad") && !asciidocext.Supports() {
|
||||
t.Log("Skip Asciidoc test case as no Asciidoc present.")
|
||||
continue
|
||||
} else if strings.HasSuffix(test.contentPath, ".rst") && !rst.Supports() {
|
||||
|
|
|
@ -1,101 +0,0 @@
|
|||
// Copyright 2019 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 converts Asciidoc to HTML using Asciidoc or Asciidoctor
|
||||
// external binaries.
|
||||
package asciidoc
|
||||
|
||||
import (
|
||||
"os/exec"
|
||||
|
||||
"github.com/gohugoio/hugo/identity"
|
||||
"github.com/gohugoio/hugo/markup/internal"
|
||||
|
||||
"github.com/gohugoio/hugo/markup/converter"
|
||||
)
|
||||
|
||||
// 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("asciidoc", 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 {
|
||||
var isAsciidoctor bool
|
||||
path := getAsciidoctorExecPath()
|
||||
if path == "" {
|
||||
path = getAsciidocExecPath()
|
||||
if path == "" {
|
||||
a.cfg.Logger.ERROR.Println("asciidoctor / asciidoc not found in $PATH: Please install.\n",
|
||||
" Leaving AsciiDoc content unrendered.")
|
||||
return src
|
||||
}
|
||||
} else {
|
||||
isAsciidoctor = true
|
||||
}
|
||||
|
||||
a.cfg.Logger.INFO.Println("Rendering", ctx.DocumentName, "with", path, "...")
|
||||
args := []string{"--no-header-footer", "--safe"}
|
||||
if isAsciidoctor {
|
||||
// asciidoctor-specific arg to show stack traces on errors
|
||||
args = append(args, "--trace")
|
||||
}
|
||||
args = append(args, "-")
|
||||
return internal.ExternallyRenderContent(a.cfg, ctx, src, path, args)
|
||||
}
|
||||
|
||||
func getAsciidocExecPath() string {
|
||||
path, err := exec.LookPath("asciidoc")
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
||||
func getAsciidoctorExecPath() string {
|
||||
path, err := exec.LookPath("asciidoctor")
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
||||
// Supports returns whether Asciidoc or Asciidoctor is installed on this computer.
|
||||
func Supports() bool {
|
||||
return (getAsciidoctorExecPath() != "" ||
|
||||
getAsciidocExecPath() != "")
|
||||
}
|
|
@ -1,38 +0,0 @@
|
|||
// Copyright 2019 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
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/gohugoio/hugo/common/loggers"
|
||||
|
||||
"github.com/gohugoio/hugo/markup/converter"
|
||||
|
||||
qt "github.com/frankban/quicktest"
|
||||
)
|
||||
|
||||
func TestConvert(t *testing.T) {
|
||||
if !Supports() {
|
||||
t.Skip("asciidoc/asciidoctor not installed")
|
||||
}
|
||||
c := qt.New(t)
|
||||
p, err := Provider.New(converter.ProviderConfig{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")
|
||||
}
|
83
markup/asciidocext/asciidocext_config/config.go
Normal file
83
markup/asciidocext/asciidocext_config/config.go
Normal file
|
@ -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
|
||||
}
|
184
markup/asciidocext/convert.go
Normal file
184
markup/asciidocext/convert.go
Normal file
|
@ -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() != ""
|
||||
}
|
210
markup/asciidocext/convert_test.go
Normal file
210
markup/asciidocext/convert_test.go
Normal file
|
@ -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")
|
||||
}
|
|
@ -119,6 +119,7 @@ type DocumentContext struct {
|
|||
Document interface{} // May be nil. Usually a page.Page
|
||||
DocumentID string
|
||||
DocumentName string
|
||||
Filename string
|
||||
ConfigOverrides map[string]interface{}
|
||||
}
|
||||
|
||||
|
|
|
@ -24,7 +24,7 @@ import (
|
|||
|
||||
"github.com/gohugoio/hugo/markup/org"
|
||||
|
||||
"github.com/gohugoio/hugo/markup/asciidoc"
|
||||
"github.com/gohugoio/hugo/markup/asciidocext"
|
||||
"github.com/gohugoio/hugo/markup/blackfriday"
|
||||
"github.com/gohugoio/hugo/markup/converter"
|
||||
"github.com/gohugoio/hugo/markup/mmark"
|
||||
|
@ -76,7 +76,7 @@ func NewConverterProvider(cfg converter.ProviderConfig) (ConverterProvider, erro
|
|||
if err := add(mmark.Provider); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := add(asciidoc.Provider, "ad", "adoc"); err != nil {
|
||||
if err := add(asciidocext.Provider, "ad", "adoc"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := add(rst.Provider); err != nil {
|
||||
|
|
|
@ -16,6 +16,7 @@ package markup_config
|
|||
import (
|
||||
"github.com/gohugoio/hugo/config"
|
||||
"github.com/gohugoio/hugo/docshelper"
|
||||
"github.com/gohugoio/hugo/markup/asciidocext/asciidocext_config"
|
||||
"github.com/gohugoio/hugo/markup/blackfriday/blackfriday_config"
|
||||
"github.com/gohugoio/hugo/markup/goldmark/goldmark_config"
|
||||
"github.com/gohugoio/hugo/markup/highlight"
|
||||
|
@ -36,6 +37,8 @@ type Config struct {
|
|||
// Content renderers
|
||||
Goldmark goldmark_config.Config
|
||||
BlackFriday blackfriday_config.Config
|
||||
|
||||
AsciidocExt asciidocext_config.Config
|
||||
}
|
||||
|
||||
func Decode(cfg config.Provider) (conf Config, err error) {
|
||||
|
@ -91,6 +94,8 @@ var Default = Config{
|
|||
|
||||
Goldmark: goldmark_config.Default,
|
||||
BlackFriday: blackfriday_config.Default,
|
||||
|
||||
AsciidocExt: asciidocext_config.Default,
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
|
|
@ -34,6 +34,11 @@ func TestConfig(t *testing.T) {
|
|||
"unsafe": true,
|
||||
},
|
||||
},
|
||||
"asciidocext": map[string]interface{}{
|
||||
"workingFolderCurrent": true,
|
||||
"safeMode": "save",
|
||||
"extensions": []string{"asciidoctor-html5s"},
|
||||
},
|
||||
})
|
||||
|
||||
conf, err := Decode(v)
|
||||
|
@ -42,6 +47,8 @@ func TestConfig(t *testing.T) {
|
|||
c.Assert(conf.Goldmark.Renderer.Unsafe, qt.Equals, true)
|
||||
c.Assert(conf.BlackFriday.Fractions, qt.Equals, true)
|
||||
|
||||
c.Assert(conf.AsciidocExt.WorkingFolderCurrent, qt.Equals, true)
|
||||
c.Assert(conf.AsciidocExt.Extensions[0], qt.Equals, "asciidoctor-html5s")
|
||||
})
|
||||
|
||||
c.Run("legacy", func(c *qt.C) {
|
||||
|
|
|
@ -42,7 +42,7 @@ func TestConverterRegistry(t *testing.T) {
|
|||
|
||||
checkName("goldmark")
|
||||
checkName("mmark")
|
||||
checkName("asciidoc")
|
||||
checkName("asciidocext")
|
||||
checkName("rst")
|
||||
checkName("pandoc")
|
||||
checkName("org")
|
||||
|
|
Loading…
Reference in a new issue