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