add line numbers and TOC to commit page, clean up markup formatter

main
jolheiser 2025-04-23 12:10:00 -05:00
parent 27915049a8
commit 9d6aa43bd4
No known key found for this signature in database
6 changed files with 37 additions and 36 deletions

View File

@ -134,6 +134,14 @@ type CommitFileEntry struct {
Commit string Commit string
} }
// Path returns either the To or From path, in order of preference
func (c CommitFile) Path() string {
if c.To.Path != "" {
return c.To.Path
}
return c.From.Path
}
// Short returns the first eight characters of the SHA // Short returns the first eight characters of the SHA
func (c Commit) Short() string { func (c Commit) Short() string {
return c.SHA[:8] return c.SHA[:8]

View File

@ -12,6 +12,7 @@ import (
"go.jolheiser.com/ugit/internal/html/markup" "go.jolheiser.com/ugit/internal/html/markup"
"github.com/alecthomas/chroma/v2/formatters/html"
"github.com/alecthomas/chroma/v2/styles" "github.com/alecthomas/chroma/v2/styles"
) )
@ -46,15 +47,16 @@ func tailwind() error {
} }
fmt.Println("generating chroma styles...") fmt.Println("generating chroma styles...")
formatter := html.New(markup.Options("")...)
latte := styles.Get("catppuccin-latte") latte := styles.Get("catppuccin-latte")
if err := markup.Formatter.WriteCSS(tmp, latte); err != nil { if err := formatter.WriteCSS(tmp, latte); err != nil {
return err return err
} }
tmp.WriteString("@media (prefers-color-scheme: dark) {") tmp.WriteString("@media (prefers-color-scheme: dark) {")
mocha := styles.Get("catppuccin-mocha") mocha := styles.Get("catppuccin-mocha")
if err := markup.Formatter.WriteCSS(tmp, mocha); err != nil { if err := formatter.WriteCSS(tmp, mocha); err != nil {
return err return err
} }
tmp.WriteString("}") tmp.WriteString("}")

View File

@ -10,27 +10,20 @@ import (
"github.com/alecthomas/chroma/v2/styles" "github.com/alecthomas/chroma/v2/styles"
) )
var (
// Formatter is the default formatter
Formatter = html.New(
html.WithLineNumbers(true),
html.WithLinkableLineNumbers(true, "L"),
html.WithClasses(true),
html.LineNumbersInTable(true),
)
basicFormatter = html.New(
html.WithClasses(true),
)
// Code is the entrypoint for formatting
Code = code{}
)
type code struct{}
var customReg = map[string]string{ var customReg = map[string]string{
".hujson": "json", ".hujson": "json",
} }
// Options are the default set of formatting options
func Options(linePrefix string) []html.Option {
return []html.Option{
html.WithLineNumbers(true),
html.WithLinkableLineNumbers(true, linePrefix),
html.WithClasses(true),
html.LineNumbersInTable(true),
}
}
func setup(source []byte, fileName string) (chroma.Iterator, *chroma.Style, error) { func setup(source []byte, fileName string) (chroma.Iterator, *chroma.Style, error) {
lexer := lexers.Match(fileName) lexer := lexers.Match(fileName)
if lexer == nil { if lexer == nil {
@ -54,22 +47,13 @@ func setup(source []byte, fileName string) (chroma.Iterator, *chroma.Style, erro
return iter, style, nil return iter, style, nil
} }
// Basic formats code without any extras
func (c code) Basic(source []byte, fileName string, writer io.Writer) error {
iter, style, err := setup(source, fileName)
if err != nil {
return err
}
return basicFormatter.Format(writer, style, iter)
}
// Convert formats code with line numbers, links, etc. // Convert formats code with line numbers, links, etc.
func (c code) Convert(source []byte, fileName string, writer io.Writer) error { func Convert(source []byte, fileName, linePrefix string, writer io.Writer) error {
iter, style, err := setup(source, fileName) iter, style, err := setup(source, fileName)
if err != nil { if err != nil {
return err return err
} }
return Formatter.Format(writer, style, iter) return html.New(Options(linePrefix)...).Format(writer, style, iter)
} }
// Snippet formats code with line numbers starting at a specific line // Snippet formats code with line numbers starting at a specific line

View File

@ -42,10 +42,17 @@ func RepoCommitTemplate(rcc RepoCommitContext) Node {
), ),
Div(Title(rcc.Commit.When.Format("01/02/2006 03:04:05 PM")), Text(humanize.Time(rcc.Commit.When))), Div(Title(rcc.Commit.When.Format("01/02/2006 03:04:05 PM")), Text(humanize.Time(rcc.Commit.When))),
), ),
Div(Class("text-text mt-5"), Textf("%d changed files, %d additions(+), %d deletions(-)", rcc.Commit.Stats.Changed, rcc.Commit.Stats.Additions, rcc.Commit.Stats.Deletions)), Details(Class("text-text mt-5"),
Summary(Class("cursor-pointer"), Textf("%d changed files, %d additions(+), %d deletions(-)", rcc.Commit.Stats.Changed, rcc.Commit.Stats.Additions, rcc.Commit.Stats.Deletions)),
Div(Class("p-3 bg-base rounded"),
Map(rcc.Commit.Files, func(file git.CommitFile) Node {
return A(Class("block underline decoration-text/50 decoration-dashed hover:decoration-solid"), Href("#"+file.Path()), Text(file.Path()))
}),
),
),
Map(rcc.Commit.Files, func(file git.CommitFile) Node { Map(rcc.Commit.Files, func(file git.CommitFile) Node {
return Group([]Node{ return Group([]Node{
Div(Class("text-text mt-5"), Div(Class("text-text mt-5"), ID(file.Path()),
Span(Class("text-text/80"), Title(file.Action), Text(string(file.Action[0]))), Span(Class("text-text/80"), Title(file.Action), Text(string(file.Action[0]))),
Text(" "), Text(" "),
If(file.From.Path != "", If(file.From.Path != "",
@ -56,7 +63,7 @@ func RepoCommitTemplate(rcc RepoCommitContext) Node {
A(Class("underline decoration-text/50 decoration-dashed hover:decoration-solid"), Href(fmt.Sprintf("/%s/tree/%s/%s", rcc.RepoHeaderComponentContext.Name, file.To.Commit, file.To.Path)), Text(file.To.Path)), A(Class("underline decoration-text/50 decoration-dashed hover:decoration-solid"), Href(fmt.Sprintf("/%s/tree/%s/%s", rcc.RepoHeaderComponentContext.Name, file.To.Commit, file.To.Path)), Text(file.To.Path)),
), ),
), ),
Div(Class("whitespace-pre code"), Div(Class("code"),
Raw(file.Patch), Raw(file.Patch),
), ),
}) })

File diff suppressed because one or more lines are too long

View File

@ -88,7 +88,7 @@ func (rh repoHandler) repoFile(w http.ResponseWriter, r *http.Request, repo *git
} }
var buf bytes.Buffer var buf bytes.Buffer
if err := markup.Code.Convert([]byte(content), filepath.Base(path), &buf); err != nil { if err := markup.Convert([]byte(content), filepath.Base(path), "L", &buf); err != nil {
return httperr.Error(err) return httperr.Error(err)
} }
@ -158,7 +158,7 @@ func (rh repoHandler) repoCommit(w http.ResponseWriter, r *http.Request) error {
for idx, p := range commit.Files { for idx, p := range commit.Files {
var patch bytes.Buffer var patch bytes.Buffer
if err := markup.Code.Basic([]byte(p.Patch), "commit.patch", &patch); err != nil { if err := markup.Convert([]byte(p.Patch), "commit.patch", p.Path()+"-L", &patch); err != nil {
return httperr.Error(err) return httperr.Error(err)
} }
commit.Files[idx].Patch = patch.String() commit.Files[idx].Patch = patch.String()