Skip to content

Commit

Permalink
Merge pull request #17 from raimon49/add-option-for-indentation
Browse files Browse the repository at this point in the history
Poposal: Add option '--indent' as Indent space of generated list
  • Loading branch information
ekalinin authored Dec 18, 2017
2 parents 742c937 + 5cfe5a7 commit 82bb51b
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 18 deletions.
31 changes: 21 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,12 @@ type GHDoc struct {
Depth int
Escape bool
GhToken string
Indent int
}

// NewGHDoc create GHDoc
func NewGHDoc(Path string, AbsPaths bool, Depth int, Escape bool, Token string) *GHDoc {
return &GHDoc{Path, AbsPaths, Depth, Escape, Token}
func NewGHDoc(Path string, AbsPaths bool, Depth int, Escape bool, Token string, Indent int) *GHDoc {
return &GHDoc{Path, AbsPaths, Depth, Escape, Token, Indent}
}

// GetToc return GHToc for a document
Expand All @@ -52,9 +53,9 @@ func (doc *GHDoc) GetToc() *GHToc {
return nil
}
if doc.AbsPaths {
return GrabTocX(htmlBody, doc.Path, doc.Depth, doc.Escape)
return GrabTocX(htmlBody, doc.Path, doc.Depth, doc.Escape, doc.Indent)
}
return GrabTocX(htmlBody, "", doc.Depth, doc.Escape)
return GrabTocX(htmlBody, "", doc.Depth, doc.Escape, doc.Indent)
}

//
Expand Down Expand Up @@ -126,6 +127,13 @@ func removeStuf(s string) string {
return res
}

// generate func of custom spaces indentation
func generateListIndentation(spaces int) func() string {
return func() string {
return strings.Repeat(" ", spaces)
}
}

// Public

// EscapeSpecChars Escapes special characters
Expand Down Expand Up @@ -174,17 +182,18 @@ func ConvertMd2Html(localpath string, token string) (string, error) {
}

// GrabToc Create TOC by html from github
func GrabToc(html string, absPath string, depth int) *GHToc {
return GrabTocX(html, absPath, depth, true)
func GrabToc(html string, absPath string, depth int, indent int) *GHToc {
return GrabTocX(html, absPath, depth, true, indent)
}

// GrabTocX Create TOC
func GrabTocX(html string, absPath string, depth int, escape bool) *GHToc {
func GrabTocX(html string, absPath string, depth int, escape bool, indent int) *GHToc {
re := `(?si)<h(?P<num>[1-6])>\s*` +
`<a\s*id="user-content-[^"]*"\s*class="anchor"\s*` +
`href="(?P<href>[^"]*)"[^>]*>\s*` +
`.*?</a>(?P<name>.*?)</h`
r := regexp.MustCompile(re)
listIndentation := generateListIndentation(indent)

toc := GHToc{}
minHeaderNum := 6
Expand Down Expand Up @@ -223,7 +232,7 @@ func GrabTocX(html string, absPath string, depth int, escape bool) *GHToc {
if escape {
tmpSection = EscapeSpecChars(tmpSection)
}
tocItem := strings.Repeat(" ", n-minHeaderNum) + "* " +
tocItem := strings.Repeat(listIndentation(), n-minHeaderNum) + "* " +
"[" + tmpSection + "]" +
"(" + link + ")"
//fmt.Println(tocItem)
Expand All @@ -244,6 +253,8 @@ func main() {
"How many levels of headings to include. Defaults to 0 (all)").Default("0").Int()
noEscape := kingpin.Flag("no-escape", "Do not escape chars in sections").Bool()
token := kingpin.Flag("token", "GitHub personal token").String()
indent := kingpin.Flag("indent",
"Indent space of generated list").Default("2").Int()

kingpin.Version(version)
kingpin.Parse()
Expand All @@ -266,7 +277,7 @@ func main() {
ch := make(chan *GHToc, pathsCount)

for _, p := range *paths {
ghdoc := NewGHDoc(p, absPathsInToc, *depth, !*noEscape, *token)
ghdoc := NewGHDoc(p, absPathsInToc, *depth, !*noEscape, *token, *indent)
if *serial {
ch <- ghdoc.GetToc()
} else {
Expand All @@ -292,7 +303,7 @@ func main() {
defer os.Remove(file.Name())

check(ioutil.WriteFile(file.Name(), bytes, 0644))
NewGHDoc(file.Name(), false, *depth, !*noEscape, *token).GetToc().Print()
NewGHDoc(file.Name(), false, *depth, !*noEscape, *token, *indent).GetToc().Print()
}

fmt.Println("Created by [gh-md-toc](https://github.com/ekalinin/github-markdown-toc.go)")
Expand Down
42 changes: 34 additions & 8 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func Test_GrabTocOneRow(t *testing.T) {
}
toc := *GrabToc(`
<h1><a id="user-content-readme-in-another-language" class="anchor" href="#readme-in-another-language" aria-hidden="true"><span class="octicon octicon-link"></span></a>README in another language</h1>
`, "", 0)
`, "", 0, 2)
if toc[0] != tocExpected[0] {
t.Error("Res :", toc, "\nExpected :", tocExpected)
}
Expand All @@ -37,7 +37,7 @@ func Test_GrabTocOneRowWithNewLines(t *testing.T) {
</a>
README in another language
</h1>
`, "", 0)
`, "", 0, 2)
if toc[0] != tocExpected[0] {
t.Error("Res :", toc, "\nExpected :", tocExpected)
}
Expand Down Expand Up @@ -67,7 +67,7 @@ case you need to implement 2 functions in the plugin's body:</p>
<p>This function should return list of available versions of the plugin.
For example:</p>
`, "", 0)
`, "", 0, 2)
for i := 0; i <= len(tocExpected)-1; i++ {
if toc[i] != tocExpected[i] {
t.Error("Res :", toc[i], "\nExpected :", tocExpected[i])
Expand Down Expand Up @@ -105,7 +105,7 @@ func Test_GrabTocBackquoted(t *testing.T) {
<a id="user-content-the-command-bar2-is-better" class="anchor" href="#the-command-bar2-is-better" aria-hidden="true"><span class="octicon octicon-link"></span></a>The command <code>bar2</code> is better</h2>
<p>Blabla...</p>
`, "", 0)
`, "", 0, 2)

for i := 0; i <= len(tocExpected)-1; i++ {
if toc[i] != tocExpected[i] {
Expand Down Expand Up @@ -142,7 +142,7 @@ func Test_GrabTocDepth(t *testing.T) {
<a id="user-content-the-command-bar2-is-better" class="anchor" href="#the-command-bar2-is-better" aria-hidden="true"><span class="octicon octicon-link"></span></a>The command <code>bar2</code> is better</h2>
<p>Blabla...</p>
`, "", 1)
`, "", 1, 2)

// fmt.Println(toc)

Expand All @@ -160,7 +160,7 @@ func Test_GrabTocWithAbspath(t *testing.T) {
}
toc := *GrabToc(`
<h1><a id="user-content-readme-in-another-language" class="anchor" href="#readme-in-another-language" aria-hidden="true"><span class="octicon octicon-link"></span></a>README in another language</h1>
`, link, 0)
`, link, 0, 2)
if toc[0] != tocExpected[0] {
t.Error("Res :", toc, "\nExpected :", tocExpected)
}
Expand All @@ -178,13 +178,39 @@ func Test_EscapedChars(t *testing.T) {
<span class="octicon octicon-link"></span>
</a>
mod_*
</h2>`, "", 0)
</h2>`, "", 0, 2)

if toc[0] != tocExpected[0] {
t.Error("Res :", toc, "\nExpected :", tocExpected)
}
}

func Test_CustomSpaceIndentation(t *testing.T) {
tocExpected := []string{
"* [Header Level1](#header-level1)",
" * [Header Level2](#header-level2)",
" * [Header Level3](#header-level3)",
}

toc := *GrabToc(`
<h1>
<a id="user-content-the-command-level1" class="anchor" href="#header-level1" aria-hidden="true"><span class="octicon octicon-link"></span></a>Header Level1
</h1>
<h2>
<a id="user-content-the-command-level2" class="anchor" href="#header-level2" aria-hidden="true"><span class="octicon octicon-link"></span></a>Header Level2
</h2>
<h3>
<a id="user-content-the-command-level3" class="anchor" href="#header-level3" aria-hidden="true"><span class="octicon octicon-link"></span></a>Header Level3
</h3>
`, "", 0, 4) // use 4 spaces indent

for i := 0; i <= len(tocExpected)-1; i++ {
if toc[i] != tocExpected[i] {
t.Error("Res :", toc[i], "\nExpected :", tocExpected[i])
}
}
}

func Test_MinHeaderNumber(t *testing.T) {
tocExpected := []string{
"* [foo](#foo)",
Expand All @@ -204,7 +230,7 @@ func Test_MinHeaderNumber(t *testing.T) {
</a>
bar
</h3>
`, "", 0)
`, "", 0, 2)

if toc[0] != tocExpected[0] {
t.Error("Res :", toc, "\nExpected :", tocExpected)
Expand Down

0 comments on commit 82bb51b

Please sign in to comment.