imagelinks.py :  » Network » Python-Markdown » Markdown-2.0.3 » markdown » extensions » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Network » Python Markdown 
Python Markdown » Markdown 2.0.3 » markdown » extensions » imagelinks.py
"""
========================= 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 = """&nbsp;<a href="%s">[%s]</a>"""


class ImageLinksExtension(markdown.Extension):

    def extendMarkdown(self, md, md_globals):

        md.preprocessors.add("imagelink", ImageLinkPreprocessor(md), "_begin")


class ImageLinkPreprocessor(markdown.preprocessors.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 += "&nbsp;"
                            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)

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.