osimons / trac-rpc-mq
patch queue to trac-rpc
Patch queue for Trac XmlRpc plugin. Work in progress.
Clone this repository and patch queue (size: 149.0 KB): HTTPS / SSH
$ hg qclone http://bitbucket.org/osimons/trac-rpc-mq
| commit 13: | 852ab5767c42 |
| parent 12: | 9689e6018102 |
| branch: | default |
New patch for `wiki.listLinks` (#1055) Supports TrackLinks + CamelCase + ...
9 months ago
Changed (Δ4.9 KB):
raw changeset »
series (1 lines added, 0 lines removed)
t1055/t1055-list_links-r7194.diff (134 lines added, 0 lines removed)
1 |
t1055/t1055-list_links-r7194.diff |
|
1 |
2 |
|
2 |
3 |
# Make protocols pluggable (including default supported ones) |
3 |
4 |
# TODO: Types cleanup (Binary + datetime + all method docs) |
Up to file-list t1055/t1055-list_links-r7194.diff:
1 |
diff -r 4663bf114c66 trunk/tracrpc/util.py |
|
2 |
--- a/trunk/tracrpc/util.py Thu Nov 26 15:30:46 2009 +0100 |
|
3 |
+++ b/trunk/tracrpc/util.py Sun Dec 20 02:15:14 2009 -0500 |
|
4 |
@@ -10,7 +10,10 @@ |
|
5 |
import time |
|
6 |
import xmlrpclib |
|
7 |
||
8 |
+from genshi.builder import Element |
|
9 |
+ |
|
10 |
from trac.util.datefmt import utc |
|
11 |
+from trac.wiki.formatter import Formatter |
|
12 |
||
13 |
### PUBLIC |
|
14 |
||
15 |
@@ -44,3 +47,75 @@ |
|
16 |
from trac.util.text import empty |
|
17 |
except ImportError: |
|
18 |
empty = None |
|
19 |
+ |
|
20 |
+class TracLinksFormatter(Formatter): |
|
21 |
+ r"""A wiki formatter that pays attention to nothing but trac |
|
22 |
+ links. |
|
23 |
+ |
|
24 |
+ Verbatim copy of tracgviz.ig.wiki.TracLinksFormatter |
|
25 |
+ """ |
|
26 |
+ for fmt_name in (x for x in Formatter.__dict__ \ |
|
27 |
+ if x.endswith('_formatter') and \ |
|
28 |
+ x not in ['_lhref_formatter', '_shref_formatter']): |
|
29 |
+ locals()[fmt_name] = lambda *args, **kwds: '' |
|
30 |
+ del fmt_name |
|
31 |
+ |
|
32 |
+class WikiCrawler(TracLinksFormatter): |
|
33 |
+ r"""Gather the all links found in a piece of text. |
|
34 |
+ """ |
|
35 |
+ def __init__(self, env, context): |
|
36 |
+ super(WikiCrawler, self).__init__(env, context) |
|
37 |
+ self._links = [] |
|
38 |
+ |
|
39 |
+ def handle_match(self, fullmatch): |
|
40 |
+ r"""Modified version of `trac.wiki.formatter.Formatter.handle_match` |
|
41 |
+ needed to gather links generated by external wiki syntax |
|
42 |
+ providers (e.g. CamelCase). |
|
43 |
+ """ |
|
44 |
+ def ext_wrapper(ext_handler): |
|
45 |
+ def _ext_wrapper(fmt, match, fullmatch): |
|
46 |
+ result = ext_handler(fmt, match, fullmatch) |
|
47 |
+ if isinstance(result, Element) and str(result.tag) == 'a': |
|
48 |
+ self._links.append(result) |
|
49 |
+ return result |
|
50 |
+ return _ext_wrapper |
|
51 |
+ |
|
52 |
+ for itype, match in fullmatch.groupdict().items(): |
|
53 |
+ if match and not itype in self.wikiparser.helper_patterns: |
|
54 |
+ # Check for preceding escape character '!' |
|
55 |
+ if match[0] == '!': |
|
56 |
+ return escape(match[1:]) |
|
57 |
+ if itype in self.wikiparser.external_handlers: |
|
58 |
+ external_handler = self.wikiparser.external_handlers[itype] |
|
59 |
+ return ext_wrapper(external_handler)(self, match, fullmatch) |
|
60 |
+ else: |
|
61 |
+ internal_handler = getattr(self, '_%s_formatter' % itype) |
|
62 |
+ return internal_handler(match, fullmatch) |
|
63 |
+ |
|
64 |
+ def _lhref_formatter(self, match, fullmatch): |
|
65 |
+ link = super(WikiCrawler, self)._lhref_formatter(match, fullmatch) |
|
66 |
+ self.env.log.debug("RPC(util) LinkFormatter LHREF rule triggered. " |
|
67 |
+ "Match= '%s' Full match= '%s' Link= '%s'", |
|
68 |
+ match, fullmatch, link) |
|
69 |
+ self._links.append(link) |
|
70 |
+ return link |
|
71 |
+ |
|
72 |
+ def _shref_formatter(self, match, fullmatch): |
|
73 |
+ link = super(WikiCrawler, self)._shref_formatter(match, fullmatch) |
|
74 |
+ self.env.log.debug("RPC(util) LinkFormatter SHREF rule triggered. " |
|
75 |
+ "Match= '%s' Full match= '%s' Link= '%s'", |
|
76 |
+ match, fullmatch, link) |
|
77 |
+ self._links.append(link) |
|
78 |
+ return link |
|
79 |
+ |
|
80 |
+ def extract_link(self, lnk_obj): |
|
81 |
+ raise TracError('Unable to extract TracLink from DOM object.') |
|
82 |
+ |
|
83 |
+ def iter_urls(self): |
|
84 |
+ for link in self._links : |
|
85 |
+ if not link: |
|
86 |
+ pass |
|
87 |
+ elif isinstance(link, Element): |
|
88 |
+ yield link.attrib.get('href') |
|
89 |
+ else: |
|
90 |
+ yield self.extract_link(link) |
|
91 |
diff -r 4663bf114c66 trunk/tracrpc/wiki.py |
|
92 |
--- a/trunk/tracrpc/wiki.py Thu Nov 26 15:30:46 2009 +0100 |
|
93 |
+++ b/trunk/tracrpc/wiki.py Sun Dec 20 02:15:14 2009 -0500 |
|
94 |
@@ -10,10 +10,12 @@ |
|
95 |
from cStringIO import StringIO |
|
96 |
except ImportError: |
|
97 |
from StringIO import StringIO |
|
98 |
+from itertools import chain |
|
99 |
import xmlrpclib |
|
100 |
import os |
|
101 |
||
102 |
from trac.core import * |
|
103 |
+from trac.mimeview.api import Context |
|
104 |
from trac.resource import Resource |
|
105 |
from trac.util.datefmt import to_timestamp, to_datetime, utc |
|
106 |
from trac.wiki.api import WikiSystem |
|
107 |
@@ -22,6 +24,7 @@ |
|
108 |
from trac.attachment import Attachment |
|
109 |
||
110 |
from tracrpc.api import IXMLRPCHandler, expose_rpc |
|
111 |
+from tracrpc.util import WikiCrawler |
|
112 |
||
113 |
class WikiRPC(Component): |
|
114 |
"""Superset of the |
|
115 |
@@ -199,8 +202,17 @@ |
|
116 |
return True |
|
117 |
||
118 |
def listLinks(self, req, pagename): |
|
119 |
- """ ''Not implemented'' """ |
|
120 |
- return [] |
|
121 |
+ """ List all URLs in links found in a wiki page. Supports both |
|
122 |
+ TracLinks and CamelCase""" |
|
123 |
+ text = self.getPage(req, pagename) |
|
124 |
+ env = self.env |
|
125 |
+ ctx = Context.from_request(req, absurls=True) |
|
126 |
+ abs_ref, href = req.abs_href, req.href |
|
127 |
+ lf = WikiCrawler(env, ctx) |
|
128 |
+ lf.format(text) |
|
129 |
+ files = self.listAttachments(req, pagename) |
|
130 |
+ href = self.env.abs_href.attachment |
|
131 |
+ return chain(lf.iter_urls(), (href('wiki', f) for f in files)) |
|
132 |
||
133 |
def wikiToHtml(self, req, text): |
|
134 |
""" Render arbitrary Wiki text as HTML. """ |
