diff options
author | Yuri Takhteyev <yuri@freewisdom.org> | 2008-05-02 10:33:47 -0700 |
---|---|---|
committer | Yuri Takhteyev <yuri@freewisdom.org> | 2008-05-02 10:33:47 -0700 |
commit | dc9e65bcdc7416c66a9cb2026616dc4cc1c9da3a (patch) | |
tree | 61dbe9a4143b99c0c9c197a51156b30e7a4e6324 /mdx_imagelinks.py | |
parent | a190c3941a7bed4274fe08e92ba4ce368c1c837f (diff) | |
download | markdown-dc9e65bcdc7416c66a9cb2026616dc4cc1c9da3a.tar.gz markdown-dc9e65bcdc7416c66a9cb2026616dc4cc1c9da3a.tar.bz2 markdown-dc9e65bcdc7416c66a9cb2026616dc4cc1c9da3a.zip |
Adding four extensions that weren't in source control before.
Diffstat (limited to 'mdx_imagelinks.py')
-rw-r--r-- | mdx_imagelinks.py | 135 |
1 files changed, 135 insertions, 0 deletions
diff --git a/mdx_imagelinks.py b/mdx_imagelinks.py new file mode 100644 index 0000000..e545b24 --- /dev/null +++ b/mdx_imagelinks.py @@ -0,0 +1,135 @@ +""" +========================= IMAGE LINKS ================================= + + +Turns paragraphs like + +<~~~~~~~~~~~~~~~~~~~~~~~~ +dir/subdir +dir/subdir +dir/subdir +~~~~~~~~~~~~~~ +dir/subdir +dir/subdir +dir/subdir +~~~~~~~~~~~~~~~~~~~> + +Into mini-photo galleries. + +""" + +import re, markdown +import url_manager + + +IMAGE_LINK = """<a href="%s"><img src="%s" title="%s"/></a>""" +SLIDESHOW_LINK = """<a href="%s" target="_blank">[slideshow]</a>""" +ALBUM_LINK = """ <a href="%s">[%s]</a>""" + + +class ImageLinksExtension (markdown.Extension): + + def __init__ (self) : + self.reset() + + def extendMarkdown(self, md, md_globals) : + + self.md = md + + # Stateless extensions do not need to be registered + md.registerExtension(self) + + # Insert a preprocessor before all preprocessors + + preprocessor = ImageLinkPreprocessor() + preprocessor.md = md + md.preprocessors.insert(0, preprocessor) + + def reset(self) : + # May be called by Markdown is state reset is desired + pass + + +class ImageLinkPreprocessor (markdown.Preprocessor): + + def run(self, lines) : + + url = url_manager.BlogEntryUrl(url_manager.BlogUrl("all"), + "2006/08/29/the_rest_of_our") + + + all_images = [] + blocks = [] + in_image_block = False + + new_lines = [] + + for line in lines : + + if line.startswith("<~~~~~~~") : + albums = [] + rows = [] + in_image_block = True + + if not in_image_block : + + new_lines.append(line) + + else : + + line = line.strip() + + if line.endswith("~~~~~~>") or not line : + in_image_block = False + new_block = "<div><br/><center><span class='image-links'>\n" + + album_url_hash = {} + + for row in rows : + for photo_url, title in row : + new_block += " " + new_block += IMAGE_LINK % (photo_url, + photo_url.get_thumbnail(), + title) + + album_url_hash[str(photo_url.get_album())] = 1 + + new_block += "<br/>" + + new_block += "</span>" + new_block += SLIDESHOW_LINK % url.get_slideshow() + + album_urls = album_url_hash.keys() + album_urls.sort() + + if len(album_urls) == 1 : + new_block += ALBUM_LINK % (album_urls[0], "complete album") + else : + for i in range(len(album_urls)) : + new_block += ALBUM_LINK % (album_urls[i], + "album %d" % (i + 1) ) + + new_lines.append(new_block + "</center><br/></div>") + + elif line[1:6] == "~~~~~" : + rows.append([]) # start a new row + else : + parts = line.split() + line = parts[0] + title = " ".join(parts[1:]) + + album, photo = line.split("/") + photo_url = url.get_photo(album, photo, + len(all_images)+1) + all_images.append(photo_url) + rows[-1].append((photo_url, title)) + + if not album in albums : + albums.append(album) + + return new_lines + + +def makeExtension(configs) : + return ImageLinksExtension(configs) + |