diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2017-11-07 14:33:06 +0800 |
---|---|---|
committer | Lauris BH <lauris@nix.lv> | 2017-11-07 08:33:06 +0200 |
commit | 62d0a4d8829f214af4f9c00ecf8a81907d86ef06 (patch) | |
tree | c227fd240dcebde532b5ecef7d4b60a042e1f77e /modules/markup | |
parent | ddb75191eca30343783521c06f8706397c9260b8 (diff) | |
download | gitea-62d0a4d8829f214af4f9c00ecf8a81907d86ef06.tar.gz gitea-62d0a4d8829f214af4f9c00ecf8a81907d86ef06.zip |
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
Diffstat (limited to 'modules/markup')
-rw-r--r-- | modules/markup/external/external.go | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/modules/markup/external/external.go b/modules/markup/external/external.go new file mode 100644 index 0000000000..676ec4b4c5 --- /dev/null +++ b/modules/markup/external/external.go @@ -0,0 +1,88 @@ +// Copyright 2017 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 external + +import ( + "bytes" + "io" + "io/ioutil" + "os" + "os/exec" + "strings" + + "code.gitea.io/gitea/modules/log" + "code.gitea.io/gitea/modules/markup" + "code.gitea.io/gitea/modules/setting" +) + +// RegisterParsers registers all supported third part parsers according settings +func RegisterParsers() { + for _, parser := range setting.ExternalMarkupParsers { + if parser.Enabled && parser.Command != "" && len(parser.FileExtensions) > 0 { + markup.RegisterParser(&Parser{parser}) + } + } +} + +// Parser implements markup.Parser for external tools +type Parser struct { + setting.MarkupParser +} + +// Name returns the external tool name +func (p *Parser) Name() string { + return p.MarkupName +} + +// Extensions returns the supported extensions of the tool +func (p *Parser) Extensions() []string { + return p.FileExtensions +} + +// Render renders the data of the document to HTML via the external tool. +func (p *Parser) Render(rawBytes []byte, urlPrefix string, metas map[string]string, isWiki bool) []byte { + var ( + bs []byte + buf = bytes.NewBuffer(bs) + rd = bytes.NewReader(rawBytes) + commands = strings.Fields(p.Command) + args = commands[1:] + ) + + if p.IsInputFile { + // write to temp file + f, err := ioutil.TempFile("", "gitea_input") + if err != nil { + log.Error(4, "%s create temp file when rendering %s failed: %v", p.Name(), p.Command, err) + return []byte("") + } + defer os.Remove(f.Name()) + + _, err = io.Copy(f, rd) + if err != nil { + f.Close() + log.Error(4, "%s write data to temp file when rendering %s failed: %v", p.Name(), p.Command, err) + return []byte("") + } + + err = f.Close() + if err != nil { + log.Error(4, "%s close temp file when rendering %s failed: %v", p.Name(), p.Command, err) + return []byte("") + } + args = append(args, f.Name()) + } + + cmd := exec.Command(commands[0], args...) + if !p.IsInputFile { + cmd.Stdin = rd + } + cmd.Stdout = buf + if err := cmd.Run(); err != nil { + log.Error(4, "%s render run command %s %v failed: %v", p.Name(), commands[0], args, err) + return []byte("") + } + return buf.Bytes() +} |