toc.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. Copyright 2015 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package main
  14. import (
  15. "fmt"
  16. "regexp"
  17. "strings"
  18. )
  19. const tocMungeTag = "GENERATED_TOC"
  20. var r = regexp.MustCompile("[^A-Za-z0-9-]")
  21. // inserts/updates a table of contents in markdown file.
  22. //
  23. // First, builds a ToC.
  24. // Then, finds the magic macro block tags and replaces anything between those with
  25. // the ToC, thereby updating any previously inserted ToC.
  26. //
  27. // TODO(erictune): put this in own package with tests
  28. func updateTOC(filePath string, mlines mungeLines) (mungeLines, error) {
  29. toc := buildTOC(mlines)
  30. updatedMarkdown, err := updateMacroBlock(mlines, tocMungeTag, toc)
  31. if err != nil {
  32. return mlines, err
  33. }
  34. return updatedMarkdown, nil
  35. }
  36. // builds table of contents for markdown file
  37. //
  38. // First scans for all section headers (lines that begin with "#" but not within code quotes)
  39. // and builds a table of contents from those. Assumes bookmarks for those will be
  40. // like #each-word-in-heading-in-lowercases-with-dashes-instead-of-spaces.
  41. // builds the ToC.
  42. func buildTOC(mlines mungeLines) mungeLines {
  43. var out mungeLines
  44. bookmarks := map[string]int{}
  45. for _, mline := range mlines {
  46. if mline.preformatted || !mline.header {
  47. continue
  48. }
  49. // Add a blank line after the munge start tag
  50. if len(out) == 0 {
  51. out = append(out, blankMungeLine)
  52. }
  53. line := mline.data
  54. noSharps := strings.TrimLeft(line, "#")
  55. numSharps := len(line) - len(noSharps)
  56. heading := strings.Trim(noSharps, " \n")
  57. if numSharps > 0 {
  58. indent := strings.Repeat(" ", numSharps-1)
  59. bookmark := strings.Replace(strings.ToLower(heading), " ", "-", -1)
  60. // remove symbols (except for -) in bookmarks
  61. bookmark = r.ReplaceAllString(bookmark, "")
  62. // Incremental counter for duplicate bookmarks
  63. next := bookmarks[bookmark]
  64. bookmarks[bookmark] = next + 1
  65. if next > 0 {
  66. bookmark = fmt.Sprintf("%s-%d", bookmark, next)
  67. }
  68. tocLine := fmt.Sprintf("%s- [%s](#%s)", indent, heading, bookmark)
  69. out = append(out, newMungeLine(tocLine))
  70. }
  71. }
  72. // Add a blank line before the munge end tag
  73. if len(out) != 0 {
  74. out = append(out, blankMungeLine)
  75. }
  76. return out
  77. }