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