-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqb64_help_parser.py
165 lines (133 loc) · 5.37 KB
/
qb64_help_parser.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
# @Author: AshishKingdom(Ashish Kushwaha)
#help files parser related functions
import requests
def getRawDataFromQB64Wiki(keyword):
#return the raw wiki data from http://qb64.org/wiki if it succeed
#otherwise, we will be using data from the stored help files
r = requests.get("http://wiki.qb64.org/wiki/index.php?action=edit&title="+keyword.upper())
if(r.status_code==200):
data = r.text
return data[data.find('name="wpTextbox1">')+18:data.find("</textarea>")]
else:
f = open('help/'+keyword.upper()+'.txt', 'r')
data = f.read()
f.close()
return data
def removeHTMLSpecialChars(s):
d = {"&":'&', "<":'<', ">":'>', " ":' '}
s2 = s
for k in d.keys():
s2 = s2.replace(k, d[k])
return s2
def reformatStr(s, put_backtick=False): #coverts {{ABC|XYZ}} or [[ABC|XYZ]] to XYZ
s2 = ""
i = 0
while(i<len(s)):
c = s[i]
if(s[i:i+2]=="{{" or s[i:i+2]=="[["):
f1 = s.find('|', i+2)
f2 = s.find('|', f1+1)
if s[i:i+2]=="{{":
end_sep = "}}"
else:
end_sep = "]]"
end_pos = s.find(end_sep, i+2)
if(f1>0):
if(f2>f1):
if(f2<end_pos):
f = f2
else:
if(f1>end_pos):
f = -1
else:
f = f1
else:
if(f1>end_pos):
f = -1
else:
f = f1
else:
f = -1
if(f==-1):
c = s[i+2:s.find(end_sep, i+2)]
#print(c)
i = s.find(end_sep, i+2)+2
if(put_backtick): c = '`'+c+'`'
else:
c = s[f+1:s.find(end_sep, f)]
i = s.find(end_sep, f)+2
#print(c, s, i)
#input()
else:
i += 1
s2 += c
return s2
def replaceStr(s, f, r): #replaces every occurance of f with r in s
s2 = s.replace(f, r)
return s2
def getDocumentation(keyword):
data = {"bytes":0, "title":keyword.upper(), "use":"", "{{PageSyntax}}":"", "{{PageParameters}}":"", "{{PageDescription}}":"", "{{PageAvailability}}":""}
file_content = getRawDataFromQB64Wiki(keyword).split('\n')
topics = ["{{PageSyntax}}", "{{PageParameters}}", "{{PageDescription}}", "{{PageAvailability}}"]
current_topic = ""
#parse the contents
done_use = False
i = 0
while i<len(file_content):
if(done_use==False):
if(file_content[0][:15]=="{{DISPLAYTITLE:"):
j = 1
else:
j = 0
while(file_content[j].strip() not in topics):
data["use"] += removeHTMLSpecialChars(file_content[j]).replace("'''",'').replace("''",'')+' '
j += 1
done_use = True
current_topic = file_content[j].strip()
i = j+1
fc = file_content[i].strip()
if(fc=="{{PageExamples}}" or fc=="{{PageSeeAlso}}" or fc=="{{Template:RelationalTable}}"):
break
if(fc in topics):
current_topic = fc
i += 1
continue
if(current_topic == "{{PageSyntax}}"):
if(fc[:2]=="::"):
fc = "`"+fc[2:]+"`\n"
elif(fc[:1]==":"):
fc = "`"+fc[1:]+"`\n"
elif(fc[:2]=="* "):
fc = "\n- "+fc[2:]
elif(fc[:3]=="** "):
fc = "\n- "+fc[3:]
if(current_topic in ["{{PageParameters}}", "{{PageDescription}}", "{{PageAvailability}}"]):
if(fc[:2]=="* "):
fc = "\n- "+fc[2:]
if(fc[:3]=="** "):
fc = "\n- "+fc[3:]
data[current_topic] += removeHTMLSpecialChars(fc)
i += 1
data["use"] = reformatStr(data["use"], True)
data["{{PageSyntax}}"] = reformatStr(data["{{PageSyntax}}"].replace("'''", "").replace("''", ""))
data["{{PageParameters}}"] = reformatStr(data["{{PageParameters}}"], True).replace("'''", "").replace("''", "")
data["{{PageDescription}}"] = reformatStr(data["{{PageDescription}}"], True).replace("'''", "").replace("''", "")
data["bytes"] = len(data["title"]) + len(data["use"]) + len(data["{{PageSyntax}}"]) + len(data["{{PageParameters}}"])
data["bytes"] += len(data["{{PageDescription}}"]) + len(data["{{PageAvailability}}"])
return data
def getExample(keyword):
res = ">>> **{}** Example:-\n```vb\n{}\n```\n"
file_content = getRawDataFromQB64Wiki(keyword)
found = False
#parse the contents
pos1 = file_content.find("{{CodeStart}}")
pos2 = file_content.find("{{CodeEnd}}")
if(pos1!=-1 and pos2!=-1):
code = removeHTMLSpecialChars(file_content[pos1+13:pos2])
return res.format(keyword.upper(), reformatStr(code).replace("'''", "").replace("''", ""))
else:
return "No example available for **{}** at Qb64 Wiki.".format(keyword.upper())
#def updatePage(keyword):
#print(getExample(input()))
#print(getDocumentation(input()))
#print(getDocumentation(input())["description"])