Skip to content

Commit

Permalink
md toc for github (#2)
Browse files Browse the repository at this point in the history
* md toc for github

* Update README.md

* Update mdtoc.go

* Update README.md

* update readme.md
  • Loading branch information
wanyxkhalil authored Nov 9, 2021
1 parent 1201502 commit c1966d8
Show file tree
Hide file tree
Showing 6 changed files with 231 additions and 31 deletions.
100 changes: 69 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
# toolbox
toolbox: mkpasswd, https-expired, mysql-to-gostruct, ip ...

toolbox: mkpasswd, https-expired, mysql-to-gostruct, ip, md-toc-github ...

Table of Contents
=================

* [toolbox](#toolbox)
* [Install](#install)
* [Completion](#completion)
* [Usage](#usage)
* [mkpasswd](#mkpasswd)
* [https-expired](#https-expired)
* [mysql-to-gostruct](#mysql-to-gostruct)
* [类型对应](#类型对应)
* [Sample](#sample)
* [ip](#ip)
* [Local machine address](#local-machine-address)
* [Remote host address](#remote-host-address)
* [Install](#install)
* [Completion](#completion)
* [Usage](#usage)
* [mkpasswd](#mkpasswd)
* [https-expired](#https-expired)
* [mysql-to-gostruct](#mysql-to-gostruct)
* [类型对应](#类型对应)
* [Sample](#sample)
* [ip](#ip)
* [Local machine address](#local-machine-address)
* [Remote host address](#remote-host-address)
* [md-toc-github](#md-toc-github)

## Install

Expand All @@ -26,8 +27,8 @@ toolbox -h

### Completion

If you use zsh, add this to ~/.zshrc & source.
Also support bash, fish, powershell
If you use zsh, add this to ~/.zshrc & source. Also support bash, fish, powershell

```shell
if [ $commands[toolbox] ]; then
source <(toolbox completion zsh)
Expand All @@ -40,6 +41,7 @@ fi
### mkpasswd

Generate password, Inspired by [gaiustech/MkPasswd](https://github.com/gaiustech/MkPasswd).

```shell
$ toolbox mkpasswd -h # help message
A tool for generating random passwords
Expand All @@ -57,6 +59,7 @@ Flags:
```

Sample

```shell
toolbox mkpasswd -l 17 # length is 17
toolbox mkpasswd -l 17 -C 4 -d 4 -s 3 # length is 17, include 4 upper char, 4 digit, 3 special char, 6 lower char
Expand All @@ -65,11 +68,13 @@ toolbox mkpasswd -l 17 -C 4 -d 4 -s 3 # length is 17, include 4 upper char, 4 di
### https-expired

Show the cert expiration time. Just like

```shell
alias ,https-expired='function _as() {echo | openssl s_client -servername $1 -connect $1:443 2>/dev/null | openssl x509 -noout -dates;};_as'
```

Sample

```shell
toolbox https-expired github.com
```
Expand Down Expand Up @@ -120,7 +125,6 @@ toolbox https-expired github.com
| mediumblob | []byte |
| longblob | []byte |


> - year 对应 uint8,无法对应 time.Time。
> - decimal 对应 github.com/shopspring/decimal
Expand Down Expand Up @@ -161,8 +165,6 @@ CREATE TABLE `my_time`
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
```



结果:/Tmp/user.go, /Tmp/my_time.go

```go
Expand All @@ -175,15 +177,15 @@ import (

// User 用户
type User struct {
Id uint64
Id uint64
// 用户名
Name string
Name string
// 是否有效:0_无效,1_有效
Valid int8
Dec decimal.NullDecimal
Udec decimal.Decimal
CreatedAt time.Time
UpdatedAt time.Time
Valid int8
Dec decimal.NullDecimal
Udec decimal.Decimal
CreatedAt time.Time
UpdatedAt time.Time
}
```

Expand All @@ -196,12 +198,12 @@ import (

// MyTime
type MyTime struct {
Id uint64
Year uint8
Time time.Time
Date time.Time
Datetime time.Time
Timestamp time.Time
Id uint64
Year uint8
Time time.Time
Date time.Time
Datetime time.Time
Timestamp time.Time
}
```

Expand All @@ -216,6 +218,7 @@ toolbox ip
```

Result:

```shell
IP Address: 180.167.000.000
Country: China
Expand All @@ -232,4 +235,39 @@ Result:

```shell
220.181.000.000
```

### md-toc-github

Generate Markdown TOC for GitHub

Sample:

```shell
## This line for toc test

toolbox md-toc-github README.md
```

Result:

```markdown

Table of Contents
=================

* [toolbox](#toolbox)
* [Install](#Install)
* [Completion](#Completion)
* [Usage](#Usage)
* [mkpasswd](#mkpasswd)
* [https-expired](#https-expired)
* [mysql-to-gostruct](#mysql-to-gostruct)
* [类型对应](#类型对应)
* [Sample](#Sample)
* [ip](#ip)
* [Local machine address](#Local machine address)
* [Remote host address](#Remote host address)
* [md-toc-github](#md-toc-github)

```
20 changes: 20 additions & 0 deletions cmd/mdToc.go
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)

}
85 changes: 85 additions & 0 deletions mdtoc/mdtoc.go
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, " ", "-")
}
9 changes: 9 additions & 0 deletions mdtoc/mdtoc_test.go
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")
}
34 changes: 34 additions & 0 deletions util/search.go
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)
}
}
14 changes: 14 additions & 0 deletions util/search_test.go
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))
}

0 comments on commit c1966d8

Please sign in to comment.