support envs on external render commands (#5278)

This commit is contained in:
Lunny Xiao 2018-11-21 06:11:21 +08:00 committed by techknowlogick
parent 8ef177f8c5
commit cef0f12c51

View File

@ -10,6 +10,7 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
"os/exec" "os/exec"
"runtime"
"strings" "strings"
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
@ -41,13 +42,24 @@ func (p *Parser) Extensions() []string {
return p.FileExtensions return p.FileExtensions
} }
func envMark(envName string) string {
if runtime.GOOS == "windows" {
return "%" + envName + "%"
}
return "$" + envName
}
// Render renders the data of the document to HTML via the external tool. // 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 { func (p *Parser) Render(rawBytes []byte, urlPrefix string, metas map[string]string, isWiki bool) []byte {
var ( var (
bs []byte bs []byte
buf = bytes.NewBuffer(bs) buf = bytes.NewBuffer(bs)
rd = bytes.NewReader(rawBytes) rd = bytes.NewReader(rawBytes)
commands = strings.Fields(p.Command) 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)
args = commands[1:] args = commands[1:]
) )
@ -79,7 +91,7 @@ func (p *Parser) Render(rawBytes []byte, urlPrefix string, metas map[string]stri
cmd.Env = append( cmd.Env = append(
os.Environ(), os.Environ(),
"GITEA_PREFIX_SRC="+urlPrefix, "GITEA_PREFIX_SRC="+urlPrefix,
"GITEA_PREFIX_RAW="+strings.Replace(urlPrefix, "/src/", "/raw/", 1), "GITEA_PREFIX_RAW="+urlRawPrefix,
) )
if !p.IsInputFile { if !p.IsInputFile {
cmd.Stdin = rd cmd.Stdin = rd