Created
February 14, 2019 02:04
-
-
Save nolanlawson/38381dd2451e4510a60dd1e8958b7008 to your computer and use it in GitHub Desktop.
create quick links for a blog post
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import sys | |
import re | |
text = sys.stdin.read() | |
regex = re.compile('### ([^\n]+)', re.DOTALL) | |
headers = regex.findall(text) | |
def create_hash (text): | |
text = re.sub('\]\([^)]+\)', '', text) | |
text = re.sub('[^a-zA-Z0-9]+', '-', text) | |
text = re.sub('^\-', '', text) | |
text = text[:30] | |
text = re.sub('\-[^-]*$', '', text) | |
return text.lower() | |
hashes = map(create_hash, headers) | |
quick_links = '' | |
for i in range(len(headers)): | |
the_header = headers[i] | |
the_hash = hashes[i] | |
text = text.replace('### ' + the_header, '''### %s<a name="%s"></a>''' % (the_header, the_hash)) | |
quick_link_header = re.sub('\]\([^)]+\)', '', the_header) | |
quick_link_header = re.sub('[\]\[]', '', quick_link_header) | |
quick_links += '''- [%s](#%s)\n''' % (quick_link_header, the_hash) | |
print quick_links + text |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment