-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* md toc for github * Update README.md * Update mdtoc.go * Update README.md * update readme.md
- Loading branch information
1 parent
1201502
commit c1966d8
Showing
6 changed files
with
231 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
"github.com/wanyxkhalil/toolbox/mdtoc" | ||
) | ||
|
||
// mdTocCmd represents the mdToc command | ||
var mdTocCmd = &cobra.Command{ | ||
Use: "md-toc-github [FILE]", | ||
Short: "Generate markdown toc for github", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
mdtoc.Run(args[0]) | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(mdTocCmd) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package mdtoc | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"github.com/wanyxkhalil/toolbox/util" | ||
"io" | ||
"os" | ||
"strings" | ||
) | ||
|
||
type header struct { | ||
level int | ||
name string | ||
line int | ||
} | ||
|
||
func Run(p string) { | ||
headers := getHeaders(p) | ||
printHeaders(headers) | ||
} | ||
|
||
func printHeaders(headers []header) { | ||
fmt.Printf("\nTable of Contents\n=================\n\n") | ||
for _, h := range headers { | ||
prefix := strings.Repeat(" ", h.level-1) | ||
fmt.Printf("%s* [%s](#%s)\n", prefix, h.name, convertName(h.name)) | ||
} | ||
fmt.Println() | ||
} | ||
|
||
func getHeaders(p string) []header { | ||
f, err := os.Open(p) | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer f.Close() | ||
|
||
reader := bufio.NewReader(f) | ||
i := 1 | ||
var headers []header | ||
var lines []int | ||
|
||
for { | ||
str, err := reader.ReadString('\n') | ||
if err == io.EOF { | ||
break | ||
} | ||
|
||
if strings.HasPrefix(str, "#") { | ||
ss := strings.SplitN(str, " ", 2) | ||
if len(ss) == 2 { | ||
i := header{ | ||
level: len(ss[0]), | ||
name: strings.TrimSpace(ss[1]), | ||
line: i, | ||
} | ||
headers = append(headers, i) | ||
} | ||
} | ||
|
||
if strings.HasPrefix(str, "```") { | ||
lines = append(lines, i) | ||
} | ||
|
||
i++ | ||
} | ||
|
||
// 清除代码块中的 header | ||
j := 0 | ||
for _, a := range headers { | ||
firstGreater := util.BinarySearchFirstGreater(lines, a.line) | ||
if firstGreater < 0 || firstGreater%2 == 0 { | ||
headers[j] = a | ||
j++ | ||
} | ||
} | ||
|
||
return headers[:j] | ||
} | ||
|
||
func convertName(s string) string { | ||
s = strings.ToLower(s) | ||
return strings.ReplaceAll(s, " ", "-") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package mdtoc | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestRun(t *testing.T) { | ||
//Run("/Users/khalil/Projects/github/toolbox/README.md") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package util | ||
|
||
// BinarySearchFirstGreater 二分查找,返回第一个大于给定值的元素索引 | ||
func BinarySearchFirstGreater(arr []int, v int) int { | ||
size := len(arr) | ||
if size == 0 { | ||
return -1 | ||
} | ||
|
||
return bsFirstGreaterRecursive(arr, v, 0, size-1) | ||
} | ||
|
||
// bsFirstGreaterRecursive 二分查找,递归实现 | ||
func bsFirstGreaterRecursive(arr []int, v, low, high int) int { | ||
maxIndex := len(arr) - 1 | ||
|
||
if low == high && low != 0 { | ||
if low >= maxIndex { | ||
return -1 | ||
} | ||
return low + 1 | ||
} else if low > high { | ||
return -1 | ||
} | ||
|
||
mid := (low + high) / 2 | ||
if arr[mid] <= v && arr[mid+1] >= v { | ||
return mid + 1 | ||
} else if arr[mid] < v { | ||
return bsFirstGreaterRecursive(arr, v, mid+1, high) | ||
} else { | ||
return bsFirstGreaterRecursive(arr, v, low, mid-1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package util | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestBinarySearchFirstGreater(t *testing.T) { | ||
arr := []int{2, 3, 10, 22} | ||
assert.Equal(t, 2, BinarySearchFirstGreater(arr, 5)) | ||
assert.Equal(t, 2, BinarySearchFirstGreater(arr, 3)) | ||
assert.Equal(t, 3, BinarySearchFirstGreater(arr, 22)) | ||
assert.Equal(t, -1, BinarySearchFirstGreater(arr, 23)) | ||
} |