Browse Source

Add external markup render support (#2570)

* add external markup render support

* bug fixed

* refacotr codes and fix wrong error log

* fix comments and add check to prevent leaks

* add check for config file and improve the example

* check file close error

* use ioutil.TempFile instead uuid

* correct Render -> Parser

* improve warning when incorrect markup setting

* fix typos
Lunny Xiao 1 year ago
parent
commit
62d0a4d882
4 changed files with 149 additions and 1 deletions
  1. 3 0
      cmd/web.go
  2. 9 0
      conf/app.ini
  3. 88 0
      modules/markup/external/external.go
  4. 49 1
      modules/setting/setting.go

+ 3 - 0
cmd/web.go

@@ -14,6 +14,7 @@ import (
14 14
 	"strings"
15 15
 
16 16
 	"code.gitea.io/gitea/modules/log"
17
+	"code.gitea.io/gitea/modules/markup/external"
17 18
 	"code.gitea.io/gitea/modules/setting"
18 19
 	"code.gitea.io/gitea/routers"
19 20
 	"code.gitea.io/gitea/routers/routes"
@@ -59,6 +60,8 @@ func runWeb(ctx *cli.Context) error {
59 60
 
60 61
 	routers.GlobalInit()
61 62
 
63
+	external.RegisterParsers()
64
+
62 65
 	m := routes.NewMacaron()
63 66
 	routes.RegisterRoutes(m)
64 67
 

+ 9 - 0
conf/app.ini

@@ -573,3 +573,12 @@ SHOW_FOOTER_BRANDING = false
573 573
 SHOW_FOOTER_VERSION = true
574 574
 ; Show time of template execution in the footer
575 575
 SHOW_FOOTER_TEMPLATE_LOAD_TIME = true
576
+
577
+[markup.asciidoc]
578
+ENABLED = false
579
+; List of file extensions that should be rendered by an external command
580
+FILE_EXTENSIONS = .adoc,.asciidoc
581
+; External command to render all matching extensions
582
+RENDER_COMMAND = "asciidoc --out-file=- -"
583
+; Input is not a standard input but a file
584
+IS_INPUT_FILE = false

+ 88 - 0
modules/markup/external/external.go

@@ -0,0 +1,88 @@
1
+// Copyright 2017 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 external
6
+
7
+import (
8
+	"bytes"
9
+	"io"
10
+	"io/ioutil"
11
+	"os"
12
+	"os/exec"
13
+	"strings"
14
+
15
+	"code.gitea.io/gitea/modules/log"
16
+	"code.gitea.io/gitea/modules/markup"
17
+	"code.gitea.io/gitea/modules/setting"
18
+)
19
+
20
+// RegisterParsers registers all supported third part parsers according settings
21
+func RegisterParsers() {
22
+	for _, parser := range setting.ExternalMarkupParsers {
23
+		if parser.Enabled && parser.Command != "" && len(parser.FileExtensions) > 0 {
24
+			markup.RegisterParser(&Parser{parser})
25
+		}
26
+	}
27
+}
28
+
29
+// Parser implements markup.Parser for external tools
30
+type Parser struct {
31
+	setting.MarkupParser
32
+}
33
+
34
+// Name returns the external tool name
35
+func (p *Parser) Name() string {
36
+	return p.MarkupName
37
+}
38
+
39
+// Extensions returns the supported extensions of the tool
40
+func (p *Parser) Extensions() []string {
41
+	return p.FileExtensions
42
+}
43
+
44
+// Render renders the data of the document to HTML via the external tool.
45
+func (p *Parser) Render(rawBytes []byte, urlPrefix string, metas map[string]string, isWiki bool) []byte {
46
+	var (
47
+		bs       []byte
48
+		buf      = bytes.NewBuffer(bs)
49
+		rd       = bytes.NewReader(rawBytes)
50
+		commands = strings.Fields(p.Command)
51
+		args     = commands[1:]
52
+	)
53
+
54
+	if p.IsInputFile {
55
+		// write to temp file
56
+		f, err := ioutil.TempFile("", "gitea_input")
57
+		if err != nil {
58
+			log.Error(4, "%s create temp file when rendering %s failed: %v", p.Name(), p.Command, err)
59
+			return []byte("")
60
+		}
61
+		defer os.Remove(f.Name())
62
+
63
+		_, err = io.Copy(f, rd)
64
+		if err != nil {
65
+			f.Close()
66
+			log.Error(4, "%s write data to temp file when rendering %s failed: %v", p.Name(), p.Command, err)
67
+			return []byte("")
68
+		}
69
+
70
+		err = f.Close()
71
+		if err != nil {
72
+			log.Error(4, "%s close temp file when rendering %s failed: %v", p.Name(), p.Command, err)
73
+			return []byte("")
74
+		}
75
+		args = append(args, f.Name())
76
+	}
77
+
78
+	cmd := exec.Command(commands[0], args...)
79
+	if !p.IsInputFile {
80
+		cmd.Stdin = rd
81
+	}
82
+	cmd.Stdout = buf
83
+	if err := cmd.Run(); err != nil {
84
+		log.Error(4, "%s render run command %s %v failed: %v", p.Name(), commands[0], args, err)
85
+		return []byte("")
86
+	}
87
+	return buf.Bytes()
88
+}

+ 49 - 1
modules/setting/setting.go

@@ -60,6 +60,15 @@ const (
60 60
 	LandingPageExplore LandingPage = "/explore"
61 61
 )
62 62
 
63
+// MarkupParser defines the external parser configured in ini
64
+type MarkupParser struct {
65
+	Enabled        bool
66
+	MarkupName     string
67
+	Command        string
68
+	FileExtensions []string
69
+	IsInputFile    bool
70
+}
71
+
63 72
 // settings
64 73
 var (
65 74
 	// AppVer settings
@@ -515,6 +524,8 @@ var (
515 524
 	HasRobotsTxt      bool
516 525
 	InternalToken     string // internal access token
517 526
 	IterateBufferSize int
527
+
528
+	ExternalMarkupParsers []MarkupParser
518 529
 )
519 530
 
520 531
 // DateLang transforms standard language locale name to corresponding value in datetime plugin.
@@ -1073,6 +1084,44 @@ func NewContext() {
1073 1084
 	UI.ShowUserEmail = Cfg.Section("ui").Key("SHOW_USER_EMAIL").MustBool(true)
1074 1085
 
1075 1086
 	HasRobotsTxt = com.IsFile(path.Join(CustomPath, "robots.txt"))
1087
+
1088
+	extensionReg := regexp.MustCompile(`\.\w`)
1089
+	for _, sec := range Cfg.Section("markup").ChildSections() {
1090
+		name := strings.TrimLeft(sec.Name(), "markup.")
1091
+		if name == "" {
1092
+			log.Warn("name is empty, markup " + sec.Name() + "ignored")
1093
+			continue
1094
+		}
1095
+
1096
+		extensions := sec.Key("FILE_EXTENSIONS").Strings(",")
1097
+		var exts = make([]string, 0, len(extensions))
1098
+		for _, extension := range extensions {
1099
+			if !extensionReg.MatchString(extension) {
1100
+				log.Warn(sec.Name() + " file extension " + extension + " is invalid. Extension ignored")
1101
+			} else {
1102
+				exts = append(exts, extension)
1103
+			}
1104
+		}
1105
+
1106
+		if len(exts) == 0 {
1107
+			log.Warn(sec.Name() + " file extension is empty, markup " + name + " ignored")
1108
+			continue
1109
+		}
1110
+
1111
+		command := sec.Key("RENDER_COMMAND").MustString("")
1112
+		if command == "" {
1113
+			log.Warn(" RENDER_COMMAND is empty, markup " + name + " ignored")
1114
+			continue
1115
+		}
1116
+
1117
+		ExternalMarkupParsers = append(ExternalMarkupParsers, MarkupParser{
1118
+			Enabled:        sec.Key("ENABLED").MustBool(false),
1119
+			MarkupName:     name,
1120
+			FileExtensions: exts,
1121
+			Command:        command,
1122
+			IsInputFile:    sec.Key("IS_INPUT_FILE").MustBool(false),
1123
+		})
1124
+	}
1076 1125
 }
1077 1126
 
1078 1127
 // Service settings
@@ -1133,7 +1182,6 @@ func newService() {
1133 1182
 			Service.OpenIDBlacklist[i] = regexp.MustCompilePOSIX(p)
1134 1183
 		}
1135 1184
 	}
1136
-
1137 1185
 }
1138 1186
 
1139 1187
 var logLevels = map[string]string{