2021-04-20 01:25:08 +03:00
// Copyright 2017 The Gitea Authors. All rights reserved.
2022-11-27 21:20:29 +03:00
// SPDX-License-Identifier: MIT
2021-04-20 01:25:08 +03:00
package markup
import (
2022-06-09 00:46:39 +03:00
"bytes"
2021-04-20 01:25:08 +03:00
"io"
2024-11-24 11:18:57 +03:00
"path"
2021-04-20 01:25:08 +03:00
"strings"
"code.gitea.io/gitea/modules/setting"
)
// Renderer defines an interface for rendering markup file to HTML
type Renderer interface {
Name ( ) string // markup format name
Extensions ( ) [ ] string
2021-06-24 00:09:51 +03:00
SanitizerRules ( ) [ ] setting . MarkupSanitizerRule
2021-04-20 01:25:08 +03:00
Render ( ctx * RenderContext , input io . Reader , output io . Writer ) error
}
2022-06-16 06:33:23 +03:00
// PostProcessRenderer defines an interface for renderers who need post process
type PostProcessRenderer interface {
NeedPostProcess ( ) bool
}
2024-11-04 13:59:50 +03:00
// ExternalRenderer defines an interface for external renderers
2022-06-16 06:33:23 +03:00
type ExternalRenderer interface {
// SanitizerDisabled disabled sanitize if return true
SanitizerDisabled ( ) bool
// DisplayInIFrame represents whether render the content with an iframe
DisplayInIFrame ( ) bool
}
2022-06-09 00:46:39 +03:00
// RendererContentDetector detects if the content can be rendered
// by specified renderer
type RendererContentDetector interface {
CanRender ( filename string , input io . Reader ) bool
}
2021-04-20 01:25:08 +03:00
var (
extRenderers = make ( map [ string ] Renderer )
renderers = make ( map [ string ] Renderer )
)
// RegisterRenderer registers a new markup file renderer
func RegisterRenderer ( renderer Renderer ) {
renderers [ renderer . Name ( ) ] = renderer
for _ , ext := range renderer . Extensions ( ) {
extRenderers [ strings . ToLower ( ext ) ] = renderer
}
}
// GetRendererByFileName get renderer by filename
func GetRendererByFileName ( filename string ) Renderer {
2024-11-24 11:18:57 +03:00
extension := strings . ToLower ( path . Ext ( filename ) )
2021-04-20 01:25:08 +03:00
return extRenderers [ extension ]
}
2022-06-09 00:46:39 +03:00
// DetectRendererType detects the markup type of the content
func DetectRendererType ( filename string , input io . Reader ) string {
buf , err := io . ReadAll ( input )
if err != nil {
return ""
}
for _ , renderer := range renderers {
if detector , ok := renderer . ( RendererContentDetector ) ; ok && detector . CanRender ( filename , bytes . NewReader ( buf ) ) {
return renderer . Name ( )
}
}
return ""
}
2024-06-18 06:09:20 +03:00
// DetectMarkupTypeByFileName returns the possible markup format type via the filename
func DetectMarkupTypeByFileName ( filename string ) string {
2021-04-20 01:25:08 +03:00
if parser := GetRendererByFileName ( filename ) ; parser != nil {
return parser . Name ( )
}
return ""
}
2023-03-24 09:12:23 +03:00
func PreviewableExtensions ( ) [ ] string {
extensions := make ( [ ] string , 0 , len ( extRenderers ) )
for extension := range extRenderers {
extensions = append ( extensions , extension )
}
return extensions
}