-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadme-build.py
46 lines (38 loc) · 1.52 KB
/
readme-build.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
import markdown
# create initial variable values
targetFile = "index.html"
targetType = "section"
targetSel = "</" + targetType
beginTarget, endTarget = 0, 0
# read markdown file
with open('README.md', 'r', encoding="utf8") as f:
readmeMarkdown = f.read()
# read destination html file
with open(targetFile, 'r', encoding="utf8") as f:
destinationLines = f.readlines()
for line in destinationLines:
# check if readme section exists & where it ends
if line.find('data-readme-marker') != -1:
# get index of where section begins
beginTarget = destinationLines.index(line) + 1
# get element type & end tag to query
targetType = "".join(line)
targetType = targetType.split("<")
targetType = targetType[1].split(" ")
targetSel = "</" + str(targetType[0])
if line.find(targetSel) != -1:
# get index of where section ends
endTarget = destinationLines.index(line) - 1
break;
# remove existing section (but keep end tag)
del destinationLines[beginTarget:endTarget]
# turn the markdown into html
# extensions for keeping code blocks intact and to account for my usual tab spacing
readmeHTML = markdown.markdown(readmeMarkdown, extensions = ['fenced_code'], tab_length = 2) + "\n"
# insert rendered HTML into destination
destinationLines.insert(beginTarget, readmeHTML)
# write to destination html file
with open(targetFile, 'w', encoding="utf8") as f:
destinationLines = "".join(destinationLines)
f.write(destinationLines)
print("README content copied into " + targetFile)