Skip to content

Commit

Permalink
chore: fork eklanin/github-markdown-toc.go with HTML parsing updates (#…
Browse files Browse the repository at this point in the history
…342)

- Fork https://github.com/ekalinin/github-markdown-toc.go with [HTML
parsing
changes](ekalinin/github-markdown-toc.go#38).
- Remove references to the original repo.
- Removed `cmd/go_deps` as it is no longer needed.
  • Loading branch information
cgrindel authored Aug 30, 2023
1 parent 5ccd331 commit 016b1d3
Show file tree
Hide file tree
Showing 22 changed files with 1,501 additions and 99 deletions.
2 changes: 2 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ _RUNTIME_PKGS = [
"//markdown",
"//markdown/private",
"//markdown/tools",
"//markdown/tools/github_markdown_toc",
"//markdown/tools/github_markdown_toc/cmd/gh-md-toc",
"//shlib/lib",
"//shlib/rules",
"//shlib/rules/private",
Expand Down
2 changes: 1 addition & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ go_deps.from_file(go_mod = "//:go.mod")
use_repo(
go_deps,
"com_github_creasty_defaults",
"com_github_ekalinin_github_markdown_toc_go",
"com_github_stretchr_testify",
"in_gopkg_alecthomas_kingpin_v2",
"in_gopkg_yaml_v3",
"org_golang_x_net",
)

# MARK: - Dev Dependencies
Expand Down
55 changes: 35 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,48 @@
[![Build](https://github.com/cgrindel/bazel-starlib/actions/workflows/ci.yml/badge.svg?event=schedule)](https://github.com/cgrindel/bazel-starlib/actions/workflows/ci.yml)

Bazel Starlib is a collection of projects that contain rulesets and libraries that are useful for
the implementation of Bazel projects.

| Project | Description | Documentation |
| ------- | ----------- | ------------- |
| bazeldoc | Generate Starlark documentation using [Bazel Stardoc](https://github.com/bazelbuild/stardoc). Formerly hosted as [bazel-doc](https://github.com/cgrindel/bazel-doc). | [API](/doc/bazeldoc/), [How-to](/bazeldoc/) |
| bzlformat | Format Bazel Starlark files using [Buildifier](https://github.com/bazelbuild/buildtools/tree/master/buildifier), test that the formatted files exist in the workspace directory, and copy formatted files to the workspace directory. Formerly hosted as [rules_bzlformat](https://github.com/cgrindel/rules_bzlformat). | [API](/doc/bzlformat/), [How-to](/bzlformat/), [Examples](/examples/bzlformat/) |
| bzllib | Collection of Starlark libraries. | [API](/doc/bzllib/), [How-to](/bzllib/) |
| bzlrelease | Automate and customize the generation of releases using GitHub Actions. | [API](/doc/bzlrelease/), [How-to](/bzlrelease/) |
| bzltidy | Collect Bazel actions that keep your source files up-to-date. | [API](/doc/bztidy/), [How-to](/bzltidy/) |
| markdown | Maintain markdown files. | [API](/doc/markdown/), [How-to](/markdown/), [Examples](/examples/markdown/) |
| shlib | Collection of libraries useful when implementing shell binaries, libraries, and tests. Formerly hosted as [bazel_shlib](https://github.com/cgrindel/bazel_shlib). | [API](/doc/shlib/), [How-to](/shlib/) |
| updatesrc | Copy files from the Bazel output directories to the workspace directory. Formerly hosted as [rules_updatesrc](https://github.com/cgrindel/rules_updatesrc) | [API](/doc/updatesrc/), [How-to](/updatesrc/), [Examples](/examples/updatesrc/) |

the implementation of Bazel projects.

| Project | Description | Documentation |
| ---------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------- |
| bazeldoc | Generate Starlark documentation using [Bazel Stardoc](https://github.com/bazelbuild/stardoc). Formerly hosted as [bazel-doc](https://github.com/cgrindel/bazel-doc). | [API](/doc/bazeldoc/), [How-to](/bazeldoc/) |
| bzlformat | Format Bazel Starlark files using [Buildifier](https://github.com/bazelbuild/buildtools/tree/master/buildifier), test that the formatted files exist in the workspace directory, and copy formatted files to the workspace directory. Formerly hosted as [rules_bzlformat](https://github.com/cgrindel/rules_bzlformat). | [API](/doc/bzlformat/), [How-to](/bzlformat/), [Examples](/examples/bzlformat/) |
| bzllib | Collection of Starlark libraries. | [API](/doc/bzllib/), [How-to](/bzllib/) |
| bzlrelease | Automate and customize the generation of releases using GitHub Actions. | [API](/doc/bzlrelease/), [How-to](/bzlrelease/) |
| bzltidy | Collect Bazel actions that keep your source files up-to-date. | [API](/doc/bztidy/), [How-to](/bzltidy/) |
| markdown | Maintain markdown files. | [API](/doc/markdown/), [How-to](/markdown/), [Examples](/examples/markdown/) |
| shlib | Collection of libraries useful when implementing shell binaries, libraries, and tests. Formerly hosted as [bazel_shlib](https://github.com/cgrindel/bazel_shlib). | [API](/doc/shlib/), [How-to](/shlib/) |
| updatesrc | Copy files from the Bazel output directories to the workspace directory. Formerly hosted as [rules_updatesrc](https://github.com/cgrindel/rules_updatesrc) | [API](/doc/updatesrc/), [How-to](/updatesrc/), [Examples](/examples/updatesrc/) |

## Table of Contents

<!-- MARKDOWN TOC: BEGIN -->
* [Quickstart](#quickstart)
* [Workspace Configuration](#workspace-configuration)
* [Other Documentation](#other-documentation)
<!-- MARKDOWN TOC: END -->

- [Quickstart](#quickstart)
- [Workspace Configuration](#workspace-configuration)
- [Other Documentation](#other-documentation)
<!-- MARKDOWN TOC: END -->

## Quickstart

The following provides a quick introduction on how to load this repository into your workspace. For
The following provides a quick introduction on how to load this repository into your workspace. For
more information on how to use the projects from this repository in your workspace, check out the
how-to links above and review the [the generated documentation](/doc/).


### `MODULE.bazel` Snippet

<!-- BEGIN MODULE SNIPPET -->

```python
bazel_dep(name = "cgrindel_bazel_starlib", version = "0.16.2")
```
<!-- END MODULE SNIPPET -->

<!-- END MODULE SNIPPET -->

### Workspace Configuration

<!-- BEGIN WORKSPACE SNIPPET -->

```python
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

Expand All @@ -64,8 +64,23 @@ load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")

bazel_skylib_workspace()
```

<!-- END WORKSPACE SNIPPET -->

## Developer Documentation

### To Add a Golang Dependency

Execute the following to update the go module files, resolve the Golang dependencies and update the
Bazel build files.

```sh
$ bazel run @io_bazel_rules_go//go -- github.com/sweet/go_pkg
$ bazel run //:go_mod_tidy
$ bazel run //:gazelle_update_repos
$ bazel run //:update_build_files
```

## Other Documentation

- [Release process for this repository](release/README.md)
34 changes: 0 additions & 34 deletions cmd/go_deps/README.md

This file was deleted.

24 changes: 0 additions & 24 deletions cmd/go_deps/main.go

This file was deleted.

3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ go 1.19

require (
github.com/creasty/defaults v1.7.0
github.com/ekalinin/github-markdown-toc.go v1.2.1
github.com/stretchr/testify v1.8.4
golang.org/x/net v0.1.0
gopkg.in/alecthomas/kingpin.v2 v2.2.4
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/alecthomas/assert v1.0.0 // indirect
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc // indirect
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
Expand Down
7 changes: 4 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
github.com/alecthomas/assert v0.0.0-20170929043011-405dbfeb8e38 h1:smF2tmSOzy2Mm+0dGI2AIUHY+w0BUc+4tn40djz7+6U=
github.com/alecthomas/assert v1.0.0 h1:3XmGh/PSuLzDbK3W2gUbRXwgW5lqPkuqvRgeQ30FI5o=
github.com/alecthomas/assert v1.0.0/go.mod h1:va/d2JC+M7F6s+80kl/R3G7FUiW6JzUO+hPhLyJ36ZY=
github.com/alecthomas/colour v0.1.0 h1:nOE9rJm6dsZ66RGWYSFrXw461ZIt9A6+nHgL7FRrDUk=
github.com/alecthomas/repr v0.0.0-20210801044451-80ca428c5142 h1:8Uy0oSf5co/NZXje7U1z8Mpep++QJOldL2hs/sBQf48=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc h1:cAKDfWh5VpdgMhJosfJnn5/FoN2SRZ4p7fJNX58YPaU=
Expand All @@ -9,14 +10,14 @@ github.com/creasty/defaults v1.7.0 h1:eNdqZvc5B509z18lD8yc212CAqJNvfT1Jq6L8WowdB
github.com/creasty/defaults v1.7.0/go.mod h1:iGzKe6pbEHnpMPtfDXZEr0NVxWnPTjb1bbDy08fPzYM=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/ekalinin/github-markdown-toc.go v1.2.1 h1:6jRFt5qg61XfXZbP3SDaeTX+1OC1EgbHvRceYDmPAUE=
github.com/ekalinin/github-markdown-toc.go v1.2.1/go.mod h1:V5aiwoSLm1+er91D4l0AXn8vr4FX07Iu+zgDMFj3FeU=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/net v0.1.0 h1:hZ/3BUoy5aId7sCpA/Tc5lt8DkFgdVS2onTpJsZ/fl0=
golang.org/x/net v0.1.0/go.mod h1:Cx3nUiGt4eDBEyega/BKRp+/AlGL8hYe7U9odMt2Cco=
golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA=
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/alecthomas/kingpin.v2 v2.2.4 h1:CC8tJ/xljioKrK6ii3IeWVXU4Tw7VB+LbjZBJaBxN50=
Expand Down
33 changes: 24 additions & 9 deletions go_deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ def bazel_starlib_go_dependencies():
name = "com_github_alecthomas_assert",
build_external = "external",
importpath = "github.com/alecthomas/assert",
sum = "h1:smF2tmSOzy2Mm+0dGI2AIUHY+w0BUc+4tn40djz7+6U=",
version = "v0.0.0-20170929043011-405dbfeb8e38",
sum = "h1:3XmGh/PSuLzDbK3W2gUbRXwgW5lqPkuqvRgeQ30FI5o=",
version = "v1.0.0",
)
go_repository(
name = "com_github_alecthomas_colour",
Expand Down Expand Up @@ -55,13 +55,6 @@ def bazel_starlib_go_dependencies():
sum = "h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=",
version = "v1.1.1",
)
go_repository(
name = "com_github_ekalinin_github_markdown_toc_go",
build_external = "external",
importpath = "github.com/ekalinin/github-markdown-toc.go",
sum = "h1:6jRFt5qg61XfXZbP3SDaeTX+1OC1EgbHvRceYDmPAUE=",
version = "v1.2.1",
)
go_repository(
name = "com_github_mattn_go_isatty",
build_external = "external",
Expand Down Expand Up @@ -118,10 +111,32 @@ def bazel_starlib_go_dependencies():
sum = "h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=",
version = "v3.0.1",
)
go_repository(
name = "org_golang_x_net",
build_external = "external",
importpath = "golang.org/x/net",
sum = "h1:hZ/3BUoy5aId7sCpA/Tc5lt8DkFgdVS2onTpJsZ/fl0=",
version = "v0.1.0",
)

go_repository(
name = "org_golang_x_sys",
build_external = "external",
importpath = "golang.org/x/sys",
sum = "h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA=",
version = "v0.10.0",
)
go_repository(
name = "org_golang_x_term",
build_external = "external",
importpath = "golang.org/x/term",
sum = "h1:g6Z6vPFA9dYBAF7DWcH6sCcOntplXsDKcliusYijMlw=",
version = "v0.1.0",
)
go_repository(
name = "org_golang_x_text",
build_external = "external",
importpath = "golang.org/x/text",
sum = "h1:BrVqGRd7+k1DiOgtnFvAkoQEWQvBc25ouMJM6429SFg=",
version = "v0.4.0",
)
2 changes: 1 addition & 1 deletion markdown/tools/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ sh_binary(
srcs = ["update_markdown_toc.sh"],
data = [
":update_markdown_doc",
"@com_github_ekalinin_github_markdown_toc_go//cmd/gh-md-toc",
"//markdown/tools/github_markdown_toc/cmd/gh-md-toc",
],
visibility = ["//visibility:public"],
deps = [
Expand Down
40 changes: 40 additions & 0 deletions markdown/tools/github_markdown_toc/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
load("@cgrindel_bazel_starlib//bzlformat:defs.bzl", "bzlformat_pkg")
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")

filegroup(
name = "all_files",
srcs = glob(["*"]),
visibility = ["//:__subpackages__"],
)

go_library(
name = "github_markdown_toc",
srcs = [
"ghdoc.go",
"headerfinder.go",
"internals.go",
],
importpath = "github.com/cgrindel/bazel-starlib/markdown/tools/github_markdown_toc",
visibility = ["//visibility:public"],
deps = [
"@org_golang_x_net//html",
"@org_golang_x_net//html/atom",
],
)

go_test(
name = "github_markdown_toc_test",
srcs = [
"ghdoc_test.go",
"headerfinder_test.go",
"internal_test.go",
],
embed = [":github_markdown_toc"],
deps = [
"@com_github_stretchr_testify//assert",
"@org_golang_x_net//html",
"@org_golang_x_net//html/atom",
],
)

bzlformat_pkg(name = "bzlformat")
20 changes: 20 additions & 0 deletions markdown/tools/github_markdown_toc/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The MIT License (MIT)

Copyright (c) 2015 Eugene Kalinin

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
40 changes: 40 additions & 0 deletions markdown/tools/github_markdown_toc/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
EXEC=gh-md-toc
CMD_SRC=cmd/${EXEC}/main.go
BUILD_DIR=build
BUILD_OS="windows darwin linux"
BUILD_ARCH="amd64"

clean:
@rm -f ${EXEC}
@rm -f ${BUILD_DIR}/*
@go clean

lint:
@golint
@golangci-lint run

# make run ARGS="--help"
run:
@go run ${CMD_SRC} $(ARGS)

build: clean lint
go build -race -o ${EXEC} ${CMD_SRC}

test: clean lint
@go test -cover -o ${EXEC}

release: test buildall
@git tag `grep "version" main.go | grep -o -E '[0-9]\.[0-9]\.[0-9]{1,2}'`
@git push --tags origin master

buildall: clean
@mkdir -p ${BUILD_DIR}
@for os in "${BUILD_OS}" ; do \
for arch in "${BUILD_ARCH}" ; do \
echo " * build $$os for $$arch"; \
GOOS=$$os GOARCH=$$arch go build -o ${BUILD_DIR}/${EXEC} ${CMD_SRC}; \
cd ${BUILD_DIR}; \
tar czf ${EXEC}.$$os.$$arch.tgz ${EXEC}; \
cd - ; \
done done
@rm ${BUILD_DIR}/${EXEC}
14 changes: 14 additions & 0 deletions markdown/tools/github_markdown_toc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# GitHub Markdown TOC

This utility is based upon [the gh-md-toc utility created by
eklanin](https://github.com/ekalinin/github-markdown-toc.go). The original implementation used
regular expressions to parse the HTML generated from GitHub's markdown rendering service.
Unfortunately, subtle changes in the output from this service can cause the regular expressions to
not work properly. I created [a pull request that replaced the regular experession logic with HTML
parsing using `golang.org/x/net/html`](https://github.com/ekalinin/github-markdown-toc.go/pull/38).
As of this writing, the pull request has not been merged.

After another outage due to the fragility of the regular expression logic on 2023-08-29, I opted to
fork the code with the HTML parsing logic and incorporate it into this repository. I preserved the
original license on the code. However, any changes to this utility may not be compatible with the
original code base.
Loading

0 comments on commit 016b1d3

Please sign in to comment.