Skip to content

Commit

Permalink
1.3.1
Browse files Browse the repository at this point in the history
  • Loading branch information
ekalinin committed Nov 18, 2023
1 parent 0084f0e commit cfef5c8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
4 changes: 3 additions & 1 deletion cmd/gh-md-toc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var (
indent = kingpin.Flag("indent", "Indent space of generated list").Default("2").Int()
debug = kingpin.Flag("debug", "Show debug info").Bool()
ghurl = kingpin.Flag("github-url", "GitHub URL, default=https://api.github.com").String()
reVersion = kingpin.Flag("re-version", "RegExp version, default=0").Default("0").String()
)

// check if there was an error (and panic if it was)
Expand All @@ -41,7 +42,7 @@ func processPaths() {

for _, p := range *paths {
ghdoc := ghtoc.NewGHDoc(p, absPathsInToc, *startDepth, *depth, !*noEscape, *token, *indent, *debug)
ghdoc.SetGHURL(*ghurl)
ghdoc.SetGHURL(*ghurl).SetReVersion(*reVersion)

if *serial {
ch <- ghdoc.GetToc()
Expand Down Expand Up @@ -74,6 +75,7 @@ func processSTDIN() {
check(os.WriteFile(file.Name(), bytes, 0644))
check(ghtoc.NewGHDoc(file.Name(), false, *startDepth, *depth, !*noEscape, *token, *indent, *debug).
SetGHURL(*ghurl).
SetReVersion(*reVersion).
GetToc().
Print(os.Stdout))
}
Expand Down
30 changes: 20 additions & 10 deletions ghdoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type GHDoc struct {
httpGetter httpGetter
httpPoster httpPoster
ghURL string
reVersion string
}

// NewGHDoc create GHDoc
Expand All @@ -64,6 +65,7 @@ func NewGHDoc(Path string, AbsPaths bool, StartDepth int, Depth int, Escape bool
httpGetter: internal.HttpGet,
httpPoster: internal.HttpPost,
ghURL: "https://api.github.com",
reVersion: "0",
}
}

Expand All @@ -81,6 +83,12 @@ func (doc *GHDoc) SetGHURL(url string) *GHDoc {
return doc
}

// SetReVersion sets reg exp version
func (doc *GHDoc) SetReVersion(v string) *GHDoc {
doc.reVersion = v
return doc
}

// IsRemoteFile checks if path is for remote file or not
func (doc *GHDoc) IsRemoteFile() bool {
u, err := url.Parse(doc.Path)
Expand Down Expand Up @@ -160,16 +168,18 @@ func (doc *GHDoc) GrabToc() *GHToc {
// si:
// - s - let . match \n (single-line mode)
// - i - case-insensitive
// re := `(?si)<h(?P<num>[1-6]) id="[^"]+">\s*` +
// `<a class="heading-link"\s*` +
// `href="(?P<href>[^"]+)">\s*` +
// `(?P<name>.*?)<span`
re := `(?si)<h(?P<num>[1-6])>\s*` +
`<a\s*id="user-content-[^"]*"\s*class="anchor"\s*` +
`(aria-hidden="[^"]*"\s*)?` +
`(tabindex="[^"]*"\s*)?` +
`href="(?P<href>[^"]*)"[^>]*>\s*` +
`.*?</a>(?P<name>.*?)</h`
re := `(?si)<h(?P<num>[1-6]) id="[^"]+">\s*` +
`<a class="heading-link"\s*` +
`href="(?P<href>[^"]+)">\s*` +
`(?P<name>.*?)<span`
if doc.reVersion == "0" {
re = `(?si)<h(?P<num>[1-6])>\s*` +
`<a\s*id="user-content-[^"]*"\s*class="anchor"\s*` +
`(aria-hidden="[^"]*"\s*)?` +
`(tabindex="[^"]*"\s*)?` +
`href="(?P<href>[^"]*)"[^>]*>\s*` +
`.*?</a>(?P<name>.*?)</h`
}
r := regexp.MustCompile(re)
listIndentation := internal.GenerateListIndentation(doc.Indent)

Expand Down

0 comments on commit cfef5c8

Please sign in to comment.