Attachment 'xml_rdf.py'

Download

   1 # -*- coding: iso-8859-1 -*-
   2 """
   3     MoinMoin - "application/rdf+xml" formatter for exporting SIOC data
   4 
   5     @copyright: 2000, 2001, 2002 by Jürgen Hermann <jh@web.de>
   6     @license: GNU GPL, see COPYING for details.
   7 """
   8 
   9 from xml.sax import saxutils
  10 from MoinMoin.formatter.base import FormatterBase
  11 from MoinMoin import config
  12 from MoinMoin.Page import Page
  13 
  14 class Formatter(FormatterBase):
  15     """
  16         Send XML data.
  17     """
  18 
  19     hardspace = '&nbsp;'
  20 
  21     def __init__(self, request, **kw):
  22         apply(FormatterBase.__init__, (self, request), kw)
  23         self._current_depth = 1
  24         self._base_depth = 0
  25         self.in_pre = 0
  26 
  27     def _escape(self, text, extra_mapping={"'": "&apos;", '"': "&quot;"}):
  28         return saxutils.escape(text, extra_mapping)
  29 
  30     def startDocument(self, pagename):
  31         encoding = config.charset
  32         return '''<?xml version="1.0" encoding="%s"?>\n
  33 <rdf:RDF
  34 	xmlns="http://xmlns.com/foaf/0.1/"
  35 	xmlns:foaf="http://xmlns.com/foaf/0.1/"
  36 	xmlns:rss="http://purl.org/rss/1.0/"
  37 	xmlns:admin="http://webns.net/mvcb/"
  38 	xmlns:dc="http://purl.org/dc/elements/1.1/"
  39 	xmlns:dcterms="http://purl.org/dc/terms/"
  40 	xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
  41 	xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  42 	xmlns:content="http://purl.org/rss/1.0/modules/content/"
  43 	xmlns:sioc="http://rdfs.org/sioc/ns#">
  44 
  45 <sioc:Post rdf:about="@@ INSERT URI HERE @@">
  46 	<dc:title>%s</dc:title>
  47 	<dcterms:modified>%s</dcterms:modified>
  48 	<rdfs:comment>some more info here, if needed.</rdfs:comment>
  49 	<content:encoded>''' % (
  50             encoding, self._escape(pagename), self.page.lastEditInfo()['time'] + ' @@ REFORMAT to format a''la 2007-02-22T15:28:35Z' )
  51 
  52     def endDocument(self):
  53         result = ""
  54         while self._current_depth > 1:
  55             result += "</s%d>" % self._current_depth
  56             self._current_depth -= 1
  57         return result + '''
  58 	</content:encoded>
  59 </sioc:Post>
  60 </rdf:RDF>
  61 '''
  62 
  63     def lang(self, on, lang_name):
  64         return ('<div lang="">' % lang_name, '</div>')[not on]
  65 
  66     def sysmsg(self, on, **kw):
  67         return ('<sysmsg>', '</sysmsg>')[not on]
  68 
  69     def rawHTML(self, markup):
  70         return '<![CDATA[' + markup.replace(']]>', ']]>]]&gt;<![CDATA[') + ']]>'
  71 
  72     def pagelink(self, on, pagename='', page=None, **kw):
  73         apply(FormatterBase.pagelink, (self, on, pagename, page), kw)
  74         if page is None:
  75             page = Page(self.request, pagename, formatter=self)
  76         return page.link_to(self.request, on=on, **kw)
  77 
  78     def interwikilink(self, on, interwiki='', pagename='', **kw):
  79         if on:
  80             return '<interwiki wiki="%s" pagename="%s">' % (interwiki, pagename)
  81         else:
  82             return '</interwiki>'
  83 
  84     def url(self, on, url='', css=None, **kw):
  85         if css:
  86             str = ' class="%s"' % css
  87         else:
  88             str = ''
  89         return ('<jump href="%s"%s>' % (self._escape(url), str), '</jump>') [not on]
  90 
  91     def attachment_link(self, url, text, **kw):
  92         return '<attachment href="%s">%s</attachment>' % (url, text)
  93 
  94     def attachment_image(self, url, **kw):
  95         return '<attachmentimage href="%s"></attachmentimage>' % (url,)
  96 
  97     def attachment_drawing(self, url, text, **kw):
  98         return '<attachmentdrawing href="%s">%s</attachmentdrawing>' % (url, text)
  99 
 100     def text(self, text, **kw):
 101         if self.in_pre:
 102             return text.replace(']]>', ']]>]]&gt;<![CDATA[')
 103         return self._escape(text)
 104 
 105     def rule(self, size=0, **kw):
 106         return "\n<br/>%s<br/>\n" % ("-" * 78,) # <hr/> not supported in stylebook
 107         if size:
 108             return '<hr size="%d"/>\n' % (size,)
 109         else:
 110             return '<hr/>\n'
 111 
 112     def icon(self, type):
 113         return '<icon type="%s" />' % type            
 114 
 115     def strong(self, on, **kw):
 116         return ['<strong>', '</strong>'][not on]
 117 
 118     def emphasis(self, on, **kw):
 119         return ['<em>', '</em>'][not on]
 120 
 121     def highlight(self, on, **kw):
 122         return ['<strong>', '</strong>'][not on]
 123 
 124     def number_list(self, on, type=None, start=None, **kw):
 125         result = ''
 126         if self.in_p:
 127             result = self.paragraph(0)
 128         return result + ['<ol>', '</ol>\n'][not on]
 129 
 130     def bullet_list(self, on, **kw):
 131         result = ''
 132         if self.in_p:
 133             result = self.paragraph(0)
 134         return result + ['<ul>', '</ul>\n'][not on]
 135 
 136     def listitem(self, on, **kw):
 137         return ['<li>', '</li>\n'][not on]
 138 
 139     def code(self, on, **kw):
 140         return ['<code>', '</code>'][not on]
 141 
 142     def sup(self, on, **kw):
 143         return ['<sup>', '</sup>'][not on]
 144 
 145     def sub(self, on, **kw):
 146         return ['<sub>', '</sub>'][not on]
 147 
 148     def strike(self, on, **kw):
 149         return ['<strike>', '</strike>'][not on]
 150 
 151     def preformatted(self, on, **kw):
 152         FormatterBase.preformatted(self, on)
 153         result = ''
 154         if self.in_p:
 155             result = self.paragraph(0)
 156         return result + ['<source><![CDATA[', ']]></source>'][not on]
 157 
 158     def paragraph(self, on, **kw):
 159         FormatterBase.paragraph(self, on)
 160         return ['<p>', '</p>\n'][not on]
 161 
 162     def linebreak(self, preformatted=1):
 163         return ['\n', '<br/>\n'][not preformatted]
 164 
 165     def heading(self, on, depth, id=None, **kw):
 166         if not on:
 167             return '">\n'
 168         # remember depth of first heading, and adapt current depth accordingly
 169         if not self._base_depth:
 170             self._base_depth = depth
 171         depth = max(depth + (2 - self._base_depth), 2)
 172 
 173         # close open sections
 174         result = ""
 175         while self._current_depth >= depth:
 176             result = result + "</s%d>\n" % self._current_depth
 177             self._current_depth -= 1
 178         self._current_depth = depth
 179 
 180         id_text = ''
 181         if id:
 182           id_text = ' id="%s"' % id
 183 
 184         return result + '<s%d%s title="' % (depth, id_text)
 185 
 186     def table(self, on, attrs={}, **kw):
 187         return ['<table>', '</table>'][not on]
 188 
 189     def table_row(self, on, attrs={}, **kw):
 190         return ['<tr>', '</tr>'][not on]
 191 
 192     def table_cell(self, on, attrs={}, **kw):
 193         return ['<td>', '</td>'][not on]
 194 
 195     def anchordef(self, id):
 196         return '<anchor id="%s"/>' % id
 197 
 198     def anchorlink(self, on, name='', **kw):
 199         id = kw.get('id',None)
 200         extra = ''
 201         if id:
 202             extra = ' id="%s"' % id
 203         return ('<link anchor="%s"%s>' % (name, extra) ,'</link>') [not on]
 204 
 205     def underline(self, on, **kw):
 206         return self.strong(on) # no underline in StyleBook
 207 
 208     def definition_list(self, on, **kw):
 209         result = ''
 210         if self.in_p:
 211             result = self.paragraph(0)
 212         return result + ['<gloss>', '</gloss>'][not on]
 213 
 214     def definition_term(self, on, compact=0, **kw):
 215         return ['<label>', '</label>'][not on]
 216 
 217     def definition_desc(self, on, **kw):
 218         return ['<item>', '</item>'][not on]
 219 
 220     def image(self, src=None, **kw):
 221         valid_attrs = ['src', 'width', 'height', 'alt', 'title']
 222         attrs = {'src': src}
 223         for key, value in kw.items():
 224             if key in valid_attrs:
 225                 attrs[key] = value
 226         return apply(FormatterBase.image, (self,), attrs) + '</img>'
 227 
 228     def code_area(self, on, code_id, code_type='code', show=0, start=-1, step=-1):
 229         return ('<codearea id="%s">' % code_id, '</codearea')[not on]
 230 
 231     def code_line(self, on):
 232         return ('<codeline>', '</codeline')[not on]
 233 
 234     def code_token(self, on, tok_type):
 235         return ('<codetoken type="%s">' % tok_type, '</codetoken')[not on]
 236 
 237     def code_line(self, on):
 238         return ('<codeline>', '</codeline')[not on]
 239 
 240     def code_token(self, on, tok_type):
 241         return ('<codetoken type="%s">' % tok_type, '</codetoken')[not on]

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2008-09-22 14:49:48, 7.6 KB) [[attachment:xml_rdf.py]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.