diff --git a/README.md b/README.md index c7b6688..cd64a23 100644 --- a/README.md +++ b/README.md @@ -122,8 +122,6 @@ Each RFD is a directory `NNNN/` with a four-digit number: :discussion: https://github.com/example/rfd/pull/12 :labels: core, storage :visibility: public - :idprefix: - :idseparator: - = RFD 12 Storage layout ``` @@ -136,10 +134,9 @@ Each RFD is a directory `NNNN/` with a four-digit number: be made public. Visibility is metadata that `rfdtool check` enforces and a site can read; `rfdtool` itself does not publish or hide anything. - Section ids follow `:idprefix:` empty and `:idseparator: -`, so `== Goal` - is `<>`. The built-in template sets both; render RFDs with them, or - `rfdtool check` accepts references that the default Asciidoctor settings - would break. + Section ids are Asciidoctor's defaults, so `== Goal and scope` is + `<<_goal_and_scope>>`. Render RFDs without changing `idprefix` or + `idseparator`, or links that `rfdtool check` accepts will break. - `IMPLEMENTATION.adoc` (or `.md` or `.org`): the implementation checklist. Its first line is `= RFD NNNN implementation checklist` (`#` in Markdown, `#+TITLE:` in Org), it links back to `README.adoc`, the README links to it, @@ -167,17 +164,21 @@ directory instead. ### Links between RFDs -Every mention of another RFD links to it with a relative source link, which -renders on GitHub and which a site can rewrite into its own pages: +Every mention of another RFD links to it with an AsciiDoc cross reference. +GitHub renders it as a link to the source, and Asciidoctor as a link to +`README.html`, which a site can rewrite into its own pages: ```asciidoc -See link:../0005/README.adoc[RFD 5], and RFDs link:../0013/README.adoc[13] -and link:../0014/README.adoc[14]. +See xref:../0005/README.adoc[RFD 5], and RFDs xref:../0013/README.adoc[13] +and xref:../0014/README.adoc[14]. xref:../0005/README.adoc#_goal[Its goal] +links to a section. ``` `rfdtool check` rejects unlinked mentions, links to RFDs or sections that do -not exist, and links from a public RFD to a private one, in `README.adoc` and -`IMPLEMENTATION.adoc`. `rfdtool link` adds the missing links. Links in +not exist, links from a public RFD to a private one, and `link:` macros to +other RFDs, in `README.adoc` and `IMPLEMENTATION.adoc`. `rfdtool link` adds +the missing links. Replacing `link:` macros with cross references is allowed +on a final RFD. Links in Markdown and Org checklists are not checked. ## Developing diff --git a/internal/rfd/check.go b/internal/rfd/check.go index 858218c..036b339 100644 --- a/internal/rfd/check.go +++ b/internal/rfd/check.go @@ -41,7 +41,9 @@ func (r *Report) problem(path, format string, args ...any) { var ( entryName = regexp.MustCompile(`^[0-9]{4}$`) - rfdLink = regexp.MustCompile(`link:\.\./([0-9]{4})/`) + // rfdLink matches any link into another RFD's directory, so visibility + // covers links to its other files too. + rfdLink = regexp.MustCompile(`(?:xref|link):\.\./([0-9]{4})/`) ) // An rfdLinkTarget is a link from one of an RFD's documents to another RFD. diff --git a/internal/rfd/check_test.go b/internal/rfd/check_test.go index 1c51b35..48b27b4 100644 --- a/internal/rfd/check_test.go +++ b/internal/rfd/check_test.go @@ -313,13 +313,18 @@ func TestIssues(t *testing.T) { {"public RFD links to private RFD", func(f *fixture) { f.writeRFD("prediscussion", "", "adoc") f.writeSecond(":visibility: private\n:labels: core") - f.append("0001/README.adoc", "\nSee link:../0002/README.adoc[RFD 2].\n") + f.append("0001/README.adoc", "\nSee xref:../0002/README.adoc[RFD 2].\n") }, "0001/README.adoc: public RFD links to private RFD 0002"}, {"public RFD's checklist links to private RFD", func(f *fixture) { f.writeRFD("prediscussion", "", "adoc") f.writeSecond(":visibility: private\n:labels: core") - f.append("0001/IMPLEMENTATION.adoc", "\nSee link:../0002/README.adoc[RFD 2].\n") + f.append("0001/IMPLEMENTATION.adoc", "\nSee xref:../0002/README.adoc[RFD 2].\n") }, "0001/IMPLEMENTATION.adoc: public RFD links to private RFD 0002"}, + {"public RFD links to private RFD's checklist", func(f *fixture) { + f.writeRFD("prediscussion", "", "adoc") + f.writeSecond(":visibility: private\n:labels: core") + f.append("0001/README.adoc", "\nSee link:../0002/IMPLEMENTATION.adoc[its checklist].\n") + }, "0001/README.adoc: public RFD links to private RFD 0002"}, {"unchecked task mentioning a checkbox", func(f *fixture) { f.writeRFD("committed", "https://example.com/pull/2", "adoc") f.replace("0001/IMPLEMENTATION.adoc", "* [ ] Complete the work.", "* [ ] Render [x] checkboxes.") @@ -397,6 +402,14 @@ func TestAllowed(t *testing.T) { f.snapshot() f.toOrg() }}, + {"final RFD's link macros become cross references", func(f *fixture) { + f.writeDone() + f.writeSecond(":labels: core") + f.append("0002/README.adoc", "\n== Design\n") + f.append("0001/README.adoc", "\nSee link:../0002/README.adoc#design[RFD 2's design].\n") + f.snapshot() + f.replace("0001/README.adoc", "link:../0002/README.adoc#design", "xref:../0002/README.adoc#_design") + }}, {"final RFD relabeled", func(f *fixture) { f.writeDone() f.snapshot() @@ -406,12 +419,12 @@ func TestAllowed(t *testing.T) { f.writeRFD("prediscussion", "", "adoc") f.replace("0001/README.adoc", ":labels:", ":visibility: private\n:labels:") f.writeSecond(":labels: internal") - f.append("0001/README.adoc", "\nSee link:../0002/README.adoc[RFD 2].\n") + f.append("0001/README.adoc", "\nSee xref:../0002/README.adoc[RFD 2].\n") }}, {"public RFD links to public RFD", func(f *fixture) { f.writeRFD("prediscussion", "", "adoc") f.writeSecond(":labels: core") - f.append("0001/README.adoc", "\nSee link:../0002/README.adoc[RFD 2].\n") + f.append("0001/README.adoc", "\nSee xref:../0002/README.adoc[RFD 2].\n") }}, {"final RFD made public", func(f *fixture) { f.writeDone() diff --git a/internal/rfd/links.go b/internal/rfd/links.go index 477f775..6a197a9 100644 --- a/internal/rfd/links.go +++ b/internal/rfd/links.go @@ -13,13 +13,15 @@ import ( "unicode" ) -// An RFD that mentions another RFD links to it, so readers can follow the -// reference on the site and on GitHub: +// An RFD that mentions another RFD links to it with an AsciiDoc cross +// reference, so readers can follow the reference on the site and on GitHub: // -// link:../0005/README.adoc[RFD 5] -// RFDs link:../0013/README.adoc[13] and link:../0014/README.adoc[14] +// xref:../0005/README.adoc[RFD 5] +// RFDs xref:../0013/README.adoc[13] and xref:../0014/README.adoc[14] +// xref:../0005/README.adoc#_goal[its goal] // -// The site rewrites these source links into page links. +// Asciidoctor renders these as links to README.html, which a site rewrites +// into page links. Section ids are Asciidoctor's defaults, such as _goal. var ( // mention matches "RFD 5", and "RFDs 12, 13, and 14" or "RFDs 1 to 7" @@ -27,15 +29,16 @@ var ( mention = regexp.MustCompile(`\b(RFDs?)[ \t\n]+([0-9]+(?:(?:,[ \t\n]*(?:(?:and|or)[ \t\n]+)?|[ \t\n]+(?:and|or|to|through)[ \t\n]+)[0-9]+)*)\b`) number = regexp.MustCompile(`[0-9]+`) - // rfdLinkMacro matches a link to another RFD, with the RFD number in group - // 1, the anchor in group 2, and the link text in group 3. - rfdLinkMacro = regexp.MustCompile(`link:\.\./([0-9]{4})/README\.adoc(?:#([^\[\s]*))?\[([^\]]*)\]`) + // rfdLinkMacro matches a link to another RFD, with the macro in group 1, + // the RFD number in group 2, the anchor in group 3, and the link text in + // group 4. Only xref is valid; link is matched to reject it. + rfdLinkMacro = regexp.MustCompile(`(xref|link):\.\./([0-9]{4})/README\.adoc(?:#([^\[\s]*))?\[([^\]]*)\]`) // localXref matches <> and <>. localXref = regexp.MustCompile(`<<([^,>]+)(?:,[^>]*)?>>`) - // Spans where a mention is not prose: link macros and URLs with their - // text, cross references, and inline literals. - skipSpan = regexp.MustCompile(`(?:link:|https?://)[^\[\s]*\[[^\]]*\]|<<[^>]*>>|` + "`[^`\n]*`" + `|\+[^+\n]*\+`) + // Spans where a mention is not prose: link and xref macros and URLs with + // their text, cross references, and inline literals. + skipSpan = regexp.MustCompile(`(?:link:|xref:|https?://)[^\[\s]*\[[^\]]*\]|<<[^>]*>>|` + "`[^`\n]*`" + `|\+[^+\n]*\+`) // Lines where a mention is not prose: titles, attributes, and comments. skipLine = regexp.MustCompile(`^(?:= |:[^:]+:|//)`) // Delimiters of blocks whose text is not prose: listings, literals, @@ -110,9 +113,12 @@ func checkLinks(text string, self int, anchors map[string]map[string]bool) []lin } for _, m := range rfdLinkMacro.FindAllStringSubmatch(text, -1) { - target, anchor, label := m[1], m[2], m[3] + macro, target, anchor, label := m[1], m[2], m[3], m[4] ids, ok := anchors[target] switch { + case macro == "link": + problems = append(problems, linkProblem{message: "links to RFD " + strings.TrimLeft(target, "0") + " with link:; use xref:"}) + continue case !ok: problems = append(problems, linkProblem{message: "links to RFD " + strings.TrimLeft(target, "0") + ", which does not exist"}) continue @@ -158,7 +164,7 @@ func LinkMentions(text string, self int, exists func(int) bool) string { if word == "RFD" { label = "RFD " + label } - fmt.Fprintf(&linked, "link:../%04d/README.adoc[%s]", num, label) + fmt.Fprintf(&linked, "xref:../%04d/README.adoc[%s]", num, label) changed = true } if !changed { @@ -177,8 +183,8 @@ func LinkMentions(text string, self int, exists func(int) bool) string { return b.String() } -// sectionIDs returns the ids a document's sections and anchors get, with the -// site's AsciiDoc settings: no prefix and "-" between words. A section whose +// sectionIDs returns the ids a document's sections and anchors get, with +// Asciidoctor's default settings: "_" before and between words. A section whose // block attributes or title set an id gets that id instead of one from its // title. Anchors in literal blocks, such as TOML tables in a listing, are // text, unless the block turns substitutions on. When unsure, it keeps an id: @@ -317,29 +323,34 @@ func literalLines(lines []string) []bool { return literal } -// sectionID mirrors Asciidoctor's generated section ids with idprefix="" and -// idseparator="-". +// sectionID mirrors the section ids asciidoctor.js generates with its default +// idprefix and idseparator, both "_": it drops other characters, turns +// spaces, dots, and hyphens into "_", collapses each run of "_" into one, and +// drops a trailing one. func sectionID(title string) string { title = regexp.MustCompile(`<[^>]+>|&[a-z][a-z]+\d{0,2};|&#\d+;|&#x[0-9a-f]+;`).ReplaceAllString(strings.ToLower(title), "") - title = strings.NewReplacer("`", "", "*", "").Replace(title) var b strings.Builder + b.WriteByte('_') for _, r := range title { switch { case r == ' ' || r == '.' || r == '-': - b.WriteRune('-') + b.WriteByte('_') case unicode.IsLetter(r) || unicode.IsDigit(r) || r == '_': b.WriteRune(r) } } - id := regexp.MustCompile(`-+`).ReplaceAllString(b.String(), "-") - return strings.Trim(id, "-") + id := regexp.MustCompile(`_+`).ReplaceAllString(b.String(), "_") + if len(id) > 1 { + id = strings.TrimSuffix(id, "_") + } + return id } // unlinkRFDs replaces links to other RFDs with their text, and drops the // target of cross references, so that adding or fixing a link is not a change // to a final RFD's decision. func unlinkRFDs(line string) string { - return xrefTarget.ReplaceAllString(rfdLinkMacro.ReplaceAllString(line, "$3"), "<<") + return xrefTarget.ReplaceAllString(rfdLinkMacro.ReplaceAllString(line, "$4"), "<<") } var xrefTarget = regexp.MustCompile(`<<[^,>]+`) diff --git a/internal/rfd/links_test.go b/internal/rfd/links_test.go index bcebb4f..78f8025 100644 --- a/internal/rfd/links_test.go +++ b/internal/rfd/links_test.go @@ -10,19 +10,19 @@ func TestLinkMentions(t *testing.T) { tests := []struct { name, text, want string }{ - {"single", "See RFD 5.", "See link:../0005/README.adoc[RFD 5]."}, + {"single", "See RFD 5.", "See xref:../0005/README.adoc[RFD 5]."}, {"list", "RFDs 12, 13, and 14 apply.", - "RFDs link:../0012/README.adoc[12], link:../0013/README.adoc[13], and link:../0014/README.adoc[14] apply."}, - {"range", "RFDs 1 to 7", "RFDs link:../0001/README.adoc[1] to link:../0007/README.adoc[7]"}, - {"across lines", "as RFD\n5 says", "as link:../0005/README.adoc[RFD 5] says"}, + "RFDs xref:../0012/README.adoc[12], xref:../0013/README.adoc[13], and xref:../0014/README.adoc[14] apply."}, + {"range", "RFDs 1 to 7", "RFDs xref:../0001/README.adoc[1] to xref:../0007/README.adoc[7]"}, + {"across lines", "as RFD\n5 says", "as xref:../0005/README.adoc[RFD 5] says"}, {"self", "This is RFD 3.", "This is RFD 3."}, - {"missing", "RFD 99 and RFD 2", "RFD 99 and link:../0002/README.adoc[RFD 2]"}, - {"already linked", "link:../0005/README.adoc[RFD 5]", "link:../0005/README.adoc[RFD 5]"}, + {"missing", "RFD 99 and RFD 2", "RFD 99 and xref:../0002/README.adoc[RFD 2]"}, + {"already linked", "xref:../0005/README.adoc[RFD 5]", "xref:../0005/README.adoc[RFD 5]"}, {"url text", "https://example.com/rfd[RFD 5]", "https://example.com/rfd[RFD 5]"}, {"literal", "Use `RFD 5` and +RFD 6+.", "Use `RFD 5` and +RFD 6+."}, {"title", "= RFD 5 Title\n", "= RFD 5 Title\n"}, {"comment", "// RFD 5\n", "// RFD 5\n"}, - {"listing", "----\nRFD 5\n----\nRFD 5\n", "----\nRFD 5\n----\nlink:../0005/README.adoc[RFD 5]\n"}, + {"listing", "----\nRFD 5\n----\nRFD 5\n", "----\nRFD 5\n----\nxref:../0005/README.adoc[RFD 5]\n"}, {"word boundary", "RFD 5s and XRFD 5", "RFD 5s and XRFD 5"}, } for _, tt := range tests { @@ -36,20 +36,22 @@ func TestLinkMentions(t *testing.T) { func TestCheckLinks(t *testing.T) { anchors := map[string]map[string]bool{ - "0001": {"goal": true}, - "0002": {"design": true}, + "0001": {"_goal": true}, + "0002": {"_design": true}, } tests := []struct { name, text, want string }{ - {"linked", "See link:../0002/README.adoc[RFD 2] and link:../0002/README.adoc#design[its design].", ""}, - {"own section", "See <> and <>.", ""}, + {"linked", "See xref:../0002/README.adoc[RFD 2] and xref:../0002/README.adoc#_design[its design].", ""}, + {"own section", "See <<_goal>> and <<_goal,the goal>>.", ""}, {"unlinked", "See RFD 2.", "RFD 2 is mentioned without a link"}, {"mentions missing", "See RFD 9.", "mentions RFD 9, which does not exist"}, - {"links missing", "See link:../0009/README.adoc[RFD 9].", "links to RFD 9, which does not exist"}, - {"missing anchor", "See link:../0002/README.adoc#goal[goal].", "links to #goal, which RFD 2 does not have"}, - {"wrong label", "See link:../0002/README.adoc[RFD 3].", `link to RFD 2 is labeled "RFD 3"`}, - {"missing xref", "See <<_goal>>.", "refers to <<_goal>>, which this RFD does not have"}, + {"links missing", "See xref:../0009/README.adoc[RFD 9].", "links to RFD 9, which does not exist"}, + {"missing anchor", "See xref:../0002/README.adoc#_goal[goal].", "links to #_goal, which RFD 2 does not have"}, + {"id without the default prefix", "See xref:../0002/README.adoc#design[design].", "links to #design, which RFD 2 does not have"}, + {"link macro", "See link:../0002/README.adoc[RFD 2].", "links to RFD 2 with link:; use xref:"}, + {"wrong label", "See xref:../0002/README.adoc[RFD 3].", `link to RFD 2 is labeled "RFD 3"`}, + {"missing xref", "See <>.", "refers to <>, which this RFD does not have"}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -66,32 +68,33 @@ func TestCheckLinks(t *testing.T) { func TestSectionIDs(t *testing.T) { ids := sectionIDs("= RFD 1 Title\n\n== Goal and scope\n\n=== `rfd check` v2.0\n\n[[custom]]\n[#other]\n----\n== Not a section\n----\n") - for _, id := range []string{"goal-and-scope", "rfd-check-v2-0", "custom", "other"} { + for _, id := range []string{"_goal_and_scope", "_rfd_check_v2_0", "custom", "other"} { if !ids[id] { t.Errorf("missing id %q in %v", id, ids) } } - if ids["not-a-section"] { + if ids["_not_a_section"] { t.Errorf("listing text became a section: %v", ids) } - // Ids as asciidoctor.js gives them with the site's settings. + // Ids as asciidoctor.js gives them with its default settings. for _, tt := range []struct { name, text string want, not []string }{ - {"anchor replaces title id", "[[values]]\n=== Values and drift\n", []string{"values"}, []string{"values-and-drift"}}, - {"anchor across attributes", "[#ext]\n[role=wide]\n== Extension points\n", []string{"ext"}, []string{"extension-points"}}, - {"anchor across a blank line", "[[a]]\n\n== Blank between\n", []string{"a"}, []string{"blank-between"}}, - {"anchor on a paragraph", "[[p]]\nA paragraph.\n\n== Para before\n", []string{"p", "para-before"}, nil}, - {"anchor in the title", "== Title anchor [[ta]]\n", []string{"ta"}, []string{"title-anchor"}}, - {"id shorthand after a style", "[discrete#dd.role]\n== Discrete\n", []string{"dd"}, []string{"discrete"}}, - {"id attribute", "[id=\"sec\"]\n== Id attr\n", []string{"sec"}, []string{"id-attr"}}, - {"anchor in a quoted value", "[role=\"[[x]]\"]\n== Title\n", []string{"title"}, nil}, + {"anchor replaces title id", "[[values]]\n=== Values and drift\n", []string{"values"}, []string{"_values_and_drift"}}, + {"anchor across attributes", "[#ext]\n[role=wide]\n== Extension points\n", []string{"ext"}, []string{"_extension_points"}}, + {"anchor across a blank line", "[[a]]\n\n== Blank between\n", []string{"a"}, []string{"_blank_between"}}, + {"anchor on a paragraph", "[[p]]\nA paragraph.\n\n== Para before\n", []string{"p", "_para_before"}, nil}, + {"anchor in the title", "== Title anchor [[ta]]\n", []string{"ta"}, []string{"_title_anchor"}}, + {"id shorthand after a style", "[discrete#dd.role]\n== Discrete\n", []string{"dd"}, []string{"_discrete"}}, + {"id attribute", "[id=\"sec\"]\n== Id attr\n", []string{"sec"}, []string{"_id_attr"}}, + {"anchor in a quoted value", "[role=\"[[x]]\"]\n== Title\n", []string{"_title"}, nil}, {"anchor in monospace", "Use `[[x]]value` here.\n", []string{"x"}, nil}, - {"TOML in a listing", "[source,toml]\n----\n[[exporters]]\n----\n\n== After\n", []string{"after"}, []string{"exporters"}}, - {"listing with macros", "[subs=\"+macros\"]\n----\n[[x]]\n----\n\n== After\n", []string{"x", "after"}, nil}, - {"positional with a space", "[foo #bar]\n== Title\n", []string{"title"}, nil}, + {"TOML in a listing", "[source,toml]\n----\n[[exporters]]\n----\n\n== After\n", []string{"_after"}, []string{"exporters"}}, + {"listing with macros", "[subs=\"+macros\"]\n----\n[[x]]\n----\n\n== After\n", []string{"x", "_after"}, nil}, + {"separators", "== .NET & the a__b - c. \n", []string{"_net_the_a_b_c"}, nil}, + {"positional with a space", "[foo #bar]\n== Title\n", []string{"_title"}, nil}, } { ids := sectionIDs("= RFD 1 Title\n\n" + tt.text) for _, id := range tt.want { @@ -111,7 +114,7 @@ func TestWrapChanged(t *testing.T) { before := "Intro.\nRFD 7 and RFD 8 shipped parts of sync before this RFD was settled. This RFD\n" after := LinkMentions(before, 1, func(int) bool { return true }) got := wrapChanged(before, after, 80) - want := "Intro.\nlink:../0007/README.adoc[RFD 7] and link:../0008/README.adoc[RFD 8] shipped\nparts of sync before this RFD was settled. This RFD\n" + want := "Intro.\nxref:../0007/README.adoc[RFD 7] and xref:../0008/README.adoc[RFD 8] shipped\nparts of sync before this RFD was settled. This RFD\n" if got != want { t.Errorf("wrapChanged = %q, want %q", got, want) } @@ -137,7 +140,7 @@ func TestLinksInCheck(t *testing.T) { if len(changed) != 1 || !strings.HasSuffix(changed[0], "0001/README.adoc") { t.Fatalf("LinkAll changed %v", changed) } - if !strings.Contains(f.read("0001/README.adoc"), "link:../0002/README.adoc[RFD 2] applies.") { + if !strings.Contains(f.read("0001/README.adoc"), "xref:../0002/README.adoc[RFD 2] applies.") { t.Fatalf("README not linked:\n%s", f.read("0001/README.adoc")) } f.pass() diff --git a/internal/rfd/template/README.adoc b/internal/rfd/template/README.adoc index 84f957b..d8af688 100644 --- a/internal/rfd/template/README.adoc +++ b/internal/rfd/template/README.adoc @@ -2,9 +2,6 @@ :state: prediscussion :discussion: :labels: {{.Labels}} -// Section ids as rfdtool checks them: <> refers to == Goal. -:idprefix: -:idseparator: - = RFD {{.Number}} {{.Title}}