Skip to content

Commit 9d25ff5

Browse files
committed
Generalize some Pandoc options.
1 parent ee674d3 commit 9d25ff5

File tree

3 files changed

+92
-29
lines changed

3 files changed

+92
-29
lines changed

markup/markup_config/config.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ import (
1919
"github.com/gohugoio/hugo/markup/asciidocext/asciidocext_config"
2020
"github.com/gohugoio/hugo/markup/blackfriday/blackfriday_config"
2121
"github.com/gohugoio/hugo/markup/goldmark/goldmark_config"
22-
"github.com/gohugoio/hugo/markup/pandoc/pandoc_config"
2322
"github.com/gohugoio/hugo/markup/highlight"
23+
"github.com/gohugoio/hugo/markup/pandoc/pandoc_config"
2424
"github.com/gohugoio/hugo/markup/tableofcontents"
2525
"github.com/gohugoio/hugo/parser"
2626
"github.com/mitchellh/mapstructure"
@@ -38,8 +38,7 @@ type Config struct {
3838
// Content renderers
3939
Goldmark goldmark_config.Config
4040
BlackFriday blackfriday_config.Config
41-
Pandoc pandoc_config.Config
42-
41+
Pandoc pandoc_config.Config
4342
AsciidocExt asciidocext_config.Config
4443
}
4544

markup/pandoc/convert.go

+1-25
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
package pandoc
1616

1717
import (
18-
"strings"
19-
"fmt"
2018
"os/exec"
2119

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

67-
args := []string{"--mathjax"}
68-
69-
if len(c.cfg.MarkupConfig.Pandoc.Filters) > 0 {
70-
for _, filter := range c.cfg.MarkupConfig.Pandoc.Filters {
71-
args = append(args, fmt.Sprintf("--filter=%s", filter))
72-
}
73-
}
74-
75-
if len(c.cfg.MarkupConfig.Pandoc.Extensions) > 0 {
76-
var b strings.Builder
77-
b.WriteString("--from=markdown")
78-
for _, extension := range c.cfg.MarkupConfig.Pandoc.Extensions {
79-
b.WriteString("+")
80-
b.WriteString(extension)
81-
}
82-
args = append(args, b.String())
83-
}
84-
85-
if len(c.cfg.MarkupConfig.Pandoc.ExtraArgs) > 0 {
86-
args = append(args, c.cfg.MarkupConfig.Pandoc.ExtraArgs...)
87-
}
88-
89-
return internal.ExternallyRenderContent(c.cfg, ctx, src, path, args)
65+
return internal.ExternallyRenderContent(c.cfg, ctx, src, path, c.cfg.MarkupConfig.Pandoc.AsPandocArguments())
9066
}
9167

9268
func getPandocExecPath() string {

markup/pandoc/pandoc_config/pandoc.go

+89-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package pandoc_config
22

3+
import (
4+
"fmt"
5+
"strings"
6+
)
7+
38
// Config contains configuration settings for Pandoc.
49
type Config struct {
510
// Input format. Use the 'Extensions' field to specify extensions thereof.
@@ -40,6 +45,89 @@ type Config struct {
4045
// --from=markdown+foo+bar on the pandoc commandline.
4146
Extensions []string
4247

43-
// Random extra arguments.
48+
// List of input format extensions to use. Specifying ["foo", "bar"] is
49+
// equivalent to specifying --from=markdown+foo+bar on the pandoc commandline
50+
// assuming InputFormat is "markdown".
51+
InputExtensions []string
52+
53+
// List of output format extensions to use. Specifying ["foo", "bar"] is
54+
// equivalent to specifying --to=html5+foo+bar on the pandoc commandline,
55+
// assuming UseLegacyHTML is false. Invoke "pandoc --list-extensions=html5" to
56+
// or "pandoc --list-extensions=html5" to see the list of extensions that can
57+
// be specified here.
58+
OutputExtensions []string
59+
60+
// Extra commandline options passed to the pandoc invocation. These options
61+
// are appended to the commandline after the format and filter options.
62+
// Arguments are passed in literally. Hence must have the "--" or "-" prefix
63+
// where applicable.
4464
ExtraArgs []string
4565
}
66+
67+
func (c *Config) getInputArg() string {
68+
var b strings.Builder
69+
b.WriteString("--from=")
70+
if len(c.InputFormat) > 0 {
71+
b.WriteString(c.InputFormat)
72+
} else {
73+
b.WriteString("markdown")
74+
}
75+
76+
for _, extension := range c.InputExtensions {
77+
b.WriteString("+")
78+
b.WriteString(extension)
79+
}
80+
return b.String()
81+
}
82+
83+
func (c *Config) getOutputArg() string {
84+
var b strings.Builder
85+
b.WriteString("--to=")
86+
if c.UseLegacyHtml {
87+
b.WriteString("html")
88+
} else {
89+
b.WriteString("html5")
90+
}
91+
92+
for _, extension := range c.OutputExtensions {
93+
b.WriteString("+")
94+
b.WriteString(extension)
95+
}
96+
return b.String()
97+
}
98+
99+
func (c *Config) getMathRenderingArg() string {
100+
switch {
101+
case c.UseMathml:
102+
return "--mathml"
103+
case c.UseWebtex:
104+
return "--webtex"
105+
case c.UseKatex:
106+
return "--katex"
107+
default:
108+
return "--mathjax"
109+
}
110+
}
111+
112+
func (c *Config) getFilterArgs() []string {
113+
var args []string
114+
for _, filter := range c.Filters {
115+
args = append(args, fmt.Sprintf("--filter=%s", filter))
116+
}
117+
return args
118+
}
119+
120+
// AsPandocArguments returns a list of strings that can be used as arguments to
121+
// a "pandoc" invocation. All the settings contained in Config are represented
122+
// in the returned list of arguments.
123+
func (c *Config) AsPandocArguments() []string {
124+
args := []string{
125+
c.getInputArg(),
126+
c.getOutputArg(),
127+
c.getMathRenderingArg()}
128+
129+
args = append(args, c.getFilterArgs()...)
130+
args = append(args, c.ExtraArgs...)
131+
132+
return args
133+
}

0 commit comments

Comments
 (0)