Permalink
Browse files

[600747] Video hook and render with Flash fallback.

* Videos are identified by title and locale
* Depends on an update in py-wikimarkup which adds support for <video> and <source> tags (py-wikimarkup commit cc06e6d264622891b6b018e8670c9ef4bb12d618)
* Attaches all the _hook_*s to the WikiParser class, because they need a contextual locale.
* Adds locale support for any of the hooks that do document lookup.
* Uses SWFobject JS lib to support flash fallback for video.
* Adds a migration for unique ('locale', 'title') on gallery_video and gallery_image
* Adds a WIKI_VIDEO_WIDTH|HEIGHT constant that may be used as MAX_WIDTH|HEIGHT in the future, once we get video thumbnails.
  • Loading branch information...
1 parent a881690 commit e664ef5c3de9c1315a3437d9636e511942ce9c19 Paul Craciunoiu committed Sep 30, 2010
View
@@ -24,9 +24,10 @@ class Media(ModelBase):
class Meta:
abstract = True
+ unique_together = ('locale', 'title')
def __unicode__(self):
- return self.title + ': ' + self.file.name[30:]
+ return '[%s] %s' % (self.locale, self.title)
class Image(Media):
View
@@ -20,6 +20,8 @@
'li': ['class'],
'span': ['class'],
'img': ['class', 'src', 'alt', 'title', 'height', 'width', 'style'],
+ 'video': ['height', 'width', 'controls', 'data-fallback'],
+ 'source': ['src', 'type'],
}
IMAGE_PARAMS = {
'align': ('none', 'left', 'center', 'right'),
@@ -33,47 +35,23 @@ def wiki_to_html(wiki_markup):
return WikiParser().parse(wiki_markup, show_toc=False)
-def _getWikiLink(link):
+def _getWikiLink(link, locale):
"""Checks the page exists, and returns its URL or the URL to create it."""
try:
- d = Document.objects.get(title=link, is_template=False)
+ d = Document.objects.get(locale=locale, title=link, is_template=False)
except Document.DoesNotExist:
# To avoid circular imports, wiki.models imports wiki_to_html
from sumo.helpers import urlparams
return urlparams(reverse('wiki.new_document'), title=link)
return d.get_absolute_url()
-def _hook_internal_link(parser, space, name):
- """Parses text and returns internal link."""
- link = text = name
-
- # Split on pipe -- [[href|name]]
- if '|' in name:
- link, text = link.split('|', 1)
-
- hash = ''
- if '#' in link:
- link, hash = link.split('#', 1)
-
- # Sections use _, page names use +
- if hash != '':
- hash = '#' + hash.replace(' ', '_')
-
- # Links to this page can just contain href="#hash"
- if link == '' and hash != '':
- return u'<a href="%s">%s</a>' % (hash, text)
-
- link = _getWikiLink(link)
- return u'<a href="%s%s">%s</a>' % (link, hash, text)
-
-
def _getImagePath(link):
"""Returns an uploaded image's path for image paths in markup."""
return settings.WIKI_UPLOAD_URL + urlquote(link)
-def _buildImageParams(items):
+def _buildImageParams(items, locale):
"""
Builds a list of items and return image-relevant parameters in a dict.
"""
@@ -90,7 +68,7 @@ def _buildImageParams(items):
params[item] = True
if 'page' in params and params['page'] is not True:
- params['link'] = _getWikiLink(params['page'])
+ params['link'] = _getWikiLink(params['page'], locale)
# Validate params with limited # of values
for param_allowed in IMAGE_PARAMS:
@@ -101,35 +79,6 @@ def _buildImageParams(items):
return params
-def _hook_image_tag(parser, space, name):
- """Adds syntax for inserting images."""
- link = name
- caption = name
- params = {}
-
- # Parse the inner syntax, e.g. [[Image:src|option=val|caption]]
- separator = name.find('|')
- items = []
- if separator != -1:
- items = link.split('|')
- link = items[0]
- # If the last item contains '=', it's not a caption
- if items[-1].find('=') == -1:
- caption = items[-1]
- items = items[1:-1]
- else:
- caption = link
- items = items[1:]
-
- # parse the relevant items
- params = _buildImageParams(items)
- img_path = _getImagePath(link)
-
- template = jingo.env.get_template('wikiparser/hook_image.html')
- r_kwargs = {'img_path': img_path, 'caption': caption, 'params': params}
- return template.render(**r_kwargs)
-
-
class WikiParser(Parser):
"""Wrapper for wikimarkup which adds Kitsune-specific callbacks and setup.
"""
@@ -138,11 +87,72 @@ def __init__(self, base_url=None):
super(WikiParser, self).__init__(base_url)
# Register default hooks
- self.registerInternalLinkHook(None, _hook_internal_link)
- self.registerInternalLinkHook('Image', _hook_image_tag)
+ self.registerInternalLinkHook(None, self._hook_internal_link)
+ self.registerInternalLinkHook('Image', self._hook_image_tag)
+
+ def parse(self, text, show_toc=None, tags=None, attributes=None,
+ locale=settings.WIKI_DEFAULT_LANGUAGE):
+ """Given wiki markup, return HTML.
+
+ Pass a locale to get all the hooks to look up Documents or Media
+ (Video, Image) for that locale. We key Documents by title and locale,
+ so both are required to identify it for a e.g. link.
+
+ Since py-wikimarkup's hooks don't offer custom paramters for callbacks,
+ we're using self.locale to keep things simple."""
+ self.locale = locale
- def parse(self, text, show_toc=None, tags=None, attributes=None):
- """Given wiki markup, return HTML."""
parser_kwargs = {'tags': tags} if tags else {}
return super(WikiParser, self).parse(text, show_toc=show_toc,
attributes=attributes or ALLOWED_ATTRIBUTES, **parser_kwargs)
+
+ def _hook_internal_link(self, parser, space, name):
+ """Parses text and returns internal link."""
+ link = text = name
+
+ # Split on pipe -- [[href|name]]
+ if '|' in name:
+ link, text = link.split('|', 1)
+
+ hash = ''
+ if '#' in link:
+ link, hash = link.split('#', 1)
+
+ # Sections use _, page names use +
+ if hash != '':
+ hash = '#' + hash.replace(' ', '_')
+
+ # Links to this page can just contain href="#hash"
+ if link == '' and hash != '':
+ return u'<a href="%s">%s</a>' % (hash, text)
+
+ link = _getWikiLink(link, self.locale)
+ return u'<a href="%s%s">%s</a>' % (link, hash, text)
+
+ def _hook_image_tag(self, parser, space, name):
+ """Adds syntax for inserting images."""
+ link = name
+ caption = name
+ params = {}
+
+ # Parse the inner syntax, e.g. [[Image:src|option=val|caption]]
+ separator = name.find('|')
+ items = []
+ if separator != -1:
+ items = link.split('|')
+ link = items[0]
+ # If the last item contains '=', it's not a caption
+ if items[-1].find('=') == -1:
+ caption = items[-1]
+ items = items[1:-1]
+ else:
+ caption = link
+ items = items[1:]
+
+ # parse the relevant items
+ params = _buildImageParams(items, self.locale)
+ img_path = _getImagePath(link)
+
+ template = jingo.env.get_template('wikiparser/hook_image.html')
+ r_kwargs = {'img_path': img_path, 'caption': caption, 'params': params}
+ return template.render(**r_kwargs)
@@ -1,3 +1,5 @@
+from functools import partial
+
from django.conf import settings
from nose.tools import eq_
@@ -22,6 +24,10 @@ def doc_rev_parser(content, title='Installing Firefox', parser_cls=WikiParser):
return (d, r, p)
+_buildImageParamsDefault = partial(_buildImageParams,
+ locale=settings.WIKI_DEFAULT_LANGUAGE)
+
+
class TestWikiParser(TestCase):
fixtures = ['users.json']
@@ -42,33 +48,33 @@ def test_image_path_special_chars(self):
def test_image_params_page(self):
"""_buildImageParams handles wiki pages."""
items = ['page=Installing Firefox']
- params = _buildImageParams(items)
+ params = _buildImageParamsDefault(items)
eq_('/en-US/kb/installing-firefox', params['link'])
def test_image_params_link(self):
"""_buildImageParams handles external links."""
items = ['link=http://example.com']
- params = _buildImageParams(items)
+ params = _buildImageParamsDefault(items)
eq_('http://example.com', params['link'])
def test_image_params_page_link(self):
"""_buildImageParams - wiki page overrides link."""
items = ['page=Installing Firefox', 'link=http://example.com']
- params = _buildImageParams(items)
+ params = _buildImageParamsDefault(items)
eq_('/en-US/kb/installing-firefox', params['link'])
def test_image_params_align(self):
"""Align valid options."""
align_vals = ('none', 'left', 'center', 'right')
for align in align_vals:
items = ['align=' + align]
- params = _buildImageParams(items)
+ params = _buildImageParamsDefault(items)
eq_(align, params['align'])
def test_image_params_align_invalid(self):
"""Align invalid options."""
items = ['align=zzz']
- params = _buildImageParams(items)
+ params = _buildImageParamsDefault(items)
assert not 'align' in params, 'Align is present in params'
def test_image_params_valign(self):
@@ -77,38 +83,39 @@ def test_image_params_valign(self):
'middle', 'bottom', 'text-bottom')
for valign in valign_vals:
items = ['valign=' + valign]
- params = _buildImageParams(items)
+ params = _buildImageParamsDefault(items)
eq_(valign, params['valign'])
def test_image_params_valign_invalid(self):
"""Vertical align invalid options."""
items = ['valign=zzz']
- params = _buildImageParams(items)
+ params = _buildImageParamsDefault(items)
assert not 'valign' in params, 'Vertical align is present in params'
def test_image_params_alt(self):
"""Image alt override."""
items = ['alt=some alternative text']
- params = _buildImageParams(items)
+ params = _buildImageParamsDefault(items)
eq_('some alternative text', params['alt'])
def test_image_params_frameless(self):
"""Frameless image."""
items = ['frameless']
- params = _buildImageParams(items)
+ params = _buildImageParamsDefault(items)
eq_(True, params['frameless'])
def test_image_params_width_height(self):
"""Image width."""
items = ['width=10', 'height=20']
- params = _buildImageParams(items)
+ params = _buildImageParamsDefault(items)
eq_('10', params['width'])
eq_('20', params['height'])
def test_get_wiki_link(self):
"""Wiki links are properly built for existing pages."""
eq_('/en-US/kb/installing-firefox',
- _getWikiLink('Installing Firefox'))
+ _getWikiLink('Installing Firefox',
+ locale=settings.WIKI_DEFAULT_LANGUAGE))
def test_showfor(self):
"""<showfor> tags should be escaped, not obeyed."""
Oops, something went wrong.

0 comments on commit e664ef5

Please sign in to comment.