roulette/types/code/code.go

231 lines
4.1 KiB
Go
Raw Normal View History

2023-09-14 22:37:22 +00:00
/*
Copyright © 2023 Seednode <seednode@seedno.de>
*/
package code
import (
"bufio"
"bytes"
"fmt"
"io"
"os"
"strings"
"github.com/alecthomas/chroma/v2"
"github.com/alecthomas/chroma/v2/formatters/html"
"github.com/alecthomas/chroma/v2/lexers"
"github.com/alecthomas/chroma/v2/styles"
"seedno.de/seednode/roulette/types"
)
2023-09-15 03:13:21 +00:00
type Format struct {
Theme string
}
2023-09-14 22:37:22 +00:00
func (t Format) Css() string {
var css strings.Builder
formatter := html.New(
html.TabWidth(4),
html.WithClasses(true),
html.WrapLongLines(true))
2023-09-15 03:13:21 +00:00
style := styles.Get(t.Theme)
2023-09-14 22:37:22 +00:00
if style == nil {
style = styles.Fallback
}
var response bytes.Buffer
w := bufio.NewWriter(&response)
r := bufio.NewReader(&response)
err := formatter.WriteCSS(w, style)
if err != nil {
return ""
}
w.Flush()
b, err := io.ReadAll(r)
if err != nil {
return ""
}
css.Write(b)
2023-09-15 03:13:21 +00:00
css.WriteString("html{height:100%;width:100%;}\n")
css.WriteString("a{bottom:0;left:0;position:absolute;right:0;top:0;margin:1rem;padding:0;height:99%;width:99%;color:inherit;text-decoration:none;}\n")
2023-09-14 22:37:22 +00:00
return css.String()
}
func (t Format) Title(rootUrl, fileUri, filePath, fileName, prefix, mime string) (string, error) {
return fmt.Sprintf(`<title>%s</title>`, fileName), nil
2023-09-14 22:37:22 +00:00
}
func (t Format) Body(rootUrl, fileUri, filePath, fileName, prefix, mime string) (string, error) {
2023-09-14 22:37:22 +00:00
contents, err := os.ReadFile(filePath)
if err != nil {
return "", err
2023-09-14 22:37:22 +00:00
}
contentString := string(contents)
lexer := lexers.Match(filePath)
if lexer == nil {
lexer = lexers.Analyse(contentString)
}
if lexer == nil {
lexer = lexers.Fallback
}
lexer = chroma.Coalesce(lexer)
var response bytes.Buffer
w := bufio.NewWriter(&response)
r := bufio.NewReader(&response)
2023-09-15 03:13:21 +00:00
style := styles.Get(t.Theme)
2023-09-14 22:37:22 +00:00
if style == nil {
style = styles.Fallback
}
formatter := html.New(
html.TabWidth(4),
html.WithClasses(true),
html.WrapLongLines(true))
iterator, err := lexer.Tokenise(nil, contentString)
if err != nil {
return "", err
2023-09-14 22:37:22 +00:00
}
err = formatter.Format(w, style, iterator)
if err != nil {
return "", err
2023-09-14 22:37:22 +00:00
}
w.Flush()
b, err := io.ReadAll(r)
if err != nil {
return "", err
2023-09-14 22:37:22 +00:00
}
return fmt.Sprintf(`<a href="%s">%s</a>`,
rootUrl,
string(b)), nil
2023-09-14 22:37:22 +00:00
}
func (t Format) Extensions() map[string]string {
return map[string]string{
`.4th`: ``,
`.abap`: ``,
`.ada`: ``,
`.ahk`: ``,
`.as`: ``,
`.awk`: ``,
`.bat`: ``,
`.bib`: ``,
`.bibtex`: ``,
`.c`: ``,
`.clj`: ``,
`.cljc`: ``,
`.cljs`: ``,
`.coffee`: ``,
`.conf`: ``,
`.cpp`: ``,
`.cr`: ``,
`.cs`: ``,
`.css`: ``,
`.d`: ``,
`.elm`: ``,
`.erl`: ``,
`.ex`: ``,
`.exs`: ``,
`.f03`: ``,
`.f90`: ``,
`.f95`: ``,
`.fs`: ``,
`.go`: ``,
`.graphql`: ``,
`.groovy`: ``,
`.gsh`: ``,
`.gvy`: ``,
`.gy`: ``,
`.hc`: ``,
`.hcl`: ``,
`.hs`: ``,
`.java`: ``,
`.jinja`: ``,
`.jl`: ``,
`.js`: ``,
`.json`: ``,
`.kt`: ``,
`.lisp`: ``,
`.lsp`: ``,
`.lua`: ``,
`.m`: ``,
`.md`: ``,
`.ml`: ``,
`.nb`: ``,
`.nim`: ``,
`.nix`: ``,
`.php`: ``,
`.pl`: ``,
`.pp`: ``,
`.proto`: ``,
`.ps`: ``,
`.ps1`: ``,
`.psl`: ``,
`.py`: ``,
`.r`: ``,
`.raku`: ``,
`.rb`: ``,
`.rs`: ``,
`.sass`: ``,
`.sc`: ``,
`.scm`: ``,
`.scss`: ``,
`.scpt`: ``,
`.sh`: ``,
`.sql`: ``,
`.swift`: ``,
`.tcl`: ``,
`.tex`: ``,
`.tf`: ``,
`.toml`: ``,
`.ts`: ``,
`.unit`: ``,
`.v`: ``,
`.vb`: ``,
`.xml`: ``,
`.yaml`: ``,
`.yml`: ``,
`.zig`: ``,
2023-09-14 22:37:22 +00:00
}
}
func (t Format) MimeType(extension string) string {
extensions := t.Extensions()
value, exists := extensions[extension]
if exists {
return value
}
return ""
}
func (t Format) Validate(filePath string) bool {
return true
}
func (t Format) Type() string {
return "inline"
}
2023-09-14 22:37:22 +00:00
func init() {
2023-09-29 19:36:11 +00:00
types.SupportedFormats.Register(Format{})
2023-09-14 22:37:22 +00:00
}