VirtualBox

source: vbox/trunk/doc/manual/refsect2_to_refsect1.py@ 99060

Last change on this file since 99060 was 98893, checked in by vboxsync, 22 months ago

Docs: bugref:10302. Using a python script to flatten out refsect1/refsect2 hierarchy.

  • Property svn:eol-style set to LF
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 3.2 KB
Line 
1#!/usr/bin/env python3
2import glob
3import getopt
4import os
5import sys
6import shutil
7import 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"""
19Copyright (C) 2009-2023 Oracle and/or its affiliates.
20
21This file is part of VirtualBox base platform packages, as
22available from https://www.virtualbox.org.
23
24This program is free software; you can redistribute it and/or
25modify it under the terms of the GNU General Public License
26as published by the Free Software Foundation, in version 3 of the
27License.
28
29This program is distributed in the hope that it will be useful, but
30WITHOUT ANY WARRANTY; without even the implied warranty of
31MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
32General Public License for more details.
33
34You should have received a copy of the GNU General Public License
35along with this program; if not, see <https://www.gnu.org/licenses>.
36
37SPDX-License-Identifier: GPL-3.0-only
38"""
39
40def 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
67def usage(arg):
68 print('refsect2_to_refsect1.py -i <input_file> -o <output_file>')
69 sys.exit(arg)
70
71def 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
102if __name__ == "__main__":
103 main()
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