-
Notifications
You must be signed in to change notification settings - Fork 5
/
post_edit_docs.py
112 lines (88 loc) · 3.15 KB
/
post_edit_docs.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
try:
from markdown import markdown
except ImportError:
from markdown2 import markdown
from bs4 import BeautifulSoup
import sys
import re
import logging
markdown_file = sys.argv[1]
http_methods = ("DELETE", "POST", "GET", "PUT", "OPTIONS", "PATCH")
logging.basicConfig(level=logging.INFO)
foundLinks = []
markdownLines = []
def findPathLine():
# look for the first line containing '## Paths' and return the line number
for c, l in enumerate(markdownLines):
if re.search("^[#]{2} Paths$", l):
logging.info("Found '%s' at line %d" % (l.strip(), c))
return c
logging.critical("Exit: ## Paths or ### Paths not found in markdown file")
sys.exit(1)
def newPathMarkdown():
newPathBlock = []
# Converting file to HTML in memory
md_str = open(markdown_file, "r").read()
html = markdown(md_str)
soup = BeautifulSoup(html, 'html.parser')
for e in soup.find_all("code"):
logging.info("newPathMarkdown: processing: '%s'" % (e.text))
text = ''.join(e.text.split())
if len(e) == 1 and text.startswith(http_methods):
link = e.findPrevious('a').get("name")
pathLine = "- [%s](#%s)\n" % (text, link)
logging.info("Adding %s to path block" % (str(pathLine).strip()))
foundLinks.append(link)
newPathBlock.append(pathLine)
newPathBlock.append("\n\n___")
return newPathBlock
def insertVerticalSeperator():
pattern = re.compile("^<a name=\"(.*)\"></a>")
count = 0
for c, l in enumerate(markdownLines[:]):
if pattern.match(l) and pattern.match(l).groups()[0] in foundLinks[1:]:
anchor = str(pattern.match(l).groups()[0])
logging.info("Adding vertical line sperator at line: %d, right on top of anchor: %s" % (c + count - 2, anchor))
markdownLines.insert(c + count - 2, "___")
count += 1
def insertNewPathMarkdown():
pathLocation = findPathLine()
newMarkdown = newPathMarkdown()
for c, l in enumerate(newMarkdown):
markdownLines.insert(pathLocation + c + 1, l)
def replaceEmptyTableCells():
global markdownLines
t = []
for c, l in enumerate(markdownLines):
if "||" in l:
logging.info("Replacing empty cell in following line: %s" % (l.strip()))
l = l.replace("||", "| |")
l = l.replace("||", "| |")
t.append(l)
else:
t.append(l)
markdownLines = t[:]
def insertPrefix():
pattern = re.compile("^#\s(.)+$")
for c, l in enumerate(markdownLines):
if pattern.match(l) and pattern.match(l).groups()[0] is not None:
logging.info("Using '%s' as the document title" % (l.strip()))
markdownLines.pop(c)
titleInPrefix = l
break
titleInPrefix = titleInPrefix.replace("#", "").strip()
markdownLines.insert(0, """---
title: %s
taxonomy:
category: docs
api: true
template: docs
---""" % (titleInPrefix))
with open(markdown_file, "r") as f:
markdownLines = f.readlines()
insertPrefix()
insertNewPathMarkdown()
insertVerticalSeperator()
replaceEmptyTableCells()
for l in markdownLines:
print(str(l), end = ''),