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 | def replaceRefsect2(file_content):
|
---|
41 | new_content = []
|
---|
42 | """
|
---|
43 | * Add a refsect1 before the first refsect2,
|
---|
44 | * convert refsect2 to refsect1,
|
---|
45 | * remove the </refsect1> which comes after a </refsect2>
|
---|
46 | """
|
---|
47 | first_refsect2 = False
|
---|
48 | # remove lines that consist of new line only
|
---|
49 | file_content = [line for line in file_content if line != "\n"]
|
---|
50 | new_content.append(file_content[0])
|
---|
51 | for current, next in zip(file_content, file_content[1:]):
|
---|
52 |
|
---|
53 | if "<refsect2" in next:
|
---|
54 | if not first_refsect2:
|
---|
55 | first_refsect2 = True
|
---|
56 | new_content.append("</refsect1>")
|
---|
57 | new_content.append("\n")
|
---|
58 | new_content.append(next.replace("<refsect2", "<refsect1"))
|
---|
59 | elif "</refsect2>" in next:
|
---|
60 | new_content.append("</refsect1>")
|
---|
61 | new_content.append("\n")
|
---|
62 | elif not ("</refsect1>" in next and "</refsect2>" in current):
|
---|
63 | new_content.append(next)
|
---|
64 | return new_content
|
---|
65 |
|
---|
66 |
|
---|
67 | def usage(arg):
|
---|
68 | print('refsect2_to_refsect1.py -i <input_file> -o <output_file>')
|
---|
69 | sys.exit(arg)
|
---|
70 |
|
---|
71 | def main():
|
---|
72 | input_file = ''
|
---|
73 | output_file = ''
|
---|
74 | try:
|
---|
75 | opts, args = getopt.getopt(sys.argv[1:],"hi:o:")
|
---|
76 | except getopt.GetoptError as err:
|
---|
77 | print(err)
|
---|
78 | usage(2)
|
---|
79 | for opt, arg in opts:
|
---|
80 | if opt == '-h':
|
---|
81 | usage(0)
|
---|
82 | elif opt in ("-i"):
|
---|
83 | input_file = arg
|
---|
84 | elif opt in ("-o"):
|
---|
85 | output_file = arg
|
---|
86 |
|
---|
87 | if not input_file:
|
---|
88 | logging.error('No input file is provided. Exiting')
|
---|
89 | usage(2)
|
---|
90 | if not output_file:
|
---|
91 | logging.error('No output file is provided. Exiting')
|
---|
92 | usage(2)
|
---|
93 |
|
---|
94 | file_handle = open(input_file, 'r', encoding="utf-8")
|
---|
95 | file_content = file_handle.readlines()
|
---|
96 | new_file_content = replaceRefsect2(file_content)
|
---|
97 | file_handle.close()
|
---|
98 | file_handle = open(output_file , 'w', encoding="utf-8")
|
---|
99 | file_handle.write("".join(new_file_content))
|
---|
100 | file_handle.close()
|
---|
101 |
|
---|
102 | if __name__ == "__main__":
|
---|
103 | main()
|
---|