1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2013, The AROS Development Team. All rights reserved.
5 """Documentation to guide.
7 Extract documentation from C source files and create guide files
9 Usage: python gendoc.py <sourcedir> <targetdir>
11 <sourcedir> will be scanned recursively for C source files.
12 <targetdir> where to place the guide. Directory will be created
21 # Regex for whole autodoc block (without surrounding comments)
22 ad_regx
= re
.compile(r
"""
30 """, re
.VERBOSE | re
.MULTILINE | re
.DOTALL
)
33 titles_regx
= re
.compile(r
"""
35 (NAME|FORMAT|SYNOPSIS|LOCATION|FUNCTION|INPUTS|TAGS|RESULT|EXAMPLE|NOTES|BUGS|SEE\ ALSO|INTERNALS|HISTORY|TEMPLATE)$
39 def parsedoc(filename
, targetdir
):
40 # print "reading " + filename
42 filehandle
= open(filename
)
43 content
= filehandle
.read()
44 doc
= ad_regx
.search(content
)
47 for line
in doc
.group(1).splitlines():
48 match
= titles_regx
.match(line
)
50 current_title
= match
.group(1)
51 blocks
[current_title
] = ""
53 blocks
[current_title
] += line
.expandtabs()[4:] + "\n"
55 # check for empty chapters, because we don't want to print them
56 for title
, content
in blocks
.iteritems():
57 if content
.strip() == "":
62 if blocks
.has_key("NAME"):
64 docname
= blocks
["NAME"].split()[0]
66 raise ValueError("docname is empty")
68 docfilename
= docname
+ ".guide"
69 today
= datetime
.date
.today()
70 filehandle
= open(os
.path
.join(targetdir
, docfilename
), "w")
72 # The titles we want to printed
73 shell_titles
= ("Name","Format","Template","Synopsis","Location","Function",
74 "Inputs","Tags","Result","Example","Notes","Bugs","See also")
76 filehandle
.write("@DATABASE %s\n\n" % (docfilename
))
77 filehandle
.write("@$VER: %s 1.0 (%d.%d.%d)\n" % (docfilename
, today
.day
, today
.month
, today
.year
))
78 filehandle
.write("@(C) Copyright (C) %d, The AROS Development Team. All rights reserved.\n" % (today
.year
))
79 filehandle
.write("@MASTER %s\n\n" %(filename))
80 filehandle
.write("@NODE MAIN \"%s\"\n\n" % (docname
))
82 for title
in shell_titles
:
83 title_key
= title
.upper()
84 if blocks
.has_key(title_key
) and blocks
[title_key
] != "":
85 filehandle
.write("@{B}" + title
+ "@{UB}\n")
86 filehandle
.write(blocks
[title_key
])
87 filehandle
.write("\n")
89 filehandle
.write('@TOC "HELP:English/Index.guide/MAIN"\n')
90 filehandle
.write("@ENDNODE\n")
94 ###############################################################################
96 sourcedir
= sys
.argv
[1]
97 targetdir
= sys
.argv
[2]
99 print "gendoc sourcedir " + sourcedir
+ " targetdir " + targetdir
101 if not os
.path
.exists(targetdir
):
104 for root
, dirs
, files
in os
.walk(sourcedir
):
105 for filename
in files
:
106 if len(filename
) > 2 and filename
[-2:] == ".c":
107 parsedoc(os
.path
.join(root
, filename
), targetdir
)