VirtualBox

source: vbox/trunk/doc/manual/htmlhelp-qthelp.py@ 98570

Last change on this file since 98570 was 98103, checked in by vboxsync, 20 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to LF
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 9.4 KB
Line 
1#!/usr/bin/python3
2
3# $Id: htmlhelp-qthelp.py 98103 2023-01-17 14:15:46Z vboxsync $
4## @file
5# A python script to create a .qhp file out of a given htmlhelp
6# folder. Lots of things about the said folder is assumed. Please
7# see the code and inlined comments.
8
9import sys, getopt
10import os.path
11import re
12import codecs
13import logging
14
15if sys.version_info >= (3, 0):
16 from html.parser import HTMLParser
17else:
18 from HTMLParser import HTMLParser
19
20
21__copyright__ = \
22"""
23Copyright (C) 2006-2023 Oracle and/or its affiliates.
24
25This file is part of VirtualBox base platform packages, as
26available from https://www.virtualbox.org.
27
28This program is free software; you can redistribute it and/or
29modify it under the terms of the GNU General Public License
30as published by the Free Software Foundation, in version 3 of the
31License.
32
33This program is distributed in the hope that it will be useful, but
34WITHOUT ANY WARRANTY; without even the implied warranty of
35MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
36General Public License for more details.
37
38You should have received a copy of the GNU General Public License
39along with this program; if not, see <https://www.gnu.org/licenses>.
40
41SPDX-License-Identifier: GPL-3.0-only
42"""
43
44# number of opened and not yet closed section tags of toc section
45open_section_tags = 0
46
47html_files = []
48
49# use html_parser stuff to collect <a name tags
50def create_keywords_section(folder):
51 keywords_section_lines = ['<keywords>']
52 for html_file_name in html_files:
53 full_html_path = os.path.join(folder, html_file_name)
54 file_content = codecs.open(full_html_path, encoding='iso-8859-1').read()
55
56 class html_parser(HTMLParser):
57 def __init__(self):
58 HTMLParser.__init__(self)
59 self.a_tag=[]
60 def handle_starttag(self, tag, attributes):
61 if tag != 'div' and tag != 'a':
62 return
63 if tag == 'a':
64 for a in attributes:
65 if a[0] == 'name':
66 self.a_tag.append(a[1])
67
68 parser = html_parser()
69 parser.feed(file_content)
70 for k in parser.a_tag:
71 line = '<keyword name="' + k + '" id="' + k + '" ref="' + html_file_name + '#' + k + '"/>'
72 keywords_section_lines.append(line);
73 keywords_section_lines.append('</keywords>')
74 return keywords_section_lines
75
76# find the png files under /images folder and create a part of the
77# qhelp project file with <file> tags
78def create_image_list(folder):
79 image_folder_name = 'images'
80 image_files_list = []
81 # Look for 'images' sub folder
82 subdirs = [x[0] for x in os.walk(folder)]
83 full_folder_path = os.path.join(folder, image_folder_name)
84 if full_folder_path not in subdirs:
85 logging.error('Image subfolder "%s" is not found under "%s".', image_folder_name, folder)
86 return image_files_list;
87 png_files = []
88 for f in os.listdir(full_folder_path):
89 png_files.append(image_folder_name + '/' + f)
90 image_files_list.append('<file>images/' + f + '</file>')
91 return image_files_list
92
93# open htmlhelp.hhp files and read the list of html files from there
94def create_html_list(folder):
95 global html_files
96 file_name = 'htmlhelp.hhp'
97 html_file_lines = []
98 if not file_name in os.listdir(folder):
99 logging.error('Could not find the file "%s" in "%s"', file_name, folder)
100 return html_file_lines
101 full_path = os.path.join(folder, 'htmlhelp.hhp')
102 file = codecs.open(full_path, encoding='iso-8859-1')
103
104 lines = file.readlines()
105 file.close()
106 # first search for the [FILES] marker then collect .html lines
107 marker_found = 0
108 for line in lines:
109 if '[FILES]' in line:
110 marker_found = 1
111 continue
112 if marker_found == 0:
113 continue
114 if '.html' in line:
115 html_file_lines.append('<file>' + line.strip('\n') + '</file>')
116 html_files.append(line.strip('\n'))
117 return html_file_lines
118
119
120def create_files_section(folder):
121 files_section_lines = ['<files>']
122 files_section_lines += create_image_list(folder)
123 files_section_lines += create_html_list(folder)
124 files_section_lines.append('</files>')
125 return files_section_lines
126
127def parse_param_tag(line):
128 label = 'value="'
129 start = line.find(label);
130 if start == -1:
131 return ''
132 start += len(label)
133 end = line.find('"', start)
134 if end == -1:
135 return '';
136 return line[start:end]
137
138# look at next two lines. they are supposed to look like the following
139# <param name="Name" value="Oracle VM VirtualBox">
140# <param name="Local" value="index.html">
141# parse out value fields and return
142# title="Oracle VM VirtualBox" ref="index.html
143def parse_object_tag(lines, index):
144 result=''
145 if index + 2 > len(lines):
146 logging.warning('Not enough tags after this one "%s"',lines[index])
147 return result
148 if not re.match(r'^\s*<param', lines[index + 1], re.IGNORECASE) or \
149 not re.match(r'^\s*<param', lines[index + 2], re.IGNORECASE):
150 logging.warning('Skipping the line "%s" since next two tags are supposed to be param tags', lines[index])
151 return result
152 title = parse_param_tag(lines[index + 1])
153 ref = parse_param_tag(lines[index + 2])
154 global open_section_tags
155 if title and ref:
156 open_section_tags += 1
157 result = '<section title="' + title + '" ref="' + ref + '">'
158 else:
159 logging.warning('Title or ref part is empty for the tag "%s"', lines[index])
160 return result
161
162# parse any string other than staring with <OBJECT
163# decide if <session tag should be closed
164def parse_non_object_tag(lines, index):
165 if index + 1 > len(lines):
166 return ''
167 global open_section_tags
168 if open_section_tags <= 0:
169 return ''
170 # replace </OBJECT with </section only if the next tag is not <UL
171 if re.match(r'^\s*</OBJECT', lines[index], re.IGNORECASE):
172 if not re.match(r'^\s*<UL', lines[index + 1], re.IGNORECASE):
173 open_section_tags -= 1
174 return '</section>'
175 elif re.match(r'^\s*</UL', lines[index], re.IGNORECASE):
176 open_section_tags -= 1
177 return '</section>'
178 return ''
179
180def parse_line(lines, index):
181 result=''
182
183 # if the line starts with <OBJECT
184 if re.match(r'^\s*<OBJECT', lines[index], re.IGNORECASE):
185 result = parse_object_tag(lines, index)
186 else:
187 result = parse_non_object_tag(lines, index)
188 return result
189
190# parse toc.hhc file. assuming all the relevant information
191# is stored in tags and attributes. whatever is outside of
192# <... > pairs is filtered out. we also assume < ..> are not nested
193# and each < matches to a >
194def create_toc(folder):
195 toc_file = 'toc.hhc'
196 content = [x[2] for x in os.walk(folder)]
197 if toc_file not in content[0]:
198 logging.error('Could not find toc file "%s" under "%s"', toc_file, folder)
199 return
200 full_path = os.path.join(folder, toc_file)
201 file = codecs.open(full_path, encoding='iso-8859-1')
202 content = file.read()
203 file.close()
204 # convert the file string into a list of tags there by eliminating whatever
205 # char reside outside of tags.
206 char_pos = 0
207 tag_list = []
208 while char_pos < len(content):
209 start = content.find('<', char_pos)
210 if start == -1:
211 break
212 end = content.find('>', start)
213 if end == -1 or end >= len(content) - 1:
214 break
215 char_pos = end
216 tag_list.append(content[start:end +1])
217
218 # # insert new line chars. to make sure each line includes at most one tag
219 # content = re.sub(r'>.*?<', r'>\n<', content)
220 # lines = content.split('\n')
221 toc_string_list = ['<toc>']
222 index = 0
223 for tag in tag_list:
224 str = parse_line(tag_list, index)
225 if str:
226 toc_string_list.append(str)
227 index += 1
228 toc_string_list.append('</toc>')
229 toc_string = '\n'.join(toc_string_list)
230
231 return toc_string_list
232
233def usage(arg):
234 print('htmlhelp-qthelp.py -d <helphtmlfolder> -o <outputfilename>')
235 sys.exit()
236
237def main(argv):
238 helphtmlfolder = ''
239 output_filename = ''
240 try:
241 opts, args = getopt.getopt(sys.argv[1:],"hd:o:")
242 except getopt.GetoptError as err:
243 print(err)
244 usage(2)
245 for opt, arg in opts:
246 if opt == '-h':
247 usage(0)
248 elif opt in ("-d"):
249 helphtmlfolder = arg
250 elif opt in ("-o"):
251 output_filename = arg
252
253 # check supplied helphtml folder argument
254 if not helphtmlfolder:
255 logging.error('No helphtml folder is provided. Exiting')
256 usage(2)
257 if not os.path.exists(helphtmlfolder):
258 logging.error('folder "%s" does not exist. Exiting', helphtmlfolder)
259 usage(2)
260 helphtmlfolder = os.path.normpath(helphtmlfolder)
261
262 # check supplied output file name
263 if not output_filename:
264 logging.error('No filename for output is given. Exiting')
265 usage(2)
266
267 out_xml_lines = ['<?xml version="1.0" encoding="UTF-8"?>', \
268 '<QtHelpProject version="1.0">' , \
269 '<namespace>org.virtualbox</namespace>', \
270 '<virtualFolder>doc</virtualFolder>', \
271 '<filterSection>']
272 out_xml_lines += create_toc(helphtmlfolder) + create_files_section(helphtmlfolder)
273 out_xml_lines += create_keywords_section(helphtmlfolder)
274 out_xml_lines += ['</filterSection>', '</QtHelpProject>']
275
276 out_file = open(output_filename, 'wb')
277 out_file.write('\n'.join(out_xml_lines).encode('utf8'))
278 out_file.close()
279
280if __name__ == '__main__':
281 main(sys.argv[1:])
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette