-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple_format
60 lines (47 loc) · 1.51 KB
/
simple_format
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?xml version="1.0" encoding="UTF-8"?>
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:rdfe="http://redfoot.net/3.0/rdf#"
xmlns:code="http://redfoot.net/3.0/code#"
>
<rdfe:RDFXMLDocument rdf:about="">
</rdfe:RDFXMLDocument>
<rdfe:Namespace rdf:about="#">
<rdfs:label>Simple Format</rdfs:label>
<rdfs:comment>The Redfoot simple format namespace.</rdfs:comment>
</rdfe:Namespace>
<code:Module rdf:ID="module">
<rdfs:label>Simple Formatter</rdfs:label>
<rdfs:comment>A formatter that turns uris into links.</rdfs:comment>
<code:python rdf:datatype="http://www.w3.org/2001/XMLSchema#string">
<![CDATA[
import re
from urllib import quote, unquote
from xml.sax.saxutils import escape
_url = re.compile(r'((http://|https://|ftp://|news://|irc://)[^\s,]+)', re.IGNORECASE)
def url(match):
url, scheme = match.groups()
url = URIRef(unquote(url))
label = redfoot.label(url, url)
return '<a href="%s">%s</a>' % (escape(url.concrete()), label)
def format(text):
if text:
text = re.sub(_url, url, text)
return text or ''
def format_uri(uri, predicate=None):
if predicate is None:
text = uri
else:
text = redfoot.value(uri, predicate)
if text:
text = re.sub(_url, url, text)
return text or ''
#example = """
#Perfect with http://donnafales.com/recipes/2002/07/28/
#"""
#print format(example)
]]>
</code:python>
</code:Module>
</rdf:RDF>