| 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 rst converts content to HTML using the RST external helper. |
| 15 |
package rst |
| 16 |
|
| 17 |
import ( |
| 18 |
"bytes" |
| 19 |
"fmt" |
| 20 |
"os/exec" |
| 21 |
"runtime" |
| 22 |
|
| 23 |
"github.com/gohugoio/hugo/common/hexec" |
| 24 |
"github.com/gohugoio/hugo/htesting" |
| 25 |
|
| 26 |
"github.com/gohugoio/hugo/identity" |
| 27 |
|
| 28 |
"github.com/gohugoio/hugo/markup/converter" |
| 29 |
"github.com/gohugoio/hugo/markup/internal" |
| 30 |
) |
| 31 |
|
| 32 |
// Provider is the package entry point. |
| 33 |
var Provider converter.ProviderProvider = provider{} |
| 34 |
|
| 35 |
type provider struct{} |
| 36 |
|
| 37 |
func (p provider) New(cfg converter.ProviderConfig) (converter.Provider, error) { |
| 38 |
return converter.NewProvider("rst", func(ctx converter.DocumentContext) (converter.Converter, error) { |
| 39 |
return &rstConverter{ |
| 40 |
ctx: ctx, |
| 41 |
cfg: cfg, |
| 42 |
}, nil |
| 43 |
}), nil |
| 44 |
} |
| 45 |
|
| 46 |
type rstConverter struct { |
| 47 |
ctx converter.DocumentContext |
| 48 |
cfg converter.ProviderConfig |
| 49 |
} |
| 50 |
|
| 51 |
func (c *rstConverter) Convert(ctx converter.RenderContext) (converter.ResultRender, error) { |
| 52 |
b, err := c.getRstContent(ctx.Src, c.ctx) |
| 53 |
if err != nil { |
| 54 |
return nil, err |
| 55 |
} |
| 56 |
return converter.Bytes(b), nil |
| 57 |
} |
| 58 |
|
| 59 |
func (c *rstConverter) Supports(feature identity.Identity) bool { |
| 60 |
return false |
| 61 |
} |
| 62 |
|
| 63 |
// getRstContent calls the Python script rst2html as an external helper |
| 64 |
// to convert reStructuredText content to HTML. |
| 65 |
func (c *rstConverter) getRstContent(src []byte, ctx converter.DocumentContext) ([]byte, error) { |
| 66 |
logger := c.cfg.Logger |
| 67 |
binaryName, binaryPath := getRstBinaryNameAndPath() |
| 68 |
|
| 69 |
if binaryName == "" { |
| 70 |
return nil, fmt.Errorf("rst2html / rst2html.py not found in $PATH, cannot render %q", ctx.DocumentName) |
| 71 |
} |
| 72 |
|
| 73 |
logger.Infoln("Rendering", ctx.DocumentName, "with", binaryName, "...") |
| 74 |
|
| 75 |
args := []string{"--leave-comments", "--initial-header-level=2"} |
| 76 |
if c.cfg.Conf != nil && c.cfg.MarkupConfig().RST.SyntaxHighlight != "long" { |
| 77 |
args = append(args, "--syntax-highlight="+c.cfg.MarkupConfig().RST.SyntaxHighlight) |
| 78 |
} |
| 79 |
|
| 80 |
var result []byte |
| 81 |
var err error |
| 82 |
|
| 83 |
// certain *nix based OSs wrap executables in scripted launchers |
| 84 |
// invoking binaries on these OSs via python interpreter causes SyntaxError |
| 85 |
// invoke directly so that shebangs work as expected |
| 86 |
// handle Windows manually because it doesn't do shebangs |
| 87 |
if runtime.GOOS == "windows" { |
| 88 |
pythonBinary, _ := internal.GetPythonBinaryAndExecPath() |
| 89 |
args = append([]string{binaryPath}, args...) |
| 90 |
result, err = internal.ExternallyRenderContent(c.cfg, ctx, src, pythonBinary, args) |
| 91 |
} else { |
| 92 |
result, err = internal.ExternallyRenderContent(c.cfg, ctx, src, binaryName, args) |
| 93 |
} |
| 94 |
|
| 95 |
if err != nil { |
| 96 |
return nil, err |
| 97 |
} |
| 98 |
|
| 99 |
// TODO(bep) check if rst2html has a body only option. |
| 100 |
bodyStart := bytes.Index(result, []byte("<body>\n")) |
| 101 |
if bodyStart < 0 { |
| 102 |
bodyStart = -7 // compensate for length |
| 103 |
} |
| 104 |
|
| 105 |
bodyEnd := bytes.Index(result, []byte("\n</body>")) |
| 106 |
if bodyEnd < 0 || bodyEnd >= len(result) { |
| 107 |
bodyEnd = max(len(result)-1, 0) |
| 108 |
} |
| 109 |
|
| 110 |
return result[bodyStart+7 : bodyEnd], err |
| 111 |
} |
| 112 |
|
| 113 |
var rst2Binaries = []string{"rst2html", "rst2html.py"} |
| 114 |
|
| 115 |
func getRstBinaryNameAndPath() (string, string) { |
| 116 |
for _, candidate := range rst2Binaries { |
| 117 |
if pth := hexec.LookPath(candidate); pth != "" { |
| 118 |
return candidate, pth |
| 119 |
} |
| 120 |
} |
| 121 |
return "", "" |
| 122 |
} |
| 123 |
|
| 124 |
// Supports returns whether rst is (or should be) installed on this computer. |
| 125 |
func Supports() bool { |
| 126 |
name, _ := getRstBinaryNameAndPath() |
| 127 |
hasBin := name != "" |
| 128 |
if htesting.SupportsAll() { |
| 129 |
if !hasBin { |
| 130 |
panic("rst not installed") |
| 131 |
} |
| 132 |
return true |
| 133 |
} |
| 134 |
return hasBin |
| 135 |
} |
| 136 |
|
| 137 |
// SupportsPygments returns whether Pygments is (or should be) installed on this computer. |
| 138 |
func SupportsPygments() bool { |
| 139 |
for _, python := range []string{"python3", "python"} { |
| 140 |
if err := exec.Command(python, "-c", "import pygments").Run(); err == nil { |
| 141 |
return true |
| 142 |
} |
| 143 |
} |
| 144 |
if htesting.SupportsAll() { |
| 145 |
panic("Pygments not installed") |
| 146 |
} |
| 147 |
return false |
| 148 |
} |