From d900c4dfcf11c3cda024e64456735c3fe7f298f9 Mon Sep 17 00:00:00 2001 From: Stefan Krulj Date: Sun, 23 Dec 2012 18:29:34 +0100 Subject: [PATCH] added markdown extension for galleries, still needs JS and css --- mdx_gallery.py | 32 ++++++++++++++++++++++++++++++++ pylog_action_impl.py | 4 ++-- 2 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 mdx_gallery.py diff --git a/mdx_gallery.py b/mdx_gallery.py new file mode 100644 index 0000000..5c1d3db --- /dev/null +++ b/mdx_gallery.py @@ -0,0 +1,32 @@ +def makeExtension(configs=None) : + return GalleryExtension() + +from markdown.inlinepatterns import Pattern +import markdown, urllib + +class GalleryPattern(Pattern): + def __init__(self,md): + Pattern.__init__(self,'!\\[(.*?)\\]\(\((.*?)\)\)',md) # + def handleMatch(self, m): + + alt=m.group(2) + imgs=m.group(3).split() + + ul = markdown.util.etree.Element('ul') + ul.attrib["class"]='gallery' + + for img in imgs: + li = markdown.util.etree.Element('li') + ul.append(li) + img_el = markdown.util.etree.Element('img') + li.append(img_el) + img_el.attrib["src"]=img + img_el.attrib["alt"]=alt + img_el.attrib["class"]='galleryimage' + + return ul + +class GalleryExtension(markdown.Extension): + def extendMarkdown(self, md, md_globals): + md.inlinePatterns.add('gallery', GalleryPattern(md), '' f = codecs.open(path, mode="r", encoding=cfg.encoding) txt=f.read() - print markdown.markdown(txt,extensions=['math']).encode(cfg.encoding) + print markdown.markdown(txt,extensions=['gallery','math']).encode(cfg.encoding) f.close() print '' @@ -161,7 +161,7 @@ def prev(path): def prev_txt(path): fi = codecs.open(path, mode="r", encoding=cfg.encoding) txt=get_n_words(fi,40) - print '
'+markdown.markdown(txt[0],extensions=['math']).encode(cfg.encoding) + print '
'+markdown.markdown(txt[0],extensions=['gallery','math']).encode(cfg.encoding) if txt[1]: print ' ...' print '
'