2017-11-07 12:03:06 +05:30
|
|
|
// 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 (
|
2021-07-01 01:37:23 +05:30
|
|
|
"context"
|
2021-04-20 03:55:08 +05:30
|
|
|
"fmt"
|
2017-11-07 12:03:06 +05:30
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2018-11-21 03:41:21 +05:30
|
|
|
"runtime"
|
2017-11-07 12:03:06 +05:30
|
|
|
"strings"
|
|
|
|
|
2021-08-29 01:45:56 +05:30
|
|
|
"code.gitea.io/gitea/modules/graceful"
|
2017-11-07 12:03:06 +05:30
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
"code.gitea.io/gitea/modules/markup"
|
2021-07-01 01:37:23 +05:30
|
|
|
"code.gitea.io/gitea/modules/process"
|
2017-11-07 12:03:06 +05:30
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2020-08-12 01:35:34 +05:30
|
|
|
"code.gitea.io/gitea/modules/util"
|
2017-11-07 12:03:06 +05:30
|
|
|
)
|
|
|
|
|
2021-04-20 03:55:08 +05:30
|
|
|
// RegisterRenderers registers all supported third part renderers according settings
|
|
|
|
func RegisterRenderers() {
|
|
|
|
for _, renderer := range setting.ExternalMarkupRenderers {
|
|
|
|
if renderer.Enabled && renderer.Command != "" && len(renderer.FileExtensions) > 0 {
|
|
|
|
markup.RegisterRenderer(&Renderer{renderer})
|
2017-11-07 12:03:06 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-20 03:55:08 +05:30
|
|
|
// Renderer implements markup.Renderer for external tools
|
|
|
|
type Renderer struct {
|
2021-06-24 02:39:51 +05:30
|
|
|
*setting.MarkupRenderer
|
2017-11-07 12:03:06 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// Name returns the external tool name
|
2021-04-20 03:55:08 +05:30
|
|
|
func (p *Renderer) Name() string {
|
2017-11-07 12:03:06 +05:30
|
|
|
return p.MarkupName
|
|
|
|
}
|
|
|
|
|
2021-04-20 03:55:08 +05:30
|
|
|
// NeedPostProcess implements markup.Renderer
|
|
|
|
func (p *Renderer) NeedPostProcess() bool {
|
|
|
|
return p.MarkupRenderer.NeedPostProcess
|
2021-04-13 12:36:31 +05:30
|
|
|
}
|
|
|
|
|
2017-11-07 12:03:06 +05:30
|
|
|
// Extensions returns the supported extensions of the tool
|
2021-04-20 03:55:08 +05:30
|
|
|
func (p *Renderer) Extensions() []string {
|
2017-11-07 12:03:06 +05:30
|
|
|
return p.FileExtensions
|
|
|
|
}
|
|
|
|
|
2021-06-24 02:39:51 +05:30
|
|
|
// SanitizerRules implements markup.Renderer
|
|
|
|
func (p *Renderer) SanitizerRules() []setting.MarkupSanitizerRule {
|
|
|
|
return p.MarkupSanitizerRules
|
|
|
|
}
|
|
|
|
|
2018-11-21 03:41:21 +05:30
|
|
|
func envMark(envName string) string {
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
return "%" + envName + "%"
|
|
|
|
}
|
|
|
|
return "$" + envName
|
|
|
|
}
|
|
|
|
|
2017-11-07 12:03:06 +05:30
|
|
|
// Render renders the data of the document to HTML via the external tool.
|
2021-04-20 03:55:08 +05:30
|
|
|
func (p *Renderer) Render(ctx *markup.RenderContext, input io.Reader, output io.Writer) error {
|
2017-11-07 12:03:06 +05:30
|
|
|
var (
|
2021-04-20 03:55:08 +05:30
|
|
|
urlRawPrefix = strings.Replace(ctx.URLPrefix, "/src/", "/raw/", 1)
|
|
|
|
command = strings.NewReplacer(envMark("GITEA_PREFIX_SRC"), ctx.URLPrefix,
|
2018-11-21 03:41:21 +05:30
|
|
|
envMark("GITEA_PREFIX_RAW"), urlRawPrefix).Replace(p.Command)
|
|
|
|
commands = strings.Fields(command)
|
2017-11-07 12:03:06 +05:30
|
|
|
args = commands[1:]
|
|
|
|
)
|
|
|
|
|
|
|
|
if p.IsInputFile {
|
|
|
|
// write to temp file
|
|
|
|
f, err := ioutil.TempFile("", "gitea_input")
|
|
|
|
if err != nil {
|
2021-04-20 03:55:08 +05:30
|
|
|
return fmt.Errorf("%s create temp file when rendering %s failed: %v", p.Name(), p.Command, err)
|
2017-11-07 12:03:06 +05:30
|
|
|
}
|
2020-08-12 01:35:34 +05:30
|
|
|
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 12:03:06 +05:30
|
|
|
|
2021-04-20 03:55:08 +05:30
|
|
|
_, err = io.Copy(f, input)
|
2017-11-07 12:03:06 +05:30
|
|
|
if err != nil {
|
|
|
|
f.Close()
|
2021-04-20 03:55:08 +05:30
|
|
|
return fmt.Errorf("%s write data to temp file when rendering %s failed: %v", p.Name(), p.Command, err)
|
2017-11-07 12:03:06 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
err = f.Close()
|
|
|
|
if err != nil {
|
2021-04-20 03:55:08 +05:30
|
|
|
return fmt.Errorf("%s close temp file when rendering %s failed: %v", p.Name(), p.Command, err)
|
2017-11-07 12:03:06 +05:30
|
|
|
}
|
|
|
|
args = append(args, f.Name())
|
|
|
|
}
|
|
|
|
|
2021-07-13 02:43:59 +05:30
|
|
|
if ctx == nil || ctx.Ctx == nil {
|
2021-08-29 01:45:56 +05:30
|
|
|
if ctx == nil {
|
|
|
|
log.Warn("RenderContext not provided defaulting to empty ctx")
|
|
|
|
ctx = &markup.RenderContext{}
|
|
|
|
}
|
|
|
|
log.Warn("RenderContext did not provide context, defaulting to Shutdown context")
|
|
|
|
ctx.Ctx = graceful.GetManager().ShutdownContext()
|
2021-07-13 02:43:59 +05:30
|
|
|
}
|
|
|
|
|
2021-07-01 01:37:23 +05:30
|
|
|
processCtx, cancel := context.WithCancel(ctx.Ctx)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
pid := process.GetManager().Add(fmt.Sprintf("Render [%s] for %s", commands[0], ctx.URLPrefix), cancel)
|
|
|
|
defer process.GetManager().Remove(pid)
|
|
|
|
|
|
|
|
cmd := exec.CommandContext(processCtx, commands[0], args...)
|
2018-10-30 20:04:12 +05:30
|
|
|
cmd.Env = append(
|
|
|
|
os.Environ(),
|
2021-04-20 03:55:08 +05:30
|
|
|
"GITEA_PREFIX_SRC="+ctx.URLPrefix,
|
2018-11-21 03:41:21 +05:30
|
|
|
"GITEA_PREFIX_RAW="+urlRawPrefix,
|
2018-10-30 20:04:12 +05:30
|
|
|
)
|
2017-11-07 12:03:06 +05:30
|
|
|
if !p.IsInputFile {
|
2021-04-20 03:55:08 +05:30
|
|
|
cmd.Stdin = input
|
2017-11-07 12:03:06 +05:30
|
|
|
}
|
2021-04-20 03:55:08 +05:30
|
|
|
cmd.Stdout = output
|
2017-11-07 12:03:06 +05:30
|
|
|
if err := cmd.Run(); err != nil {
|
2021-04-20 03:55:08 +05:30
|
|
|
return fmt.Errorf("%s render run command %s %v failed: %v", p.Name(), commands[0], args, err)
|
2017-11-07 12:03:06 +05:30
|
|
|
}
|
2021-04-20 03:55:08 +05:30
|
|
|
return nil
|
2017-11-07 12:03:06 +05:30
|
|
|
}
|