|
| 1 | +// Copyright 2020 The Gitea Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a MIT-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package config |
| 6 | + |
| 7 | +import ( |
| 8 | + "fmt" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/yuin/goldmark/ast" |
| 12 | + "github.com/yuin/goldmark/parser" |
| 13 | + "gopkg.in/yaml.v3" |
| 14 | +) |
| 15 | + |
| 16 | +var renderConfigKey = parser.NewContextKey() |
| 17 | + |
| 18 | +func GetRenderConfig(pc parser.Context) *RenderConfig { |
| 19 | + return pc.Get(renderConfigKey).(*RenderConfig) |
| 20 | +} |
| 21 | + |
| 22 | +func SetRenderConfig(pc parser.Context, rc *RenderConfig) { |
| 23 | + pc.Set(renderConfigKey, rc) |
| 24 | +} |
| 25 | + |
| 26 | +// RenderConfig represents rendering configuration for this file |
| 27 | +type RenderConfig struct { |
| 28 | + Meta string |
| 29 | + Icon string |
| 30 | + TOC bool |
| 31 | + Lang string |
| 32 | + Math *MathConfig |
| 33 | + yamlNode *yaml.Node |
| 34 | +} |
| 35 | + |
| 36 | +type MathConfig struct { |
| 37 | + InlineDollar bool `yaml:"inline_dollar"` |
| 38 | + InlineLatex bool `yaml:"inline_latex"` |
| 39 | + DisplayDollar bool `yaml:"display_dollar"` |
| 40 | + DisplayLatex bool `yaml:"display_latex"` |
| 41 | +} |
| 42 | + |
| 43 | +// UnmarshalYAML implement yaml.v3 UnmarshalYAML |
| 44 | +func (rc *RenderConfig) UnmarshalYAML(value *yaml.Node) error { |
| 45 | + rc.yamlNode = value |
| 46 | + |
| 47 | + basic := &yamlRenderConfig{} |
| 48 | + err := value.Decode(basic) |
| 49 | + if err != nil { |
| 50 | + return fmt.Errorf("failed to decode basic: %w", err) |
| 51 | + } |
| 52 | + |
| 53 | + if basic.Lang != "" { |
| 54 | + rc.Lang = basic.Lang |
| 55 | + } |
| 56 | + |
| 57 | + rc.TOC = basic.TOC |
| 58 | + |
| 59 | + if basic.Math != nil { |
| 60 | + rc.Math = basic.Math |
| 61 | + } |
| 62 | + |
| 63 | + if basic.Gitea != nil { |
| 64 | + if basic.Gitea.Meta != nil { |
| 65 | + rc.Meta = *basic.Gitea.Meta |
| 66 | + } |
| 67 | + if basic.Gitea.Icon != nil { |
| 68 | + rc.Icon = *basic.Gitea.Icon |
| 69 | + } |
| 70 | + if basic.Gitea.Lang != nil { |
| 71 | + rc.Lang = *basic.Gitea.Lang |
| 72 | + } |
| 73 | + if basic.Gitea.TOC != nil { |
| 74 | + rc.TOC = *basic.Gitea.TOC |
| 75 | + } |
| 76 | + if basic.Gitea.Math != nil { |
| 77 | + rc.Math = basic.Gitea.Math |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + return nil |
| 82 | +} |
| 83 | + |
| 84 | +type yamlRenderConfig struct { |
| 85 | + TOC bool `yaml:"include_toc"` |
| 86 | + Lang string `yaml:"lang"` |
| 87 | + Math *MathConfig `yaml:"math"` |
| 88 | + Gitea *yamlGitea `yaml:"gitea"` |
| 89 | +} |
| 90 | + |
| 91 | +type yamlGitea struct { |
| 92 | + Meta *string |
| 93 | + Icon *string `yaml:"details_icon"` |
| 94 | + TOC *bool `yaml:"include_toc"` |
| 95 | + Lang *string |
| 96 | + Math *MathConfig |
| 97 | +} |
| 98 | + |
| 99 | +func (y *yamlGitea) UnmarshalYAML(node *yaml.Node) error { |
| 100 | + var controlString string |
| 101 | + if err := node.Decode(&controlString); err == nil { |
| 102 | + var meta string |
| 103 | + switch strings.TrimSpace(strings.ToLower(controlString)) { |
| 104 | + case "none": |
| 105 | + meta = "none" |
| 106 | + case "table": |
| 107 | + meta = "table" |
| 108 | + default: // "details" |
| 109 | + meta = "details" |
| 110 | + } |
| 111 | + y.Meta = &meta |
| 112 | + return nil |
| 113 | + } |
| 114 | + |
| 115 | + type yExactType yamlGitea |
| 116 | + yExact := (*yExactType)(y) |
| 117 | + if err := node.Decode(yExact); err != nil { |
| 118 | + return fmt.Errorf("unable to parse yamlGitea: %w", err) |
| 119 | + } |
| 120 | + y = (*yamlGitea)(yExact) |
| 121 | + |
| 122 | + return nil |
| 123 | +} |
| 124 | + |
| 125 | +func (m *MathConfig) UnmarshalYAML(node *yaml.Node) error { |
| 126 | + var controlBool bool |
| 127 | + if err := node.Decode(&controlBool); err == nil { |
| 128 | + m.InlineLatex = controlBool |
| 129 | + m.DisplayLatex = controlBool |
| 130 | + m.DisplayDollar = controlBool |
| 131 | + // Not InlineDollar |
| 132 | + m.InlineDollar = false |
| 133 | + return nil |
| 134 | + } |
| 135 | + |
| 136 | + var enableMathStrs []string |
| 137 | + if err := node.Decode(&enableMathStrs); err != nil { |
| 138 | + var enableMathStr string |
| 139 | + if err := node.Decode(&enableMathStr); err == nil { |
| 140 | + m.InlineLatex = false |
| 141 | + m.DisplayLatex = false |
| 142 | + m.DisplayDollar = false |
| 143 | + m.InlineDollar = false |
| 144 | + if enableMathStr == "" { |
| 145 | + enableMathStr = "true" |
| 146 | + } |
| 147 | + enableMathStrs = strings.Split(enableMathStr, ",") |
| 148 | + } |
| 149 | + } |
| 150 | + if enableMathStrs != nil { |
| 151 | + for _, value := range enableMathStrs { |
| 152 | + switch strings.TrimSpace(strings.ToLower(value)) { |
| 153 | + case "all": |
| 154 | + m.InlineLatex = true |
| 155 | + m.DisplayLatex = true |
| 156 | + m.DisplayDollar = true |
| 157 | + m.InlineDollar = true |
| 158 | + break |
| 159 | + case "inline_dollar": |
| 160 | + m.InlineDollar = true |
| 161 | + case "inline_latex": |
| 162 | + m.InlineLatex = true |
| 163 | + case "display_dollar": |
| 164 | + m.DisplayDollar = true |
| 165 | + case "display_latex": |
| 166 | + m.DisplayLatex = true |
| 167 | + case "true": |
| 168 | + m.InlineLatex = true |
| 169 | + m.DisplayLatex = true |
| 170 | + m.DisplayDollar = true |
| 171 | + } |
| 172 | + } |
| 173 | + return nil |
| 174 | + } |
| 175 | + |
| 176 | + type mExactType MathConfig |
| 177 | + mExact := (*mExactType)(m) |
| 178 | + if err := node.Decode(mExact); err != nil { |
| 179 | + return fmt.Errorf("unable to parse MathConfig: %w", err) |
| 180 | + } |
| 181 | + m = (*MathConfig)(mExact) |
| 182 | + return nil |
| 183 | +} |
| 184 | + |
| 185 | +func (rc *RenderConfig) ToMetaNode() ast.Node { |
| 186 | + if rc.yamlNode == nil { |
| 187 | + return nil |
| 188 | + } |
| 189 | + switch rc.Meta { |
| 190 | + case "table": |
| 191 | + return nodeToTable(rc.yamlNode) |
| 192 | + case "details": |
| 193 | + return nodeToDetails(rc.yamlNode, rc.Icon) |
| 194 | + default: |
| 195 | + return nil |
| 196 | + } |
| 197 | +} |
0 commit comments