-
Notifications
You must be signed in to change notification settings - Fork 0
/
markdownanchormaker.py
executable file
·151 lines (127 loc) · 4.38 KB
/
markdownanchormaker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/env python3
import re
import sys
import argparse
def parse_args(args):
parser.add_argument(
"-f",
"--file",
help="Create anchor links for every heading line (ie, lines starting with '#') in the supplied markdown file <FILE>",
action="store",
)
parser.add_argument(
"-a",
"--anchor",
help="Create one anchor link out of the double quoted string <ANCHOR>",
action="store",
)
parser.add_argument(
"-o",
"--only-headers",
help="Don't show headers found. Only output the created header links",
action="store_true",
)
return parser.parse_args(args)
def get_pretty_title(string):
"""
Grab everything after the # and {one_or_more_spaces} and make it the pretty title
"""
match = re.search(r"\#\s+(.*?)$", string)
pretty_part = match.group(1) if match else "COULD NOT GET TITLE"
return pretty_part
def replace_spaces_with_dashes(string):
"""
Replace spaces with dashes
"""
string = re.sub(r"\s", r"-", string, flags=re.IGNORECASE)
return string
def drop_unwanted_chars(string):
"""
Drop any characters that aren't "-", "_", a letter, a number or a space
"""
string = re.sub(r"[^-_a-z0-9\s]", r"", string, flags=re.IGNORECASE)
return string
def prepend_octothorpe(string):
"""
Add a single # to the front of the string
"""
string = re.sub(r"^(.*)$", r"#\1", string, flags=re.IGNORECASE)
return string
def parenthesis_surround(string):
"""
Add parenthesis around the whole string
"""
string = re.sub(r"^(.*)$", r"(\1)", string, flags=re.IGNORECASE)
return string
def remove_leading_dashes(string):
"""
If there is a leading dash(es) (ie, #-something-something or #--something) get rid of them
"""
string = re.sub(r"^\(#-+", r"(#", string, flags=re.IGNORECASE)
return string
def make_lowercase(string):
"""
Make everything lowercase
"""
string = string.lower()
return string
def anchor_maker(string):
"""
Run all the functions that make the anchor and return the string
"""
string = replace_spaces_with_dashes(string)
string = drop_unwanted_chars(string)
string = prepend_octothorpe(string)
string = parenthesis_surround(string)
string = remove_leading_dashes(string)
string = make_lowercase(string)
return string
def output_title_and_link(pretty_part, anchor_link):
"""
Combine the pretty part of the title (ie, just the text) with the actual
inline/anchor link and format it as a markdown hyperlink, ie: [title](link)
Also adds two spaces at the end of the string; for a newline in Markdown
"""
full_title_and_link = f"[{pretty_part}]{anchor_link} "
return full_title_and_link
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Generate Markdown inline anchor links"
)
args = parse_args(sys.argv[1:])
if not len(sys.argv) > 1:
parser.print_help()
if args.file:
collection_of_titles_before = []
collection_of_titles = []
no_titles_found = True
with open(args.file) as myfile:
contents = myfile.readlines()
for i in contents:
i = i.rstrip()
match = re.search(r"^#.*?$", i)
if match:
no_titles_found = False
collection_of_titles_before.append(i)
pretty_part = get_pretty_title(i)
anchor_link = anchor_maker(i)
full_title_and_link = output_title_and_link(pretty_part, anchor_link)
collection_of_titles.append(full_title_and_link)
if no_titles_found is True:
print(f"""No lines starting with "#" were found in {args.file}""")
elif no_titles_found is False:
if not args.only_headers:
print(f"Headings found in {args.file}:")
print()
for j in collection_of_titles_before:
print(j)
print()
print("Created anchor links:")
print()
for k in collection_of_titles:
print(k)
if args.anchor:
pretty_part = get_pretty_title(args.anchor)
anchor_link = anchor_maker(args.anchor)
full_title_and_link = output_title_and_link(pretty_part, anchor_link)
print(full_title_and_link)