|
| 1 | +package release |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "path" |
| 6 | + "path/filepath" |
| 7 | + "regexp" |
| 8 | + "strings" |
| 9 | +) |
| 10 | + |
| 11 | +// benchReadmeRel is the repo-relative path of the benchmark research |
| 12 | +// README whose rendered, link-rewritten copy release.yml publishes to |
| 13 | +// the orphan assets branch (assets/benchmarks/pages/benchmark.md). It |
| 14 | +// is the prose write-up the performance page links to: a copy carrying |
| 15 | +// this release's freshly measured numbers, where main's committed |
| 16 | +// snapshot lags between deliberate run.sh refreshes. |
| 17 | +const benchReadmeRel = benchDirRel + "/README.md" |
| 18 | + |
| 19 | +// linkScheme matches a leading URI scheme (https:, mailto:, …) so an |
| 20 | +// already-absolute link target is left untouched by the rewrite. |
| 21 | +var linkScheme = regexp.MustCompile(`^[a-zA-Z][a-zA-Z0-9+.\-]*:`) |
| 22 | + |
| 23 | +// benchInlineLink matches an inline Markdown link's `](target)` tail, |
| 24 | +// capturing the target (group 1, up to the first whitespace or `)`) |
| 25 | +// and an optional double-quoted title (group 2). Image embeds share |
| 26 | +// the same tail and are rewritten identically — both are real link |
| 27 | +// targets once the page is lifted off the repo tree. |
| 28 | +var benchInlineLink = regexp.MustCompile(`\]\(([^)\s]+)((?:\s+"[^"]*")?)\)`) |
| 29 | + |
| 30 | +// benchRefDef matches a reference-style link definition line, |
| 31 | +// capturing the `[label]: ` prefix (group 1), the target (group 2), |
| 32 | +// and an optional title (group 3). Multiline so `^`/`$` anchor at each |
| 33 | +// line within the non-code segments applyOutsideCode hands it. |
| 34 | +var benchRefDef = regexp.MustCompile( |
| 35 | + `(?m)^(\[[^\]]+\]:[ \t]+)(\S+)((?:[ \t]+"[^"]*")?)[ \t]*$`) |
| 36 | + |
| 37 | +// rewriteRelativeLinksToGitHub rewrites every repo-relative Markdown |
| 38 | +// link in data — resolved against srcDirRel, a repo-root-relative |
| 39 | +// directory — to an absolute GitHub URL on main, so a page lifted out |
| 40 | +// of the repo tree has no link that 404s. The benchmark README is the |
| 41 | +// caller: published to the orphan assets branch, none of its sibling |
| 42 | +// files (run.sh, the coverage matrix, the rule READMEs it cites) exist |
| 43 | +// there, so each relative link must point back at github.com/main. |
| 44 | +// |
| 45 | +// Targets that already resolve as-is are left untouched: anchor-only |
| 46 | +// (`#sec`), site-absolute (`/x`), and scheme-qualified (`https://…`, |
| 47 | +// `mailto:…`) links, plus anything inside a fenced block or inline |
| 48 | +// code span — those are documentation examples, not real targets, and |
| 49 | +// applyOutsideCode keeps the rewrite away from them. |
| 50 | +func rewriteRelativeLinksToGitHub(data []byte, srcDirRel string) []byte { |
| 51 | + return applyOutsideCode(data, func(seg []byte) []byte { |
| 52 | + seg = benchInlineLink.ReplaceAllFunc(seg, func(m []byte) []byte { |
| 53 | + sub := benchInlineLink.FindSubmatch(m) |
| 54 | + url, ok := githubURLForRelativeTarget(string(sub[1]), srcDirRel) |
| 55 | + if !ok { |
| 56 | + return m |
| 57 | + } |
| 58 | + return []byte("](" + url + string(sub[2]) + ")") |
| 59 | + }) |
| 60 | + return benchRefDef.ReplaceAllFunc(seg, func(m []byte) []byte { |
| 61 | + sub := benchRefDef.FindSubmatch(m) |
| 62 | + url, ok := githubURLForRelativeTarget(string(sub[2]), srcDirRel) |
| 63 | + if !ok { |
| 64 | + return m |
| 65 | + } |
| 66 | + return []byte(string(sub[1]) + url + string(sub[3])) |
| 67 | + }) |
| 68 | + }) |
| 69 | +} |
| 70 | + |
| 71 | +// githubURLForRelativeTarget resolves a single Markdown link target |
| 72 | +// against srcDirRel and returns its absolute GitHub URL on main plus |
| 73 | +// true, or ("", false) when the target must be left as-is (empty, |
| 74 | +// anchor-only, site-absolute, or already scheme-qualified). A trailing |
| 75 | +// `#fragment` is preserved, and a trailing slash routes to /tree/ |
| 76 | +// (GitHub's directory view) rather than /blob/ via githubURLForPath. |
| 77 | +func githubURLForRelativeTarget(target, srcDirRel string) (string, bool) { |
| 78 | + if target == "" || target[0] == '#' || target[0] == '/' || |
| 79 | + linkScheme.MatchString(target) { |
| 80 | + return "", false |
| 81 | + } |
| 82 | + rel, frag := target, "" |
| 83 | + if i := strings.IndexByte(target, '#'); i >= 0 { |
| 84 | + rel, frag = target[:i], target[i:] |
| 85 | + } |
| 86 | + // rel is always non-empty here: the guard above rejects a |
| 87 | + // leading '#', so any '#' found sits at index >= 1. |
| 88 | + resolved := path.Join(srcDirRel, rel) |
| 89 | + if strings.HasSuffix(rel, "/") { |
| 90 | + resolved += "/" |
| 91 | + } |
| 92 | + return githubURLForPath([]byte(resolved)) + frag, true |
| 93 | +} |
| 94 | + |
| 95 | +// RenderBenchPage reads the benchmark README (already `mdsmith fix`ed |
| 96 | +// upstream so its <?include?> tables carry this release's freshly |
| 97 | +// measured numbers), rewrites every repo-relative link to an absolute |
| 98 | +// GitHub URL on main, and writes the result to outPath. release.yml's |
| 99 | +// benchmark-publish job publishes that file to |
| 100 | +// assets/benchmarks/pages/benchmark.md so the performance page links a |
| 101 | +// rendered, fresh-numbers copy whose inner links all resolve. |
| 102 | +func (t *Toolkit) RenderBenchPage(root, outPath string) error { |
| 103 | + src := filepath.Join(root, filepath.FromSlash(benchReadmeRel)) |
| 104 | + data, err := t.fs.ReadFile(src) |
| 105 | + if err != nil { |
| 106 | + return fmt.Errorf("read benchmark README %s: %w", src, err) |
| 107 | + } |
| 108 | + page := rewriteRelativeLinksToGitHub(data, benchDirRel) |
| 109 | + dir := filepath.Dir(outPath) |
| 110 | + if err := t.fs.MkdirAll(dir, 0o755); err != nil { |
| 111 | + return fmt.Errorf("mkdir %s: %w", dir, err) |
| 112 | + } |
| 113 | + if err := t.fs.WriteFile(outPath, page, 0o644); err != nil { |
| 114 | + return fmt.Errorf("write benchmark page %s: %w", outPath, err) |
| 115 | + } |
| 116 | + return nil |
| 117 | +} |
| 118 | + |
| 119 | +// RenderBenchPage delegates to a default-OS Toolkit (see Stamp). |
| 120 | +func RenderBenchPage(root, outPath string) error { |
| 121 | + return New().RenderBenchPage(root, outPath) |
| 122 | +} |
0 commit comments