Skip to content

Commit 62d0a4d

Browse files
lunnylafriks
authored andcommitted
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
1 parent ddb7519 commit 62d0a4d

File tree

4 files changed

+149
-1
lines changed

4 files changed

+149
-1
lines changed

cmd/web.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"strings"
1515

1616
"code.gitea.io/gitea/modules/log"
17+
"code.gitea.io/gitea/modules/markup/external"
1718
"code.gitea.io/gitea/modules/setting"
1819
"code.gitea.io/gitea/routers"
1920
"code.gitea.io/gitea/routers/routes"
@@ -59,6 +60,8 @@ func runWeb(ctx *cli.Context) error {
5960

6061
routers.GlobalInit()
6162

63+
external.RegisterParsers()
64+
6265
m := routes.NewMacaron()
6366
routes.RegisterRoutes(m)
6467

conf/app.ini

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,3 +573,12 @@ SHOW_FOOTER_BRANDING = false
573573
SHOW_FOOTER_VERSION = true
574574
; Show time of template execution in the footer
575575
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

modules/markup/external/external.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}

modules/setting/setting.go

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,15 @@ const (
6060
LandingPageExplore LandingPage = "/explore"
6161
)
6262

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+
6372
// settings
6473
var (
6574
// AppVer settings
@@ -515,6 +524,8 @@ var (
515524
HasRobotsTxt bool
516525
InternalToken string // internal access token
517526
IterateBufferSize int
527+
528+
ExternalMarkupParsers []MarkupParser
518529
)
519530

520531
// DateLang transforms standard language locale name to corresponding value in datetime plugin.
@@ -1073,6 +1084,44 @@ func NewContext() {
10731084
UI.ShowUserEmail = Cfg.Section("ui").Key("SHOW_USER_EMAIL").MustBool(true)
10741085

10751086
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+
}
10761125
}
10771126

10781127
// Service settings
@@ -1133,7 +1182,6 @@ func newService() {
11331182
Service.OpenIDBlacklist[i] = regexp.MustCompilePOSIX(p)
11341183
}
11351184
}
1136-
11371185
}
11381186

11391187
var logLevels = map[string]string{

0 commit comments

Comments
 (0)