| 1 | # Copyright 2004 Matthew Good |
|---|
| 2 | # "THE BEER-WARE LICENSE" (Revision 42): |
|---|
| 3 | # Matthew Good <matt-good.net> wrote this file. As long as you retain |
|---|
| 4 | # this notice you can do whatever you want with this stuff. If we meet some |
|---|
| 5 | # day, and you think this stuff is worth it, you can buy me a beer in return. |
|---|
| 6 | # Matthew Good |
|---|
| 7 | |
|---|
| 8 | # BEER-WARE LICENSE courtesy of Poul-Henning Kamp <phk@login.dknet.dk> |
|---|
| 9 | |
|---|
| 10 | # Author Matthew Good <trac@matt-good.net> |
|---|
| 11 | |
|---|
| 12 | # Trac WikiMacro for rendering links to Javadoc urls |
|---|
| 13 | # Accepts one or two arguments separated by a comma. |
|---|
| 14 | |
|---|
| 15 | # The first argument is the fully qualified Java class or package. |
|---|
| 16 | # Classes are assumed to begin with an uppercase letter and packages |
|---|
| 17 | # with a lowercase letter according to standard Java naming conventions. |
|---|
| 18 | |
|---|
| 19 | # The second optional argument is the link text. |
|---|
| 20 | # If the second argument is ommitted the class name is used, |
|---|
| 21 | # or in the case of a package the full package name is used. |
|---|
| 22 | |
|---|
| 23 | #------------------------------------------------------------------------------- |
|---|
| 24 | # This block can be edited to change the urls used for the Javadocs |
|---|
| 25 | # Note: all Javadoc urls should end with a '/' or index.html |
|---|
| 26 | |
|---|
| 27 | j2se_url = 'http://java.sun.com/j2se/1.4.2/docs/api/index.html' |
|---|
| 28 | j2ee_url = 'http://java.sun.com/j2ee/1.4/docs/api/index.html' |
|---|
| 29 | xerces_url = 'http://xml.apache.org/xerces2-j/javadocs/api/index.html' |
|---|
| 30 | |
|---|
| 31 | urls = { |
|---|
| 32 | # The URL for the standard Java API |
|---|
| 33 | '': j2se_url, |
|---|
| 34 | |
|---|
| 35 | # Mappings from package names to Javadoc URLs |
|---|
| 36 | 'javax.activation': j2ee_url, |
|---|
| 37 | 'javax.ejb': j2ee_url, |
|---|
| 38 | 'javax.enterprise': j2ee_url, |
|---|
| 39 | 'javax.jms': j2ee_url, |
|---|
| 40 | 'javax.mail': j2ee_url, |
|---|
| 41 | 'javax.management': j2ee_url, |
|---|
| 42 | 'javax.resource': j2ee_url, |
|---|
| 43 | 'javax.security.jacc': j2ee_url, |
|---|
| 44 | 'javax.servlet': j2ee_url, |
|---|
| 45 | 'javax.transaction': j2ee_url, |
|---|
| 46 | 'javax.xml': j2ee_url, |
|---|
| 47 | |
|---|
| 48 | 'org.apache.commmons.beanutils': |
|---|
| 49 | 'http://jakarta.apache.org/commons/beanutils/commons-beanutils-1.7.0/docs/api/', |
|---|
| 50 | 'org.apache.commons.collections': |
|---|
| 51 | 'http://jakarta.apache.org/commons/collections/apidocs-COLLECTIONS_3_1/index.html', |
|---|
| 52 | 'org.apache.commons.lang': |
|---|
| 53 | 'http://jakarta.apache.org/commons/lang/api/index.html', |
|---|
| 54 | |
|---|
| 55 | 'org.w3c.dom': xerces_url, |
|---|
| 56 | 'org.xml.sax': xerces_url |
|---|
| 57 | } |
|---|
| 58 | #------------------------------------------------------------------------------- |
|---|
| 59 | |
|---|
| 60 | from urlparse import urljoin |
|---|
| 61 | from string import uppercase |
|---|
| 62 | |
|---|
| 63 | def best_match(value, items): |
|---|
| 64 | match = '' |
|---|
| 65 | for i in items: |
|---|
| 66 | if len(i) > len(match) and value.startswith(i): |
|---|
| 67 | match = i |
|---|
| 68 | |
|---|
| 69 | return match |
|---|
| 70 | |
|---|
| 71 | def base_url(package): |
|---|
| 72 | return urls[best_match(package, urls.keys())] |
|---|
| 73 | |
|---|
| 74 | def package_path(package): |
|---|
| 75 | return package.replace('.', '/') + '/' |
|---|
| 76 | |
|---|
| 77 | def type_path(package, clss): |
|---|
| 78 | return package_path(package) + (clss and (clss + '.html') or 'package-summary.html') |
|---|
| 79 | |
|---|
| 80 | def javadoc_url(package, clss): |
|---|
| 81 | return urljoin(base_url(package), type_path(package, clss)) |
|---|
| 82 | |
|---|
| 83 | def split_at(a, index): |
|---|
| 84 | return (a[:index], a[index + 1:]) |
|---|
| 85 | |
|---|
| 86 | def split_type(value): |
|---|
| 87 | package, clss = split_at(value, value.rfind('.')) |
|---|
| 88 | if clss[0] in uppercase: |
|---|
| 89 | return (package, clss) |
|---|
| 90 | else: |
|---|
| 91 | return (value, None) |
|---|
| 92 | |
|---|
| 93 | def link(href, text): |
|---|
| 94 | return '<a href="%s" class="javadoc">%s</a>' % (href, text) |
|---|
| 95 | |
|---|
| 96 | def javadoc_link(value, linktext): |
|---|
| 97 | package, clss = split_type(value) |
|---|
| 98 | return link(javadoc_url(package, clss), linktext or clss or package) |
|---|
| 99 | |
|---|
| 100 | def execute(hdf, txt, env): |
|---|
| 101 | args = txt.split(',', 1) |
|---|
| 102 | javatype = args[0].strip() |
|---|
| 103 | linktext = len(args) > 1 and args[1].strip() or None |
|---|
| 104 | return javadoc_link(javatype, linktext) |
|---|