convert.go - hugo - [fork] hugo port for 9front
HTML git clone https://git.drkhsh.at/hugo.git
DIR Log
DIR Files
DIR Refs
DIR Submodules
DIR README
DIR LICENSE
---
convert.go (2613B)
---
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 "github.com/gohugoio/hugo/common/hexec"
19 "github.com/gohugoio/hugo/htesting"
20 "github.com/gohugoio/hugo/identity"
21
22 "github.com/gohugoio/hugo/markup/converter"
23 "github.com/gohugoio/hugo/markup/internal"
24 )
25
26 // Provider is the package entry point.
27 var Provider converter.ProviderProvider = provider{}
28
29 type provider struct{}
30
31 func (p provider) New(cfg converter.ProviderConfig) (converter.Provider, error) {
32 return converter.NewProvider("pandoc", func(ctx converter.DocumentContext) (converter.Converter, error) {
33 return &pandocConverter{
34 ctx: ctx,
35 cfg: cfg,
36 }, nil
37 }), nil
38 }
39
40 type pandocConverter struct {
41 ctx converter.DocumentContext
42 cfg converter.ProviderConfig
43 }
44
45 func (c *pandocConverter) Convert(ctx converter.RenderContext) (converter.ResultRender, error) {
46 b, err := c.getPandocContent(ctx.Src, c.ctx)
47 if err != nil {
48 return nil, err
49 }
50 return converter.Bytes(b), nil
51 }
52
53 func (c *pandocConverter) Supports(feature identity.Identity) bool {
54 return false
55 }
56
57 // getPandocContent calls pandoc as an external helper to convert pandoc markdown to HTML.
58 func (c *pandocConverter) getPandocContent(src []byte, ctx converter.DocumentContext) ([]byte, error) {
59 logger := c.cfg.Logger
60 binaryName := getPandocBinaryName()
61 if binaryName == "" {
62 logger.Println("pandoc not found in $PATH: Please install.\n",
63 " Leaving pandoc content unrendered.")
64 return src, nil
65 }
66 args := []string{"--mathjax"}
67 return internal.ExternallyRenderContent(c.cfg, ctx, src, binaryName, args)
68 }
69
70 const pandocBinary = "pandoc"
71
72 func getPandocBinaryName() string {
73 if hexec.InPath(pandocBinary) {
74 return pandocBinary
75 }
76 return ""
77 }
78
79 // Supports returns whether Pandoc is installed on this computer.
80 func Supports() bool {
81 hasBin := getPandocBinaryName() != ""
82 if htesting.SupportsAll() {
83 if !hasBin {
84 panic("pandoc not installed")
85 }
86 return true
87 }
88 return hasBin
89 }