diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2018-11-21 06:11:21 +0800 |
---|---|---|
committer | techknowlogick <hello@techknowlogick.com> | 2018-11-20 17:11:21 -0500 |
commit | cef0f12c51cee9edeb636fd2174f8d4eb00ea955 (patch) | |
tree | 4d403e95befb5bf0071d101459f75f71b1b12224 /modules/markup | |
parent | 8ef177f8c5d0968e732daf6b62fe77d3c2555527 (diff) | |
download | gitea-cef0f12c51cee9edeb636fd2174f8d4eb00ea955.tar.gz gitea-cef0f12c51cee9edeb636fd2174f8d4eb00ea955.zip |
support envs on external render commands (#5278)
Diffstat (limited to 'modules/markup')
-rw-r--r-- | modules/markup/external/external.go | 22 |
1 files changed, 17 insertions, 5 deletions
diff --git a/modules/markup/external/external.go b/modules/markup/external/external.go index 73bf7a327e..5963a06d30 100644 --- a/modules/markup/external/external.go +++ b/modules/markup/external/external.go @@ -10,6 +10,7 @@ import ( "io/ioutil" "os" "os/exec" + "runtime" "strings" "code.gitea.io/gitea/modules/log" @@ -41,13 +42,24 @@ func (p *Parser) Extensions() []string { 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. 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) + 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) args = commands[1:] ) @@ -79,7 +91,7 @@ func (p *Parser) Render(rawBytes []byte, urlPrefix string, metas map[string]stri cmd.Env = append( os.Environ(), "GITEA_PREFIX_SRC="+urlPrefix, - "GITEA_PREFIX_RAW="+strings.Replace(urlPrefix, "/src/", "/raw/", 1), + "GITEA_PREFIX_RAW="+urlRawPrefix, ) if !p.IsInputFile { cmd.Stdin = rd |