github.com/graemephi/kahugo@v0.62.3-0.20211121071557-d78c0423784d/markup/pandoc/convert.go (about)

     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/cli/safeexec"
    19  	"github.com/gohugoio/hugo/htesting"
    20  	"github.com/gohugoio/hugo/identity"
    21  	"github.com/gohugoio/hugo/markup/internal"
    22  
    23  	"github.com/gohugoio/hugo/markup/converter"
    24  )
    25  
    26  // Provider is the package entry point.
    27  var Provider converter.ProviderProvider = provider{}
    28  
    29  type provider struct {
    30  }
    31  
    32  func (p provider) New(cfg converter.ProviderConfig) (converter.Provider, error) {
    33  	return converter.NewProvider("pandoc", func(ctx converter.DocumentContext) (converter.Converter, error) {
    34  		return &pandocConverter{
    35  			ctx: ctx,
    36  			cfg: cfg,
    37  		}, nil
    38  	}), nil
    39  }
    40  
    41  type pandocConverter struct {
    42  	ctx converter.DocumentContext
    43  	cfg converter.ProviderConfig
    44  }
    45  
    46  func (c *pandocConverter) Convert(ctx converter.RenderContext) (converter.Result, error) {
    47  	return converter.Bytes(c.getPandocContent(ctx.Src, c.ctx)), nil
    48  }
    49  
    50  func (c *pandocConverter) Supports(feature identity.Identity) bool {
    51  	return false
    52  }
    53  
    54  // getPandocContent calls pandoc as an external helper to convert pandoc markdown to HTML.
    55  func (c *pandocConverter) getPandocContent(src []byte, ctx converter.DocumentContext) []byte {
    56  	logger := c.cfg.Logger
    57  	path := getPandocExecPath()
    58  	if path == "" {
    59  		logger.Println("pandoc not found in $PATH: Please install.\n",
    60  			"                 Leaving pandoc content unrendered.")
    61  		return src
    62  	}
    63  	args := []string{"--mathjax"}
    64  	return internal.ExternallyRenderContent(c.cfg, ctx, src, path, args)
    65  }
    66  
    67  func getPandocExecPath() string {
    68  	path, err := safeexec.LookPath("pandoc")
    69  	if err != nil {
    70  		return ""
    71  	}
    72  
    73  	return path
    74  }
    75  
    76  // Supports returns whether Pandoc is installed on this computer.
    77  func Supports() bool {
    78  	if htesting.SupportsAll() {
    79  		return true
    80  	}
    81  	return getPandocExecPath() != ""
    82  }