1""" 2========================= IMAGE LINKS ================================= 3 4 5Turns paragraphs like 6 7<~~~~~~~~~~~~~~~~~~~~~~~~ 8dir/subdir 9dir/subdir 10dir/subdir 11~~~~~~~~~~~~~~ 12dir/subdir 13dir/subdir 14dir/subdir 15~~~~~~~~~~~~~~~~~~~> 16 17Into mini-photo galleries. 18 19""" 20 21import re, markdown 22import url_manager 23 24 25IMAGE_LINK = """<a href="%s"><img src="%s" title="%s"/></a>""" 26SLIDESHOW_LINK = """<a href="%s" target="_blank">[slideshow]</a>""" 27ALBUM_LINK = """ <a href="%s">[%s]</a>""" 28 29 30class ImageLinksExtension(markdown.Extension): 31 32 def extendMarkdown(self, md, md_globals): 33 34 md.preprocessors.add("imagelink", ImageLinkPreprocessor(md), "_begin") 35 36 37class ImageLinkPreprocessor(markdown.preprocessors.Preprocessor): 38 39 def run(self, lines): 40 41 url = url_manager.BlogEntryUrl(url_manager.BlogUrl("all"), 42 "2006/08/29/the_rest_of_our") 43 44 45 all_images = [] 46 blocks = [] 47 in_image_block = False 48 49 new_lines = [] 50 51 for line in lines: 52 53 if line.startswith("<~~~~~~~"): 54 albums = [] 55 rows = [] 56 in_image_block = True 57 58 if not in_image_block: 59 60 new_lines.append(line) 61 62 else: 63 64 line = line.strip() 65 66 if line.endswith("~~~~~~>") or not line: 67 in_image_block = False 68 new_block = "<div><br/><center><span class='image-links'>\n" 69 70 album_url_hash = {} 71 72 for row in rows: 73 for photo_url, title in row: 74 new_block += " " 75 new_block += IMAGE_LINK % (photo_url, 76 photo_url.get_thumbnail(), 77 title) 78 79 album_url_hash[str(photo_url.get_album())] = 1 80 81 new_block += "<br/>" 82 83 new_block += "</span>" 84 new_block += SLIDESHOW_LINK % url.get_slideshow() 85 86 album_urls = album_url_hash.keys() 87 album_urls.sort() 88 89 if len(album_urls) == 1: 90 new_block += ALBUM_LINK % (album_urls[0], "complete album") 91 else : 92 for i in range(len(album_urls)) : 93 new_block += ALBUM_LINK % (album_urls[i], 94 "album %d" % (i + 1) ) 95 96 new_lines.append(new_block + "</center><br/></div>") 97 98 elif line[1:6] == "~~~~~" : 99 rows.append([]) # start a new row 100 else : 101 parts = line.split() 102 line = parts[0] 103 title = " ".join(parts[1:]) 104 105 album, photo = line.split("/") 106 photo_url = url.get_photo(album, photo, 107 len(all_images)+1) 108 all_images.append(photo_url) 109 rows[-1].append((photo_url, title)) 110 111 if not album in albums : 112 albums.append(album) 113 114 return new_lines 115 116 117def makeExtension(configs): 118 return ImageLinksExtension(configs) 119 120