VirtualBox

source: vbox/trunk/doc/manual/correct_references.py@ 98950

Last change on this file since 98950 was 98897, checked in by vboxsync, 20 months ago

Docs: bugref:10302. First take on migrating html creation to dita. missing some parts.

  • Property svn:eol-style set to LF
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 4.7 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
40"""
41key is the id, value is the name of the containing file.
42"""
43xref_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
51def 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
63def 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
74def 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
86def 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
106def usage(arg):
107 print('correct_references.py -d <ditafolder>')
108 sys.exit(arg)
109
110def 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
138if __name__ == "__main__":
139 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