Ticket #977: JavaDoc.py

File JavaDoc.py, 2.3 kB (added by boerst, 2 years ago)

very limited javadoc macro. needs javadoc and jeldoclet

Line 
1 import re
2 import string
3 import os
4 import sha
5
6 def make_error(element):
7         msg = """\
8 <div class="system-message">
9         <strong>Error: the <code>JavaDoc</code> macro returned an error:
10         %s
11         </strong>
12 </div>
13 """
14         return msg % element
15
16 def render(hdf, env, sourcefile, mode):
17         cfg = env.config
18         jeldoclet_path = cfg.get('javadoc', 'jeldoclet_path')
19         tmp_path = cfg.get('javadoc', 'tmp_path')
20
21         if not jeldoclet_path:
22                 return make_error('jeldoclet_path not set in config')
23         if not tmp_path:
24                 tmp_path = "/tmp"
25
26         repos = env.get_repository()
27         try:
28                 src = repos.get_node(sourcefile).get_content().read()
29         finally:
30                 repos.close()
31        
32         FILE = open("%s/trac_javadoc.java"%tmp_path, "w")
33         FILE.write(src)
34         FILE.close()
35
36         cmd = "javadoc -docletpath \"%s\" -doclet com.jeldoclet.JELDoclet -%s -d \"%s\" -filename trac_javadoc.xml \"%s/trac_javadoc.java\""%(jeldoclet_path,mode,tmp_path,tmp_path)
37         #os.system(cmd)
38        
39         html = ""
40         import xml.dom.minidom
41         doc = xml.dom.minidom.parse("%s/trac_javadoc.xml"%tmp_path)
42        
43         def getText(nodelist):
44                 rc = ""
45                 for node in nodelist:
46                         if node.nodeType == node.TEXT_NODE:
47                                 rc = rc + node.data
48                 return rc
49
50         def getFirstElementText(element,tagname):
51                 if element.getElementsByTagName(tagname)[0]:
52                         return getText(element.getElementsByTagName(tagname)[0].childNodes)
53                 return ""
54
55         for jelclass in doc.getElementsByTagName("jelclass"):
56                 html += "<h1>"+jelclass.getAttribute("fulltype")+"</h1>"
57                 html += "<h2>Methods</h2>"
58                 for method in jelclass.getElementsByTagName("method"):
59                         html+="<h3>"+method.getAttribute("name")+"</h3><table><tr><td>Description:</td><td>"
60                         html+=getFirstElementText(method,"description")
61                         html+="</td></tr><tr><td>Parameters:</td><td>"
62                         for param in method.getElementsByTagName("param"):
63                                 html+="<i>"+param.getAttribute("fulltype")+"</i> <b>"+param.getAttribute("name")+"</b>"
64                                 if param.getAttribute("comment"):
65                                         html+=" ("+param.getAttribute("comment")+")"
66                                 html+="<br>"
67                         html+="</td></tr></table>"
68        
69         return html
70
71 def execute(hdf, text, env):
72         cfg = env.config
73         mode = 'private'
74         text = text.split(",")
75         if len(text)>1:
76                 mode = text[1]
77                 if not mode in ('private','public','package','protected'):
78                         return make_error("Second argument is invalid")
79         elif len(text)==0: return make_error("JavaDoc expects at least one argument")
80         return render(hdf,env,text[0],mode)