2020-04-24 18:52:36 +05:30
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2020-04-24 18:52:36 +05:30
|
|
|
|
|
|
|
package markdown
|
|
|
|
|
|
|
|
import (
|
2022-10-06 00:25:36 +05:30
|
|
|
"fmt"
|
2020-04-24 18:52:36 +05:30
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/yuin/goldmark/ast"
|
2022-09-13 22:03:37 +05:30
|
|
|
"gopkg.in/yaml.v3"
|
2020-04-24 18:52:36 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
// RenderConfig represents rendering configuration for this file
|
|
|
|
type RenderConfig struct {
|
2022-09-13 22:03:37 +05:30
|
|
|
Meta string
|
|
|
|
Icon string
|
|
|
|
TOC bool
|
|
|
|
Lang string
|
|
|
|
yamlNode *yaml.Node
|
2020-04-24 18:52:36 +05:30
|
|
|
}
|
|
|
|
|
2022-09-13 22:03:37 +05:30
|
|
|
// 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 18:52:36 +05:30
|
|
|
}
|
|
|
|
}
|
2022-09-13 22:03:37 +05:30
|
|
|
rc.yamlNode = value
|
2020-04-24 18:52:36 +05:30
|
|
|
|
2022-10-06 00:25:36 +05:30
|
|
|
type commonRenderConfig struct {
|
|
|
|
TOC bool `yaml:"include_toc"`
|
|
|
|
Lang string `yaml:"lang"`
|
2020-04-24 18:52:36 +05:30
|
|
|
}
|
2022-10-06 00:25:36 +05:30
|
|
|
var basic commonRenderConfig
|
|
|
|
if err := value.Decode(&basic); err != nil {
|
|
|
|
return fmt.Errorf("unable to decode into commonRenderConfig %w", err)
|
2022-09-13 22:03:37 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if basic.Lang != "" {
|
|
|
|
rc.Lang = basic.Lang
|
|
|
|
}
|
|
|
|
|
|
|
|
rc.TOC = basic.TOC
|
2022-10-06 00:25:36 +05:30
|
|
|
|
|
|
|
type controlStringRenderConfig struct {
|
|
|
|
Gitea string `yaml:"gitea"`
|
2020-04-24 18:52:36 +05:30
|
|
|
}
|
|
|
|
|
2022-10-06 00:25:36 +05:30
|
|
|
var stringBasic controlStringRenderConfig
|
|
|
|
|
|
|
|
if err := value.Decode(&stringBasic); err == nil {
|
|
|
|
if stringBasic.Gitea != "" {
|
|
|
|
switch strings.TrimSpace(strings.ToLower(stringBasic.Gitea)) {
|
|
|
|
case "none":
|
|
|
|
rc.Meta = "none"
|
|
|
|
case "table":
|
|
|
|
rc.Meta = "table"
|
|
|
|
default: // "details"
|
|
|
|
rc.Meta = "details"
|
|
|
|
}
|
2022-09-13 22:03:37 +05:30
|
|
|
}
|
|
|
|
return nil
|
2020-04-24 18:52:36 +05:30
|
|
|
}
|
2022-09-13 22:03:37 +05:30
|
|
|
|
|
|
|
type giteaControl struct {
|
2022-10-06 00:25:36 +05:30
|
|
|
Meta *string `yaml:"meta"`
|
|
|
|
Icon *string `yaml:"details_icon"`
|
|
|
|
TOC *bool `yaml:"include_toc"`
|
|
|
|
Lang *string `yaml:"lang"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type complexGiteaConfig struct {
|
|
|
|
Gitea *giteaControl `yaml:"gitea"`
|
|
|
|
}
|
|
|
|
var complex complexGiteaConfig
|
|
|
|
if err := value.Decode(&complex); err != nil {
|
|
|
|
return fmt.Errorf("unable to decode into complexRenderConfig %w", err)
|
2020-04-24 18:52:36 +05:30
|
|
|
}
|
|
|
|
|
2022-10-06 00:25:36 +05:30
|
|
|
if complex.Gitea == nil {
|
|
|
|
return nil
|
2020-04-24 18:52:36 +05:30
|
|
|
}
|
|
|
|
|
2022-10-06 00:25:36 +05:30
|
|
|
if complex.Gitea.Meta != nil {
|
|
|
|
switch strings.TrimSpace(strings.ToLower(*complex.Gitea.Meta)) {
|
|
|
|
case "none":
|
|
|
|
rc.Meta = "none"
|
|
|
|
case "table":
|
|
|
|
rc.Meta = "table"
|
|
|
|
default: // "details"
|
|
|
|
rc.Meta = "details"
|
|
|
|
}
|
2022-09-13 22:03:37 +05:30
|
|
|
}
|
|
|
|
|
2022-10-06 00:25:36 +05:30
|
|
|
if complex.Gitea.Icon != nil {
|
|
|
|
rc.Icon = strings.TrimSpace(strings.ToLower(*complex.Gitea.Icon))
|
|
|
|
}
|
2022-09-13 22:03:37 +05:30
|
|
|
|
2022-10-06 00:25:36 +05:30
|
|
|
if complex.Gitea.Lang != nil && *complex.Gitea.Lang != "" {
|
|
|
|
rc.Lang = *complex.Gitea.Lang
|
2022-09-13 22:03:37 +05:30
|
|
|
}
|
|
|
|
|
2022-10-06 00:25:36 +05:30
|
|
|
if complex.Gitea.TOC != nil {
|
|
|
|
rc.TOC = *complex.Gitea.TOC
|
2022-09-13 22:03:37 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-04-24 18:52:36 +05:30
|
|
|
|
2022-09-13 22:03:37 +05:30
|
|
|
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 18:52:36 +05:30
|
|
|
}
|