|
| 1 | +package pandoc_config |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | +) |
| 7 | + |
| 8 | +// Config contains configuration settings for Pandoc. |
| 9 | +type Config struct { |
| 10 | + // Input format. Use the 'Extensions' field to specify extensions thereof. |
| 11 | + // Only specify the bare format here. Defaults to 'markdown' if empty. Invoke |
| 12 | + // "pandoc --list-input-formats" to see the list of supported input formats |
| 13 | + // including various Markdown dialects. |
| 14 | + InputFormat string |
| 15 | + |
| 16 | + // If true, the output format is HTML (i.e. "--to=html"). Otherwise the output |
| 17 | + // format is HTML5 (i.e. "--to=html5"). |
| 18 | + UseLegacyHtml bool |
| 19 | + |
| 20 | + // Equivalent to specifying "--mathjax". For compatibility, this option is |
| 21 | + // always true if none of the other math options are used. |
| 22 | + // See https://pandoc.org/MANUAL.html#math-rendering-in-html |
| 23 | + UseMathjax bool |
| 24 | + |
| 25 | + // Equivalent to specifying "--mathml". |
| 26 | + // See https://pandoc.org/MANUAL.html#math-rendering-in-html |
| 27 | + UseMathml bool |
| 28 | + |
| 29 | + // Equivalent to specifying "--webtex". |
| 30 | + // See https://pandoc.org/MANUAL.html#math-rendering-in-html. Uses the default |
| 31 | + // Webtex rendering URL. |
| 32 | + UseWebtex bool |
| 33 | + |
| 34 | + // Equivalent to specifying "--katex". |
| 35 | + // See https://pandoc.org/MANUAL.html#math-rendering-in-html |
| 36 | + UseKatex bool |
| 37 | + |
| 38 | + // List of filters to use. These translate to '--filter=' or '--lua-filter' |
| 39 | + // arguments to the pandoc invocation. The order of elements in `Filters` |
| 40 | + // is preserved when constructing the `pandoc` commandline. |
| 41 | + // |
| 42 | + // Use the prefix 'lua:' or the suffix '.lua' to indicate Lua filters. |
| 43 | + Filters []string |
| 44 | + |
| 45 | + // List of Pandoc Markdown extensions to use. No need to include default |
| 46 | + // extensions. Specifying ["foo", "bar"] is equivalent to specifying |
| 47 | + // --from=markdown+foo+bar on the pandoc commandline. |
| 48 | + Extensions []string |
| 49 | + |
| 50 | + // List of input format extensions to use. Specifying ["foo", "bar"] is |
| 51 | + // equivalent to specifying --from=markdown+foo+bar on the pandoc commandline |
| 52 | + // assuming InputFormat is "markdown". |
| 53 | + InputExtensions []string |
| 54 | + |
| 55 | + // List of output format extensions to use. Specifying ["foo", "bar"] is |
| 56 | + // equivalent to specifying --to=html5+foo+bar on the pandoc commandline, |
| 57 | + // assuming UseLegacyHTML is false. Invoke "pandoc --list-extensions=html5" to |
| 58 | + // or "pandoc --list-extensions=html5" to see the list of extensions that can |
| 59 | + // be specified here. |
| 60 | + OutputExtensions []string |
| 61 | + |
| 62 | + // Metadata. The dictionary keys and values are handled in the obvious way. |
| 63 | + Metadata map[string]interface{} |
| 64 | + |
| 65 | + // Extra commandline options passed to the pandoc invocation. These options |
| 66 | + // are appended to the commandline after the format and filter options. |
| 67 | + // Arguments are passed in literally. Hence must have the "--" or "-" prefix |
| 68 | + // where applicable. |
| 69 | + ExtraArgs []string |
| 70 | +} |
| 71 | + |
| 72 | +func (c *Config) getInputArg() string { |
| 73 | + var b strings.Builder |
| 74 | + b.WriteString("--from=") |
| 75 | + if len(c.InputFormat) > 0 { |
| 76 | + b.WriteString(c.InputFormat) |
| 77 | + } else { |
| 78 | + b.WriteString("markdown") |
| 79 | + } |
| 80 | + |
| 81 | + for _, extension := range c.InputExtensions { |
| 82 | + b.WriteString("+") |
| 83 | + b.WriteString(extension) |
| 84 | + } |
| 85 | + return b.String() |
| 86 | +} |
| 87 | + |
| 88 | +func (c *Config) getOutputArg() string { |
| 89 | + var b strings.Builder |
| 90 | + b.WriteString("--to=") |
| 91 | + if c.UseLegacyHtml { |
| 92 | + b.WriteString("html") |
| 93 | + } else { |
| 94 | + b.WriteString("html5") |
| 95 | + } |
| 96 | + |
| 97 | + for _, extension := range c.OutputExtensions { |
| 98 | + b.WriteString("+") |
| 99 | + b.WriteString(extension) |
| 100 | + } |
| 101 | + return b.String() |
| 102 | +} |
| 103 | + |
| 104 | +func (c *Config) getMathRenderingArg() string { |
| 105 | + switch { |
| 106 | + case c.UseMathml: |
| 107 | + return "--mathml" |
| 108 | + case c.UseWebtex: |
| 109 | + return "--webtex" |
| 110 | + case c.UseKatex: |
| 111 | + return "--katex" |
| 112 | + default: |
| 113 | + return "--mathjax" |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +func (c *Config) getMetadataArgs() []string { |
| 118 | + var args []string |
| 119 | + for k, iv := range c.Metadata { |
| 120 | + var v string |
| 121 | + if sv, ok := iv.(string); ok { |
| 122 | + v = sv |
| 123 | + } else if sv, ok := iv.(fmt.Stringer); ok { |
| 124 | + v = sv.String() |
| 125 | + } else { |
| 126 | + v = fmt.Sprintf("%v", iv) |
| 127 | + } |
| 128 | + args = append(args, fmt.Sprintf("-M%s=%s", k, v)) |
| 129 | + } |
| 130 | + return args |
| 131 | +} |
| 132 | + |
| 133 | +func (c *Config) getFilterArgs() []string { |
| 134 | + var args []string |
| 135 | + for _, filterPath := range c.Filters { |
| 136 | + if strings.HasPrefix(filterPath, "lua:") || strings.HasSuffix(filterPath, ".lua") { |
| 137 | + args = append(args, fmt.Sprintf("--lua-filter=%s", strings.TrimPrefix(filterPath, "lua:"))) |
| 138 | + } else { |
| 139 | + args = append(args, fmt.Sprintf("--filter=%s", filterPath)) |
| 140 | + } |
| 141 | + } |
| 142 | + return args |
| 143 | +} |
| 144 | + |
| 145 | +// AsPandocArguments returns a list of strings that can be used as arguments to |
| 146 | +// a "pandoc" invocation. All the settings contained in Config are represented |
| 147 | +// in the returned list of arguments. |
| 148 | +func (c *Config) AsPandocArguments() []string { |
| 149 | + args := []string{ |
| 150 | + c.getInputArg(), |
| 151 | + c.getOutputArg(), |
| 152 | + c.getMathRenderingArg()} |
| 153 | + |
| 154 | + args = append(args, c.getMetadataArgs()...) |
| 155 | + args = append(args, c.getFilterArgs()...) |
| 156 | + args = append(args, c.ExtraArgs...) |
| 157 | + |
| 158 | + return args |
| 159 | +} |
0 commit comments