]> source.dussan.org Git - gitea.git/commitdiff
support envs on external render commands (#5278)
authorLunny Xiao <xiaolunwen@gmail.com>
Tue, 20 Nov 2018 22:11:21 +0000 (06:11 +0800)
committertechknowlogick <hello@techknowlogick.com>
Tue, 20 Nov 2018 22:11:21 +0000 (17:11 -0500)
modules/markup/external/external.go

index 73bf7a327e7afe72928e889bf6455bec2d5ffd12..5963a06d30a9d81b7db19c7a44494f879cf451f3 100644 (file)
@@ -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