-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake_scite_chm_files.py
220 lines (160 loc) · 6.73 KB
/
make_scite_chm_files.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
#!python3
'''Make hhc, hhk and hhp files to make lexilla.chm, scintilla.chm and scite.chm.'''
import glob, os, re, subprocess
import common
settings = common.settings
# Make only scite.chm. False=Make all chm files.
settings['scite_only'] = False
def get_absent_property_items(content):
'''Get property items absent of an id attribute.'''
properties = []
# Get command, indicator, marker and style properties.
alt = (r'command\.name\.<i>number</i>\.<i>filepattern|'
r'style\.<i>lexer</i>\.\d|'
r'change.history')
matches = re.findall(r'<tr.*?>\s*<td>\s*((?:' + alt + r').*?)\s*</td>',
content, re.S|re.I)
if matches:
for item in matches:
# Split into items.
items = re.split(r'<br />\s*', item, flags=re.S|re.I)
if items:
for item in items:
# Remove tags.
item = re.sub('<.+?>', '', item)
# Add to the list.
if item:
properties.append(item)
return properties
def make_index(file, outfile):
'''Make index file for scite.chm.'''
with open(file) as r:
content = r.read()
# Get property items with a id|name attribute.
matches = re.findall(r"(?:id|name)=[\"']property-(.+?)[\"']", content, re.I)
if not matches:
return
# Get property items absent of an id|name attribute.
# Anchor to a known id|name preceding its location.
items = get_absent_property_items(content)
if items:
for index, item in enumerate(items):
if item not in matches:
if ('property-' + item) not in content:
if item.startswith('command.'):
if 'property-command.go.needs' in content:
items[index] = item + '#command.go.needs'
elif item.startswith('indicator.') or item.startswith('marker.'):
if 'property-change.history' in content:
items[index] = item + '#change.history'
elif item.startswith('style.'):
if 'property-style.*' in content:
items[index] = item + '#style.*'
matches.extend(items)
# Sort as unique items.
matches = list(set(matches))
matches.sort(key=str.lower)
# Write the index file.
with open(outfile, 'w') as w:
w.write('<!DOCTYPE HTML>\n'
'<HTML>\n<BODY>\n<UL>\n')
for item in matches:
if '#' not in item:
item = item + '#' + item
items = item.split('#')
w.write('\t<LI> <OBJECT type="text/sitemap">\n'
'\t\t<param name="Name" value="{1}">\n'
'\t\t<param name="Local" value="{0}#property-{2}">\n'
'\t\t</OBJECT>\n'.format(os.path.basename(file), *items))
w.write('</UL>\n</BODY>\n</HTML>\n')
if __name__ == '__main__':
# Set output path.
output = settings['output']
if not os.path.isdir(output):
os.makedirs(output)
print('output:')
# Change directory to these folders.
for folder in ('lexilla', 'scintilla', 'scite'):
# Only allow scite.
if settings['scite_only']:
if folder != 'scite':
continue
# Folders are optional considering lexilla may not exist < SciTE 5.0.
if not os.path.isdir(folder):
continue
# Go to the root folder of the html files.
os.chdir(folder)
os.chdir('doc')
files = glob.glob('*.html')
# Set initial html file to show in the chm.
for item in files:
if item.endswith('Doc.html'):
default_topic = item
break
else:
default_topic = files[0]
# Make index file.
if folder == 'scite' and default_topic.endswith('Doc.html'):
make_index(default_topic, folder + '.hhk')
index_key = 'Index file=scite.hhk\n'
else:
index_key = ''
# Set app name.
app = 'SciTE' if folder == 'scite' else folder.title()
# Write the project file.
with open(folder + '.hhp', 'w') as w:
w.write(('[OPTIONS]\n'
'Binary Index=No\n'
'Compatibility=1.1 or later\n'
'Compiled file={folder}.chm\n'
'Contents file={folder}.hhc\n'
'Default topic={default_topic}\n'
'Display compile progress=No\n'
'Enhanced decompilation=Yes\n'
'Full-text search=Yes\n'
'{index_key}'
'Title={app} Help {version}\n'
'\n'
'[FILES]\n').format(folder=folder,
default_topic=default_topic,
index_key=index_key,
app=app,
version=common.scite_version('.', folder)))
for item in files:
w.write(item + '\n')
# Write the contents file.
with open(folder + '.hhc', 'w') as w:
w.write('<!DOCTYPE HTML>\n'
'<HTML>\n<BODY>\n'
'<OBJECT type="text/site properties">\n'
'\t<param name="ImageType" value="Folder">\n'
'</OBJECT>\n'
'<UL>\n')
for item in files:
name = item[:-5]
if name == 'index':
name = 'Index'
w.write('\t<LI> <OBJECT type="text/sitemap">\n'
'\t\t<param name="Name" value="' + name + '">\n'
'\t\t<param name="Local" value="' + item + '">\n'
'\t\t</OBJECT>\n')
w.write('</UL>\n</BODY>\n</HTML>')
# Compile chm files.
if settings['compiler']:
outfile = folder + '.chm'
print(' ', outfile)
command = [settings['compiler'], folder + '.hhp']
with subprocess.Popen(command, stdout=subprocess.PIPE) as p:
stdout = p.communicate()[0]
stdout = stdout.decode().replace('\r', '')
if settings['compiler_stdout']:
print(stdout)
if os.path.isfile(outfile):
destination = os.path.join(settings['output'], outfile)
if os.path.isfile(destination):
os.remove(destination)
os.rename(outfile, destination)
# Change directory back to the main root folder.
os.chdir('..')
os.chdir('..')
print('done')