Commit b96331c66b8421d10f9e040758c82badf97191b1
- Diff rendering mode:
- inline
- side by side
markdown/extensions/imagelinks.py
(0 / 118)
|   | |||
| 1 | """ | ||
| 2 | ========================= IMAGE LINKS ================================= | ||
| 3 | |||
| 4 | |||
| 5 | Turns paragraphs like | ||
| 6 | |||
| 7 | <~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 8 | dir/subdir | ||
| 9 | dir/subdir | ||
| 10 | dir/subdir | ||
| 11 | ~~~~~~~~~~~~~~ | ||
| 12 | dir/subdir | ||
| 13 | dir/subdir | ||
| 14 | dir/subdir | ||
| 15 | ~~~~~~~~~~~~~~~~~~~> | ||
| 16 | |||
| 17 | Into mini-photo galleries. | ||
| 18 | |||
| 19 | """ | ||
| 20 | |||
| 21 | import re, markdown | ||
| 22 | import url_manager | ||
| 23 | |||
| 24 | |||
| 25 | IMAGE_LINK = """<a href="%s"><img src="%s" title="%s"/></a>""" | ||
| 26 | SLIDESHOW_LINK = """<a href="%s" target="_blank">[slideshow]</a>""" | ||
| 27 | ALBUM_LINK = """ <a href="%s">[%s]</a>""" | ||
| 28 | |||
| 29 | |||
| 30 | class ImageLinksExtension(markdown.Extension): | ||
| 31 | |||
| 32 | def extendMarkdown(self, md, md_globals): | ||
| 33 | |||
| 34 | md.preprocessors.add("imagelink", ImageLinkPreprocessor(md), "_begin") | ||
| 35 | |||
| 36 | |||
| 37 | class 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 | |||
| 117 | def makeExtension(configs): | ||
| 118 | return ImageLinksExtension(configs) |

