Goit

Simple and lightweight Git web server
git clone http://git.omkov.net/Goit
Log | Tree | Refs | README | Download

Goit/src/repo/highlight.go (79 lines, 2.4 KiB) -rw-r--r-- file download

d737f58 Jakob Wakeling 2023-12-28 21:15:51
0
// Copyright (C) 2023, Jakob Wakeling
d737f58 Jakob Wakeling 2023-12-28 21:15:51
1
// All rights reserved.
d737f58 Jakob Wakeling 2023-12-28 21:15:51
2
d737f58 Jakob Wakeling 2023-12-28 21:15:51
3
package repo
d737f58 Jakob Wakeling 2023-12-28 21:15:51
4
d737f58 Jakob Wakeling 2023-12-28 21:15:51
5
import (
d737f58 Jakob Wakeling 2023-12-28 21:15:51
6
	"bytes"
d737f58 Jakob Wakeling 2023-12-28 21:15:51
7
d737f58 Jakob Wakeling 2023-12-28 21:15:51
8
	"github.com/alecthomas/chroma"
d737f58 Jakob Wakeling 2023-12-28 21:15:51
9
	"github.com/alecthomas/chroma/formatters/html"
d737f58 Jakob Wakeling 2023-12-28 21:15:51
10
	"github.com/alecthomas/chroma/lexers"
d737f58 Jakob Wakeling 2023-12-28 21:15:51
11
	"github.com/alecthomas/chroma/styles"
d737f58 Jakob Wakeling 2023-12-28 21:15:51
12
)
d737f58 Jakob Wakeling 2023-12-28 21:15:51
13
a0ac27c Jakob Wakeling 2024-07-06 23:32:05
14
func Highlight(name, input string, splittable bool) (string, string, error) {
d737f58 Jakob Wakeling 2023-12-28 21:15:51
15
	var buf, css bytes.Buffer
d737f58 Jakob Wakeling 2023-12-28 21:15:51
16
d737f58 Jakob Wakeling 2023-12-28 21:15:51
17
	lexer := lexers.Match(name)
d737f58 Jakob Wakeling 2023-12-28 21:15:51
18
	if lexer == nil {
d737f58 Jakob Wakeling 2023-12-28 21:15:51
19
		lexer = lexers.Fallback
d737f58 Jakob Wakeling 2023-12-28 21:15:51
20
	}
d737f58 Jakob Wakeling 2023-12-28 21:15:51
21
a0ac27c Jakob Wakeling 2024-07-06 23:32:05
22
	formatter := html.New(html.WithClasses(!splittable), html.PreventSurroundingPre(splittable))
d737f58 Jakob Wakeling 2023-12-28 21:15:51
23
d737f58 Jakob Wakeling 2023-12-28 21:15:51
24
	iter, err := lexer.Tokenise(nil, input)
d737f58 Jakob Wakeling 2023-12-28 21:15:51
25
	if err != nil {
d737f58 Jakob Wakeling 2023-12-28 21:15:51
26
		return "", "", err
d737f58 Jakob Wakeling 2023-12-28 21:15:51
27
	}
d737f58 Jakob Wakeling 2023-12-28 21:15:51
28
d737f58 Jakob Wakeling 2023-12-28 21:15:51
29
	if err := formatter.Format(&buf, goitStyle, iter); err != nil {
d737f58 Jakob Wakeling 2023-12-28 21:15:51
30
		return "", "", err
d737f58 Jakob Wakeling 2023-12-28 21:15:51
31
	}
d737f58 Jakob Wakeling 2023-12-28 21:15:51
32
d737f58 Jakob Wakeling 2023-12-28 21:15:51
33
	if err := formatter.WriteCSS(&css, goitStyle); err != nil {
d737f58 Jakob Wakeling 2023-12-28 21:15:51
34
		return "", "", err
d737f58 Jakob Wakeling 2023-12-28 21:15:51
35
	}
d737f58 Jakob Wakeling 2023-12-28 21:15:51
36
d737f58 Jakob Wakeling 2023-12-28 21:15:51
37
	return buf.String(), css.String(), nil
d737f58 Jakob Wakeling 2023-12-28 21:15:51
38
}
d737f58 Jakob Wakeling 2023-12-28 21:15:51
39
d737f58 Jakob Wakeling 2023-12-28 21:15:51
40
var goitStyle = styles.Register(chroma.MustNewStyle("goit", chroma.StyleEntries{
d737f58 Jakob Wakeling 2023-12-28 21:15:51
41
	chroma.Background:            "#888888",
d737f58 Jakob Wakeling 2023-12-28 21:15:51
42
	chroma.Comment:               "italic #666666",
d737f58 Jakob Wakeling 2023-12-28 21:15:51
43
	chroma.CommentPreproc:        "noinherit #8ec07c",
d737f58 Jakob Wakeling 2023-12-28 21:15:51
44
	chroma.CommentPreprocFile:    "noinherit #b8bb26",
d737f58 Jakob Wakeling 2023-12-28 21:15:51
45
	chroma.GenericDeleted:        "#d65d0e",
d737f58 Jakob Wakeling 2023-12-28 21:15:51
46
	chroma.GenericEmph:           "italic",
d737f58 Jakob Wakeling 2023-12-28 21:15:51
47
	chroma.GenericError:          "bold bg:#fb4934",
d737f58 Jakob Wakeling 2023-12-28 21:15:51
48
	chroma.GenericHeading:        "bold #fabd2f",
d737f58 Jakob Wakeling 2023-12-28 21:15:51
49
	chroma.GenericInserted:       "#b8bb26",
d737f58 Jakob Wakeling 2023-12-28 21:15:51
50
	chroma.GenericOutput:         "#504945",
d737f58 Jakob Wakeling 2023-12-28 21:15:51
51
	chroma.GenericPrompt:         "#ebdbb2",
d737f58 Jakob Wakeling 2023-12-28 21:15:51
52
	chroma.GenericStrong:         "bold",
d737f58 Jakob Wakeling 2023-12-28 21:15:51
53
	chroma.GenericSubheading:     "bold #fabd2f",
d737f58 Jakob Wakeling 2023-12-28 21:15:51
54
	chroma.GenericTraceback:      "bold bg:#fb4934",
d737f58 Jakob Wakeling 2023-12-28 21:15:51
55
	chroma.GenericUnderline:      "underline",
d737f58 Jakob Wakeling 2023-12-28 21:15:51
56
	chroma.Keyword:               "#fb4934",
d737f58 Jakob Wakeling 2023-12-28 21:15:51
57
	chroma.KeywordNamespace:      "#d3869b",
d737f58 Jakob Wakeling 2023-12-28 21:15:51
58
	chroma.KeywordType:           "#fabd2f",
d737f58 Jakob Wakeling 2023-12-28 21:15:51
59
	chroma.LiteralNumber:         "#d3869b",
d737f58 Jakob Wakeling 2023-12-28 21:15:51
60
	chroma.LiteralString:         "#b8bb26",
d737f58 Jakob Wakeling 2023-12-28 21:15:51
61
	chroma.LiteralStringEscape:   "#d3869b",
d737f58 Jakob Wakeling 2023-12-28 21:15:51
62
	chroma.LiteralStringInterpol: "#8ec07c",
d737f58 Jakob Wakeling 2023-12-28 21:15:51
63
	chroma.LiteralStringRegex:    "#fe8019",
d737f58 Jakob Wakeling 2023-12-28 21:15:51
64
	chroma.LiteralStringSymbol:   "#83a598",
d737f58 Jakob Wakeling 2023-12-28 21:15:51
65
	chroma.Name:                  "#ebdbb2",
d737f58 Jakob Wakeling 2023-12-28 21:15:51
66
	chroma.NameAttribute:         "#fabd2f",
d737f58 Jakob Wakeling 2023-12-28 21:15:51
67
	chroma.NameBuiltin:           "#fabd2f",
d737f58 Jakob Wakeling 2023-12-28 21:15:51
68
	chroma.NameClass:             "#fabd2f",
d737f58 Jakob Wakeling 2023-12-28 21:15:51
69
	chroma.NameConstant:          "#d3869b",
d737f58 Jakob Wakeling 2023-12-28 21:15:51
70
	chroma.NameEntity:            "#fabd2f",
d737f58 Jakob Wakeling 2023-12-28 21:15:51
71
	chroma.NameException:         "#fb4934",
d737f58 Jakob Wakeling 2023-12-28 21:15:51
72
	chroma.NameFunction:          "#fabd2f",
d737f58 Jakob Wakeling 2023-12-28 21:15:51
73
	chroma.NameLabel:             "#fb4934",
d737f58 Jakob Wakeling 2023-12-28 21:15:51
74
	chroma.NameTag:               "#8ec07c",
d737f58 Jakob Wakeling 2023-12-28 21:15:51
75
	chroma.NameVariable:          "#83a598",
d737f58 Jakob Wakeling 2023-12-28 21:15:51
76
	chroma.Operator:              "#8ec07c",
d737f58 Jakob Wakeling 2023-12-28 21:15:51
77
}))
78