hugo

Fork of github.com/gohugoio/hugo with reverse pagination support

git clone git://git.shimmy1996.com/hugo.git

convert.go (2251B)

    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 org converts Emacs Org-Mode to HTML.
   15 package org
   16 
   17 import (
   18 	"bytes"
   19 
   20 	"github.com/gohugoio/hugo/identity"
   21 
   22 	"github.com/gohugoio/hugo/markup/converter"
   23 	"github.com/niklasfasching/go-org/org"
   24 	"github.com/spf13/afero"
   25 )
   26 
   27 // Provider is the package entry point.
   28 var Provider converter.ProviderProvider = provide{}
   29 
   30 type provide struct{}
   31 
   32 func (p provide) New(cfg converter.ProviderConfig) (converter.Provider, error) {
   33 	return converter.NewProvider("org", func(ctx converter.DocumentContext) (converter.Converter, error) {
   34 		return &orgConverter{
   35 			ctx: ctx,
   36 			cfg: cfg,
   37 		}, nil
   38 	}), nil
   39 }
   40 
   41 type orgConverter struct {
   42 	ctx converter.DocumentContext
   43 	cfg converter.ProviderConfig
   44 }
   45 
   46 func (c *orgConverter) Convert(ctx converter.RenderContext) (converter.Result, error) {
   47 	logger := c.cfg.Logger
   48 	config := org.New()
   49 	config.Log = logger.Warn()
   50 	config.ReadFile = func(filename string) ([]byte, error) {
   51 		return afero.ReadFile(c.cfg.ContentFs, filename)
   52 	}
   53 	writer := org.NewHTMLWriter()
   54 	writer.HighlightCodeBlock = func(source, lang string, inline bool) string {
   55 		highlightedSource, err := c.cfg.Highlight(source, lang, "")
   56 		if err != nil {
   57 			logger.Errorf("Could not highlight source as lang %s. Using raw source.", lang)
   58 			return source
   59 		}
   60 		return highlightedSource
   61 	}
   62 
   63 	html, err := config.Parse(bytes.NewReader(ctx.Src), c.ctx.DocumentName).Write(writer)
   64 	if err != nil {
   65 		logger.Errorf("Could not render org: %s. Using unrendered content.", err)
   66 		return converter.Bytes(ctx.Src), nil
   67 	}
   68 	return converter.Bytes([]byte(html)), nil
   69 }
   70 
   71 func (c *orgConverter) Supports(feature identity.Identity) bool {
   72 	return false
   73 }