Skip to content

Commit 3b2a3a7

Browse files
authored
Merge pull request #61 from AryaHassanli/fix-normalize-anchor
Set normalizeAnchor to true by default
2 parents 0ba81dd + 48580b6 commit 3b2a3a7

5 files changed

Lines changed: 91 additions & 5 deletions

File tree

disco/baller_test.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import (
44
"bytes"
55
"context"
66
"os"
7+
"reflect"
78
"strings"
89
"testing"
10+
"unsafe"
911

1012
"github.com/project-chip/alchemy/asciidoc"
1113
"github.com/project-chip/alchemy/asciidoc/parse"
@@ -160,3 +162,77 @@ func TestXrefStyleOnlyInRootWithFile(t *testing.T) {
160162
}
161163
}
162164
}
165+
166+
func TestNormalizeAnchorCrossFile(t *testing.T) {
167+
pathA := asciidoc.Path{Relative: "testdata/anchor_file.adoc"}
168+
inA, err := os.ReadFile("testdata/anchor_file.adoc")
169+
if err != nil {
170+
t.Fatalf("error reading file A: %v", err)
171+
}
172+
docA, err := parse.Reader(pathA, bytes.NewReader(inA))
173+
if err != nil {
174+
t.Fatalf("error parsing file A: %v", err)
175+
}
176+
177+
pathB := asciidoc.Path{Relative: "testdata/reference_file.adoc"}
178+
inB, err := os.ReadFile("testdata/reference_file.adoc")
179+
if err != nil {
180+
t.Fatalf("error reading file B: %v", err)
181+
}
182+
docB, err := parse.Reader(pathB, bytes.NewReader(inB))
183+
if err != nil {
184+
t.Fatalf("error parsing file B: %v", err)
185+
}
186+
187+
s := &spec.Specification{}
188+
lib := spec.NewLibrary(docA, config.Library{}, nil, nil)
189+
lib.Reader = asciidoc.RawReader
190+
191+
// Use reflect to set s.libraryIndex
192+
v := reflect.ValueOf(s).Elem()
193+
f := v.FieldByName("libraryIndex")
194+
195+
ptr := unsafe.Pointer(f.UnsafeAddr())
196+
mPtr := (*map[*asciidoc.Document]*spec.Library)(ptr)
197+
198+
*mPtr = make(map[*asciidoc.Document]*spec.Library)
199+
(*mPtr)[docA] = lib
200+
(*mPtr)[docB] = lib
201+
202+
an := AnchorNormalizer{
203+
spec: s,
204+
options: DiscoOptions{NormalizeAnchors: true},
205+
}
206+
207+
_, err = lib.Anchors(asciidoc.RawReader)
208+
if err != nil {
209+
t.Fatalf("error indexing anchors: %v", err)
210+
}
211+
212+
// Run it on docB
213+
an.rewriteCrossReferences(docB)
214+
215+
// Verify that docB retains its label!
216+
var foundXref *asciidoc.CrossReference
217+
parse.Search(docB, asciidoc.RawReader, nil, docB.Children(), func(doc *asciidoc.Document, el asciidoc.Element, parent asciidoc.ParentElement, index int) parse.SearchShould {
218+
if xref, ok := el.(*asciidoc.CrossReference); ok {
219+
foundXref = xref
220+
return parse.SearchShouldStop
221+
}
222+
return parse.SearchShouldContinue
223+
})
224+
225+
if foundXref == nil {
226+
t.Fatal("expected to find cross reference in docB")
227+
}
228+
229+
if len(foundXref.Elements) == 0 {
230+
t.Error("expected reference to retain its label, but it was removed")
231+
}
232+
233+
label := labelText(foundXref.Elements)
234+
expectedLabel := "A Non-Normalized Replacement Text Different Than Section Name"
235+
if label != expectedLabel {
236+
t.Errorf("expected label %q, got %q", expectedLabel, label)
237+
}
238+
}

disco/option.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type DiscoOptions struct {
1919
RemoveExtraSpaces bool `default:"true" aliases:"removeExtraSpaces" help:"remove extraneous spaces" group:"Discoballing:"`
2020
NormalizeFeatureNames bool `default:"true" aliases:"normalizeFeatureNames" help:"correct invalid feature names" group:"Discoballing:"`
2121
DisambiguateConformanceChoice bool `default:"true" aliases:"disambiguateConformanceChoice" help:"ensure conformance choices are only used once per document" group:"Discoballing:"`
22-
NormalizeAnchors bool `default:"false" aliases:"normalizeAnchors" help:"rewrite anchors and references without labels" group:"Discoballing:"`
22+
NormalizeAnchors bool `default:"true" aliases:"normalizeAnchors" help:"rewrite anchors and references without labels" group:"Discoballing:"`
2323
RemoveMandatoryFallbacks bool `default:"true" aliases:"removeMandatoryFallbacks" help:"remove fallback values for mandatory fields" group:"Discoballing:"`
2424
RenameSections bool `default:"false" help:"rename sections to disco-ball standard names" group:"Discoballing:"`
2525
XrefStyleOnlyInRoot bool `default:"true" aliases:"xrefStyleOnlyInRoot" help:"enforce xrefstyle: basic only in root" group:"Discoballing:"`
@@ -42,7 +42,7 @@ var DefaultOptions = DiscoOptions{
4242
RemoveExtraSpaces: true,
4343
NormalizeFeatureNames: true,
4444
DisambiguateConformanceChoice: true,
45-
NormalizeAnchors: false,
45+
NormalizeAnchors: true,
4646
RemoveMandatoryFallbacks: true,
4747
RenameSections: false,
4848
XrefStyleOnlyInRoot: true,

disco/references.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,14 @@ func (an AnchorNormalizer) rewriteCrossReferences(doc *asciidoc.Document) {
5353
}
5454
continue
5555
}
56-
anchorLabel := labelText(anchor.LabelElements)
56+
var anchorLabel string
57+
section, isSection := anchor.Element.(*asciidoc.Section)
58+
if an.options.NormalizeAnchors && isSection {
59+
anchorLabel = library.SectionName(section)
60+
}
5761
if anchorLabel == "" {
58-
section, isSection := anchor.Element.(*asciidoc.Section)
59-
if isSection {
62+
anchorLabel = labelText(anchor.LabelElements)
63+
if anchorLabel == "" && isSection {
6064
anchorLabel = library.SectionName(section)
6165
}
6266
}
@@ -96,6 +100,9 @@ func (an AnchorNormalizer) rewriteCrossReferences(doc *asciidoc.Document) {
96100
}
97101

98102
func removeCrossReferenceStutter(library *spec.Library, doc *asciidoc.Document, icr *asciidoc.CrossReference, parent asciidoc.Parent, index int) {
103+
if parent == nil {
104+
return
105+
}
99106
if len(icr.Elements) > 0 {
100107
return
101108
}

disco/testdata/anchor_file.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[[ref_external_anchor, A Non-Normalized Replacement Text Different Than Section Name]]
2+
== Section Name Different Than Replacement Text

disco/testdata/reference_file.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is a reference to <<ref_external_anchor, A Non-Normalized Replacement Text Different Than Section Name>>.

0 commit comments

Comments
 (0)