1 | #!/usr/bin/env python3
|
---|
2 | import glob
|
---|
3 | import getopt
|
---|
4 | import os
|
---|
5 | import sys
|
---|
6 | import shutil
|
---|
7 | import logging
|
---|
8 |
|
---|
9 | """
|
---|
10 | correct_references.py:
|
---|
11 | This script scans all .dita files found in the as-argument-passed folder
|
---|
12 | and creates a dictionary of topic id and file names. This is done since
|
---|
13 | DITA expects href targets in the form of filename#topic-id. Then all the
|
---|
14 | href targets found in man_VBoxManage... files are prefixed with files names.
|
---|
15 | """
|
---|
16 |
|
---|
17 | __copyright__ = \
|
---|
18 | """
|
---|
19 | Copyright (C) 2009-2023 Oracle and/or its affiliates.
|
---|
20 |
|
---|
21 | This file is part of VirtualBox base platform packages, as
|
---|
22 | available from https://www.virtualbox.org.
|
---|
23 |
|
---|
24 | This program is free software; you can redistribute it and/or
|
---|
25 | modify it under the terms of the GNU General Public License
|
---|
26 | as published by the Free Software Foundation, in version 3 of the
|
---|
27 | License.
|
---|
28 |
|
---|
29 | This program is distributed in the hope that it will be useful, but
|
---|
30 | WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
31 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
32 | General Public License for more details.
|
---|
33 |
|
---|
34 | You should have received a copy of the GNU General Public License
|
---|
35 | along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
36 |
|
---|
37 | SPDX-License-Identifier: GPL-3.0-only
|
---|
38 | """
|
---|
39 |
|
---|
40 | """
|
---|
41 | key is the id, value is the name of the containing file.
|
---|
42 | """
|
---|
43 | xref_dictionary = {}
|
---|
44 |
|
---|
45 | """"
|
---|
46 | =================================================================
|
---|
47 | Cross reference utility functions to add file name to target name.
|
---|
48 | =================================================================
|
---|
49 | """
|
---|
50 | # Search for hrefs and return a tuple where 0th element is target and 1st element is its index in @p line
|
---|
51 | def extractHrefTarget(line, start = 0):
|
---|
52 | target = ""
|
---|
53 | pointer0 = line.find("href=\"", start)
|
---|
54 | if pointer0 == -1:
|
---|
55 | return (target, -1)
|
---|
56 | pointer0 += len("href=\"")
|
---|
57 | pointer1 = line.find("\"", pointer0)
|
---|
58 | if pointer1 == -1:
|
---|
59 | return (target, -1)
|
---|
60 | return (line[pointer0:pointer1], pointer0)
|
---|
61 |
|
---|
62 | # Return list of href targets contained in @p line
|
---|
63 | def extractHrefList(line):
|
---|
64 | targets = []
|
---|
65 | index = 0
|
---|
66 | while index < len(line):
|
---|
67 | ttarget = extractHrefTarget(line, index)
|
---|
68 | index = ttarget[1]
|
---|
69 | if index == -1:
|
---|
70 | break
|
---|
71 | targets.append(ttarget[0])
|
---|
72 | return targets
|
---|
73 |
|
---|
74 | def correctHrefTargets(file_content):
|
---|
75 | for idx, line in enumerate(file_content):
|
---|
76 | if "href" in line:
|
---|
77 | targets = extractHrefList(line)
|
---|
78 | newline = line
|
---|
79 | for target in targets:
|
---|
80 | if target in xref_dictionary and os.path.basename != xref_dictionary[target]:
|
---|
81 | old = "\"" + target + "\""
|
---|
82 | new = "\"" + xref_dictionary[target] + "#" + target + "\""
|
---|
83 | newline = newline.replace(old, new)
|
---|
84 | file_content[idx] = newline
|
---|
85 |
|
---|
86 | def createXrefIdDictionary(ditafolder):
|
---|
87 | xref_dictionary.clear()
|
---|
88 | dita_files = glob.glob(ditafolder + "/*.dita")
|
---|
89 | for file in dita_files:
|
---|
90 | file_content = open(file, 'r', encoding="utf-8")
|
---|
91 | for line in file_content:
|
---|
92 | if "reference" in line or "topic" in line or "section" in line:
|
---|
93 | start = line.find("id=\"")
|
---|
94 | if start != -1:
|
---|
95 | start += len("id=\"")
|
---|
96 | end = line.find("\"", start)
|
---|
97 | if end != -1:
|
---|
98 | id = line[start:end]
|
---|
99 | if len(id) > 0:
|
---|
100 | if id not in xref_dictionary:
|
---|
101 | xref_dictionary[id] = os.path.basename(file)
|
---|
102 | else:
|
---|
103 | logging.warning('Non unique topic/section id %s in file %s. This is already found in %s'
|
---|
104 | % (id, os.path.basename(file), os.path.basename(xref_dictionary[id])))
|
---|
105 |
|
---|
106 | def usage(arg):
|
---|
107 | print('correct_references.py -d <ditafolder>')
|
---|
108 | sys.exit(arg)
|
---|
109 |
|
---|
110 | def main():
|
---|
111 | ditafolder = ''
|
---|
112 | try:
|
---|
113 | opts, args = getopt.getopt(sys.argv[1:],"hd:")
|
---|
114 | except getopt.GetoptError as err:
|
---|
115 | print(err)
|
---|
116 | usage(2)
|
---|
117 | for opt, arg in opts:
|
---|
118 | if opt == '-h':
|
---|
119 | usage(0)
|
---|
120 | elif opt in ("-d"):
|
---|
121 | ditafolder = arg
|
---|
122 |
|
---|
123 | if not ditafolder:
|
---|
124 | logging.error('No folder is provided. Exiting')
|
---|
125 | usage(2)
|
---|
126 | createXrefIdDictionary(ditafolder)
|
---|
127 |
|
---|
128 | vboxmanage_dita_files = glob.glob(ditafolder + "/man_V*dita")
|
---|
129 | for file in vboxmanage_dita_files:
|
---|
130 | file_handle = open(file, 'r', encoding="utf-8")
|
---|
131 | file_content = file_handle.readlines()
|
---|
132 | correctHrefTargets(file_content)
|
---|
133 | file_handle.close()
|
---|
134 | file_handle = open(file, 'w', encoding="utf-8")
|
---|
135 | file_handle.write("".join(file_content))
|
---|
136 | file_handle.close()
|
---|
137 |
|
---|
138 | if __name__ == "__main__":
|
---|
139 | main()
|
---|