summaryrefslogtreecommitdiffstats
path: root/modules/setting/markup.go
blob: 41f3cdd3a199d11d97246f41c60681f720190d77 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright 2019 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 setting

import (
	"regexp"
	"strings"

	"code.gitea.io/gitea/modules/log"
)

// ExternalMarkupParsers represents the external markup parsers
var (
	ExternalMarkupParsers []MarkupParser
)

// MarkupParser defines the external parser configured in ini
type MarkupParser struct {
	Enabled        bool
	MarkupName     string
	Command        string
	FileExtensions []string
	IsInputFile    bool
}

func newMarkup() {
	extensionReg := regexp.MustCompile(`\.\w`)
	for _, sec := range Cfg.Section("markup").ChildSections() {
		name := strings.TrimPrefix(sec.Name(), "markup.")
		if name == "" {
			log.Warn("name is empty, markup " + sec.Name() + "ignored")
			continue
		}

		extensions := sec.Key("FILE_EXTENSIONS").Strings(",")
		var exts = make([]string, 0, len(extensions))
		for _, extension := range extensions {
			if !extensionReg.MatchString(extension) {
				log.Warn(sec.Name() + " file extension " + extension + " is invalid. Extension ignored")
			} else {
				exts = append(exts, extension)
			}
		}

		if len(exts) == 0 {
			log.Warn(sec.Name() + " file extension is empty, markup " + name + " ignored")
			continue
		}

		command := sec.Key("RENDER_COMMAND").MustString("")
		if command == "" {
			log.Warn(" RENDER_COMMAND is empty, markup " + name + " ignored")
			continue
		}

		ExternalMarkupParsers = append(ExternalMarkupParsers, MarkupParser{
			Enabled:        sec.Key("ENABLED").MustBool(false),
			MarkupName:     name,
			FileExtensions: exts,
			Command:        command,
			IsInputFile:    sec.Key("IS_INPUT_FILE").MustBool(false),
		})
	}
}