VirtualBox

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

Last change on this file since 103131 was 99038, checked in by vboxsync, 21 months ago

manual: Got rid of the correct_reference_targets Makefile-file rule, the new tactic is to scan the checked in .dita-files and .xml-files, store the id->filename mappings in file, and apply them as part of the refentry->dita conversion recipe for each individual Docbook-file. Use pattern rules for copying files to the staging area. bugref:10302

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.0 KB
Line 
1# -*- coding: utf-8 -*-
2# $Id: add_file_to_id_only_references.py 99038 2023-03-18 05:03:58Z vboxsync $
3
4"""
5Makes id-only reference in the given file into dita-compliant file#id
6references, using the mapping database generated by build_id_to_file_mapping.py.
7"""
8
9__copyright__ = \
10"""
11Copyright (C) 2023 Oracle and/or its affiliates.
12
13This file is part of VirtualBox base platform packages, as
14available from https://www.virtualbox.org.
15
16This program is free software; you can redistribute it and/or
17modify it under the terms of the GNU General Public License
18as published by the Free Software Foundation, in version 3 of the
19License.
20
21This program is distributed in the hope that it will be useful, but
22WITHOUT ANY WARRANTY; without even the implied warranty of
23MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24General Public License for more details.
25
26You should have received a copy of the GNU General Public License
27along with this program; if not, see <https://www.gnu.org/licenses>.
28
29SPDX-License-Identifier: GPL-3.0-only
30"""
31__version__ = "$Revision: 99038 $"
32
33
34# Standard python imports.
35import glob;
36import os;
37import re;
38import sys;
39
40
41g_oReHref = re.compile(r'\bhref=("[^">#./]+"|\'[^\'>#./]+\')');
42
43def 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
58def info(sMessage):
59 """ Info message. """
60 print('add_file_to_id_only_references.py: info: %s' % sMessage);
61 return 1;
62
63def 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
68def 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
73def 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
78def 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
148if __name__ == "__main__":
149 sys.exit(main(sys.argv));
150
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