2017-11-06 23:33:06 -07:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
2022-11-27 11:20:29 -07:00
|
|
|
// SPDX-License-Identifier: MIT
|
2017-11-06 23:33:06 -07:00
|
|
|
|
|
|
|
package external
|
|
|
|
|
|
|
|
import (
|
2023-01-13 13:41:23 -07:00
|
|
|
"bytes"
|
2021-04-19 16:25:08 -06:00
|
|
|
"fmt"
|
2017-11-06 23:33:06 -07:00
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2018-11-20 15:11:21 -07:00
|
|
|
"runtime"
|
2017-11-06 23:33:06 -07:00
|
|
|
"strings"
|
|
|
|
|
2021-08-28 14:15:56 -06:00
|
|
|
"code.gitea.io/gitea/modules/graceful"
|
2017-11-06 23:33:06 -07:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
"code.gitea.io/gitea/modules/markup"
|
2021-06-30 14:07:23 -06:00
|
|
|
"code.gitea.io/gitea/modules/process"
|
2017-11-06 23:33:06 -07:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2020-08-11 14:05:34 -06:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2017-11-06 23:33:06 -07:00
|
|
|
)
|
|
|
|
|
2021-04-19 16:25:08 -06:00
|
|
|
// 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-06 23:33:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-19 16:25:08 -06:00
|
|
|
// Renderer implements markup.Renderer for external tools
|
|
|
|
type Renderer struct {
|
2021-06-23 15:09:51 -06:00
|
|
|
*setting.MarkupRenderer
|
2017-11-06 23:33:06 -07:00
|
|
|
}
|
|
|
|
|
2022-06-15 21:33:23 -06:00
|
|
|
var (
|
|
|
|
_ markup.PostProcessRenderer = (*Renderer)(nil)
|
|
|
|
_ markup.ExternalRenderer = (*Renderer)(nil)
|
|
|
|
)
|
|
|
|
|
2017-11-06 23:33:06 -07:00
|
|
|
// Name returns the external tool name
|
2021-04-19 16:25:08 -06:00
|
|
|
func (p *Renderer) Name() string {
|
2017-11-06 23:33:06 -07:00
|
|
|
return p.MarkupName
|
|
|
|
}
|
|
|
|
|
2021-04-19 16:25:08 -06:00
|
|
|
// NeedPostProcess implements markup.Renderer
|
|
|
|
func (p *Renderer) NeedPostProcess() bool {
|
|
|
|
return p.MarkupRenderer.NeedPostProcess
|
2021-04-13 01:06:31 -06:00
|
|
|
}
|
|
|
|
|
2017-11-06 23:33:06 -07:00
|
|
|
// Extensions returns the supported extensions of the tool
|
2021-04-19 16:25:08 -06:00
|
|
|
func (p *Renderer) Extensions() []string {
|
2017-11-06 23:33:06 -07:00
|
|
|
return p.FileExtensions
|
|
|
|
}
|
|
|
|
|
2021-06-23 15:09:51 -06:00
|
|
|
// SanitizerRules implements markup.Renderer
|
|
|
|
func (p *Renderer) SanitizerRules() []setting.MarkupSanitizerRule {
|
|
|
|
return p.MarkupSanitizerRules
|
|
|
|
}
|
|
|
|
|
2022-03-06 01:41:54 -07:00
|
|
|
// SanitizerDisabled disabled sanitize if return true
|
|
|
|
func (p *Renderer) SanitizerDisabled() bool {
|
2022-06-15 21:33:23 -06:00
|
|
|
return p.RenderContentMode == setting.RenderContentModeNoSanitizer || p.RenderContentMode == setting.RenderContentModeIframe
|
|
|
|
}
|
|
|
|
|
|
|
|
// DisplayInIFrame represents whether render the content with an iframe
|
|
|
|
func (p *Renderer) DisplayInIFrame() bool {
|
|
|
|
return p.RenderContentMode == setting.RenderContentModeIframe
|
2022-03-06 01:41:54 -07:00
|
|
|
}
|
|
|
|
|
2018-11-20 15:11:21 -07:00
|
|
|
func envMark(envName string) string {
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
return "%" + envName + "%"
|
|
|
|
}
|
|
|
|
return "$" + envName
|
|
|
|
}
|
|
|
|
|
2017-11-06 23:33:06 -07:00
|
|
|
// Render renders the data of the document to HTML via the external tool.
|
2021-04-19 16:25:08 -06:00
|
|
|
func (p *Renderer) Render(ctx *markup.RenderContext, input io.Reader, output io.Writer) error {
|
2017-11-06 23:33:06 -07:00
|
|
|
var (
|
2024-01-15 19:13:29 -07:00
|
|
|
command = strings.NewReplacer(
|
|
|
|
envMark("GITEA_PREFIX_SRC"), ctx.Links.SrcLink(),
|
|
|
|
envMark("GITEA_PREFIX_RAW"), ctx.Links.RawLink(),
|
|
|
|
).Replace(p.Command)
|
2018-11-20 15:11:21 -07:00
|
|
|
commands = strings.Fields(command)
|
2017-11-06 23:33:06 -07:00
|
|
|
args = commands[1:]
|
|
|
|
)
|
|
|
|
|
|
|
|
if p.IsInputFile {
|
|
|
|
// write to temp file
|
2021-09-21 23:38:34 -06:00
|
|
|
f, err := os.CreateTemp("", "gitea_input")
|
2017-11-06 23:33:06 -07:00
|
|
|
if err != nil {
|
2022-10-24 13:29:17 -06:00
|
|
|
return fmt.Errorf("%s create temp file when rendering %s failed: %w", p.Name(), p.Command, err)
|
2017-11-06 23:33:06 -07:00
|
|
|
}
|
2020-08-11 14:05:34 -06:00
|
|
|
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-06 23:33:06 -07:00
|
|
|
|
2021-04-19 16:25:08 -06:00
|
|
|
_, err = io.Copy(f, input)
|
2017-11-06 23:33:06 -07:00
|
|
|
if err != nil {
|
|
|
|
f.Close()
|
2022-10-24 13:29:17 -06:00
|
|
|
return fmt.Errorf("%s write data to temp file when rendering %s failed: %w", p.Name(), p.Command, err)
|
2017-11-06 23:33:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
err = f.Close()
|
|
|
|
if err != nil {
|
2022-10-24 13:29:17 -06:00
|
|
|
return fmt.Errorf("%s close temp file when rendering %s failed: %w", p.Name(), p.Command, err)
|
2017-11-06 23:33:06 -07:00
|
|
|
}
|
|
|
|
args = append(args, f.Name())
|
|
|
|
}
|
|
|
|
|
2021-07-12 15:13:59 -06:00
|
|
|
if ctx == nil || ctx.Ctx == nil {
|
2021-08-28 14:15:56 -06:00
|
|
|
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-12 15:13:59 -06:00
|
|
|
}
|
|
|
|
|
2024-01-15 19:13:29 -07:00
|
|
|
processCtx, _, finished := process.GetManager().AddContext(ctx.Ctx, fmt.Sprintf("Render [%s] for %s", commands[0], ctx.Links.SrcLink()))
|
2021-11-30 13:06:32 -07:00
|
|
|
defer finished()
|
2021-06-30 14:07:23 -06:00
|
|
|
|
|
|
|
cmd := exec.CommandContext(processCtx, commands[0], args...)
|
2018-10-30 08:34:12 -06:00
|
|
|
cmd.Env = append(
|
|
|
|
os.Environ(),
|
2024-01-15 19:13:29 -07:00
|
|
|
"GITEA_PREFIX_SRC="+ctx.Links.SrcLink(),
|
|
|
|
"GITEA_PREFIX_RAW="+ctx.Links.RawLink(),
|
2018-10-30 08:34:12 -06:00
|
|
|
)
|
2017-11-06 23:33:06 -07:00
|
|
|
if !p.IsInputFile {
|
2021-04-19 16:25:08 -06:00
|
|
|
cmd.Stdin = input
|
2017-11-06 23:33:06 -07:00
|
|
|
}
|
2023-01-13 13:41:23 -07:00
|
|
|
var stderr bytes.Buffer
|
2021-04-19 16:25:08 -06:00
|
|
|
cmd.Stdout = output
|
2023-01-13 13:41:23 -07:00
|
|
|
cmd.Stderr = &stderr
|
2022-06-03 08:36:18 -06:00
|
|
|
process.SetSysProcAttribute(cmd)
|
|
|
|
|
2017-11-06 23:33:06 -07:00
|
|
|
if err := cmd.Run(); err != nil {
|
2023-01-13 13:41:23 -07:00
|
|
|
return fmt.Errorf("%s render run command %s %v failed: %w\nStderr: %s", p.Name(), commands[0], args, err, stderr.String())
|
2017-11-06 23:33:06 -07:00
|
|
|
}
|
2021-04-19 16:25:08 -06:00
|
|
|
return nil
|
2017-11-06 23:33:06 -07:00
|
|
|
}
|