VirtualBox

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

Last change on this file since 98865 was 98865, checked in by vboxsync, 15 months ago

Docs: bugref:10302. Don't use iso-8859-1 encoding.

  • Property svn:eol-style set to LF
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 9.6 KB
Line 
1#!/usr/bin/python3
2
3# $Id: htmlhelp-qthelp.py 98865 2023-03-07 16:08:56Z 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
22__copyright__ = \
23"""
24Copyright (C) 2006-2023 Oracle and/or its affiliates.
25
26This file is part of VirtualBox base platform packages, as
27available from https://www.virtualbox.org.
28
29This program is free software; you can redistribute it and/or
30modify it under the terms of the GNU General Public License
31as published by the Free Software Foundation, in version 3 of the
32License.
33
34This program is distributed in the hope that it will be useful, but
35WITHOUT ANY WARRANTY; without even the implied warranty of
36MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
37General Public License for more details.
38
39You should have received a copy of the GNU General Public License
40along with this program; if not, see <https://www.gnu.org/licenses>.
41
42SPDX-License-Identifier: GPL-3.0-only
43"""
44
45# number of opened and not yet closed section tags of toc section
46open_section_tags = 0
47
48html_files = []
49
50# use html_parser stuff to collect <a name tags
51def create_keywords_section(folder):
52 keywords_section_lines = ['<keywords>']
53 for html_file_name in html_files:
54 full_html_path = os.path.join(folder, html_file_name)
55 file_content = codecs.open(full_html_path, encoding='utf-8').read()
56
57 class html_parser(HTMLParser):
58 def __init__(self):
59 HTMLParser.__init__(self)
60 self.a_tag=[]
61 def handle_starttag(self, tag, attributes):
62 if tag != 'div' and tag != 'a':
63 return
64 if tag == 'a':
65 for a in attributes:
66 if a[0] == 'name':
67 self.a_tag.append(a[1])
68
69 parser = html_parser()
70 parser.feed(file_content)
71 for k in parser.a_tag:
72 line = '<keyword name="' + k + '" id="' + k + '" ref="' + html_file_name + '#' + k + '"/>'
73 keywords_section_lines.append(line);
74 keywords_section_lines.append('</keywords>')
75 return keywords_section_lines
76
77# find the png files under /images folder and create a part of the
78# qhelp project file with <file> tags
79def create_image_list(folder):
80 image_folder_name = 'images'
81 image_files_list = []
82 # Look for 'images' sub folder
83 subdirs = [x[0] for x in os.walk(folder)]
84 full_folder_path = os.path.join(folder, image_folder_name)
85 if full_folder_path not in subdirs:
86 logging.error('Image subfolder "%s" is not found under "%s".', image_folder_name, folder)
87 return image_files_list;
88 png_files = []
89 for f in os.listdir(full_folder_path):
90 png_files.append(image_folder_name + '/' + f)
91 image_files_list.append('<file>images/' + f + '</file>')
92 return image_files_list
93
94# open files list and read the list of html files from there
95def create_html_list(folder, list_file):
96 global html_files
97 html_file_lines = []
98 if not list_file in os.listdir(folder):
99 logging.error('Could not find the file "%s" in "%s"', list_file, folder)
100 return html_file_lines
101 full_path = os.path.join(folder, list_file)
102 file = codecs.open(full_path, encoding='utf-8')
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, list_file):
121 files_section_lines = ['<files>']
122 files_section_lines += create_image_list(folder)
123 files_section_lines += create_html_list(folder, list_file)
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 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, toc_file):
195 toc_string_list = []
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 toc_string_list
200 full_path = os.path.join(folder, toc_file)
201 file = codecs.open(full_path, encoding='utf-8')
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.append('<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 list_file = ''
241 toc_file = ''
242 try:
243 opts, args = getopt.getopt(sys.argv[1:],"hd:o:f:t:")
244 except getopt.GetoptError as err:
245 print(err)
246 usage(2)
247 for opt, arg in opts:
248 if opt == '-h':
249 usage(0)
250 elif opt in ("-d"):
251 helphtmlfolder = arg
252 print(helphtmlfolder)
253 elif opt in ("-f"):
254 list_file = arg
255 elif opt in ("-t"):
256 toc_file = arg
257 print(toc_file)
258 elif opt in ("-o"):
259 output_filename = arg
260
261 # check supplied helphtml folder argument
262 if not helphtmlfolder:
263 logging.error('No helphtml folder is provided. Exiting')
264 usage(2)
265 if not os.path.exists(helphtmlfolder):
266 logging.error('folder "%s" does not exist. Exiting', helphtmlfolder)
267 usage(2)
268 helphtmlfolder = os.path.normpath(helphtmlfolder)
269
270 # check supplied output file name
271 if not output_filename:
272 logging.error('No filename for output is given. Exiting')
273 usage(2)
274
275 out_xml_lines = ['<?xml version="1.0" encoding="UTF-8"?>', \
276 '<QtHelpProject version="1.0">' , \
277 '<namespace>org.virtualbox</namespace>', \
278 '<virtualFolder>doc</virtualFolder>', \
279 '<filterSection>']
280 out_xml_lines += create_toc(helphtmlfolder, toc_file) + create_files_section(helphtmlfolder, list_file)
281 out_xml_lines += create_keywords_section(helphtmlfolder)
282 out_xml_lines += ['</filterSection>', '</QtHelpProject>']
283
284 out_file = open(output_filename, 'wb')
285 out_file.write('\n'.join(out_xml_lines).encode('utf8'))
286 out_file.close()
287
288if __name__ == '__main__':
289 main(sys.argv[1:])
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use