VirtualBox

source: vbox/trunk/doc/manual/add_file_to_id_only_references.py@ 107399

Last change on this file since 107399 was 107399, checked in by vboxsync, 4 weeks ago

fix for r166433, fixed typo made in r166447

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.2 KB
Line 
1#!/usr/bin/env python3
2__copyright__ = \
3"""
4Copyright (C) 2023-2024 Oracle and/or its affiliates.
5
6This file is part of VirtualBox base platform packages, as
7available from https://www.virtualbox.org.
8
9This program is free software; you can redistribute it and/or
10modify it under the terms of the GNU General Public License
11as published by the Free Software Foundation, in version 3 of the
12License.
13
14This program is distributed in the hope that it will be useful, but
15WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, see <https://www.gnu.org/licenses>.
21
22SPDX-License-Identifier: GPL-3.0-only
23"""
24
25# Standard python imports.
26import glob;
27import os;
28import re;
29import sys;
30
31
32g_oReHref = re.compile(r'\bhref=("[^">#./]+"|\'[^\'>#./]+\')');
33
34def modifyDitaFile(dIdToFile, sContent):
35 """
36 Modifies the href attributes in this file.
37 """
38 current_file_is_refentry = False
39 if any(current_file_name.lower() in s.lower() for s in refentry_files):
40 current_file_is_refentry = True
41 sModified = '';
42 offPrev = 0;
43 for oMatch in g_oReHref.finditer(sContent):
44 sId = oMatch.group(1)[1:-1];
45 if sId in dIdToFile:
46 reference_is_refentry = False
47 if any(os.path.splitext(dIdToFile[sId])[0].lower() in s.lower() for s in refentry_files):
48 reference_is_refentry = True
49 if current_file_is_refentry and not reference_is_refentry:
50 sModified += sContent[offPrev : oMatch.start(1)] + '"../topics/' + dIdToFile[sId] + '#' + sId + '"';
51 else:
52 sModified += sContent[offPrev : oMatch.start(1)] + '"' + dIdToFile[sId] + '#' + sId + '"';
53 if sId == "vboxmanage-modifyvm-vrde":
54 print("========*******=======")
55 print(reference_is_refentry)
56 print(current_file_name)
57 print(current_file_is_refentry)
58 print(dIdToFile[sId])
59 print("ppppppp %s" % (sId,))
60 print(refentry_files)
61 print("========xxxxxxx=======")
62
63 offPrev = oMatch.end(1);
64 if offPrev < len(sContent):
65 sModified += sContent[offPrev:];
66 return sModified;
67
68def info(sMessage):
69 """ Info message. """
70 print('add_file_to_id_only_references.py: info: %s' % sMessage);
71 return 1;
72
73def error(sMessage):
74 """ Reports an error. """
75 print('add_file_to_id_only_references.py: error: %s' % sMessage, file = sys.stderr);
76 return 1;
77
78def syntax(sMessage):
79 """ Reports a syntax error. """
80 print('add_file_to_id_only_references.py: syntax error: %s' % sMessage, file = sys.stderr);
81 return 2;
82
83def usage():
84 """ Reports usage. """
85 print('usage: add_file_to_id_only_references.py [--verbose|--quiet] --mapping-file <map.db> file1.dita [file2.dita [...]]');
86 return 0;
87
88def main(asArgs):
89 dIdToFile = None;
90 fEndOfArgs = False;
91 fVerbose = False;
92 iArg = 1;
93 while iArg < len(asArgs):
94 sArg = asArgs[iArg];
95 if sArg[0] == '-' and not fEndOfArgs:
96 # Options.
97 if sArg == '--':
98 fEndOfArgs = True;
99 elif sArg in ('--help', '-h', '-?'):
100 return usage();
101 elif sArg in ('--version', '-V' ):
102 print(__version__[__version__.find(':') + 2:-2]);
103 elif sArg in ('--quiet', '-q' ):
104 fVerbose = False;
105 elif sArg in ('--verbose', '-v' ):
106 fVerbose = True;
107 elif sArg in ('--refentry_file_list', '-f' ):
108 iArg += 1;
109 if iArg >= len(asArgs):
110 return syntax('Expected a list of refentry files following "--refentry_file_list"!');
111 global refentry_files
112 refentry_files = asArgs[iArg].split(' ')
113 elif sArg in ('--mapping-file', '-m'):
114 iArg += 1;
115 if iArg >= len(asArgs):
116 return syntax('Expected filename following "--mapping-file"!');
117 # Load the database file.
118 sArg = asArgs[iArg];
119 try:
120 with open(sArg, 'r', encoding = 'utf-8') as oFile:
121 dIdToFile = {};
122 for sLine in oFile:
123 sId, sFile = sLine.split('=');
124 dIdToFile[sId.strip()] = sFile.strip();
125 except Exception as oXcpt: # pylint: disable=broad-exception-caught
126 return error('Failed to open and parse "%s": %s' % (sArg, oXcpt,));
127 if fVerbose:
128 info('Loaded %s IDs from "%s"' % (len(dIdToFile), sArg));
129 else:
130 return syntax('Unknown option: %s' % (sArg,));
131 else:
132 # File to modify.
133 if dIdToFile is None:
134 return syntax('A mapping database must be given before any other files!');
135
136 try:
137 with open(sArg, 'r', encoding = 'utf-8') as oFile:
138 sContent = oFile.read();
139 except Exception as oXcpt: # pylint: disable=broad-exception-caught
140 return error('Failed to open and read "%s": %s' % (sArg, oXcpt,));
141 global current_file_name
142 current_file_name = os.path.splitext(os.path.basename(sArg))[0]
143 prefix = "flat-"
144 if current_file_name.startswith(prefix):
145 current_file_name = current_file_name[len(prefix):]
146 sModified = modifyDitaFile(dIdToFile, sContent);
147 if sModified != sContent:
148 if fVerbose:
149 info('Writing out modified "%s"...' % (sArg,));
150 try:
151 with open(sArg, 'w', encoding = 'utf-8') as oFile:
152 oFile.write(sModified);
153 except Exception as oXcpt: # pylint: disable=broad-exception-caught
154 return error('Failed to open and write back "%s": %s' % (sArg, oXcpt,));
155 elif fVerbose:
156 info('No changes to "%s"...' % (sArg,));
157
158 iArg += 1;
159 return 0;
160
161if __name__ == "__main__":
162 main(sys.argv)
163
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