VirtualBox

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

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

Docs: bugref:10302. Do not use obsolete codecs module for file open. Now that open() can take encoding parameter.

  • 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 98950 2023-03-14 09:00:33Z 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 logging
13
14if sys.version_info >= (3, 0):
15 from html.parser import HTMLParser
16else:
17 from HTMLParser import HTMLParser
18
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 = open(full_html_path, encoding='utf-8').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 files list and read the list of html files from there
94def create_html_list(folder, list_file):
95 global html_files
96 html_file_lines = []
97 if not list_file in os.listdir(folder):
98 logging.error('Could not find the file "%s" in "%s"', list_file, folder)
99 return html_file_lines
100 full_path = os.path.join(folder, list_file)
101 file = open(full_path, encoding='utf-8')
102
103 lines = file.readlines()
104 file.close()
105 # first search for the [FILES] marker then collect .html lines
106 marker_found = 0
107 for line in lines:
108 if '[FILES]' in line:
109 marker_found = 1
110 continue
111 if marker_found == 0:
112 continue
113 if '.html' in line:
114 html_file_lines.append('<file>' + line.strip('\n') + '</file>')
115 html_files.append(line.strip('\n'))
116 return html_file_lines
117
118
119def create_files_section(folder, list_file):
120 files_section_lines = ['<files>']
121 files_section_lines += create_image_list(folder)
122 files_section_lines += create_html_list(folder, list_file)
123 files_section_lines.append('</files>')
124 return files_section_lines
125
126def parse_param_tag(line):
127 label = 'value="'
128 start = line.find(label);
129 if start == -1:
130 return ''
131 start += len(label)
132 end = line.find('"', start)
133 if end == -1:
134 return '';
135 return line[start:end]
136
137# look at next two lines. they are supposed to look like the following
138# <param name="Name" value="Oracle VM VirtualBox">
139# <param name="Local" value="index.html">
140# parse out value fields and return
141# title="Oracle VM VirtualBox" ref="index.html
142def parse_object_tag(lines, index):
143 result=''
144 if index + 2 > len(lines):
145 logging.warning('Not enough tags after this one "%s"',lines[index])
146 return result
147 if not re.match(r'^\s*<param', lines[index + 1], re.IGNORECASE) or \
148 not re.match(r'^\s*<param', lines[index + 2], re.IGNORECASE):
149 logging.warning('Skipping the line "%s" since next two tags are supposed to be param tags', lines[index])
150 return result
151 title = parse_param_tag(lines[index + 1])
152 ref = parse_param_tag(lines[index + 2])
153 global open_section_tags
154 if title and ref:
155 open_section_tags += 1
156 result = '<section title="' + title + '" ref="' + ref + '">'
157 else:
158 logging.warning('Title or ref part is empty for the tag "%s"', lines[index])
159 return result
160
161# parse any string other than staring with <OBJECT
162# decide if <session tag should be closed
163def parse_non_object_tag(lines, index):
164 if index + 1 > len(lines):
165 return ''
166 global open_section_tags
167 if open_section_tags <= 0:
168 return ''
169 # replace </OBJECT with </section only if the next tag is not <UL
170 if re.match(r'^\s*</OBJECT', lines[index], re.IGNORECASE):
171 if not re.match(r'^\s*<UL', lines[index + 1], re.IGNORECASE):
172 open_section_tags -= 1
173 return '</section>'
174 elif re.match(r'^\s*</UL', lines[index], re.IGNORECASE):
175 open_section_tags -= 1
176 return '</section>'
177 return ''
178
179def parse_line(lines, index):
180 result=''
181
182 # if the line starts with <OBJECT
183 if re.match(r'^\s*<OBJECT', lines[index], re.IGNORECASE):
184 result = parse_object_tag(lines, index)
185 else:
186 result = parse_non_object_tag(lines, index)
187 return result
188
189# parse TOC file. assuming all the relevant information
190# is stored in tags and attributes. whatever is outside of
191# <... > pairs is filtered out. we also assume < ..> are not nested
192# and each < matches to a >
193def create_toc(folder, toc_file):
194 toc_string_list = []
195 content = [x[2] for x in os.walk(folder)]
196 if toc_file not in content[0]:
197 logging.error('Could not find toc file "%s" under "%s"', toc_file, folder)
198 return toc_string_list
199 full_path = os.path.join(folder, toc_file)
200 file = open(full_path, encoding='utf-8')
201 content = file.read()
202 file.close()
203 # convert the file string into a list of tags there by eliminating whatever
204 # char reside outside of tags.
205 char_pos = 0
206 tag_list = []
207 while char_pos < len(content):
208 start = content.find('<', char_pos)
209 if start == -1:
210 break
211 end = content.find('>', start)
212 if end == -1 or end >= len(content) - 1:
213 break
214 char_pos = end
215 tag_list.append(content[start:end +1])
216
217 # # insert new line chars. to make sure each line includes at most one tag
218 # content = re.sub(r'>.*?<', r'>\n<', content)
219 # lines = content.split('\n')
220 toc_string_list.append('<toc>')
221 index = 0
222 for tag in tag_list:
223 str = parse_line(tag_list, index)
224 if str:
225 toc_string_list.append(str)
226 index += 1
227 toc_string_list.append('</toc>')
228 toc_string = '\n'.join(toc_string_list)
229
230 return toc_string_list
231
232def usage(arg):
233 print('htmlhelp-qthelp.py -d <helphtmlfolder> -o <outputfilename>')
234 sys.exit()
235
236def main(argv):
237 helphtmlfolder = ''
238 output_filename = ''
239 list_file = ''
240 toc_file = ''
241 try:
242 opts, args = getopt.getopt(sys.argv[1:],"hd:o:f:t:")
243 except getopt.GetoptError as err:
244 print(err)
245 usage(2)
246 for opt, arg in opts:
247 if opt == '-h':
248 usage(0)
249 elif opt in ("-d"):
250 helphtmlfolder = arg
251 print(helphtmlfolder)
252 elif opt in ("-f"):
253 list_file = arg
254 elif opt in ("-t"):
255 toc_file = arg
256 print(toc_file)
257 elif opt in ("-o"):
258 output_filename = arg
259 # check supplied helphtml folder argument
260 if not helphtmlfolder:
261 logging.error('No helphtml folder is provided. Exiting')
262 usage(2)
263 if not os.path.exists(helphtmlfolder):
264 logging.error('folder "%s" does not exist. Exiting', helphtmlfolder)
265 usage(2)
266 helphtmlfolder = os.path.normpath(helphtmlfolder)
267
268 # check supplied output file name
269 if not output_filename:
270 logging.error('No filename for output is given. Exiting')
271 usage(2)
272
273 out_xml_lines = ['<?xml version="1.0" encoding="UTF-8"?>', \
274 '<QtHelpProject version="1.0">' , \
275 '<namespace>org.virtualbox</namespace>', \
276 '<virtualFolder>doc</virtualFolder>', \
277 '<filterSection>']
278 out_xml_lines += create_toc(helphtmlfolder, toc_file) + create_files_section(helphtmlfolder, list_file)
279 out_xml_lines += create_keywords_section(helphtmlfolder)
280 out_xml_lines += ['</filterSection>', '</QtHelpProject>']
281
282 out_file = open(output_filename, 'wb')
283 out_file.write('\n'.join(out_xml_lines).encode('utf8'))
284 out_file.close()
285
286if __name__ == '__main__':
287 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