Skip to content

Commit

Permalink
Filesystem UI improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
kyleu committed Sep 22, 2024
1 parent 6737555 commit 7c18d51
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 17 deletions.
46 changes: 37 additions & 9 deletions app/controller/cutil/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cutil
import (
"fmt"
h "html"
"regexp"
"strings"

"github.com/alecthomas/chroma"
Expand All @@ -19,32 +20,32 @@ var (
noLineNums *html.Formatter
)

func FormatJSON(v any) (string, error) {
return FormatLang(util.ToJSON(v), util.KeyJSON)
func FormatJSON(v any, lineNumLinkAndTitle ...string) (string, error) {
return FormatLang(util.ToJSON(v), util.KeyJSON, lineNumLinkAndTitle...)
}

func FormatLang(content string, lang string) (string, error) {
func FormatLang(content string, lang string, lineNumLinkAndTitle ...string) (string, error) {
l := lexers.Get(lang)
return FormatString(content, l)
return FormatString(content, l, lineNumLinkAndTitle...)
}

func FormatLangIgnoreErrors(content string, lang string) string {
ret, err := FormatLang(content, lang)
func FormatLangIgnoreErrors(content string, lang string, lineNumLinkAndTitle ...string) string {
ret, err := FormatLang(content, lang, lineNumLinkAndTitle...)
if err != nil {
return fmt.Sprintf("encoding error: %s\n%s", err.Error(), content)
}
return ret
}

func FormatFilename(content string, filename string) (string, error) {
func FormatFilename(content string, filename string, lineNumLinkAndTitle ...string) (string, error) {
l := lexers.Match(filename)
if l == nil {
l = lexers.Fallback
}
return FormatString(content, l)
return FormatString(content, l, lineNumLinkAndTitle...)
}

func FormatString(content string, l chroma.Lexer) (string, error) {
func FormatString(content string, l chroma.Lexer, lineNumLinkAndTitle ...string) (string, error) {
if l == nil {
return "", errors.New("no lexer available for this content")
}
Expand Down Expand Up @@ -79,10 +80,37 @@ func FormatString(content string, l chroma.Lexer) (string, error) {
if l.Config().Name == "SQL" {
ret = strings.ReplaceAll(ret, `<span class="err">$</span>`, `<span class="mi">$</span>`)
}
if len(lineNumLinkAndTitle) > 0 {
title := ""
if len(lineNumLinkAndTitle) > 1 {
title = lineNumLinkAndTitle[1]
}
ret = injectLinks(ret, lineNumLinkAndTitle[0], title)
}
ret = strings.Replace(ret, `<td class="lntd"><pre tabindex="0" class="chroma"><span class="lnt">1<br /></span></pre></td>`, "", 1)
return ret, nil
}

var injectLinksRegex = regexp.MustCompile(`<span class="lnt">(.*?)</span>`)

func injectLinks(ret string, url string, title string) string {
return injectLinksRegex.ReplaceAllStringFunc(ret, func(match string) string {
content := injectLinksRegex.FindStringSubmatch(match)[1]
var num int
for _, x := range strings.TrimSpace(content) {
if x >= '0' && x <= '9' {
num = num*10 + int(x-'0')
} else {
break
}
}
u := strings.ReplaceAll(url, "{}", fmt.Sprint(num))
t := strings.ReplaceAll(title, "{}", fmt.Sprint(num))
anchor := fmt.Sprintf(`<a target="_blank" rel="noopener noreferrer" title=%q href=%q>`, t, u)
return strings.Replace(match, content, anchor+content+"</a>", 1)
})
}

func FormatMarkdown(s string) (string, error) {
match, end := "<pre><code class=\"language-", "</code></pre>"
idx := strings.Index(s, match)
Expand Down
4 changes: 2 additions & 2 deletions views/vfile/Detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"github.com/kyleu/rituals/app/util"
) %}

{% func Detail(path []string, b []byte, urlPrefix string, additionalLinks map[string]string, as *app.State, ps *cutil.PageState) %}
{% func Detail(path []string, b []byte, urlPrefix string, additionalLinks map[string]string, as *app.State, ps *cutil.PageState, lineNumLinkAndTitle ...string) %}
{%- if additionalLinks != nil && len(additionalLinks) > 0 -%}
<div class="right">
{%- for k, v := range additionalLinks -%}
Expand All @@ -25,7 +25,7 @@ <h3>
{%- if len(b) > (1024 * 128) -%}
<em>File is {%d len(b) %} bytes, which is too large for the file viewer</em>
{%- elseif utf8.Valid(b) -%}
{%- code out, _ := cutil.FormatFilename(string(b), path[len(path)-1]) -%}
{%- code out, _ := cutil.FormatFilename(string(b), path[len(path)-1], lineNumLinkAndTitle...) -%}
{%s= out %}
{%- else -%}

Expand Down
12 changes: 6 additions & 6 deletions views/vfile/Detail.html.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7c18d51

Please sign in to comment.