Skip to content

Instantly share code, notes, and snippets.

@martin-martin
Created June 2, 2021 12:56
Show Gist options
  • Save martin-martin/b4147391eedd136f1fb5e359f6dc2379 to your computer and use it in GitHub Desktop.
Save martin-martin/b4147391eedd136f1fb5e359f6dc2379 to your computer and use it in GitHub Desktop.
import argparse
from pathlib import Path
import re
import sys
my_parser = argparse.ArgumentParser(description='List Markdown headings')
my_parser.add_argument('Path',
metavar='path',
type=str,
help='the file to check')
args = my_parser.parse_args()
input_path = Path(args.Path)
if (not input_path.is_file()) or (not input_path.suffix == ".md"):
print("Specify path to a Markdown file.")
sys.exit()
with input_path.open() as fin:
content = fin.read()
# Remove meta-tags that aren't part of the actual heading
content = content.replace("```python\n#", "```")
content = content.replace("<!-- fit --> ", "")
# TODO: Currently not accounted for:
# Python code comments
# <i class="fa-fw fas fa-times-circle color-no"></i>
# @[info](Header)
headings = re.findall(r'#.*?\n', content)
# Add one heading level for Preflight detection
# Convert to set because we only need to see each heading once
unique_headings = {f"#{h.strip()}" for h in headings}
print("Copy-paste the following headings into a Post draft:", end="\n\n")
for h in unique_headings:
print(h)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment