Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

List item indentation #12

Merged
merged 3 commits into from
Apr 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,35 +163,46 @@ func ConvertMd2Html(localpath string) string {
}

// GrabToc Create TOC by html from github
func GrabToc(html string, absPath string, Depth int) *GHToc {
func GrabToc(html string, absPath string, depth 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)

toc := GHToc{}
groups := make(map[string]string)
minHeaderNum := 6
var groups []map[string]string
for _, match := range r.FindAllStringSubmatch(html, -1) {
group := make(map[string]string)
// fill map for groups
for i, name := range r.SubexpNames() {
if i == 0 || name == "" {
continue
}
groups[name] = removeStuf(match[i])
group[name] = removeStuf(match[i])
}
// update minimum header number
n, _ := strconv.Atoi(group["num"])
if n < minHeaderNum {
minHeaderNum = n
}
groups = append(groups, group)
}

for _, group := range groups {
// format result
n, _ := strconv.Atoi(groups["num"])
if Depth > 0 && n > Depth {
n, _ := strconv.Atoi(group["num"])
if depth > 0 && n > depth {
continue
}

link := groups["href"]
link := group["href"]
if len(absPath) > 0 {
link = absPath + link
}
tocItem := strings.Repeat(" ", n) + "* " +
"[" + EscapeSpecChars(removeStuf(groups["name"])) + "]" +
tocItem := strings.Repeat(" ", n-minHeaderNum) + "* " +
"[" + EscapeSpecChars(removeStuf(group["name"])) + "]" +
"(" + link + ")"
//fmt.Println(tocItem)
toc = append(toc, tocItem)
Expand Down
54 changes: 40 additions & 14 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func Test_IsUrl(t *testing.T) {

func Test_GrabTocOneRow(t *testing.T) {
tocExpected := []string{
" * [README in another language](#readme-in-another-language)",
"* [README in another language](#readme-in-another-language)",
}
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>
Expand All @@ -28,7 +28,7 @@ func Test_GrabTocOneRow(t *testing.T) {

func Test_GrabTocOneRowWithNewLines(t *testing.T) {
tocExpected := []string{
" * [README in another language](#readme-in-another-language)",
"* [README in another language](#readme-in-another-language)",
}
toc := *GrabToc(`
<h1>
Expand All @@ -46,9 +46,9 @@ func Test_GrabTocOneRowWithNewLines(t *testing.T) {
func Test_GrabTocMultilineOriginGithub(t *testing.T) {

tocExpected := []string{
" * [How to add a plugin?](#how-to-add-a-plugin)",
" * [Mandatory elements](#mandatory-elements)",
" * [plug\\_list\\_versions](#plug_list_versions)",
"* [How to add a plugin?](#how-to-add-a-plugin)",
" * [Mandatory elements](#mandatory-elements)",
" * [plug\\_list\\_versions](#plug_list_versions)",
}
toc := *GrabToc(`
<h1><a id="user-content-how-to-add-a-plugin" class="anchor" href="#how-to-add-a-plugin" aria-hidden="true"><span class="octicon octicon-link"></span></a>How to add a plugin?</h1>
Expand Down Expand Up @@ -77,10 +77,10 @@ For example:</p>

func Test_GrabTocBackquoted(t *testing.T) {
tocExpected := []string{
" * [The command foo1](#the-command-foo1)",
" * [The command foo2 is better](#the-command-foo2-is-better)",
" * [The command bar1](#the-command-bar1)",
" * [The command bar2 is better](#the-command-bar2-is-better)",
"* [The command foo1](#the-command-foo1)",
" * [The command foo2 is better](#the-command-foo2-is-better)",
"* [The command bar1](#the-command-bar1)",
" * [The command bar2 is better](#the-command-bar2-is-better)",
}

toc := *GrabToc(`
Expand Down Expand Up @@ -116,8 +116,8 @@ func Test_GrabTocBackquoted(t *testing.T) {

func Test_GrabTocDepth(t *testing.T) {
tocExpected := []string{
" * [The command foo1](#the-command-foo1)",
" * [The command bar1](#the-command-bar1)",
"* [The command foo1](#the-command-foo1)",
"* [The command bar1](#the-command-bar1)",
}

toc := *GrabToc(`
Expand Down Expand Up @@ -156,7 +156,7 @@ func Test_GrabTocDepth(t *testing.T) {
func Test_GrabTocWithAbspath(t *testing.T) {
link := "https://github.com/ekalinin/envirius/blob/master/README.md"
tocExpected := []string{
" * [README in another language](" + link + "#readme-in-another-language)",
"* [README in another language](" + link + "#readme-in-another-language)",
}
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>
Expand All @@ -168,12 +168,12 @@ func Test_GrabTocWithAbspath(t *testing.T) {

func Test_EscapedChars(t *testing.T) {
tocExpected := []string{
" * [mod\\_\\*](#mod_)",
"* [mod\\_\\*](#mod_)",
}

toc := *GrabToc(`
<h2>
<a id="user-content-mod_" class="anchor"
<a id="user-content-mod_" class="anchor"
href="#mod_" aria-hidden="true">
<span class="octicon octicon-link"></span>
</a>
Expand All @@ -184,3 +184,29 @@ func Test_EscapedChars(t *testing.T) {
t.Error("Res :", toc, "\nExpected :", tocExpected)
}
}

func Test_MinHeaderNumber(t *testing.T) {
tocExpected := []string{
"* [foo](#foo)",
" * [bar](#bar)",
}

toc := *GrabToc(`
<h3>
<a id="user-content-" class="anchor" href="#foo" aria-hidden="true">
<span class="octicon octicon-link"></span>
</a>
foo
</h3>
<h4>
<a id="user-content-" class="anchor" href="#bar" aria-hidden="true">
<span class="octicon octicon-link"></span>
</a>
bar
</h3>
`, "", 0)

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