Skip to content

Commit

Permalink
Generalize some Pandoc options.
Browse files Browse the repository at this point in the history
  • Loading branch information
asankah committed Aug 1, 2020
1 parent 1016d21 commit a9701cb
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 30 deletions.
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,6 @@ github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s=
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
Expand Down
5 changes: 2 additions & 3 deletions markup/markup_config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import (
"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/pandoc/pandoc_config"
"github.com/gohugoio/hugo/markup/highlight"
"github.com/gohugoio/hugo/markup/pandoc/pandoc_config"
"github.com/gohugoio/hugo/markup/tableofcontents"
"github.com/gohugoio/hugo/parser"
"github.com/mitchellh/mapstructure"
Expand All @@ -38,8 +38,7 @@ type Config struct {
// Content renderers
Goldmark goldmark_config.Config
BlackFriday blackfriday_config.Config
Pandoc pandoc_config.Config

Pandoc pandoc_config.Config
AsciidocExt asciidocext_config.Config
}

Expand Down
26 changes: 1 addition & 25 deletions markup/pandoc/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
package pandoc

import (
"strings"
"fmt"
"os/exec"

"github.com/gohugoio/hugo/identity"
Expand Down Expand Up @@ -64,29 +62,7 @@ func (c *pandocConverter) getPandocContent(src []byte, ctx converter.DocumentCon
return src
}

args := []string{"--mathjax"}

if len(c.cfg.MarkupConfig.Pandoc.Filters) > 0 {
for _, filter := range c.cfg.MarkupConfig.Pandoc.Filters {
args = append(args, fmt.Sprintf("--filter=%s", filter))
}
}

if len(c.cfg.MarkupConfig.Pandoc.Extensions) > 0 {
var b strings.Builder
b.WriteString("--from=markdown")
for _, extension := range c.cfg.MarkupConfig.Pandoc.Extensions {
b.WriteString("+")
b.WriteString(extension)
}
args = append(args, b.String())
}

if len(c.cfg.MarkupConfig.Pandoc.ExtraArgs) > 0 {
args = append(args, c.cfg.MarkupConfig.Pandoc.ExtraArgs...)
}

return internal.ExternallyRenderContent(c.cfg, ctx, src, path, args)
return internal.ExternallyRenderContent(c.cfg, ctx, src, path, c.cfg.MarkupConfig.Pandoc.AsPandocArguments())
}

func getPandocExecPath() string {
Expand Down
90 changes: 89 additions & 1 deletion markup/pandoc/pandoc_config/pandoc.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package pandoc_config

import (
"fmt"
"strings"
)

// Config contains configuration settings for Pandoc.
type Config struct {
// Input format. Use the 'Extensions' field to specify extensions thereof.
Expand Down Expand Up @@ -40,6 +45,89 @@ type Config struct {
// --from=markdown+foo+bar on the pandoc commandline.
Extensions []string

// Random extra arguments.
// List of input format extensions to use. Specifying ["foo", "bar"] is
// equivalent to specifying --from=markdown+foo+bar on the pandoc commandline
// assuming InputFormat is "markdown".
InputExtensions []string

// List of output format extensions to use. Specifying ["foo", "bar"] is
// equivalent to specifying --to=html5+foo+bar on the pandoc commandline,
// assuming UseLegacyHTML is false. Invoke "pandoc --list-extensions=html5" to
// or "pandoc --list-extensions=html5" to see the list of extensions that can
// be specified here.
OutputExtensions []string

// Extra commandline options passed to the pandoc invocation. These options
// are appended to the commandline after the format and filter options.
// Arguments are passed in literally. Hence must have the "--" or "-" prefix
// where applicable.
ExtraArgs []string
}

func (c *Config) getInputArg() string {
var b strings.Builder
b.WriteString("--from=")
if len(c.InputFormat) > 0 {
b.WriteString(c.InputFormat)
} else {
b.WriteString("markdown")
}

for _, extension := range c.InputExtensions {
b.WriteString("+")
b.WriteString(extension)
}
return b.String()
}

func (c *Config) getOutputArg() string {
var b strings.Builder
b.WriteString("--to=")
if c.UseLegacyHtml {
b.WriteString("html")
} else {
b.WriteString("html5")
}

for _, extension := range c.OutputExtensions {
b.WriteString("+")
b.WriteString(extension)
}
return b.String()
}

func (c *Config) getMathRenderingArg() string {
switch {
case c.UseMathml:
return "--mathml"
case c.UseWebtex:
return "--webtex"
case c.UseKatex:
return "--katex"
default:
return "--mathjax"
}
}

func (c *Config) getFilterArgs() []string {
var args []string
for _, filter := range c.Filters {
args = append(args, fmt.Sprintf("--filter=%s", filter))
}
return args
}

// AsPandocArguments returns a list of strings that can be used as arguments to
// a "pandoc" invocation. All the settings contained in Config are represented
// in the returned list of arguments.
func (c *Config) AsPandocArguments() []string {
args := []string{
c.getInputArg(),
c.getOutputArg(),
c.getMathRenderingArg()}

args = append(args, c.getFilterArgs()...)
args = append(args, c.ExtraArgs...)

return args
}

0 comments on commit a9701cb

Please sign in to comment.