2017-11-07 06:33:06 +00:00
|
|
|
// 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"
|
2018-11-20 22:11:21 +00:00
|
|
|
"runtime"
|
2017-11-07 06:33:06 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
"code.gitea.io/gitea/modules/markup"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2020-08-11 20:05:34 +00:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2017-11-07 06:33:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2021-04-13 07:06:31 +00:00
|
|
|
// NeedPostProcess implements markup.Parser
|
|
|
|
func (p *Parser) NeedPostProcess() bool {
|
|
|
|
return p.MarkupParser.NeedPostProcess
|
|
|
|
}
|
|
|
|
|
2017-11-07 06:33:06 +00:00
|
|
|
// Extensions returns the supported extensions of the tool
|
|
|
|
func (p *Parser) Extensions() []string {
|
|
|
|
return p.FileExtensions
|
|
|
|
}
|
|
|
|
|
2018-11-20 22:11:21 +00:00
|
|
|
func envMark(envName string) string {
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
return "%" + envName + "%"
|
|
|
|
}
|
|
|
|
return "$" + envName
|
|
|
|
}
|
|
|
|
|
2017-11-07 06:33:06 +00:00
|
|
|
// 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 (
|
2018-11-20 22:11:21 +00:00
|
|
|
bs []byte
|
|
|
|
buf = bytes.NewBuffer(bs)
|
|
|
|
rd = bytes.NewReader(rawBytes)
|
|
|
|
urlRawPrefix = strings.Replace(urlPrefix, "/src/", "/raw/", 1)
|
|
|
|
|
|
|
|
command = strings.NewReplacer(envMark("GITEA_PREFIX_SRC"), urlPrefix,
|
|
|
|
envMark("GITEA_PREFIX_RAW"), urlRawPrefix).Replace(p.Command)
|
|
|
|
commands = strings.Fields(command)
|
2017-11-07 06:33:06 +00:00
|
|
|
args = commands[1:]
|
|
|
|
)
|
|
|
|
|
|
|
|
if p.IsInputFile {
|
|
|
|
// write to temp file
|
|
|
|
f, err := ioutil.TempFile("", "gitea_input")
|
|
|
|
if err != nil {
|
2019-04-02 07:48:31 +00:00
|
|
|
log.Error("%s create temp file when rendering %s failed: %v", p.Name(), p.Command, err)
|
2017-11-07 06:33:06 +00:00
|
|
|
return []byte("")
|
|
|
|
}
|
2020-08-11 20:05:34 +00:00
|
|
|
tmpPath := f.Name()
|
|
|
|
defer func() {
|
|
|
|
if err := util.Remove(tmpPath); err != nil {
|
|
|
|
log.Warn("Unable to remove temporary file: %s: Error: %v", tmpPath, err)
|
|
|
|
}
|
|
|
|
}()
|
2017-11-07 06:33:06 +00:00
|
|
|
|
|
|
|
_, err = io.Copy(f, rd)
|
|
|
|
if err != nil {
|
|
|
|
f.Close()
|
2019-04-02 07:48:31 +00:00
|
|
|
log.Error("%s write data to temp file when rendering %s failed: %v", p.Name(), p.Command, err)
|
2017-11-07 06:33:06 +00:00
|
|
|
return []byte("")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = f.Close()
|
|
|
|
if err != nil {
|
2019-04-02 07:48:31 +00:00
|
|
|
log.Error("%s close temp file when rendering %s failed: %v", p.Name(), p.Command, err)
|
2017-11-07 06:33:06 +00:00
|
|
|
return []byte("")
|
|
|
|
}
|
|
|
|
args = append(args, f.Name())
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd := exec.Command(commands[0], args...)
|
2018-10-30 14:34:12 +00:00
|
|
|
cmd.Env = append(
|
|
|
|
os.Environ(),
|
|
|
|
"GITEA_PREFIX_SRC="+urlPrefix,
|
2018-11-20 22:11:21 +00:00
|
|
|
"GITEA_PREFIX_RAW="+urlRawPrefix,
|
2018-10-30 14:34:12 +00:00
|
|
|
)
|
2017-11-07 06:33:06 +00:00
|
|
|
if !p.IsInputFile {
|
|
|
|
cmd.Stdin = rd
|
|
|
|
}
|
|
|
|
cmd.Stdout = buf
|
|
|
|
if err := cmd.Run(); err != nil {
|
2019-04-02 07:48:31 +00:00
|
|
|
log.Error("%s render run command %s %v failed: %v", p.Name(), commands[0], args, err)
|
2017-11-07 06:33:06 +00:00
|
|
|
return []byte("")
|
|
|
|
}
|
|
|
|
return buf.Bytes()
|
|
|
|
}
|