1 | #!/usr/bin/python
|
---|
2 |
|
---|
3 | # $Id: htmlhelp-qthelp.py 86707 2020-10-26 13:36:51Z vboxsync $
|
---|
4 | ## @file
|
---|
5 | # A python 2.x script to create a .qhp file outof a given htmlhelp
|
---|
6 | # folder. Lots of things about the said folder is assumed. Please
|
---|
7 | # read the code and inlined comments.
|
---|
8 |
|
---|
9 | import sys, getopt
|
---|
10 | import os.path
|
---|
11 | import re
|
---|
12 | import codecs
|
---|
13 | import logging
|
---|
14 |
|
---|
15 | __copyright__ = \
|
---|
16 | """
|
---|
17 | Copyright (C) 2006-2020 Oracle Corporation
|
---|
18 |
|
---|
19 | This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
20 | available from http://www.virtualbox.org. This file is free software;
|
---|
21 | you can redistribute it and/or modify it under the terms of the GNU
|
---|
22 | General Public License (GPL) as published by the Free Software
|
---|
23 | Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
24 | VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
25 | hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
26 | """
|
---|
27 |
|
---|
28 |
|
---|
29 | # number of opened and not yet closed section tags of toc section
|
---|
30 | open_section_tags = 0
|
---|
31 |
|
---|
32 | # find the png files under /images folder and create a part of the
|
---|
33 | # qhelp project file with <file> tags
|
---|
34 | def create_image_list(folder):
|
---|
35 | image_folder_name = 'images'
|
---|
36 | image_files_list = []
|
---|
37 | # Look for 'images' sub folder
|
---|
38 | subdirs = [x[0] for x in os.walk(folder)]
|
---|
39 | full_folder_path = os.path.join(folder, image_folder_name)
|
---|
40 | if full_folder_path not in subdirs:
|
---|
41 | logging.error('Image subfolder "%s" is not found under "%s".', image_folder_name, folder)
|
---|
42 | return image_files_list;
|
---|
43 | png_files = []
|
---|
44 | for f in os.listdir(full_folder_path):
|
---|
45 | png_files.append(image_folder_name + '/' + f)
|
---|
46 | image_files_list.append('<file>images/' + f + '</file>')
|
---|
47 | return image_files_list
|
---|
48 |
|
---|
49 | # open htmlhelp.hhp files and read the list of html files from there
|
---|
50 | def create_html_list(folder):
|
---|
51 | file_name = 'htmlhelp.hhp'
|
---|
52 | html_files_list = []
|
---|
53 | if not file_name in os.listdir(folder):
|
---|
54 | logging.error('Could not find the file "%s" in "%s"', file_name, folder)
|
---|
55 | return html_files_list
|
---|
56 | full_path = os.path.join(folder, 'htmlhelp.hhp')
|
---|
57 | file = open(full_path, "r")
|
---|
58 | lines = file.readlines()
|
---|
59 | file.close()
|
---|
60 | # first search for the [FILES] marker then collect .html lines
|
---|
61 | marker_found = 0
|
---|
62 | for line in lines:
|
---|
63 | if '[FILES]' in line:
|
---|
64 | marker_found = 1
|
---|
65 | continue
|
---|
66 | if marker_found == 0:
|
---|
67 | continue
|
---|
68 | if '.html' in line:
|
---|
69 | html_files_list.append('<file>' + line.strip('\n') + '</file>')
|
---|
70 | return html_files_list
|
---|
71 |
|
---|
72 |
|
---|
73 | def create_files_section(folder):
|
---|
74 | files_section_lines = ['<files>']
|
---|
75 | files_section_lines += create_image_list(folder)
|
---|
76 | files_section_lines += create_html_list(folder)
|
---|
77 | files_section_lines.append('</files>')
|
---|
78 | return files_section_lines
|
---|
79 |
|
---|
80 | def parse_param_tag(line):
|
---|
81 | label = 'value="'
|
---|
82 | start = line.find(label);
|
---|
83 | if start == -1:
|
---|
84 | return ''
|
---|
85 | start += len(label)
|
---|
86 | end = line.find('"', start)
|
---|
87 | if end == -1:
|
---|
88 | return '';
|
---|
89 | return line[start:end]
|
---|
90 |
|
---|
91 | # look at next two lines. they are supposed to look like the following
|
---|
92 | # <param name="Name" value="Oracle VM VirtualBox">
|
---|
93 | # <param name="Local" value="index.html">
|
---|
94 | # parse out value fields and return
|
---|
95 | # title="Oracle VM VirtualBox" ref="index.html
|
---|
96 | def parse_object_tag(lines, index):
|
---|
97 | result=''
|
---|
98 | if index + 2 > len(lines):
|
---|
99 | logging.warning('Not enough tags after this one "%s"',lines[index])
|
---|
100 | return result
|
---|
101 | if not re.match(r'^\s*<param', lines[index + 1], re.IGNORECASE) or \
|
---|
102 | not re.match(r'^\s*<param', lines[index + 2], re.IGNORECASE):
|
---|
103 | logging.warning('Skipping the line "%s" since next two tags are supposed to be param tags', lines[index])
|
---|
104 | return result
|
---|
105 |
|
---|
106 | title = parse_param_tag(lines[index + 1])
|
---|
107 | ref = parse_param_tag(lines[index + 2])
|
---|
108 | global open_section_tags
|
---|
109 | if title and ref:
|
---|
110 | open_section_tags += 1
|
---|
111 | result = '<section title="' + title + '" ref="' + ref + '">'
|
---|
112 | else:
|
---|
113 | logging.warning('Title or ref part is empty for the tag "%s"', lines[index])
|
---|
114 | return result
|
---|
115 |
|
---|
116 | # parse any string other than staring with <OBJECT
|
---|
117 | # decide if <session tag should be closed
|
---|
118 | def parse_non_object_tag(lines, index):
|
---|
119 | if index + 1 > len(lines):
|
---|
120 | return ''
|
---|
121 | global open_section_tags
|
---|
122 | if open_section_tags <= 0:
|
---|
123 | return ''
|
---|
124 | # replace </OBJECT with </section only if the next tag is not <UL
|
---|
125 | if re.match(r'^\s*</OBJECT', lines[index], re.IGNORECASE):
|
---|
126 | if not re.match(r'^\s*<UL', lines[index + 1], re.IGNORECASE):
|
---|
127 | open_section_tags -= 1
|
---|
128 | return '</section>'
|
---|
129 | elif re.match(r'^\s*</UL', lines[index], re.IGNORECASE):
|
---|
130 | open_section_tags -= 1
|
---|
131 | return '</section>'
|
---|
132 | return ''
|
---|
133 |
|
---|
134 | def parse_line(lines, index):
|
---|
135 | result=''
|
---|
136 |
|
---|
137 | # if the line starts with <OBJECT
|
---|
138 | if re.match(r'^\s*<OBJECT', lines[index], re.IGNORECASE):
|
---|
139 | result = parse_object_tag(lines, index)
|
---|
140 | else:
|
---|
141 | result = parse_non_object_tag(lines, index)
|
---|
142 | return result
|
---|
143 |
|
---|
144 | # parse toc.hhc file. assuming all the relevant informations
|
---|
145 | # is stored in tags and attributes. data "whatever is outside of
|
---|
146 | # <... > pairs is filtered out. we also assume < ..> are not nested
|
---|
147 | # and each < matches to a >
|
---|
148 | def parse_toc(folder):
|
---|
149 | toc_file = 'toc.hhc'
|
---|
150 | content = [x[2] for x in os.walk(folder)]
|
---|
151 | if toc_file not in content[0]:
|
---|
152 | logging.error('Could not find toc file "%s" under "%s"', toc_file, folder)
|
---|
153 | return
|
---|
154 | full_path = os.path.join(folder, toc_file)
|
---|
155 | file = codecs.open(full_path, encoding='iso-8859-1')
|
---|
156 | content = file.read()
|
---|
157 | file.close()
|
---|
158 | # convert the file string into a list of tags there by eliminating whatever
|
---|
159 | # char reside outside of tags.
|
---|
160 | char_pos = 0
|
---|
161 | tag_list = []
|
---|
162 | while char_pos < len(content):
|
---|
163 | start = content.find('<', char_pos)
|
---|
164 | if start == -1:
|
---|
165 | break
|
---|
166 | end = content.find('>', start)
|
---|
167 | if end == -1 or end >= len(content) - 1:
|
---|
168 | break
|
---|
169 | char_pos = end
|
---|
170 | tag_list.append(content[start:end +1])
|
---|
171 |
|
---|
172 | # # insert new line chars. to make sure each line includes at most one tag
|
---|
173 | # content = re.sub(r'>.*?<', r'>\n<', content)
|
---|
174 | # lines = content.split('\n')
|
---|
175 | toc_string_list = ['<toc>']
|
---|
176 | index = 0
|
---|
177 | for tag in tag_list:
|
---|
178 | str = parse_line(tag_list, index)
|
---|
179 | if str:
|
---|
180 | toc_string_list.append(str)
|
---|
181 | index += 1
|
---|
182 | toc_string_list.append('</toc>')
|
---|
183 | toc_string = '\n'.join(toc_string_list)
|
---|
184 |
|
---|
185 | return toc_string_list
|
---|
186 |
|
---|
187 | def usage(arg):
|
---|
188 | print 'test.py -d <helphtmlfolder> -o <outputfilename>'
|
---|
189 | sys.exit(arg)
|
---|
190 |
|
---|
191 | def main(argv):
|
---|
192 | helphtmlfolder = ''
|
---|
193 | output_filename = ''
|
---|
194 | try:
|
---|
195 | opts, args = getopt.getopt(sys.argv[1:],"hd:o:")
|
---|
196 | except getopt.GetoptError as err:
|
---|
197 | print err
|
---|
198 | usage(2)
|
---|
199 | for opt, arg in opts:
|
---|
200 | if opt == '-h':
|
---|
201 | usage(0)
|
---|
202 | elif opt in ("-d"):
|
---|
203 | helphtmlfolder = arg
|
---|
204 | elif opt in ("-o"):
|
---|
205 | output_filename = arg
|
---|
206 |
|
---|
207 | # check supplied helphtml folder argument
|
---|
208 | if not helphtmlfolder:
|
---|
209 | logging.error('No helphtml folder is provided. Exiting')
|
---|
210 | usage(2)
|
---|
211 | if not os.path.exists(helphtmlfolder):
|
---|
212 | logging.error('folder "%s" does not exist. Exiting', helphtmlfolder)
|
---|
213 | usage(2)
|
---|
214 | helphtmlfolder = os.path.normpath(helphtmlfolder)
|
---|
215 |
|
---|
216 | # check supplied output file name
|
---|
217 | if not output_filename:
|
---|
218 | logging.error('No filename for output is given. Exiting')
|
---|
219 | usage(2)
|
---|
220 |
|
---|
221 | out_xml_lines = ['<?xml version="1.0" encoding="UTF-8"?>', \
|
---|
222 | '<QtHelpProject version="1.0">' , \
|
---|
223 | '<namespace>org.virtualbox</namespace>', \
|
---|
224 | '<virtualFolder>doc</virtualFolder>', \
|
---|
225 | '<filterSection>']
|
---|
226 | out_xml_lines += parse_toc(helphtmlfolder) + create_files_section(helphtmlfolder)
|
---|
227 | out_xml_lines += ['</filterSection>', '</QtHelpProject>']
|
---|
228 |
|
---|
229 | out_file = open(output_filename, 'w')
|
---|
230 | out_file.write('\n'.join(out_xml_lines).encode('utf8'))
|
---|
231 | out_file.close()
|
---|
232 |
|
---|
233 | if __name__ == '__main__':
|
---|
234 | main(sys.argv[1:])
|
---|