2023-09-14 22:37:22 +00:00
|
|
|
/*
|
2024-01-14 18:39:14 +00:00
|
|
|
Copyright © 2024 Seednode <seednode@seedno.de>
|
2023-09-14 22:37:22 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
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 {
|
2023-10-04 22:33:03 +00:00
|
|
|
Fun bool
|
2023-09-15 03:13:21 +00:00
|
|
|
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-10-04 22:33:03 +00:00
|
|
|
if t.Fun {
|
|
|
|
css.WriteString("body{font-family: \"Comic Sans MS\", cursive, \"Brush Script MT\", sans-serif;}\n")
|
|
|
|
}
|
2023-09-14 22:37:22 +00:00
|
|
|
|
|
|
|
return css.String()
|
|
|
|
}
|
|
|
|
|
2023-09-15 19:28:21 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-09-15 19:28:21 +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 {
|
2023-09-15 19:28:21 +00:00
|
|
|
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 {
|
2023-09-15 19:28:21 +00:00
|
|
|
return "", err
|
2023-09-14 22:37:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = formatter.Format(w, style, iterator)
|
|
|
|
if err != nil {
|
2023-09-15 19:28:21 +00:00
|
|
|
return "", err
|
2023-09-14 22:37:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
w.Flush()
|
|
|
|
|
|
|
|
b, err := io.ReadAll(r)
|
|
|
|
if err != nil {
|
2023-09-15 19:28:21 +00:00
|
|
|
return "", err
|
2023-09-14 22:37:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf(`<a href="%s">%s</a>`,
|
|
|
|
rootUrl,
|
2023-09-15 19:28:21 +00:00
|
|
|
string(b)), nil
|
2023-09-14 22:37:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t Format) Extensions() map[string]string {
|
|
|
|
return map[string]string{
|
2023-09-15 02:02:48 +00:00
|
|
|
`.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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-09 14:47:17 +00:00
|
|
|
func (t Format) MediaType(extension string) string {
|
2023-09-14 22:37:22 +00:00
|
|
|
extensions := t.Extensions()
|
|
|
|
|
|
|
|
value, exists := extensions[extension]
|
|
|
|
if exists {
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t Format) Validate(filePath string) bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-09-15 20:13:45 +00:00
|
|
|
func (t Format) Type() string {
|
2023-09-15 20:30:52 +00:00
|
|
|
return "inline"
|
2023-09-15 20:13:45 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|