You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@allura.apache.org by jo...@apache.org on 2013/07/16 22:17:04 UTC
[03/27] git commit: [#6220] ticket:383 embed macro (youtube support
only)
[#6220] ticket:383 embed macro (youtube support only)
Project: http://git-wip-us.apache.org/repos/asf/incubator-allura/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-allura/commit/f1938c5e
Tree: http://git-wip-us.apache.org/repos/asf/incubator-allura/tree/f1938c5e
Diff: http://git-wip-us.apache.org/repos/asf/incubator-allura/diff/f1938c5e
Branch: refs/heads/cj/4656
Commit: f1938c5e421cc7f77f1c6c02c7bf18fbfbfdd912
Parents: 885ef99
Author: Yuriy Arhipov <yu...@yandex.ru>
Authored: Tue Jul 9 15:14:47 2013 +0400
Committer: Tim Van Steenburgh <tv...@gmail.com>
Committed: Wed Jul 10 21:29:19 2013 +0000
----------------------------------------------------------------------
Allura/allura/lib/macro.py | 11 +++++++++++
Allura/allura/lib/markdown_extensions.py | 6 +++---
Allura/allura/lib/utils.py | 11 +++++++++++
Allura/allura/tests/test_globals.py | 7 +++++++
Allura/allura/tests/test_utils.py | 12 ++++++++++++
requirements-common.txt | 1 +
6 files changed, 45 insertions(+), 3 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/f1938c5e/Allura/allura/lib/macro.py
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/macro.py b/Allura/allura/lib/macro.py
index 27c0d35..aac20ff 100644
--- a/Allura/allura/lib/macro.py
+++ b/Allura/allura/lib/macro.py
@@ -20,6 +20,8 @@ import random
import shlex
import logging
import traceback
+import oembed
+import jinja2
from operator import attrgetter
import pymongo
@@ -374,3 +376,12 @@ def members(limit=20):
response = users.display(users=output, over_limit=over_limit)
return response
+@macro()
+def embed(url=None):
+ consumer = oembed.OEmbedConsumer()
+ endpoint = oembed.OEmbedEndpoint('http://www.youtube.com/oembed', ['http://*.youtube.com/*', 'https://*.youtube.com/*'])
+ consumer.addEndpoint(endpoint)
+ try:
+ return jinja2.Markup('<div class="grid-20">%s</div>' % consumer.embed(url)['html'])
+ except oembed.OEmbedNoEndpoint as e:
+ return '[[embed url=%s]]' % url
http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/f1938c5e/Allura/allura/lib/markdown_extensions.py
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/markdown_extensions.py b/Allura/allura/lib/markdown_extensions.py
index be2805a..4879bf4 100644
--- a/Allura/allura/lib/markdown_extensions.py
+++ b/Allura/allura/lib/markdown_extensions.py
@@ -24,11 +24,11 @@ from pylons import request
from BeautifulSoup import BeautifulSoup
import markdown
-import feedparser
from . import macro
from . import helpers as h
from allura import model as M
+from allura.lib.utils import ForgeHTMLSanitizer
log = logging.getLogger(__name__)
@@ -288,9 +288,9 @@ class HTMLSanitizer(markdown.postprocessors.Postprocessor):
def run(self, text):
try:
- p = feedparser._HTMLSanitizer('utf-8')
+ p = ForgeHTMLSanitizer('utf-8')
except TypeError: # $@%## pre-released versions from SOG
- p = feedparser._HTMLSanitizer('utf-8', '')
+ p = ForgeHTMLSanitizer('utf-8', '')
p.feed(text.encode('utf-8'))
return unicode(p.output(), 'utf-8')
http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/f1938c5e/Allura/allura/lib/utils.py
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/utils.py b/Allura/allura/lib/utils.py
index a88511c..d6796e5 100644
--- a/Allura/allura/lib/utils.py
+++ b/Allura/allura/lib/utils.py
@@ -43,6 +43,7 @@ from webhelpers.html import literal
from webob import exc
from pygments.formatters import HtmlFormatter
from setproctitle import getproctitle
+from feedparser import _HTMLSanitizer
from ew import jinja2_ew as ew
from ming.utils import LazyProperty
@@ -494,3 +495,13 @@ def serve_file(fp, filename, content_type, last_modified=None, cache_expires=Non
return tg.request.environ['wsgi.file_wrapper'](fp, block_size)
else:
return iter(lambda: fp.read(block_size), '')
+
+
+class ForgeHTMLSanitizer(_HTMLSanitizer):
+
+ def unknown_starttag(self, tag, attrs):
+ if 'iframe' in self.acceptable_elements:
+ self.acceptable_elements.remove('iframe')
+ if (tag == 'iframe') and (dict(attrs).get('src', '').startswith('http://www.youtube.com/embed/')):
+ self.acceptable_elements.append('iframe')
+ _HTMLSanitizer.unknown_starttag(self, tag, attrs)
http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/f1938c5e/Allura/allura/tests/test_globals.py
----------------------------------------------------------------------
diff --git a/Allura/allura/tests/test_globals.py b/Allura/allura/tests/test_globals.py
index 429ca27..60860a5 100644
--- a/Allura/allura/tests/test_globals.py
+++ b/Allura/allura/tests/test_globals.py
@@ -256,6 +256,13 @@ def test_macro_include_extra_br():
assert html.strip().replace('\n', '') == expected_html, html
+def test_macro_embed():
+ r = g.markdown_wiki.convert('[[embed url=http://www.youtube.com/watch?v=kOLpSPEA72U]]')
+ assert '''<div class="grid-20"><iframe height="270" src="http://www.youtube.com/embed/kOLpSPEA72U?feature=oembed" width="480"></iframe></div>''' in r
+ r = g.markdown_wiki.convert('[[embed url=http://vimeo.com/46163090]]')
+ assert_equal(r, '<div class="markdown_content"><p>[[embed url=http://vimeo.com/46163090]]</p></div>')
+
+
def test_markdown_toc():
with h.push_context('test', neighborhood='Projects'):
r = g.markdown_wiki.convert("""[TOC]
http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/f1938c5e/Allura/allura/tests/test_utils.py
----------------------------------------------------------------------
diff --git a/Allura/allura/tests/test_utils.py b/Allura/allura/tests/test_utils.py
index bd44ca2..494e893 100644
--- a/Allura/allura/tests/test_utils.py
+++ b/Allura/allura/tests/test_utils.py
@@ -229,3 +229,15 @@ class TestCodeStats(unittest.TestCase):
assert stats['line_count'] == 8
assert stats['data_line_count'] == 5
assert stats['code_size'] == len(blob.text)
+
+class TestHTMLSanitizer(unittest.TestCase):
+
+ def test_html_sanitizer_iframe(self):
+ p = utils.ForgeHTMLSanitizer('utf-8', '')
+ p.feed('<div><iframe></iframe></div>')
+ assert_equal(p.output(), '<div></div>')
+
+ def test_html_sanitizer_youtube_iframe(self):
+ p = utils.ForgeHTMLSanitizer('utf-8', '')
+ p.feed('<div><iframe src="http://www.youtube.com/embed/kOLpSPEA72U?feature=oembed"></iframe></div>')
+ assert_equal(p.output(),'<div><iframe src="http://www.youtube.com/embed/kOLpSPEA72U?feature=oembed"></iframe></div>')
http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/f1938c5e/requirements-common.txt
----------------------------------------------------------------------
diff --git a/requirements-common.txt b/requirements-common.txt
index 8dd3b5b..d496708 100644
--- a/requirements-common.txt
+++ b/requirements-common.txt
@@ -36,6 +36,7 @@ pysolr==2.1.0-beta
python-dateutil==1.5
python-magic==0.4.3
python-openid==2.2.5
+python-oembed==0.2.1
pytidylib==0.2.1
# for taskd proc name switching
setproctitle==1.1.7