hugo

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

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

markup.go (3479B)

    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 markup
   15 
   16 import (
   17 	"fmt"
   18 	"strings"
   19 
   20 	"github.com/gohugoio/hugo/markup/highlight"
   21 
   22 	"github.com/gohugoio/hugo/markup/markup_config"
   23 
   24 	"github.com/gohugoio/hugo/markup/goldmark"
   25 
   26 	"github.com/gohugoio/hugo/markup/org"
   27 
   28 	"github.com/gohugoio/hugo/markup/asciidocext"
   29 	"github.com/gohugoio/hugo/markup/converter"
   30 	"github.com/gohugoio/hugo/markup/pandoc"
   31 	"github.com/gohugoio/hugo/markup/rst"
   32 )
   33 
   34 func NewConverterProvider(cfg converter.ProviderConfig) (ConverterProvider, error) {
   35 	converters := make(map[string]converter.Provider)
   36 
   37 	markupConfig, err := markup_config.Decode(cfg.Cfg)
   38 	if err != nil {
   39 		return nil, err
   40 	}
   41 
   42 	if cfg.Highlighter == nil {
   43 		cfg.Highlighter = highlight.New(markupConfig.Highlight)
   44 	}
   45 
   46 	cfg.MarkupConfig = markupConfig
   47 	defaultHandler := cfg.MarkupConfig.DefaultMarkdownHandler
   48 	var defaultFound bool
   49 
   50 	add := func(p converter.ProviderProvider, aliases ...string) error {
   51 		c, err := p.New(cfg)
   52 		if err != nil {
   53 			return err
   54 		}
   55 
   56 		name := c.Name()
   57 
   58 		aliases = append(aliases, name)
   59 
   60 		if strings.EqualFold(name, defaultHandler) {
   61 			aliases = append(aliases, "markdown")
   62 			defaultFound = true
   63 		}
   64 
   65 		addConverter(converters, c, aliases...)
   66 		return nil
   67 	}
   68 
   69 	if err := add(goldmark.Provider); err != nil {
   70 		return nil, err
   71 	}
   72 	if err := add(asciidocext.Provider, "ad", "adoc"); err != nil {
   73 		return nil, err
   74 	}
   75 	if err := add(rst.Provider); err != nil {
   76 		return nil, err
   77 	}
   78 	if err := add(pandoc.Provider, "pdc"); err != nil {
   79 		return nil, err
   80 	}
   81 	if err := add(org.Provider); err != nil {
   82 		return nil, err
   83 	}
   84 
   85 	if !defaultFound {
   86 		msg := "markup: Configured defaultMarkdownHandler %q not found."
   87 		if defaultHandler == "blackfriday" {
   88 			msg += " Did you mean to use goldmark? Blackfriday was removed in Hugo v0.100.0."
   89 		}
   90 		return nil, fmt.Errorf(msg, defaultHandler)
   91 	}
   92 
   93 	return &converterRegistry{
   94 		config:     cfg,
   95 		converters: converters,
   96 	}, nil
   97 }
   98 
   99 type ConverterProvider interface {
  100 	Get(name string) converter.Provider
  101 	// Default() converter.Provider
  102 	GetMarkupConfig() markup_config.Config
  103 	GetHighlighter() highlight.Highlighter
  104 }
  105 
  106 type converterRegistry struct {
  107 	// Maps name (md, markdown, goldmark etc.) to a converter provider.
  108 	// Note that this is also used for aliasing, so the same converter
  109 	// may be registered multiple times.
  110 	// All names are lower case.
  111 	converters map[string]converter.Provider
  112 
  113 	config converter.ProviderConfig
  114 }
  115 
  116 func (r *converterRegistry) Get(name string) converter.Provider {
  117 	return r.converters[strings.ToLower(name)]
  118 }
  119 
  120 func (r *converterRegistry) GetHighlighter() highlight.Highlighter {
  121 	return r.config.Highlighter
  122 }
  123 
  124 func (r *converterRegistry) GetMarkupConfig() markup_config.Config {
  125 	return r.config.MarkupConfig
  126 }
  127 
  128 func addConverter(m map[string]converter.Provider, c converter.Provider, aliases ...string) {
  129 	for _, alias := range aliases {
  130 		m[alias] = c
  131 	}
  132 }