Migration
This is my new public git source viewer. Source coming soon. Please contact with concerns/bugs.
Contact me
internal/modules/blob.go
c0427d5fc7c0fcf8606c546071085e8af231fb81
· 4.1 KB · 140 lines
raw
| 1 | package modules |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "html/template" |
| 6 | "net/http" |
| 7 | "strings" |
| 8 | "unicode/utf8" |
| 9 | |
| 10 | "gsg/internal/core" |
| 11 | ) |
| 12 | |
| 13 | type Blob struct { |
| 14 | Path string |
| 15 | Size int |
| 16 | Binary bool |
| 17 | Lines []Line |
| 18 | Submodule *Submodule |
| 19 | Lang string // "json" or "yaml", when the path implies one; "" otherwise |
| 20 | ParseErr *ParseNote // set when Lang is non-empty and the content doesn't parse |
| 21 | Oversize bool // Lang is set, but the file is too large to parse and index |
| 22 | } |
| 23 | |
| 24 | // Structured reports whether the fold/search/reference tooling applies to |
| 25 | // this file. A recognised extension is not enough on its own: past |
| 26 | // maxStructuredSize the view falls back to plain numbered lines. |
| 27 | func (b Blob) Structured() bool { return b.Lang != "" && !b.Oversize } |
| 28 | |
| 29 | // StructuredLimit is the size past which that tooling switches itself off, |
| 30 | // so the banner explaining the fallback can name the threshold. |
| 31 | func (b Blob) StructuredLimit() int { return maxStructuredSize } |
| 32 | |
| 33 | // Submodule describes a gitlink entry (mode 160000): a pointer to a commit |
| 34 | // in another repository rather than file content, so there is nothing to |
| 35 | // diff or display line-by-line. |
| 36 | type Submodule struct { |
| 37 | SHA string |
| 38 | Remote string // from .gitmodules, best-effort; empty if not found |
| 39 | } |
| 40 | |
| 41 | // Line is one rendered source line. Everything past Text is structure the |
| 42 | // blob view uses to fold, filter and cross-reference JSON/YAML, and is zero |
| 43 | // for every other language. |
| 44 | type Line struct { |
| 45 | N int |
| 46 | Text template.HTML |
| 47 | |
| 48 | Depth int // nesting level, in indent units |
| 49 | FoldTo int // last line of the block this one opens; 0 if it opens none |
| 50 | Key string // mapping key declared here, for key-scoped search |
| 51 | Anchor *Anchor // YAML anchor declared here |
| 52 | Ref *StructRef // YAML alias resolved here |
| 53 | } |
| 54 | |
| 55 | func init() { core.Register("blob", blobMod) } |
| 56 | |
| 57 | // blob: file contents at Ctx.Path, split into numbered lines. |
| 58 | // Binary detection is git's own heuristic: a NUL in the first 8000 bytes. |
| 59 | func blobMod(c *core.Ctx, args ...string) (any, error) { |
| 60 | path := c.Path |
| 61 | if len(args) > 0 { |
| 62 | path = args[0] |
| 63 | } |
| 64 | if path == "" { |
| 65 | return nil, core.Errorf(http.StatusNotFound, "no path") |
| 66 | } |
| 67 | if entry, ok, _ := c.Repo.Stat(c.Commit, path); ok && entry.Type == "commit" { |
| 68 | return Blob{Path: path, Submodule: &Submodule{ |
| 69 | SHA: entry.SHA, |
| 70 | Remote: c.Repo.SubmoduleURL(c.Commit, path), |
| 71 | }}, nil |
| 72 | } |
| 73 | raw, err := c.Repo.Blob(c.Commit, path) |
| 74 | if err != nil { |
| 75 | return nil, core.Errorf(http.StatusNotFound, "no such file %q", path) |
| 76 | } |
| 77 | b := Blob{Path: path, Size: len(raw)} |
| 78 | |
| 79 | head := raw |
| 80 | if len(head) > 8000 { |
| 81 | head = head[:8000] |
| 82 | } |
| 83 | if bytes.IndexByte(head, 0) >= 0 || !utf8.Valid(head) { |
| 84 | b.Binary = true |
| 85 | return b, nil |
| 86 | } |
| 87 | b.Lang = structuredLang(path) |
| 88 | b.Oversize = b.Lang != "" && len(raw) > maxStructuredSize |
| 89 | if b.Structured() { |
| 90 | b.ParseErr = validateStructured(b.Lang, raw) |
| 91 | } |
| 92 | |
| 93 | hl, ok := highlightLines(path, raw) |
| 94 | if !ok { |
| 95 | for _, t := range strings.Split(strings.TrimRight(string(raw), "\n"), "\n") { |
| 96 | hl = append(hl, template.HTML(template.HTMLEscapeString(t))) |
| 97 | } |
| 98 | } else if b.Structured() { |
| 99 | hl = withIndentGuides(hl, indentUnit(raw)) |
| 100 | } |
| 101 | for i, t := range hl { |
| 102 | b.Lines = append(b.Lines, Line{N: i + 1, Text: t}) |
| 103 | } |
| 104 | if b.Structured() { |
| 105 | annotateStructure(b.Lines, raw, b.Lang) |
| 106 | } |
| 107 | return b, nil |
| 108 | } |
| 109 | |
| 110 | // RawBlob is the unmodified object, for the /raw/ route. Kept separate from |
| 111 | // Blob so the line-split view and the byte stream cache independently. |
| 112 | type RawBlob struct { |
| 113 | Path string |
| 114 | Data []byte |
| 115 | Binary bool |
| 116 | } |
| 117 | |
| 118 | func (r RawBlob) Bytes() []byte { return r.Data } |
| 119 | func (r RawBlob) IsBinary() bool { return r.Binary } |
| 120 | |
| 121 | func init() { core.Register("blobraw", blobRawMod) } |
| 122 | |
| 123 | func blobRawMod(c *core.Ctx, args ...string) (any, error) { |
| 124 | path := c.Path |
| 125 | if len(args) > 0 { |
| 126 | path = args[0] |
| 127 | } |
| 128 | if path == "" { |
| 129 | return nil, core.Errorf(http.StatusNotFound, "no path") |
| 130 | } |
| 131 | raw, err := c.Repo.Blob(c.Commit, path) |
| 132 | if err != nil { |
| 133 | return nil, core.Errorf(http.StatusNotFound, "no such file %q", path) |
| 134 | } |
| 135 | head := raw |
| 136 | if len(head) > 8000 { |
| 137 | head = head[:8000] |
| 138 | } |
| 139 | return RawBlob{Path: path, Data: raw, Binary: bytes.IndexByte(head, 0) >= 0}, nil |
| 140 | } |