Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
rootDocPreprocessor.py
Go to the documentation of this file.
1#! /usr/bin/env python3
2"""
3A tool to preprocess root documentation in order to provide missing
4functionality in the documentation tools like doxygen or pandoc.
5At the moment filters are just functions. A proper implementation would
6go through the creation of a filter class which is very genereic and can be
7composed with a trigger and an action.
8"""
9
10
11import sys
12import os
13
14usage="Usage: %s fileToBeTreated.md\n" %__file__
15
17 print(msg)
18 print(usage)
19
21 """
22 Check if the invocation is correct. If not print help
23 """
24 retcode=0
25 if len(argv) != 2:
26 printErrAndUsage("Number of arguments different from 2")
27 retcode+=1
28 if not os.path.exists(argv[1]):
29 printErrAndUsage("File %s does not exist." %argv[1])
30 retcode+=1
31 return retcode
32
34 """
35 Create a name for the preprocessed version of filename
36 """
37 fileName, fileExtension = os.path.splitext(filename)
38 return "%s_preprocessed%s" %(fileName, fileExtension)
39
40def includeFilter(text):
41 """
42 If a line of the form
43 @ROOT_INCLUDE_FILE filename
44 is encountered, it is replaced with the content of filename
45 """
46 newText=""
47 retcode=0
48 for line in text.split("\n"):
49 if line.startswith("@ROOT_INCLUDE_FILE"):
50 inclFileName = line.split()[1]
51 if not os.path.exists(inclFileName):
52 print("[includeFilter] Error: file", inclFileName, "does not exist.")
53 retcode+=1
54 continue
55 for inclFileLine in open(inclFileName).readlines():
56 newText+=inclFileLine
57 else:
58 newText+=line+"\n"
59 return retcode, newText
60
61
62textFilters=[includeFilter]
63
64def applyFilters(filename):
65 """
66 Apply a series of filters to the file modifiying its content.
67 A new file is created with the suffix _preprocessed before the extension.
68 For example:
69 myChapter.md --> myChapter_preprocessed.md
70 """
71 filecontent = open(filename).read()
72 retcode=0
73 for textFilter in textFilters:
74 tmpretcode,filecontent=textFilter(filecontent)
75 retcode+=tmpretcode
76 preprocessedName = createPreprocessedName(filename)
77 ofile = open(preprocessedName,"w")
78 ofile.write(filecontent)
79 return retcode
80
81
83 """
84 Apply a series of "filters" to the input file.
85 """
86 argv=sys.argv
87 retcode = checkInvocation(argv)
88 if 0 != retcode:
89 return retcode
90
91 filename = argv[1]
92 retcode = applyFilters(filename)
93 if retcode != 0:
94 print("Errors during the preprocessing.")
95 return retcode
96
97if __name__ == "__main__":
98 sys.exit(preprocessFile())
double read()
preprocessFile()
Apply a series of "filters" to the input file.
createPreprocessedName(filename)
Create a name for the preprocessed version of filename.
applyFilters(filename)
Apply a series of filters to the file modifiying its content.
includeFilter(text)
If a line of the form @ROOT_INCLUDE_FILE filename is encountered, it is replaced with the content of ...
checkInvocation(argv)
Check if the invocation is correct.