github.com/kovansky/hugo@v0.92.3-0.20220224232819-63076e4ff19f/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 15 16 import ( 17 "strings" 18 19 "github.com/gohugoio/hugo/markup/highlight" 20 21 "github.com/gohugoio/hugo/markup/markup_config" 22 23 "github.com/gohugoio/hugo/markup/goldmark" 24 25 "github.com/gohugoio/hugo/markup/org" 26 27 "github.com/gohugoio/hugo/markup/asciidocext" 28 "github.com/gohugoio/hugo/markup/blackfriday" 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 48 add := func(p converter.ProviderProvider, aliases ...string) error { 49 c, err := p.New(cfg) 50 if err != nil { 51 return err 52 } 53 54 name := c.Name() 55 56 aliases = append(aliases, name) 57 58 if strings.EqualFold(name, cfg.MarkupConfig.DefaultMarkdownHandler) { 59 aliases = append(aliases, "markdown") 60 } 61 62 addConverter(converters, c, aliases...) 63 return nil 64 } 65 66 if err := add(goldmark.Provider); err != nil { 67 return nil, err 68 } 69 if err := add(blackfriday.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 return &converterRegistry{ 86 config: cfg, 87 converters: converters, 88 }, nil 89 } 90 91 type ConverterProvider interface { 92 Get(name string) converter.Provider 93 // Default() converter.Provider 94 GetMarkupConfig() markup_config.Config 95 GetHighlighter() highlight.Highlighter 96 } 97 98 type converterRegistry struct { 99 // Maps name (md, markdown, blackfriday etc.) to a converter provider. 100 // Note that this is also used for aliasing, so the same converter 101 // may be registered multiple times. 102 // All names are lower case. 103 converters map[string]converter.Provider 104 105 config converter.ProviderConfig 106 } 107 108 func (r *converterRegistry) Get(name string) converter.Provider { 109 return r.converters[strings.ToLower(name)] 110 } 111 112 func (r *converterRegistry) GetHighlighter() highlight.Highlighter { 113 return r.config.Highlighter 114 } 115 116 func (r *converterRegistry) GetMarkupConfig() markup_config.Config { 117 return r.config.MarkupConfig 118 } 119 120 func addConverter(m map[string]converter.Provider, c converter.Provider, aliases ...string) { 121 for _, alias := range aliases { 122 m[alias] = c 123 } 124 }