[wiki/parser/{Box,Portail}.py] plus propre

Ignore-this: 2618612a02dee4b0ad0e7b8f77d2aae3

darcs-hash:20100326175721-bd074-0cd6e7d2189770b28c00cd85d8a45bca28183a16.gz
This commit is contained in:
Antoine Durand-Gasselin 2010-03-26 18:57:21 +01:00
parent ad66550320
commit 8428b1b746
2 changed files with 33 additions and 131 deletions

View file

@ -32,7 +32,7 @@ MoinMoin - Portail parser
Copyright 2008,2009 Antoine Durand-Gasselin <adg@crans.org>
"""
import os,string,re,StringIO,urllib
import urllib
from MoinMoin import wikiutil
from MoinMoin.action import AttachFile
@ -51,100 +51,31 @@ class Parser:
self.request.write(self.request.formatter.rawHTML(self.crans_portal(entries)))
def parse(self, formatter):
# on divise le textes en lignes (1 ligne = une entree dans le portail)
quotes = self.raw.split('\n')
# on récupere le chemin des fichiers attaches pour les images
current_pagename=formatter.page.page_name
attachment_path = AttachFile.getAttachDir(self.request, current_pagename, create=1)
# on traite les ligne une à une
entries = []
for line in quotes:
line=line.strip()
for line in self.raw.split('\n'):
items=line.split('@@',2)
if len(items)<3:
# Voir ce qu'on fait
## self.request.write('<!-- error, wrong number of parameters -->')
pass
else:
# On récupère le nom de l'image
if len(items) >2:
pic = items[0].strip()
title = wikiutil.renderText(self.request, self.parser, items[1])
body = wikiutil.renderText(self.request, self.parser, items[2])
# On va formatter le titre et la légende
tit_buf = StringIO.StringIO()
bod_buf = StringIO.StringIO()
self.request.redirect(tit_buf)
self.parser(items[1], self.request).format(formatter)
title = tit_buf.getvalue()
self.request.redirect()
self.request.redirect(bod_buf)
self.parser(items[2], self.request).format(formatter)
body = bod_buf.getvalue()
self.request.redirect()
# Qu'on rajoute à la liste des entrées
entries += [ (( pic, title, body )) ]
return entries
yield (( pic, title, body ))
def crans_portal(self, entries):
html = [ '\n\n<div class="portal" style="padding: 0 0 2em 0;">' ]
format_pe = '''
<div style="background:transparent url('%s') center left no-repeat;
width: 38%%; height: 5em; padding: 1.5em 20px 0 60px; float:left;">
<ul style="list-style: none; margin: 0; padding: 0;">
<li>%s</li>
<li>%s</li>
</ul>
</div>'''
format_pne = '''
<div style="width: 38%%; height: 5em; padding: 10px 20px 0 0px; float:left;">
<div style="float: left; padding-top: 1.5em; width: 20%%;">
<a href="%s" style="font-size: 5pt" alt="envoyer l'image">envoyer<br>%s</a>
<div style="padding: 0 20px 0 0; float: left; clear: none;
display: table; height: 5em; width: 46%%;">
<div style="padding: 0; overflow: hidden ; display: table-cell; width: 60px; vertical-align: middle;">
%s </div>
<div style="padding: 0; display: table-cell; vertical-align: middle;">
%s %s </div>
</div>
<div style="float: left; width: 75%%; padding: 0; margin: 0;">
<ul style="list-style: none; margin: 0; padding: 0;">
<li>%s</li>
<li>%s</li>
</ul>
</div>
</div>'''
'''
pn = self.request.formatter.page.page_name
while len(entries) != 0:
pic0, tit0, bod0 = entries[0]
fname = urllib.url2pathname(pic0)
if AttachFile.exists(self.request, pn, fname):
pic0 = AttachFile.getAttachUrl(pn, pic0, self.request)
pic0 = wikiutil.escape(pic0)
html += [ format_pe % (pic0, tit0, bod0) ]
else:
pic0 = AttachFile.getAttachUrl(pn, pic0, self.request, upload=True)
pic0 = wikiutil.escape(pic0)
html += [ format_pne % (pic0, fname, tit0, bod0) ]
if len(entries) >= 2:
pic1, tit1, bod1 = entries[1]
entries = entries[2:]
fname = urllib.url2pathname(pic1)
if AttachFile.exists(self.request, pn, fname):
pic1 = AttachFile.getAttachUrl(pn, pic1, self.request)
pic1 = wikiutil.escape(pic1)
html += [ format_pe % (pic1, tit1, bod1) ]
else:
pic1 = AttachFile.getAttachUrl(pn, pic1, self.request, upload=True)
pic1 = wikiutil.escape(pic1)
html += [ format_pne % (pic1, fname, tit1, bod1) ]
else:
entries = [] # plus propre que {{{ break() }}}
for pic0, tit0, bod0 in entries:
pic = wikiutil.renderText(self.request, self.parser, '{{attachment:%s}}' % pic0)
html += [ format_pne % (pic, tit0, bod0) ]
html += [ '\n</div>\n<hr style="visibility:hidden; clear: both">\n\n' ]
return('\n'.join(html))