Skip to content

Commit

Permalink
Restructure RenderConfig to allow the markdown header to define if ma…
Browse files Browse the repository at this point in the history
…th should

be enabled.

Signed-off-by: Andrew Thornton <art27@cantab.net>
  • Loading branch information
zeripath committed Aug 2, 2022
1 parent 1d6a332 commit 3f868fc
Show file tree
Hide file tree
Showing 12 changed files with 546 additions and 378 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package markdown
package config

import (
"code.gitea.io/gitea/modules/markup/markdown/extension"
"github.com/yuin/goldmark/ast"
east "github.com/yuin/goldmark/extension/ast"
"gopkg.in/yaml.v3"
Expand Down Expand Up @@ -74,9 +75,9 @@ func sequenceNodeToTable(meta *yaml.Node) ast.Node {
}

func nodeToDetails(meta *yaml.Node, icon string) ast.Node {
details := NewDetails()
summary := NewSummary()
summary.AppendChild(summary, NewIcon(icon))
details := extension.NewDetails()
summary := extension.NewSummary()
summary.AppendChild(summary, extension.NewIcon(icon))
details.AppendChild(details, summary)
details.AppendChild(details, nodeToTable(meta))

Expand Down
197 changes: 197 additions & 0 deletions modules/markup/markdown/config/renderconfig.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
// 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 config

import (
"fmt"
"strings"

"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/parser"
"gopkg.in/yaml.v3"
)

var renderConfigKey = parser.NewContextKey()

func GetRenderConfig(pc parser.Context) *RenderConfig {
return pc.Get(renderConfigKey).(*RenderConfig)
}

func SetRenderConfig(pc parser.Context, rc *RenderConfig) {
pc.Set(renderConfigKey, rc)
}

// RenderConfig represents rendering configuration for this file
type RenderConfig struct {
Meta string
Icon string
TOC bool
Lang string
Math *MathConfig
yamlNode *yaml.Node
}

type MathConfig struct {
InlineDollar bool `yaml:"inline_dollar"`
InlineLatex bool `yaml:"inline_latex"`
DisplayDollar bool `yaml:"display_dollar"`
DisplayLatex bool `yaml:"display_latex"`
}

// UnmarshalYAML implement yaml.v3 UnmarshalYAML
func (rc *RenderConfig) UnmarshalYAML(value *yaml.Node) error {
rc.yamlNode = value

basic := &yamlRenderConfig{}
err := value.Decode(basic)
if err != nil {
return fmt.Errorf("failed to decode basic: %w", err)
}

if basic.Lang != "" {
rc.Lang = basic.Lang
}

rc.TOC = basic.TOC

if basic.Math != nil {
rc.Math = basic.Math
}

if basic.Gitea != nil {
if basic.Gitea.Meta != nil {
rc.Meta = *basic.Gitea.Meta
}
if basic.Gitea.Icon != nil {
rc.Icon = *basic.Gitea.Icon
}
if basic.Gitea.Lang != nil {
rc.Lang = *basic.Gitea.Lang
}
if basic.Gitea.TOC != nil {
rc.TOC = *basic.Gitea.TOC
}
if basic.Gitea.Math != nil {
rc.Math = basic.Gitea.Math
}
}

return nil
}

type yamlRenderConfig struct {
TOC bool `yaml:"include_toc"`
Lang string `yaml:"lang"`
Math *MathConfig `yaml:"math"`
Gitea *yamlGitea `yaml:"gitea"`
}

type yamlGitea struct {
Meta *string
Icon *string `yaml:"details_icon"`
TOC *bool `yaml:"include_toc"`
Lang *string
Math *MathConfig
}

func (y *yamlGitea) UnmarshalYAML(node *yaml.Node) error {
var controlString string
if err := node.Decode(&controlString); err == nil {
var meta string
switch strings.TrimSpace(strings.ToLower(controlString)) {
case "none":
meta = "none"
case "table":
meta = "table"
default: // "details"
meta = "details"
}
y.Meta = &meta
return nil
}

type yExactType yamlGitea
yExact := (*yExactType)(y)
if err := node.Decode(yExact); err != nil {
return fmt.Errorf("unable to parse yamlGitea: %w", err)
}
y = (*yamlGitea)(yExact)

return nil
}

func (m *MathConfig) UnmarshalYAML(node *yaml.Node) error {
var controlBool bool
if err := node.Decode(&controlBool); err == nil {
m.InlineLatex = controlBool
m.DisplayLatex = controlBool
m.DisplayDollar = controlBool
// Not InlineDollar
m.InlineDollar = false
return nil
}

var enableMathStrs []string
if err := node.Decode(&enableMathStrs); err != nil {
var enableMathStr string
if err := node.Decode(&enableMathStr); err == nil {
m.InlineLatex = false
m.DisplayLatex = false
m.DisplayDollar = false
m.InlineDollar = false
if enableMathStr == "" {
enableMathStr = "true"
}
enableMathStrs = strings.Split(enableMathStr, ",")
}
}
if enableMathStrs != nil {
for _, value := range enableMathStrs {
switch strings.TrimSpace(strings.ToLower(value)) {
case "all":
m.InlineLatex = true
m.DisplayLatex = true
m.DisplayDollar = true
m.InlineDollar = true
break
case "inline_dollar":
m.InlineDollar = true
case "inline_latex":
m.InlineLatex = true
case "display_dollar":
m.DisplayDollar = true
case "display_latex":
m.DisplayLatex = true
case "true":
m.InlineLatex = true
m.DisplayLatex = true
m.DisplayDollar = true
}
}
return nil
}

type mExactType MathConfig
mExact := (*mExactType)(m)
if err := node.Decode(mExact); err != nil {
return fmt.Errorf("unable to parse MathConfig: %w", err)
}
m = (*MathConfig)(mExact)
return nil
}

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
}
}
Loading

0 comments on commit 3f868fc

Please sign in to comment.