VirtualBox

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

Last change on this file since 86670 was 86670, checked in by vboxsync, 4 years ago

FE/Qt: bugref:9831. Adding a python 2.7 to create a .qhp file out of a htmhelp folder.

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