Login

Restructuredtext directive for photolouge

Author:
fivethreeo
Posted:
November 26, 2008
Language:
Python
Version:
1.0
Tags:
image insert rst directive photolouge
Score:
0 (after 0 ratings)

Directive for inserting images using photolouge in django.

Usage:

Just make a .py with this code and import it in some apps models.py

Or make a custom formatter with django-template-utils.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
from photologue.models import Photo, PhotoSize

from docutils import nodes
from docutils.parsers.rst import directives
from docutils.parsers.rst.directives.images import image

OPTIONS = {
    'alt': directives.unchanged,
    'class': directives.class_option
}

def imageblock_directive(name, arguments, options, content, lineno,
                       content_offset, block_text, state, state_machine):
    """
    Directive for inserting images using photolouge in django.
    
    Syntax:
        
        .. imageblock:: photo_title_slug photo_size_name left|right|center
            :class: optionalclassoption
        
            optional description for image, can be multiple lines
    """
    try: 
        slug, attr, pos = arguments[0].split(' ')
    except ValueError:
        error = state_machine.reporter.error(
            'Error in "%s" directive: Too many arguments. '
            'Valid arguments are "title_slug image_size position". '
            % (name,) ,
            nodes.literal_block(block_text, block_text), line=lineno)
        return [error]
        
    if pos not in ('left', 'right', 'center'):
        error = state_machine.reporter.error(
                    'Error in "%s" directive: "%s" is not a valid position for '
                    'images. Valid positions are: "left right center". '
                    % (name, pos) ,
                    nodes.literal_block(block_text, block_text), line=lineno)
        return [error]
        
    img = Photo.objects.get(title_slug=slug)
    
    try:
        imgurl = getattr(img, 'get_%s_url' % attr)()
    except AttributeError:
        error = state_machine.reporter.error(
                    'Error in "%s" directive: "%s" is not a valid size for '
                    'images. Valid sizes are: "%s" '
                    % (name, attr, ", ".join([s.name for s in PhotoSize.objects.all()]) ) ,
                    nodes.literal_block(block_text, block_text), line=lineno)
        return [error]
        
    imgsize = getattr(img, 'get_%s_size' % attr)()
    
    options['width'] = str(imgsize[0])
    options['height'] = str(imgsize[1])
    
    if not 'alt' in options:
        options['alt'] = img.title
        
    text = '\n'.join(content)      
    node = nodes.container(text)
    
    classes = ['imageblock', '%simage' % attr, '%spos' % pos]
    
    if 'class' in options:
        classes.append(''.join(options['class']))
        del options['class']
        
    node['classes'].extend(classes)
    
    imagenode = image(name, [imgurl], options, content, lineno,
          content_offset, block_text, state, state_machine)          
        
    node.insert(0, imagenode)
    
    state.nested_parse(content, content_offset, node)
    
    return [node]

imageblock_directive.arguments = (1, 0, 1)
imageblock_directive.content = 1
imageblock_directive.options = OPTIONS

directives.register_directive('imageblock', imageblock_directive)

More like this

  1. ShrinkTheWeb Template Tag by agilitynerd 5 years, 11 months ago
  2. Admob Ad Insertion Snippet by johnboxall 7 years, 1 month ago
  3. Image gradients on the fly by jensbreit 7 years, 3 months ago
  4. Image inlining template tag lib by rbeck 4 years, 9 months ago
  5. Image inlining template tag example by rbeck 4 years, 9 months ago

Comments

Please login first before commenting.