| 1 |
// Copyright 2019 The Hugo Authors. All rights reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
// Package pandoc converts content to HTML using Pandoc as an external helper. |
| 15 |
package pandoc |
| 16 |
|
| 17 |
import ( |
| 18 |
"bytes" |
| 19 |
"fmt" |
| 20 |
"io" |
| 21 |
"sync" |
| 22 |
|
| 23 |
"github.com/gohugoio/hugo/common/hexec" |
| 24 |
"github.com/gohugoio/hugo/htesting" |
| 25 |
"github.com/gohugoio/hugo/identity" |
| 26 |
|
| 27 |
"github.com/gohugoio/hugo/markup/converter" |
| 28 |
"github.com/gohugoio/hugo/markup/internal" |
| 29 |
) |
| 30 |
|
| 31 |
// Provider is the package entry point. |
| 32 |
var Provider converter.ProviderProvider = provider{} |
| 33 |
|
| 34 |
type provider struct{} |
| 35 |
|
| 36 |
func (p provider) New(cfg converter.ProviderConfig) (converter.Provider, error) { |
| 37 |
return converter.NewProvider("pandoc", func(ctx converter.DocumentContext) (converter.Converter, error) { |
| 38 |
return &pandocConverter{ |
| 39 |
ctx: ctx, |
| 40 |
cfg: cfg, |
| 41 |
}, nil |
| 42 |
}), nil |
| 43 |
} |
| 44 |
|
| 45 |
type pandocConverter struct { |
| 46 |
ctx converter.DocumentContext |
| 47 |
cfg converter.ProviderConfig |
| 48 |
|
| 49 |
mathMethodSupportedOnce sync.Once |
| 50 |
mathMethodSupported bool |
| 51 |
} |
| 52 |
|
| 53 |
func (c *pandocConverter) Convert(ctx converter.RenderContext) (converter.ResultRender, error) { |
| 54 |
b, err := c.getPandocContent(ctx.Src, c.ctx) |
| 55 |
if err != nil { |
| 56 |
return nil, err |
| 57 |
} |
| 58 |
return converter.Bytes(b), nil |
| 59 |
} |
| 60 |
|
| 61 |
func (c *pandocConverter) Supports(feature identity.Identity) bool { |
| 62 |
return false |
| 63 |
} |
| 64 |
|
| 65 |
// getPandocContent calls pandoc as an external helper to convert pandoc markdown to HTML. |
| 66 |
func (c *pandocConverter) getPandocContent(src []byte, ctx converter.DocumentContext) ([]byte, error) { |
| 67 |
binaryName := getPandocBinaryName() |
| 68 |
if binaryName == "" { |
| 69 |
return nil, fmt.Errorf("pandoc not found in $PATH, cannot render %q", ctx.DocumentName) |
| 70 |
} |
| 71 |
mathFlag := "--mathjax" |
| 72 |
if c.pandocSupportsMathMethod() { |
| 73 |
mathFlag = mathMethodFlag |
| 74 |
} |
| 75 |
args := []string{mathFlag, "--citeproc"} |
| 76 |
return internal.ExternallyRenderContent(c.cfg, ctx, src, binaryName, args) |
| 77 |
} |
| 78 |
|
| 79 |
const pandocBinary = "pandoc" |
| 80 |
|
| 81 |
func getPandocBinaryName() string { |
| 82 |
if hexec.InPath(pandocBinary) { |
| 83 |
return pandocBinary |
| 84 |
} |
| 85 |
return "" |
| 86 |
} |
| 87 |
|
| 88 |
// mathMethodFlag replaces the deprecated --mathjax flag in pandoc 3.11 and later. |
| 89 |
const mathMethodFlag = "--math-method=mathjax" |
| 90 |
|
| 91 |
// pandocSupportsMathMethod reports whether the installed pandoc binary |
| 92 |
// supports the --math-method flag. |
| 93 |
func (c *pandocConverter) pandocSupportsMathMethod() bool { |
| 94 |
c.mathMethodSupportedOnce.Do(func() { |
| 95 |
cmd, err := c.cfg.Exec.New( |
| 96 |
pandocBinary, mathMethodFlag, "--dump-args", |
| 97 |
hexec.WithStdout(io.Discard), |
| 98 |
hexec.WithStderr(io.Discard), |
| 99 |
hexec.WithStdin(bytes.NewReader(nil)), |
| 100 |
) |
| 101 |
if err == nil { |
| 102 |
c.mathMethodSupported = cmd.Run() == nil |
| 103 |
} |
| 104 |
}) |
| 105 |
return c.mathMethodSupported |
| 106 |
} |
| 107 |
|
| 108 |
// Supports returns whether Pandoc is installed on this computer. |
| 109 |
func Supports() bool { |
| 110 |
hasBin := getPandocBinaryName() != "" |
| 111 |
if htesting.SupportsAll() { |
| 112 |
if !hasBin { |
| 113 |
panic("pandoc not installed") |
| 114 |
} |
| 115 |
return true |
| 116 |
} |
| 117 |
return hasBin |
| 118 |
} |