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 '
'