Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 819643dd3a
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 433 lines (363 sloc) 18.342 kb
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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
# -*- coding: UTF-8 -*-
"""
Custom Django template filters and template tags for Microformats.

Copyright (c) 2009 Nicholas H.Tollervey (http://ntoll.org/contact)

All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
* Neither the name of ntoll.org nor the names of its
contributors may be used to endorse or promote products
derived from this software without specific prior written
permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

"""
from django.template import Template, Context
from django import template
from django.template.loader import select_template
from django.conf import settings
from django.utils.translation import ugettext as _
from django.utils.html import conditional_escape
from django.utils.safestring import mark_safe
from django.forms.fields import email_re, url_re
# We'll be using all the models at some point or other
import microformats.models
import datetime

################################################
# Default templates (over-ridden in settings.py)
################################################
GEO_MICROFORMAT_TEMPLATE = 'geo.html'
HCARD_MICROFORMAT_TEMPLATE = 'hcard.html'
HCAL_MICROFORMAT_TEMPLATE = 'hcal.html'
HLISTING_MICROFORMAT_TEMPLATE = 'hlisting.html'
HREVIEW_MICROFORMAT_TEMPLATE = 'hreview.html'
ADR_MICROFORMAT_TEMPLATE = 'adr.html'
HFEED_MICROFORMAT_TEMPLATE = 'hfeed.html'
HENTRY_MICROFORMAT_TEMPLATE = 'hentry.html'
HNEWS_MICROFORMAT_TEMPLATE = 'hnews.html'

# For registering the templates
register = template.Library()

########################
# Some utility functions
########################

def is_valid_email(email):
    """
Is the string a valid email?
(We use the regex Django uses to define an email address)
"""
    return True if email_re.match(email) else False

def is_valid_url(url):
    """
Is the string a valid url?
(We use the regex Django uses to define a URL)
"""
    return True if url_re.match(url) else False

def fragment(value, arg, autoescape=None):
    """
A generic utility function that takes the value and arg and returns a
<span> enclosed version.

The arg should contain the contents of what is to be the class attribute of
the <span> element.

For example, where value = "Microsoft" and arg = "org fn" then the result
will be:

<span class="org fn">Microsoft</span>

The function does inspect the value type and attempts to select the most
appropriate element for display (99.9% of the time this will be a <span>
element, but for the case of datetime values <abbr> is more appropriate). It
also checks if the arg is either longitude or latitude and uses the more
appropriate <abbr> element.

In the case of a datetime value only the first arg will be used for the
class, anything else will be assumed to be arguments for strftime. e.g. an
arg containing "dtstart %a %d %b %y" will result in "dtstart" as the <attr>
class and the rest as arguments for date/time formatting:

<abbr class="dtstart" title="2009-05-03">Sun 3 May 2009</abbr>

Verging of the redundant, this function *does* save typing, incorporates
some useful heuristics and abstracts the output of individual field values
in a neat way.
"""
    if autoescape:
        esc = conditional_escape
    else:
        esc = lambda x: x
    # Make sure we have an arg
    if arg:
        if isinstance(value, datetime.datetime):
            # check we have a datetime field that might need
            # formatting
            args = arg.split()
            klass = args[0]
            if len(args)>1:
                # We're assuming these are strftime formatting instructions
                format = str(' '.join(args[1:]).strip())
            else:
                # Safe default
                format = '%c'
            result = u'<abbr class="%s" title="%s">%s</abbr>' % (
                            esc(klass),
                            esc(value.isoformat()),
                            esc(value.strftime(format))
                        )
        elif arg == 'longitude' or arg == 'latitude' or arg == 'long' or arg == 'lat':
            # Check for geo related fields so we can use the abbr pattern
            if arg == 'latitude' or arg == 'lat':
                klass = u'latitude'
            else:
                klass = u'longitude'
            result = u'<abbr class="%s" title="%s">%s</abbr>' % (
                            esc(klass),
                            esc(value),
                            esc(value)
                        )
        elif is_valid_email(esc(value)):
            # If the field is an email address we need to wrap it in an anchor
            # element
            result = u'<a class="%s" href="mailto:%s">%s</a>'%(
                            esc(arg),
                            esc(value),
                            esc(value)
                        )
        elif is_valid_url(esc(value)):
            # If the field is a URL we need to wrap it in an anchor element
            result = u'<a class="%s" href="%s">%s</a>'%(
                            esc(arg),
                            esc(value),
                            esc(value)
                        )
        else:
            # if not just return the raw value in a span with arg as the class
            result = u'<span class="%s">%s</span>' % (esc(arg), esc(value))
    else:
        # We don't have an arg
        result = esc(value)
    return mark_safe(result)

def render_microformat(instance, template_name):
    """
A generic function that simply takes an instance of a microformat and a
template name, creates an appropriate context object and returns the rendered
result.
"""
    template = select_template([template_name,])
    adr_template = getattr(settings, 'ADR_MICROFORMAT_TEMPLATE', False) and settings.ADR_MICROFORMAT_TEMPLATE or ADR_MICROFORMAT_TEMPLATE
    context = Context({
        'instance': instance,
        'adr_microformat_template': adr_template,
        })
    return template.render(context)

@register.filter
def geo(value, arg=None, autoescape=None):
    """
Formats a value to conform with the geo microformat.

{{value|geo:"longitude"}}

or

{{geo_instance|geo}}

If rendering a fragment arg to be one of:
['long', 'lat', 'longitude', 'latitude']

See:

http://microformats.org/wiki/geo

geo (pronounced "gee-oh") is a simple format for marking up WGS84 geographic
coordinates (latitude; longitude), suitable for embedding in HTML or XHTML,
Atom, RSS, and arbitrary XML. geo is a 1:1 representation of the "geo"
property in the vCard standard (RFC2426) in HTML, one of several open
microformat standards.
"""
    if isinstance(value, datetime.datetime) or isinstance(value, str) or isinstance(value, unicode) or isinstance(value, float) or isinstance(value, int) or isinstance(value, long) or isinstance(value, complex):
        return fragment(value, arg, autoescape)
    else:
        # lets try rendering something with the correct attributes for this
        # microformat
        template_name = getattr(settings, 'GEO_MICROFORMAT_TEMPLATE', False) and settings.GEO_MICROFORMAT_TEMPLATE or GEO_MICROFORMAT_TEMPLATE
        return mark_safe(render_microformat(value, template_name))
geo.needs_autoescape = True

@register.filter
def hcard(value, arg=None, autoescape=None):
    """
Formats a value to conform with the hCard microformat.

arg to be one of the field names referenced here:

http://microformats.org/wiki/hcard-cheatsheet

If an instance of the hCard model is passed as a value then the arg is not
required and the microformat will be rendered using the hcard template
specified in settings.HCARD_MICROFORMAT_TEMPLATE (attempts to default to
the one found in the templates directory of this application).

See:

http://microformats.org/wiki/hcard

hCard is a simple, open, distributed format for representing people,
companies, organizations, and places, using a 1:1 representation of vCard
(RFC2426) properties and values in semantic HTML or XHTML. hCard is one of
several open microformat standards suitable for embedding in HTML, XHTML,
Atom, RSS, and arbitrary XML.
"""
    if isinstance(value, datetime.datetime) or isinstance(value, str) or isinstance(value, unicode) or isinstance(value, float) or isinstance(value, int) or isinstance(value, long) or isinstance(value, complex):
        return fragment(value, arg, autoescape)
    else:
        # lets try rendering something with the correct attributes for this
        # microformat
        template_name = getattr(settings, 'HCARD_MICROFORMAT_TEMPLATE', False) and settings.HCARD_MICROFORMAT_TEMPLATE or HCARD_MICROFORMAT_TEMPLATE
        return mark_safe(render_microformat(value, template_name))
hcard.needs_autoescape = True

@register.filter
def adr(value, arg=None, autoescape=None):
    """
Formats a value to conform with the adr Microformat.

args to be one of the field names referenced here:

http://microformats.org/wiki/adr-cheatsheet

If an instance of the adr model is passed as a value then the arg is not
required.
See:

http://microformats.org/wiki/adr

adr (pronounced "adder"; FAQ: "why 'adr'?") is a simple format for marking
up address information, suitable for embedding in HTML, XHTML, Atom, RSS,
and arbitrary XML. adr is a 1:1 representation of the adr property in the
vCard standard (RFC2426) in HTML, one of several open microformat standards.
It is also a property of hCard.
"""
    if isinstance(value, datetime.datetime) or isinstance(value, str) or isinstance(value, unicode) or isinstance(value, float) or isinstance(value, int) or isinstance(value, long) or isinstance(value, complex):
        return fragment(value, arg, autoescape)
    else:
        # lets try rendering something with the correct attributes for this
        # microformat
        template_name = getattr(settings, 'ADR_MICROFORMAT_TEMPLATE', False) and settings.ADR_MICROFORMAT_TEMPLATE or ADR_MICROFORMAT_TEMPLATE
        return mark_safe(render_microformat(value, template_name))
adr.needs_autoescape = True

@register.filter
def hcal(value, arg=None, autoescape=None):
    """
Formats a value to conform with the hCalendar Microformat.

args to be one of the field names referenced here:

http://microformats.org/wiki/hcalendar-cheatsheet

If an instance of the hCalendar model is passed as a value then the arg is
not required.

Inspired by the markup found here:

http://microformats.org/code/hcalendar/creator

For more information see:

http://microformats.org/wiki/hcalendar

hCalendar is a simple, open, distributed calendaring and events format,
based on the iCalendar standard (RFC2445), suitable for embedding in HTML or
XHTML, Atom, RSS, and arbitrary XML. hCalendar is one of several open
microformat standards.
"""
    if isinstance(value, datetime.datetime) or isinstance(value, str) or isinstance(value, unicode) or isinstance(value, float) or isinstance(value, int) or isinstance(value, long) or isinstance(value, complex):
        return fragment(value, arg, autoescape)
    else:
        # lets try rendering something with the correct attributes for this
        # microformat
        template_name = getattr(settings, 'HCAL_MICROFORMAT_TEMPLATE', False) and settings.HCAL_MICROFORMAT_TEMPLATE or HCAL_MICROFORMAT_TEMPLATE
        return mark_safe(render_microformat(value, template_name))
hcal.needs_autoescape = True

@register.filter
def hlisting(value, arg=None, autoescape=None):
    """
Formats a value to conform with the hListing Microformat
"""
    if isinstance(value, datetime.datetime) or isinstance(value, str) or isinstance(value, unicode) or isinstance(value, float) or isinstance(value, int) or isinstance(value, long) or isinstance(value, complex):
        return fragment(value, arg, autoescape)
    else:
        # lets try rendering something with the correct attributes for this
        # microformat
        template_name = getattr(settings, 'HLISTING_MICROFORMAT_TEMPLATE', False) and settings.HLISTING_MICROFORMAT_TEMPLATE or HLISTING_MICROFORMAT_TEMPLATE
        return mark_safe(render_microformat(value, template_name))
hlisting.needs_autoescape = True

@register.filter
def hreview(value, arg=None, autoescape=None):
    """
Formats a value to conform with the hReview Microformat
Inspired by the markup found here:

http://microformats.org/code/hreview/creator
"""
    if isinstance(value, datetime.datetime) or isinstance(value, str) or isinstance(value, unicode) or isinstance(value, float) or isinstance(value, int) or isinstance(value, long) or isinstance(value, complex):
        return fragment(value, arg, autoescape)
    else:
        # lets try rendering something with the correct attributes for this
        # microformat
        template_name = getattr(settings, 'HREVIEW_MICROFORMAT_TEMPLATE', False) and settings.HREVIEW_MICROFORMAT_TEMPLATE or HREVIEW_MICROFORMAT_TEMPLATE
        return mark_safe(render_microformat(value, template_name))
hreview.needs_autoescape = True

@register.filter
def xfn(value, arg=None, autoescape=None):
    """
Formats an instance of the xfn model to conform with the XFN microformat.

XFN™ (XHTML Friends Network) is a simple way to represent human
relationships using hyperlinks. In recent years, blogs and blogrolls have
become the fastest growing area of the Web. XFN enables web authors to
indicate their relationship(s) to the people in their blogrolls simply by
adding a 'rel' attribute to their <a href> tags, e.g.:

<a href="http://jeff.example.org" rel="friend met">...
"""
    if autoescape:
        esc = conditional_escape
    else:
        esc = lambda x: x
    if isinstance(value, datetime.datetime) or isinstance(value, str) or isinstance(value, unicode) or isinstance(value, float) or isinstance(value, int) or isinstance(value, long) or isinstance(value, complex):
        return mark_safe(esc(value))
    else:
        # lets try rendering something with the correct attributes for this
        # microformat
        vals = ' '.join(esc(x.value) for x in value.relationships.all())
        result = u'<a href="%s" rel="%s">%s</a>' % (
                            esc(value.url),
                            vals,
                            esc(value.target)
                            )
        return mark_safe(result)
xfn.needs_autoescape = True

@register.filter
def hfeed(value, arg=None, autoescape=None):
    """
Formats a value to conform with the hFeed Microformat fragment
Inspired by the markup found here:

http://microformats.org/wiki/hatom-examples
"""
    if isinstance(value, datetime.datetime) or isinstance(value, str) or isinstance(value, unicode) or isinstance(value, float) or isinstance(value, int) or isinstance(value, long) or isinstance(value, complex):
        return fragment(value, arg, autoescape)
    else:
        # lets try rendering something with the correct attributes for this
        # microformat
        template_name = getattr(settings, 'HFEED_MICROFORMAT_TEMPLATE', False) and settings.HFEED_MICROFORMAT_TEMPLATE or HFEED_MICROFORMAT_TEMPLATE
        return mark_safe(render_microformat(value, template_name))
hfeed.needs_autoescape = True

@register.filter
def hentry(value, arg=None, autoescape=None):
    """
Formats a value to conform with the hEntry Microformat fragment
Inspired by the markup found here:

http://microformats.org/wiki/hatom-examples
"""
    if isinstance(value, datetime.datetime) or isinstance(value, str) or isinstance(value, unicode) or isinstance(value, float) or isinstance(value, int) or isinstance(value, long) or isinstance(value, complex):
        return fragment(value, arg, autoescape)
    else:
        # lets try rendering something with the correct attributes for this
        # microformat
        template_name = getattr(settings, 'HENTRY_MICROFORMAT_TEMPLATE', False) and settings.HENTRY_MICROFORMAT_TEMPLATE or HENTRY_MICROFORMAT_TEMPLATE
        return mark_safe(render_microformat(value, template_name))
hentry.needs_autoescape = True

@register.filter
def hnews(value, arg=None, autoescape=None):
    """
Formats a value to conform with the hNews Microformat fragment
Inspired by the markup found here:

http://microformats.org/wiki/hnews-examples
"""
    if isinstance(value, datetime.datetime) or isinstance(value, str) or isinstance(value, unicode) or isinstance(value, float) or isinstance(value, int) or isinstance(value, long) or isinstance(value, complex):
        return fragment(value, arg, autoescape)
    else:
        # lets try rendering something with the correct attributes for this
        # microformat
        template_name = getattr(settings, 'HNEWS_MICROFORMAT_TEMPLATE', False) and settings.HNEWS_MICROFORMAT_TEMPLATE or HNEWS_MICROFORMAT_TEMPLATE
        return mark_safe(render_microformat(value, template_name))
hentry.needs_autoescape = True
Something went wrong with that request. Please try again.