2024-01-19 04:41:16 +00:00
|
|
|
package markup
|
2024-01-15 22:26:51 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
2024-08-01 03:18:17 +00:00
|
|
|
"path/filepath"
|
2024-01-15 22:26:51 +00:00
|
|
|
|
|
|
|
"github.com/alecthomas/chroma/v2"
|
|
|
|
"github.com/alecthomas/chroma/v2/formatters/html"
|
|
|
|
"github.com/alecthomas/chroma/v2/lexers"
|
|
|
|
"github.com/alecthomas/chroma/v2/styles"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2024-01-19 04:41:16 +00:00
|
|
|
// Formatter is the default formatter
|
2024-01-15 22:26:51 +00:00
|
|
|
Formatter = html.New(
|
|
|
|
html.WithLineNumbers(true),
|
|
|
|
html.WithLinkableLineNumbers(true, "L"),
|
|
|
|
html.WithClasses(true),
|
|
|
|
html.LineNumbersInTable(true),
|
|
|
|
)
|
2024-01-17 03:37:25 +00:00
|
|
|
basicFormatter = html.New(
|
|
|
|
html.WithClasses(true),
|
|
|
|
)
|
2024-01-19 04:41:16 +00:00
|
|
|
// Code is the entrypoint for formatting
|
2024-01-15 22:26:51 +00:00
|
|
|
Code = code{}
|
|
|
|
)
|
|
|
|
|
|
|
|
type code struct{}
|
|
|
|
|
2024-08-01 03:18:17 +00:00
|
|
|
var customReg = map[string]string{
|
|
|
|
".hujson": "json",
|
|
|
|
}
|
|
|
|
|
2024-03-01 17:58:05 +00:00
|
|
|
func setup(source []byte, fileName string) (chroma.Iterator, *chroma.Style, error) {
|
2024-01-15 22:26:51 +00:00
|
|
|
lexer := lexers.Match(fileName)
|
|
|
|
if lexer == nil {
|
|
|
|
lexer = lexers.Fallback
|
2024-08-01 03:18:17 +00:00
|
|
|
if name, ok := customReg[filepath.Ext(fileName)]; ok {
|
|
|
|
lexer = lexers.Get(name)
|
|
|
|
}
|
2024-01-15 22:26:51 +00:00
|
|
|
}
|
|
|
|
lexer = chroma.Coalesce(lexer)
|
|
|
|
|
|
|
|
style := styles.Get("catppuccin-mocha")
|
|
|
|
if style == nil {
|
|
|
|
style = styles.Fallback
|
|
|
|
}
|
|
|
|
|
|
|
|
iter, err := lexer.Tokenise(nil, string(source))
|
2024-01-17 03:37:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return iter, style, nil
|
|
|
|
}
|
|
|
|
|
2024-01-19 04:41:16 +00:00
|
|
|
// Basic formats code without any extras
|
2024-01-17 03:37:25 +00:00
|
|
|
func (c code) Basic(source []byte, fileName string, writer io.Writer) error {
|
2024-03-01 17:58:05 +00:00
|
|
|
iter, style, err := setup(source, fileName)
|
2024-01-15 22:26:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-01-17 03:37:25 +00:00
|
|
|
return basicFormatter.Format(writer, style, iter)
|
|
|
|
}
|
2024-01-15 22:26:51 +00:00
|
|
|
|
2024-01-19 04:41:16 +00:00
|
|
|
// Convert formats code with line numbers, links, etc.
|
2024-01-17 03:37:25 +00:00
|
|
|
func (c code) Convert(source []byte, fileName string, writer io.Writer) error {
|
2024-03-01 17:58:05 +00:00
|
|
|
iter, style, err := setup(source, fileName)
|
2024-01-17 03:37:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-01-15 22:26:51 +00:00
|
|
|
return Formatter.Format(writer, style, iter)
|
|
|
|
}
|
2024-03-01 17:58:05 +00:00
|
|
|
|
|
|
|
// Snippet formats code with line numbers starting at a specific line
|
|
|
|
func Snippet(source []byte, fileName string, line int, writer io.Writer) error {
|
|
|
|
iter, style, err := setup(source, fileName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
formatter := html.New(
|
|
|
|
html.WithLineNumbers(true),
|
|
|
|
html.WithClasses(true),
|
|
|
|
html.LineNumbersInTable(true),
|
|
|
|
html.BaseLineNumber(line),
|
|
|
|
)
|
|
|
|
return formatter.Format(writer, style, iter)
|
|
|
|
}
|