2020-04-24 13:22:36 +00:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package markdown
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
2022-09-13 16:33:37 +00:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
2022-09-19 12:02:29 +00:00
|
|
|
|
2020-04-24 13:22:36 +00:00
|
|
|
"github.com/yuin/goldmark/ast"
|
2022-09-13 16:33:37 +00:00
|
|
|
"gopkg.in/yaml.v3"
|
2020-04-24 13:22:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// RenderConfig represents rendering configuration for this file
|
|
|
|
type RenderConfig struct {
|
2022-09-13 16:33:37 +00:00
|
|
|
Meta string
|
|
|
|
Icon string
|
|
|
|
TOC bool
|
|
|
|
Lang string
|
|
|
|
yamlNode *yaml.Node
|
2020-04-24 13:22:36 +00:00
|
|
|
}
|
|
|
|
|
2022-09-13 16:33:37 +00:00
|
|
|
// UnmarshalYAML implement yaml.v3 UnmarshalYAML
|
|
|
|
func (rc *RenderConfig) UnmarshalYAML(value *yaml.Node) error {
|
|
|
|
if rc == nil {
|
|
|
|
rc = &RenderConfig{
|
|
|
|
Meta: "table",
|
|
|
|
Icon: "table",
|
|
|
|
Lang: "",
|
2020-04-24 13:22:36 +00:00
|
|
|
}
|
|
|
|
}
|
2022-09-13 16:33:37 +00:00
|
|
|
rc.yamlNode = value
|
2020-04-24 13:22:36 +00:00
|
|
|
|
2022-09-13 16:33:37 +00:00
|
|
|
type basicRenderConfig struct {
|
|
|
|
Gitea *yaml.Node `yaml:"gitea"`
|
|
|
|
TOC bool `yaml:"include_toc"`
|
|
|
|
Lang string `yaml:"lang"`
|
2020-04-24 13:22:36 +00:00
|
|
|
}
|
|
|
|
|
2022-09-13 16:33:37 +00:00
|
|
|
var basic basicRenderConfig
|
|
|
|
|
|
|
|
err := value.Decode(&basic)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if basic.Lang != "" {
|
|
|
|
rc.Lang = basic.Lang
|
|
|
|
}
|
|
|
|
|
|
|
|
rc.TOC = basic.TOC
|
|
|
|
if basic.Gitea == nil {
|
2020-04-24 13:22:36 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-13 16:33:37 +00:00
|
|
|
var control *string
|
|
|
|
if err := basic.Gitea.Decode(&control); err == nil && control != nil {
|
|
|
|
log.Info("control %v", control)
|
|
|
|
switch strings.TrimSpace(strings.ToLower(*control)) {
|
|
|
|
case "none":
|
|
|
|
rc.Meta = "none"
|
|
|
|
case "table":
|
|
|
|
rc.Meta = "table"
|
|
|
|
default: // "details"
|
|
|
|
rc.Meta = "details"
|
|
|
|
}
|
|
|
|
return nil
|
2020-04-24 13:22:36 +00:00
|
|
|
}
|
2022-09-13 16:33:37 +00:00
|
|
|
|
|
|
|
type giteaControl struct {
|
|
|
|
Meta string `yaml:"meta"`
|
|
|
|
Icon string `yaml:"details_icon"`
|
|
|
|
TOC *yaml.Node `yaml:"include_toc"`
|
|
|
|
Lang string `yaml:"lang"`
|
2020-04-24 13:22:36 +00:00
|
|
|
}
|
|
|
|
|
2022-09-13 16:33:37 +00:00
|
|
|
var controlStruct *giteaControl
|
|
|
|
if err := basic.Gitea.Decode(controlStruct); err != nil || controlStruct == nil {
|
|
|
|
return err
|
2020-04-24 13:22:36 +00:00
|
|
|
}
|
|
|
|
|
2022-09-13 16:33:37 +00:00
|
|
|
switch strings.TrimSpace(strings.ToLower(controlStruct.Meta)) {
|
|
|
|
case "none":
|
|
|
|
rc.Meta = "none"
|
|
|
|
case "table":
|
|
|
|
rc.Meta = "table"
|
|
|
|
default: // "details"
|
|
|
|
rc.Meta = "details"
|
|
|
|
}
|
|
|
|
|
|
|
|
rc.Icon = strings.TrimSpace(strings.ToLower(controlStruct.Icon))
|
|
|
|
|
|
|
|
if controlStruct.Lang != "" {
|
|
|
|
rc.Lang = controlStruct.Lang
|
|
|
|
}
|
|
|
|
|
|
|
|
var toc bool
|
|
|
|
if err := controlStruct.TOC.Decode(&toc); err == nil {
|
|
|
|
rc.TOC = toc
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-04-24 13:22:36 +00:00
|
|
|
|
2022-09-13 16:33:37 +00:00
|
|
|
func (rc *RenderConfig) toMetaNode() ast.Node {
|
|
|
|
if rc.yamlNode == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
switch rc.Meta {
|
|
|
|
case "table":
|
|
|
|
return nodeToTable(rc.yamlNode)
|
|
|
|
case "details":
|
|
|
|
return nodeToDetails(rc.yamlNode, rc.Icon)
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
2020-04-24 13:22:36 +00:00
|
|
|
}
|