-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_TOC_for_md.py
executable file
·223 lines (153 loc) · 6.85 KB
/
create_TOC_for_md.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#! /usr/bin/env python
# 3.7
# create MD for TOC
# set DEFAULT_FILE to relevant RTF course notes
# DEFAULT_README = Path('./README.md') # < in not using equations
# DEFAULT_README = Path('./README.tex.md') # < if Texify installed it will convert the Latex into equations
# passing a .md file as argument will print a TOC for that file
import os
import subprocess
import re
from pprint import pprint
import sys # argv
#from pprint import pprint
from pathlib import Path
# RTF conversion to text
from striprtf.striprtf import rtf_to_text
# convert RTF to txt
def get_text_content_of_file(rtf_filepath):
with open(rtf_filepath,'r') as f:
rtf = f.read()
return rtf_to_text(rtf) # convert to text and return
def create_toc_link_text(title):
# downcase, remove all non alphanumeric, replace space with hyphen
# leave hyphens in!
toc_link_text = title.strip().strip("\\").lower()
toc_link_text = re.sub(r'[^a-z 0-9\-]', '', toc_link_text)
toc_link_text = re.sub(r' ', '-', toc_link_text)
return toc_link_text
MAX_NO_CONTENT_ITEMS_PER_INDENT = 100
MAX_INDENT_DEPTH = 12
FRONT_OF_QUEUE = 0
INDENT_DEPTH = 0
LINK_LINE = 1
def create_indented_md_link_lines(link_tuples, indent=1):
if indent > MAX_INDENT_DEPTH: return
toc_lines = []
bullet = 1
while(len(link_tuples) > 0):
#print(f"INDENT:{indent} - len(link_tuples):{len(link_tuples)}")
if( link_tuples[FRONT_OF_QUEUE][INDENT_DEPTH] == indent ):
# pop it
line = link_tuples.pop(FRONT_OF_QUEUE)
# create toc line
tabs ="\t" * (indent - 1)
print(f"{tabs}{bullet}. {line[LINK_LINE]}")
toc_lines.append(f"{tabs}{bullet}. {line[LINK_LINE]}")
bullet += 1
elif( link_tuples[FRONT_OF_QUEUE][INDENT_DEPTH] > indent ):
# call this function to go to next level
toc_lines.append( create_indented_md_link_lines(link_tuples, indent+1) )
else:
# return to go to down a level
return toc_lines
return toc_lines
def create_TOC_from_text(text):
replacement = ''
links_with_no_of_indents = []
# how to enumerate - replacement with a tag? for later returning snippet?
# remove code snippets
text_lite = re.sub(r'^```.*?```', replacement, text, flags = re.MULTILINE | re.DOTALL)
# match for one or more # and title
collect_toc_lines = re.findall(r'^(#+)(.*?)$', text_lite, flags = re.MULTILINE | re.DOTALL)
#print('\n\n> found - - - - S\n')
for m in collect_toc_lines:
#print(f"[{m[1].strip()}](#{create_toc_link_text(m[1])})\\")
#print(m)
md_href = f"[{m[1].strip()}](#{create_toc_link_text(m[1])})"
links_with_no_of_indents.append( (len(m[0]) - 1,md_href) )
links_with_no_of_indents.pop(0) # remove headline
indented_md_hrefs = create_indented_md_link_lines(links_with_no_of_indents)
return indented_md_hrefs
# Look at how to do this with functools.reduce()
# https://stackoverflow.com/questions/952914/how-to-make-a-flat-list-out-of-list-of-lists
#
def create_TOC_as_string_from_TOC_nested_list(toc_list):
lines = ""
for l in toc_list:
if type(l).__name__ == 'str':
lines += f"{l} \n"
elif type(l).__name__ == 'list':
lines += create_TOC_as_string_from_TOC_nested_list(l)
return lines
# INPUT
#DEFAULT_FILE = Path('/_COURSES_00_WIP/ALGO_00_Intro_2_Algorithms_MIT.rtf')
#DEFAULT_FILE = Path('context.md')
DEFAULT_FILE = Path('README.md')
def get_mark_down(filename=DEFAULT_FILE):
with open(filename) as f:
content = f.read()
# incase string passed in
if Path(filename).suffix == '.rtf':
content = rtf_to_text(content)
else:
print(f"> > > > - - - - - - - - - - < < < < {Path(filename).suffix} > > > >")
replacement = ''
# remove anything inside 'comment' delimiters //* this is a comment *//
content = re.sub(r'\/\/\*.*?\*\/\/', replacement, content, flags = re.MULTILINE | re.DOTALL)
for line in iter(content.splitlines()):
print(line)
print("\n\n\n\n\n\n")
return content
# OUTPUT - w/ TOC added
DEFAULT_README = Path('./README.md')
#DEFAULT_README = Path('./README.tex.md') # if Texify installed it will convert the Latex into equations
def save_text_w_toc_to_readme(text, toc_string):
replacement = "## Contents \n" + toc_string + "\n\n## AIM: \n"
print(f"***\n***\n{replacement}\n***\n***\n")
text = re.sub(r'^## Contents.*?## AIM:.*?$', replacement, text, flags = re.MULTILINE | re.DOTALL)
with open(DEFAULT_README, 'w') as f:
f.write(text)
return text
if __name__ == '__main__':
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# title = 'Logarithms - identities & basic manipulation'
# link_sb = 'logarithms---identities--basic-manipulation'
# link = create_toc_link_text(title)
# print(link_sb)
# print(link)
# print(link == link_sb)
#
# sys.exit(0)
report = f"PWD: {os.getcwd()}"
# sys.argv[0] is name of this file
if len(sys.argv) > 1 and Path(sys.argv[1]).exists():
report += f"\nCreating TOC for {sys.argv[1]}"
text = get_mark_down(sys.argv[1])
else:
report += f"\n* * USING DEFAULT FILE * * - Creating TOC for {DEFAULT_FILE}"
text = get_mark_down()
toc = create_TOC_from_text(text)
toc_string = create_TOC_as_string_from_TOC_nested_list(toc)
print(save_text_w_toc_to_readme(text, toc_string))
return_code = 'WARNING: No push to git!'
commit_comment = 'No comment'
if '-p' in sys.argv:
try:
commit_comment = sys.argv[sys.argv.index('-p')+1]
except IndexError:
commit_comment = input("Commit comment:")
return_code = subprocess.call(['git','add',DEFAULT_README]) # note command separation inside list! [ ]
print(f"git add {DEFAULT_README}: {return_code}") # ^----/
return_code = subprocess.call(["git","commit",f"-m autogen {DEFAULT_README}:{commit_comment}"])
print(f"git commit -m'autogen {DEFAULT_README}: {commit_comment}'")
return_code = subprocess.call(['git','push'])
print(f"git push: {return_code}")
else:
print(f"\n\n{return_code}\n")
print(f"\n\n{toc_string}\n\n")
print(f"\n\n{report}")