|
1 | 1 | package pandoc_config
|
2 | 2 |
|
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | +) |
| 7 | + |
3 | 8 | // Config contains configuration settings for Pandoc.
|
4 | 9 | type Config struct {
|
5 | 10 | // Input format. Use the 'Extensions' field to specify extensions thereof.
|
@@ -40,6 +45,89 @@ type Config struct {
|
40 | 45 | // --from=markdown+foo+bar on the pandoc commandline.
|
41 | 46 | Extensions []string
|
42 | 47 |
|
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. |
44 | 64 | ExtraArgs []string
|
45 | 65 | }
|
| 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