From 297e62a3846e0913444e6c3709dd65ff3bda3a20 Mon Sep 17 00:00:00 2001 From: oppiliappan Date: Tue, 11 Nov 2025 11:23:41 +0000 Subject: [PATCH] appview/pages: rework blob template to work with models.BlobView Change-Id: xuqqqnmtlrpzsutswktonzkyyypqvmow Signed-off-by: oppiliappan --- appview/pages/pages.go | 65 +++------------- appview/pages/templates/repo/blob.html | 101 +++++++++++++++---------- 2 files changed, 72 insertions(+), 94 deletions(-) diff --git a/appview/pages/pages.go b/appview/pages/pages.go index 7771121d..6a27f1ae 100644 --- a/appview/pages/pages.go +++ b/appview/pages/pages.go @@ -744,9 +744,9 @@ type RepoTreeStats struct { func (r RepoTreeParams) TreeStats() RepoTreeStats { numFolders, numFiles := 0, 0 for _, f := range r.Files { - if !f.IsFile { + if !f.IsFile() { numFolders += 1 - } else if f.IsFile { + } else if f.IsFile() { numFiles += 1 } } @@ -817,65 +817,20 @@ func (p *Pages) RepoArtifactFragment(w io.Writer, params RepoArtifactParams) err } type RepoBlobParams struct { - LoggedInUser *oauth.User - RepoInfo repoinfo.RepoInfo - Active string - Unsupported bool - IsImage bool - IsVideo bool - ContentSrc string - BreadCrumbs [][]string - ShowRendered bool - RenderToggle bool - RenderedContents template.HTML + LoggedInUser *oauth.User + RepoInfo repoinfo.RepoInfo + Active string + BreadCrumbs [][]string + BlobView models.BlobView *tangled.RepoBlob_Output - // Computed fields for template compatibility - Contents string - Lines int - SizeHint uint64 - IsBinary bool } func (p *Pages) RepoBlob(w io.Writer, params RepoBlobParams) error { - var style *chroma.Style = styles.Get("catpuccin-latte") - - if params.ShowRendered { - switch markup.GetFormat(params.Path) { - case markup.FormatMarkdown: - p.rctx.RepoInfo = params.RepoInfo - p.rctx.RendererType = markup.RendererTypeRepoMarkdown - htmlString := p.rctx.RenderMarkdown(params.Contents) - sanitized := p.rctx.SanitizeDefault(htmlString) - params.RenderedContents = template.HTML(sanitized) - } - } - - c := params.Contents - formatter := chromahtml.New( - chromahtml.InlineCode(false), - chromahtml.WithLineNumbers(true), - chromahtml.WithLinkableLineNumbers(true, "L"), - chromahtml.Standalone(false), - chromahtml.WithClasses(true), - ) - - lexer := lexers.Get(filepath.Base(params.Path)) - if lexer == nil { - lexer = lexers.Fallback - } - - iterator, err := lexer.Tokenise(nil, c) - if err != nil { - return fmt.Errorf("chroma tokenize: %w", err) - } - - var code bytes.Buffer - err = formatter.Format(&code, style, iterator) - if err != nil { - return fmt.Errorf("chroma format: %w", err) + switch params.BlobView.ContentType { + case models.BlobContentTypeMarkup: + p.rctx.RepoInfo = params.RepoInfo } - params.Contents = code.String() params.Active = "overview" return p.executeRepo("repo/blob", w, params) } diff --git a/appview/pages/templates/repo/blob.html b/appview/pages/templates/repo/blob.html index 9c5368d4..6a4566b1 100644 --- a/appview/pages/templates/repo/blob.html +++ b/appview/pages/templates/repo/blob.html @@ -11,10 +11,6 @@ {{ end }} {{ define "repoContent" }} - {{ $lines := split .Contents }} - {{ $tot_lines := len $lines }} - {{ $tot_chars := len (printf "%d" $tot_lines) }} - {{ $code_number_style := "text-gray-400 dark:text-gray-500 left-0 bg-white dark:bg-gray-800 text-right mr-6 select-none inline-block w-12" }} {{ $linkstyle := "no-underline hover:underline" }}
@@ -36,47 +32,74 @@
at {{ .Ref }} - - {{ .Lines }} lines - - {{ byteFmt .SizeHint }} - - view raw - {{ if .RenderToggle }} - - view {{ if .ShowRendered }}code{{ else }}rendered{{ end }} + + {{ if .BlobView.ShowingText }} + + {{ .Lines }} lines + {{ end }} + + {{ if .BlobView.SizeHint }} + + {{ byteFmt .BlobView.SizeHint }} + {{ end }} + + {{ if .BlobView.HasRawView }} + + view raw + {{ end }} + + {{ if .BlobView.ShowToggle }} + + + view {{ if .BlobView.ShowingRendered }}code{{ else }}rendered{{ end }} + {{ end }}
- {{ if and .IsBinary .Unsupported }} -

- Previews are not supported for this file type. -

- {{ else if .IsBinary }} -
- {{ if .IsImage }} - {{ .Path }} - {{ else if .IsVideo }} - - {{ end }} -
- {{ else }} -
- {{ if .ShowRendered }} -
{{ .RenderedContents }}
+ {{ if .BlobView.IsUnsupported }} +

+ Previews are not supported for this file type. +

+ {{ else if .BlobView.ContentType.IsSubmodule }} +

+ This directory is a git submodule of {{ .BlobView.ContentSrc }}. +

+ {{ else if .BlobView.ContentType.IsImage }} +
+ {{ .Path }} +
+ {{ else if .BlobView.ContentType.IsVideo }} +
+ +
+ {{ else if .BlobView.ContentType.IsSvg }} +
+ {{ if .BlobView.ShowingRendered }} +
+ {{ .Path }} +
{{ else }} -
{{ $.Contents | escapeHtml }}
+
{{ code .BlobView.Contents .Path | escapeHtml }}
{{ end }} -
+
+ {{ else if .BlobView.ContentType.IsMarkup }} +
+ {{ if .BlobView.ShowingRendered }} +
{{ .BlobView.Contents | readme }}
+ {{ else }} +
{{ code .BlobView.Contents .Path | escapeHtml }}
+ {{ end }} +
+ {{ else if .BlobView.ContentType.IsCode }} +
{{ code .BlobView.Contents .Path | escapeHtml }}
{{ end }} {{ template "fragments/multiline-select" }} {{ end }} -- 2.43.0