Skip to content

Instantly share code, notes, and snippets.

@orisano

orisano/fmt.py Secret

Created November 1, 2021 15:08
Show Gist options
  • Save orisano/95b0d59bed94234762d77be808c752a8 to your computer and use it in GitHub Desktop.
Save orisano/95b0d59bed94234762d77be808c752a8 to your computer and use it in GitHub Desktop.
import glob
import re
import subprocess
def main():
for md_path in glob.glob("./docs/**/*.md"):
md = open(md_path).read()
begin = 0
while True:
p = md.find("```go\n", begin)
if p == -1:
break
code = md[p + 6 :]
code = code[: code.find("```")]
proc = subprocess.run(["gofmt"], input=code.encode(), capture_output=True)
md = md[: p + 6] + proc.stdout.decode() + md[p + 6 + len(code) :]
begin = p + len(proc.stdout.decode()) + 3
open(md_path, "w").write(md)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment