diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2021-06-19 18:19:46 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2021-06-19 18:19:46 +0200 |
commit | a7e3da242f98d4799dad013d7ba2f285717640d6 (patch) | |
tree | 90c4b9691da4454de6df23e2cc25711648834df4 /markup/tableofcontents | |
parent | b70a12ec45ffd2afea6137bc25b8f3aa1337268f (diff) |
markup: Rename Header(s) to Heading(s) in ToC struct
Because that is what it is.
Diffstat (limited to 'markup/tableofcontents')
-rw-r--r-- | markup/tableofcontents/tableofcontents.go | 52 | ||||
-rw-r--r-- | markup/tableofcontents/tableofcontents_test.go | 32 |
2 files changed, 42 insertions, 42 deletions
diff --git a/markup/tableofcontents/tableofcontents.go b/markup/tableofcontents/tableofcontents.go index b7e630d6f..2e7f47d20 100644 --- a/markup/tableofcontents/tableofcontents.go +++ b/markup/tableofcontents/tableofcontents.go @@ -17,55 +17,55 @@ import ( "strings" ) -// Headers holds the top level (h1) headers. -type Headers []Header +// Headings holds the top level headings. +type Headings []Heading -// Header holds the data about a header and its children. -type Header struct { +// Heading holds the data about a heading and its children. +type Heading struct { ID string Text string - Headers Headers + Headings Headings } // IsZero is true when no ID or Text is set. -func (h Header) IsZero() bool { +func (h Heading) IsZero() bool { return h.ID == "" && h.Text == "" } // Root implements AddAt, which can be used to build the // data structure for the ToC. type Root struct { - Headers Headers + Headings Headings } -// AddAt adds the header into the given location. -func (toc *Root) AddAt(h Header, row, level int) { - for i := len(toc.Headers); i <= row; i++ { - toc.Headers = append(toc.Headers, Header{}) +// AddAt adds the heading into the given location. +func (toc *Root) AddAt(h Heading, row, level int) { + for i := len(toc.Headings); i <= row; i++ { + toc.Headings = append(toc.Headings, Heading{}) } if level == 0 { - toc.Headers[row] = h + toc.Headings[row] = h return } - header := &toc.Headers[row] + heading := &toc.Headings[row] for i := 1; i < level; i++ { - if len(header.Headers) == 0 { - header.Headers = append(header.Headers, Header{}) + if len(heading.Headings) == 0 { + heading.Headings = append(heading.Headings, Heading{}) } - header = &header.Headers[len(header.Headers)-1] + heading = &heading.Headings[len(heading.Headings)-1] } - header.Headers = append(header.Headers, h) + heading.Headings = append(heading.Headings, h) } // ToHTML renders the ToC as HTML. func (toc Root) ToHTML(startLevel, stopLevel int, ordered bool) string { b := &tocBuilder{ s: strings.Builder{}, - h: toc.Headers, + h: toc.Headings, startLevel: startLevel, stopLevel: stopLevel, ordered: ordered, @@ -76,7 +76,7 @@ func (toc Root) ToHTML(startLevel, stopLevel int, ordered bool) string { type tocBuilder struct { s strings.Builder - h Headers + h Headings startLevel int stopLevel int @@ -87,16 +87,16 @@ func (b *tocBuilder) Build() { b.writeNav(b.h) } -func (b *tocBuilder) writeNav(h Headers) { +func (b *tocBuilder) writeNav(h Headings) { b.s.WriteString("<nav id=\"TableOfContents\">") - b.writeHeaders(1, 0, b.h) + b.writeHeadings(1, 0, b.h) b.s.WriteString("</nav>") } -func (b *tocBuilder) writeHeaders(level, indent int, h Headers) { +func (b *tocBuilder) writeHeadings(level, indent int, h Headings) { if level < b.startLevel { for _, h := range h { - b.writeHeaders(level+1, indent, h.Headers) + b.writeHeadings(level+1, indent, h.Headings) } return } @@ -118,7 +118,7 @@ func (b *tocBuilder) writeHeaders(level, indent int, h Headers) { } for _, h := range h { - b.writeHeader(level+1, indent+2, h) + b.writeHeading(level+1, indent+2, h) } if hasChildren { @@ -133,13 +133,13 @@ func (b *tocBuilder) writeHeaders(level, indent int, h Headers) { } } -func (b *tocBuilder) writeHeader(level, indent int, h Header) { +func (b *tocBuilder) writeHeading(level, indent int, h Heading) { b.indent(indent) b.s.WriteString("<li>") if !h.IsZero() { b.s.WriteString("<a href=\"#" + h.ID + "\">" + h.Text + "</a>") } - b.writeHeaders(level, indent, h.Headers) + b.writeHeadings(level, indent, h.Headings) b.s.WriteString("</li>\n") } diff --git a/markup/tableofcontents/tableofcontents_test.go b/markup/tableofcontents/tableofcontents_test.go index 753408ef9..daeb9f991 100644 --- a/markup/tableofcontents/tableofcontents_test.go +++ b/markup/tableofcontents/tableofcontents_test.go @@ -24,16 +24,16 @@ func TestToc(t *testing.T) { toc := &Root{} - toc.AddAt(Header{Text: "Header 1", ID: "h1-1"}, 0, 0) - toc.AddAt(Header{Text: "1-H2-1", ID: "1-h2-1"}, 0, 1) - toc.AddAt(Header{Text: "1-H2-2", ID: "1-h2-2"}, 0, 1) - toc.AddAt(Header{Text: "1-H3-1", ID: "1-h2-2"}, 0, 2) - toc.AddAt(Header{Text: "Header 2", ID: "h1-2"}, 1, 0) + toc.AddAt(Heading{Text: "Heading 1", ID: "h1-1"}, 0, 0) + toc.AddAt(Heading{Text: "1-H2-1", ID: "1-h2-1"}, 0, 1) + toc.AddAt(Heading{Text: "1-H2-2", ID: "1-h2-2"}, 0, 1) + toc.AddAt(Heading{Text: "1-H3-1", ID: "1-h2-2"}, 0, 2) + toc.AddAt(Heading{Text: "Heading 2", ID: "h1-2"}, 1, 0) got := toc.ToHTML(1, -1, false) c.Assert(got, qt.Equals, `<nav id="TableOfContents"> <ul> - <li><a href="#h1-1">Header 1</a> + <li><a href="#h1-1">Heading 1</a> <ul> <li><a href="#1-h2-1">1-H2-1</a></li> <li><a href="#1-h2-2">1-H2-2</a> @@ -43,28 +43,28 @@ func TestToc(t *testing.T) { </li> </ul> </li> - <li><a href="#h1-2">Header 2</a></li> + <li><a href="#h1-2">Heading 2</a></li> </ul> </nav>`, qt.Commentf(got)) got = toc.ToHTML(1, 1, false) c.Assert(got, qt.Equals, `<nav id="TableOfContents"> <ul> - <li><a href="#h1-1">Header 1</a></li> - <li><a href="#h1-2">Header 2</a></li> + <li><a href="#h1-1">Heading 1</a></li> + <li><a href="#h1-2">Heading 2</a></li> </ul> </nav>`, qt.Commentf(got)) got = toc.ToHTML(1, 2, false) c.Assert(got, qt.Equals, `<nav id="TableOfContents"> <ul> - <li><a href="#h1-1">Header 1</a> + <li><a href="#h1-1">Heading 1</a> <ul> <li><a href="#1-h2-1">1-H2-1</a></li> <li><a href="#1-h2-2">1-H2-2</a></li> </ul> </li> - <li><a href="#h1-2">Header 2</a></li> + <li><a href="#h1-2">Heading 2</a></li> </ul> </nav>`, qt.Commentf(got)) @@ -79,7 +79,7 @@ func TestToc(t *testing.T) { got = toc.ToHTML(1, -1, true) c.Assert(got, qt.Equals, `<nav id="TableOfContents"> <ol> - <li><a href="#h1-1">Header 1</a> + <li><a href="#h1-1">Heading 1</a> <ol> <li><a href="#1-h2-1">1-H2-1</a></li> <li><a href="#1-h2-2">1-H2-2</a> @@ -89,7 +89,7 @@ func TestToc(t *testing.T) { </li> </ol> </li> - <li><a href="#h1-2">Header 2</a></li> + <li><a href="#h1-2">Heading 2</a></li> </ol> </nav>`, qt.Commentf(got)) } @@ -99,9 +99,9 @@ func TestTocMissingParent(t *testing.T) { toc := &Root{} - toc.AddAt(Header{Text: "H2", ID: "h2"}, 0, 1) - toc.AddAt(Header{Text: "H3", ID: "h3"}, 1, 2) - toc.AddAt(Header{Text: "H3", ID: "h3"}, 1, 2) + toc.AddAt(Heading{Text: "H2", ID: "h2"}, 0, 1) + toc.AddAt(Heading{Text: "H3", ID: "h3"}, 1, 2) + toc.AddAt(Heading{Text: "H3", ID: "h3"}, 1, 2) got := toc.ToHTML(1, -1, false) c.Assert(got, qt.Equals, `<nav id="TableOfContents"> |