Re: Plugin implementation help
"Igor Khavkine" <[email protected]>
| Newsgroups | gmane.comp.web.pyblosxom.user |
|---|---|
| Organization | Lycos Mail (http://www.mail.lycos.com:80) |
| Message-ID | <[email protected]> |
--------- Original Message --------- DATE: Mon, 17 May 2004 20:23:06 From: will guaraldi <[email protected]> To: Igor Khavkine <[email protected]> Cc: >On Mon, 17 May 2004, Igor Khavkine wrote: >> >> Ah. So at the point of cb_story(), the entry text has been processed and >> has become an HTML fragment, and only the overall style of the blog page >> has not yet been determined. Right? > >Yeah--you've totally got it. All the template variables for the entry are >available in the entry object. Ok, thanks for your help. I managed to implement what I wanted. I can now just put my images in the same directory as the entry that uses them. In case anyone else finds this little hack useful, I'm attaching the code. >> I'm still not very clear how to get the category of the story from >> cb_story(args). I presume that I have to look in args['entry'], but what >> am I looking for? How do I get the category and the filename? For some reason args['entry']['story'] didn't work for me. I had to use args['entry']['body']. >Though I would grab the wbgdebug plugin at >http://bluesock.org/~willg/dev/pyblosxom/ which enables you to force the >debug renderer to kick in on your own blog when you do "?debug=yes". Hm... This plugin didn't work out of the box for me, but I havn't really checked why. Igor ____________________________________________________________ Find what you are looking for with the Lycos Yellow Pages http://r.lycos.com/r/yp_emailfooter/http://yellowpages.lycos.com/default.asp?SRC=lycos10
showimg.py
(text/x-python, 3.2 KB)
# vim: ts=2 sw=2 tw=72 expandtab
"""
This plugin changes references in <img/> tags so that entries could
refer to them without specifying the absolute path. The plugin requires
a special variable to be set in the config file: py['data_base_uri'].
If py['data_base_uri'] is set to '/~user/blog/data', then for an entry
in category 'cat' the tag '<img src="image.png"/>' would be replaced by
'<img src="/~user/blog/data/cat/image.png"/>'. Basically, with this
plugin, as long as py['datadir'] is web accessible, it is possible to
include images in entries by specifying their location relative to the
entry file.
One caveat is that <img/> replacement is done using an HTML parser, so
invalid HTML code produces errors which are not handled gracefully (yet).
"""
__author__ = "Igor Khavkine <k_igor_k AT lycos DOT com>"
__version__ = "0.1"
import Pyblosxom, os, re
from HTMLParser import HTMLParser
# global variables
base_uri = ""
data_dir = ""
category = ""
def relocate(uri):
"""
This function checks whether the category-relative uri points to an
actual file.
@param uri: relative location of file with respect to the category
@type uri: string
"""
path = os.path.join(data_dir, category, uri)
if os.path.exists(path):
return base_uri + "/" + category + "/" + uri
# more tests can go here
else:
return uri
class IMGReplacer(HTMLParser):
"""
This class parses an HTML fragmet and replaces <img/> tags when
possible.
"""
filtered_text = ''
def reset(self):
filtered_text = ''
HTMLParser.reset(self)
def handle_starttag(self, tag, attrs):
self.filtered_text += "<" + tag
for (attr, val) in attrs:
self.filtered_text += " " + attr + '="' + val.replace('"',r'\"') + '"'
self.filtered_text += '>'
def handle_startendtag(self, tag, attrs):
self.filtered_text += "<" + tag
for (attr, val) in attrs:
if (tag == "img" and attr == "src"):
val = relocate(val)
self.filtered_text += " " + attr + '="' + val.replace('"',r'\"') + '"'
self.filtered_text += '/>'
def handle_endtag(self, tag):
self.filtered_text += "</" + tag + ">"
def handle_data(self, data):
self.filtered_text += data
def handle_charref(self, name):
self.filtered_text += "&#" + name + ";"
def handle_entityref(self, name):
self.filtered_text += "&" + name + ";"
def handle_comment(self, data):
self.filtered_text += "<!--" + data + "-->"
def replace_img(text):
"""
Replace relative references in <img/> tags by absolute ones.
@param text: an HTML fragment
@type text: string
"""
ir = IMGReplacer()
ir.feed(text)
ir.close()
return ir.filtered_text
def cb_story(args):
"""
Story callback. It is run after the story has been processed and
just before the blog page style is rendered.
@param args: a dict with 'entry' Entry object
@type args: dict
"""
global category, data_dir, base_uri
# set some global vars to be used in replaced paths
entry = args['entry']
data_dir = entry['datadir']
base_uri = entry.get('data_base_uri', '')
category = entry.get('absolute_path', '')
# perhaps 'file_path' is also useful
story = replace_img(entry['body'])
entry['body'] = story
return args