Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Optional syntax highlighting #28

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@ server:
name: git.icyphox.sh
host: 127.0.0.1
port: 5555
highlight:
enable: true
style: igor
4 changes: 4 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ type Config struct {
Host string `yaml:"host"`
Port int `yaml:"port"`
} `yaml:"server"`
Highlight struct{
Enable bool `yaml:"enable"`
Style string `yaml:"style"`
} `yaml:"highlight"`
}

func Read(f string) (*Config, error) {
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ require (
github.com/Microsoft/go-winio v0.6.0 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20230331115716-d34776aa93ec // indirect
github.com/acomagu/bufpipe v1.0.4 // indirect
github.com/alecthomas/chroma/v2 v2.7.0 // indirect
github.com/aymerick/douceur v0.2.0 // indirect
github.com/cloudflare/circl v1.3.2 // indirect
github.com/dlclark/regexp2 v1.4.0 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
github.com/go-git/gcfg v1.5.0 // indirect
github.com/gorilla/css v1.0.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ github.com/ProtonMail/go-crypto v0.0.0-20230331115716-d34776aa93ec h1:eQusauqzE1
github.com/ProtonMail/go-crypto v0.0.0-20230331115716-d34776aa93ec/go.mod h1:8TI4H3IbrackdNgv+92dI+rhpCaLqM0IfpgCgenFvRE=
github.com/acomagu/bufpipe v1.0.4 h1:e3H4WUzM3npvo5uv95QuJM3cQspFNtFBzvJ2oNjKIDQ=
github.com/acomagu/bufpipe v1.0.4/go.mod h1:mxdxdup/WdsKVreO5GpW4+M/1CE2sMG4jeGJ2sYmHc4=
github.com/alecthomas/chroma/v2 v2.7.0 h1:hm1rY6c/Ob4eGclpQ7X/A3yhqBOZNUTk9q+yhyLIViI=
github.com/alecthomas/chroma/v2 v2.7.0/go.mod h1:yrkMI9807G1ROx13fhe1v6PN2DDeaR73L3d+1nmYQtw=
github.com/alexedwards/flow v0.0.0-20220806114457-cf11be9e0e03 h1:r07xZN3ENBWdxGuU/feCsnpsgHJ7+3uLm7cq9S0sqoI=
github.com/alexedwards/flow v0.0.0-20220806114457-cf11be9e0e03/go.mod h1:1rjOQiOqQlmMdUMuvlJFjldqTnE/tQULE7qPIu4aq3U=
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be h1:9AeTilPcZAjCFIImctFaOjnTIavg87rW78vTPkQqLI8=
Expand All @@ -24,6 +26,8 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dlclark/regexp2 v1.4.0 h1:F1rxgk7p4uKjwIQxBs9oAXe5CqrXlCduYEJvrF4u93E=
github.com/dlclark/regexp2 v1.4.0/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc=
Expand Down
74 changes: 73 additions & 1 deletion routes/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import (
"strings"

"git.icyphox.sh/legit/git"
"github.com/alecthomas/chroma/v2"
"github.com/alecthomas/chroma/v2/formatters/html"
"github.com/alecthomas/chroma/v2/lexers"
"github.com/alecthomas/chroma/v2/styles"
)

func (d *deps) Write404(w http.ResponseWriter) {
Expand Down Expand Up @@ -69,6 +73,62 @@ func countLines(r io.Reader) (int, error) {
}
}

func doHighlight(filename string, content string, d *deps) (string , bool){

if d.c.Highlight.Enable{
var style *chroma.Style

if d.c.Highlight.Style == "" {
style = styles.Get("github")
} else {
style = styles.Get(d.c.Highlight.Style)
}

if style == nil{
style = styles.Fallback
}

lexer := lexers.Match(filename)
if lexer == nil{
lexer = lexers.Fallback
}


formatter := html.New(
html.WithClasses(true),
html.WithLineNumbers(true),
html.WithLinkableLineNumbers(true, "L"),
)

itr , err := lexer.Tokenise(nil , content)
if err != nil{
return content, false
}
buff := new(bytes.Buffer)
_ , err = buff.WriteString("<style>")

if err != nil{
return content, false
}

formatter.WriteCSS(buff , style)

_ , err = buff.WriteString("</style>")

if err != nil{
return content, false
}

err = formatter.Format(buff , style , itr)

if err != nil{
return content, false
}
return buff.String(), true
}
return content , false
}

func (d *deps) showFile(content string, data map[string]any, w http.ResponseWriter) {
tpath := filepath.Join(d.c.Dirs.Templates, "*")
t := template.Must(template.ParseGlob(tpath))
Expand All @@ -86,8 +146,20 @@ func (d *deps) showFile(content string, data map[string]any, w http.ResponseWrit
}
}

newContent , hasHighlight := doHighlight(
filepath.Base(data["path"].(string)),
content,
d,
)
if hasHighlight {
data["content"] = template.HTML(newContent)
data["highlight"] = true
}else {
data["content"] = content
data["highlight"] = false
}

data["linecount"] = lines
data["content"] = content
data["meta"] = d.c.Meta

if err := t.ExecuteTemplate(w, "file", data); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion static/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ a:hover {
}

.file-content {
background: var(--light-gray);
/*background: var(--light-gray);*/
overflow-y: hidden;
overflow-x: auto;
}
Expand Down
6 changes: 6 additions & 0 deletions templates/file.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
<main>
<p>{{ .path }}</p>
<div class="file-wrapper">
{{ if .highlight }}
<td class="file-content">
{{- .content -}}
</td>
{{ else }}
<table >
<tbody><tr>
<td class="line-numbers">
Expand All @@ -23,6 +28,7 @@
</td>
</tbody></tr>
</table>
{{end}}
</div>
</main>
</body>
Expand Down