Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support envs on external render commands #5278

Merged
merged 2 commits into from
Nov 20, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions modules/markup/external/external.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"io/ioutil"
"os"
"os/exec"
"runtime"
"strings"

"code.gitea.io/gitea/modules/log"
Expand Down Expand Up @@ -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:]
)

Expand Down Expand Up @@ -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
Expand Down