1 | #!/usr/bin/env python3
|
---|
2 |
|
---|
3 | # Standard python imports.
|
---|
4 | import glob;
|
---|
5 | import os;
|
---|
6 | import re;
|
---|
7 | import sys;
|
---|
8 |
|
---|
9 |
|
---|
10 | g_oReHref = re.compile(r'\bhref=("[^">#./]+"|\'[^\'>#./]+\')');
|
---|
11 |
|
---|
12 | def modifyDitaFile(dIdToFile, sContent):
|
---|
13 | """
|
---|
14 | Modifies the href attributes in this file.
|
---|
15 | """
|
---|
16 | current_file_is_refentry = False
|
---|
17 | if any(current_file_name.lower() in s.lower() for s in refentry_files):
|
---|
18 | current_file_is_refentry = True
|
---|
19 | sModified = '';
|
---|
20 | offPrev = 0;
|
---|
21 | for oMatch in g_oReHref.finditer(sContent):
|
---|
22 | sId = oMatch.group(1)[1:-1];
|
---|
23 | if sId in dIdToFile:
|
---|
24 | reference_is_refentry = False
|
---|
25 | if any(os.path.splitext(dIdToFile[sId])[0].lower() in s.lower() for s in refentry_files):
|
---|
26 | reference_is_refentry = True
|
---|
27 | if current_file_is_refentry and not reference_is_refentry:
|
---|
28 | sModified += sContent[offPrev : oMatch.start(1)] + '"../topics/' + dIdToFile[sId] + '#' + sId + '"';
|
---|
29 | else:
|
---|
30 | sModified += sContent[offPrev : oMatch.start(1)] + '"' + dIdToFile[sId] + '#' + sId + '"';
|
---|
31 | if sId == "vboxmanage-modifyvm-vrde":
|
---|
32 | print("========*******=======")
|
---|
33 | print(reference_is_refentry)
|
---|
34 | print(current_file_name)
|
---|
35 | print(current_file_is_refentry)
|
---|
36 | print(dIdToFile[sId])
|
---|
37 | print(f"ppppppp{sId}")
|
---|
38 | print(refentry_files)
|
---|
39 | print("========xxxxxxx=======")
|
---|
40 |
|
---|
41 | offPrev = oMatch.end(1);
|
---|
42 | if offPrev < len(sContent):
|
---|
43 | sModified += sContent[offPrev:];
|
---|
44 | return sModified;
|
---|
45 |
|
---|
46 | def info(sMessage):
|
---|
47 | """ Info message. """
|
---|
48 | print('add_file_to_id_only_references.py: info: %s' % sMessage);
|
---|
49 | return 1;
|
---|
50 |
|
---|
51 | def error(sMessage):
|
---|
52 | """ Reports an error. """
|
---|
53 | print('add_file_to_id_only_references.py: error: %s' % sMessage, file = sys.stderr);
|
---|
54 | return 1;
|
---|
55 |
|
---|
56 | def syntax(sMessage):
|
---|
57 | """ Reports a syntax error. """
|
---|
58 | print('add_file_to_id_only_references.py: syntax error: %s' % sMessage, file = sys.stderr);
|
---|
59 | return 2;
|
---|
60 |
|
---|
61 | def usage():
|
---|
62 | """ Reports usage. """
|
---|
63 | print('usage: add_file_to_id_only_references.py [--verbose|--quiet] --mapping-file <map.db> file1.dita [file2.dita [...]]');
|
---|
64 | return 0;
|
---|
65 |
|
---|
66 | def main(asArgs):
|
---|
67 | dIdToFile = None;
|
---|
68 | fEndOfArgs = False;
|
---|
69 | fVerbose = False;
|
---|
70 | iArg = 1;
|
---|
71 | while iArg < len(asArgs):
|
---|
72 | sArg = asArgs[iArg];
|
---|
73 | if sArg[0] == '-' and not fEndOfArgs:
|
---|
74 | # Options.
|
---|
75 | if sArg == '--':
|
---|
76 | fEndOfArgs = True;
|
---|
77 | elif sArg in ('--help', '-h', '-?'):
|
---|
78 | return usage();
|
---|
79 | elif sArg in ('--version', '-V' ):
|
---|
80 | print(__version__[__version__.find(':') + 2:-2]);
|
---|
81 | elif sArg in ('--quiet', '-q' ):
|
---|
82 | fVerbose = False;
|
---|
83 | elif sArg in ('--verbose', '-v' ):
|
---|
84 | fVerbose = True;
|
---|
85 | elif sArg in ('--refentry_file_list', '-f' ):
|
---|
86 | iArg += 1;
|
---|
87 | if iArg >= len(asArgs):
|
---|
88 | return syntax('Expected a list of refentry files following "--refentry_file_list"!');
|
---|
89 | global refentry_files
|
---|
90 | refentry_files = asArgs[iArg].split(' ')
|
---|
91 | elif sArg in ('--mapping-file', '-m'):
|
---|
92 | iArg += 1;
|
---|
93 | if iArg >= len(asArgs):
|
---|
94 | return syntax('Expected filename following "--mapping-file"!');
|
---|
95 | # Load the database file.
|
---|
96 | sArg = asArgs[iArg];
|
---|
97 | try:
|
---|
98 | with open(sArg, 'r', encoding = 'utf-8') as oFile:
|
---|
99 | dIdToFile = {};
|
---|
100 | for sLine in oFile:
|
---|
101 | sId, sFile = sLine.split('=');
|
---|
102 | dIdToFile[sId.strip()] = sFile.strip();
|
---|
103 | except Exception as oXcpt: # pylint: disable=broad-exception-caught
|
---|
104 | return error('Failed to open and parse "%s": %s' % (sArg, oXcpt,));
|
---|
105 | if fVerbose:
|
---|
106 | info('Loaded %s IDs from "%s"' % (len(dIdToFile), sArg));
|
---|
107 | else:
|
---|
108 | return syntax('Unknown option: %s' % (sArg,));
|
---|
109 | else:
|
---|
110 | # File to modify.
|
---|
111 | if dIdToFile is None:
|
---|
112 | return syntax('A mapping database must be given before any other files!');
|
---|
113 |
|
---|
114 | try:
|
---|
115 | with open(sArg, 'r', encoding = 'utf-8') as oFile:
|
---|
116 | sContent = oFile.read();
|
---|
117 | except Exception as oXcpt: # pylint: disable=broad-exception-caught
|
---|
118 | return error('Failed to open and read "%s": %s' % (sArg, oXcpt,));
|
---|
119 | global current_file_name
|
---|
120 | current_file_name = os.path.splitext(os.path.basename(sArg))[0]
|
---|
121 | prefix = "flat-"
|
---|
122 | if current_file_name.startswith(prefix):
|
---|
123 | current_file_name = current_file_name[len(prefix):]
|
---|
124 | sModified = modifyDitaFile(dIdToFile, sContent);
|
---|
125 | if sModified != sContent:
|
---|
126 | if fVerbose:
|
---|
127 | info('Writing out modified "%s"...' % (sArg,));
|
---|
128 | try:
|
---|
129 | with open(sArg, 'w', encoding = 'utf-8') as oFile:
|
---|
130 | oFile.write(sModified);
|
---|
131 | except Exception as oXcpt: # pylint: disable=broad-exception-caught
|
---|
132 | return error('Failed to open and write back "%s": %s' % (sArg, oXcpt,));
|
---|
133 | elif fVerbose:
|
---|
134 | info('No changes to "%s"...' % (sArg,));
|
---|
135 |
|
---|
136 | iArg += 1;
|
---|
137 | return 0;
|
---|
138 |
|
---|
139 | if __name__ == "__main__":
|
---|
140 | main(sys.argv)
|
---|
141 |
|
---|