Debian Codegraph @

markup/markup.go

raw
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 "github.com/gohugoio/hugo/media"
23
24 "github.com/gohugoio/hugo/markup/markup_config"
25
26 "github.com/gohugoio/hugo/markup/goldmark"
27
28 "github.com/gohugoio/hugo/markup/org"
29
30 "github.com/gohugoio/hugo/markup/asciidocext"
31 "github.com/gohugoio/hugo/markup/converter"
32 "github.com/gohugoio/hugo/markup/pandoc"
33 "github.com/gohugoio/hugo/markup/rst"
34 )
35
36 func NewConverterProvider(cfg converter.ProviderConfig) (ConverterProvider, error) {
37 converters := make(map[string]converter.Provider)
38
39 mcfg := cfg.MarkupConfig()
40
41 if cfg.Highlighter == nil {
42 cfg.Highlighter = highlight.New(mcfg.Highlight)
43 }
44
45 defaultHandler := mcfg.DefaultMarkdownHandler
46 var defaultFound bool
47
48 add := func(p converter.ProviderProvider, subType string, 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 aliases = append(aliases, subType)
58
59 if strings.EqualFold(name, defaultHandler) {
60 aliases = append(aliases, "markdown")
61 defaultFound = true
62 }
63
64 addConverter(converters, c, aliases...)
65 return nil
66 }
67
68 contentTypes := cfg.Conf.ContentTypes().(media.ContentTypes)
69
70 if err := add(goldmark.Provider, contentTypes.Markdown.SubType, contentTypes.Markdown.Suffixes()...); err != nil {
71 return nil, err
72 }
73 if err := add(asciidocext.Provider, contentTypes.AsciiDoc.SubType, contentTypes.AsciiDoc.Suffixes()...); err != nil {
74 return nil, err
75 }
76 if err := add(rst.Provider, contentTypes.ReStructuredText.SubType, contentTypes.ReStructuredText.Suffixes()...); err != nil {
77 return nil, err
78 }
79 if err := add(pandoc.Provider, contentTypes.Pandoc.SubType, contentTypes.Pandoc.Suffixes()...); err != nil {
80 return nil, err
81 }
82 if err := add(org.Provider, contentTypes.EmacsOrgMode.SubType, contentTypes.EmacsOrgMode.Suffixes()...); 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 IsGoldmark(name string) bool
103 // Default() converter.Provider
104 GetMarkupConfig() markup_config.Config
105 GetHighlighter() highlight.Highlighter
106 }
107
108 type converterRegistry struct {
109 // Maps name (md, markdown, goldmark etc.) to a converter provider.
110 // Note that this is also used for aliasing, so the same converter
111 // may be registered multiple times.
112 // All names are lower case.
113 converters map[string]converter.Provider
114
115 config converter.ProviderConfig
116 }
117
118 func (r *converterRegistry) IsGoldmark(name string) bool {
119 cp := r.Get(name)
120 return cp != nil && cp.Name() == "goldmark"
121 }
122
123 func (r *converterRegistry) Get(name string) converter.Provider {
124 return r.converters[strings.ToLower(name)]
125 }
126
127 func (r *converterRegistry) GetHighlighter() highlight.Highlighter {
128 return r.config.Highlighter
129 }
130
131 func (r *converterRegistry) GetMarkupConfig() markup_config.Config {
132 return r.config.MarkupConfig()
133 }
134
135 func addConverter(m map[string]converter.Provider, c converter.Provider, aliases ...string) {
136 for _, alias := range aliases {
137 m[alias] = c
138 }
139 }
140
141 // ResolveMarkup returns the markup type.
142 func ResolveMarkup(s string) string {
143 s = strings.ToLower(s)
144 switch s {
145 case "goldmark":
146 return media.DefaultContentTypes.Markdown.SubType
147 case "asciidocext":
148 return media.DefaultContentTypes.AsciiDoc.SubType
149 default:
150 return s
151 }
152 }