diff --git a/main.go b/main.go index 2c4d406..2fb2d57 100644 --- a/main.go +++ b/main.go @@ -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 @@ -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) } // @@ -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 @@ -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)[1-6])>\s*` + `]*>\s*` + `.*?(?P.*?)README in another language - `, "", 0) + `, "", 0, 2) if toc[0] != tocExpected[0] { t.Error("Res :", toc, "\nExpected :", tocExpected) } @@ -37,7 +37,7 @@ func Test_GrabTocOneRowWithNewLines(t *testing.T) { README in another language - `, "", 0) + `, "", 0, 2) if toc[0] != tocExpected[0] { t.Error("Res :", toc, "\nExpected :", tocExpected) } @@ -67,7 +67,7 @@ case you need to implement 2 functions in the plugin's body:

This function should return list of available versions of the plugin. For example:

- `, "", 0) + `, "", 0, 2) for i := 0; i <= len(tocExpected)-1; i++ { if toc[i] != tocExpected[i] { t.Error("Res :", toc[i], "\nExpected :", tocExpected[i]) @@ -105,7 +105,7 @@ func Test_GrabTocBackquoted(t *testing.T) { The command bar2 is better

Blabla...

- `, "", 0) + `, "", 0, 2) for i := 0; i <= len(tocExpected)-1; i++ { if toc[i] != tocExpected[i] { @@ -142,7 +142,7 @@ func Test_GrabTocDepth(t *testing.T) { The command bar2 is better

Blabla...

- `, "", 1) + `, "", 1, 2) // fmt.Println(toc) @@ -160,7 +160,7 @@ func Test_GrabTocWithAbspath(t *testing.T) { } toc := *GrabToc(`

README in another language

- `, link, 0) + `, link, 0, 2) if toc[0] != tocExpected[0] { t.Error("Res :", toc, "\nExpected :", tocExpected) } @@ -178,13 +178,39 @@ func Test_EscapedChars(t *testing.T) { mod_* - `, "", 0) + `, "", 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(` +

+Header Level1 +

+

+Header Level2 +

+

+Header Level3 +

+ `, "", 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)", @@ -204,7 +230,7 @@ func Test_MinHeaderNumber(t *testing.T) { bar - `, "", 0) + `, "", 0, 2) if toc[0] != tocExpected[0] { t.Error("Res :", toc, "\nExpected :", tocExpected)