Skip to content

Commit

Permalink
Implement media rst directive as shortcode for markdown (Issue #2170)
Browse files Browse the repository at this point in the history
  • Loading branch information
ralsina committed Feb 14, 2016
1 parent 7638ac8 commit 3a67fc6
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
8 changes: 8 additions & 0 deletions CHANGES.txt
@@ -1,3 +1,11 @@
New in master
=============

Features
--------

* New Oembed-based media shortcode (Issue #2170)

New in v7.7.5
=============

Expand Down
7 changes: 7 additions & 0 deletions docs/manual.txt
Expand Up @@ -828,6 +828,13 @@ Built-in shortcodes

post-list
Will show a list of posts, see the `Post List directive for details <#post-list>`__

media
Display media embedded from a URL, for example, this will embed a youtube video::

{{% media https://www.youtube.com/watch?v=Nck6BZga7TQ %}}

In restructured text this shortcode will fail because docutils turns that URL to a link and everything breaks, use the `media directive <#media>`__ instead.


Template-based shortcodes
Expand Down
16 changes: 10 additions & 6 deletions nikola/plugins/compile/rest/media.py
Expand Up @@ -48,6 +48,7 @@ def set_site(self, site):
"""Set Nikola site."""
self.site = site
directives.register_directive('media', Media)
self.site.register_shortcode('media', _gen_html)
return super(Plugin, self).set_site(site)


Expand All @@ -60,9 +61,12 @@ class Media(Directive):

def run(self):
"""Run media directive."""
if micawber is None:
msg = req_missing(['micawber'], 'use the media directive', optional=True)
return [nodes.raw('', '<div class="text-error">{0}</div>'.format(msg), format='html')]

providers = micawber.bootstrap_basic()
return [nodes.raw('', micawber.parse_text(" ".join(self.arguments), providers), format='html')]
html = _gen_html(" ".join(self.arguments))
return [nodes.raw('', html, format='html')]

def _gen_html(url, *q, **kw):
if micawber is None:
msg = req_missing(['micawber'], 'use the media directive', optional=True)
return '<div class="text-error">{0}</div>'.format(msg)
providers = micawber.bootstrap_basic()
return micawber.parse_text(url, providers)
1 change: 1 addition & 0 deletions nikola/shortcodes.py
Expand Up @@ -186,6 +186,7 @@ def _parse_shortcode_args(data, start, shortcode_name, start_pos):
try:
pos = _skip_whitespace(data, pos, must_be_nontrivial=True)
except ParsingError:
from doit.tools import set_trace; set_trace()
if not args and not kwargs:
raise ParsingError("Shortcode '{0}' starting at {1} is not terminated correctly with '%}}}}'!".format(shortcode_name, _format_position(data, start_pos)))
else:
Expand Down

0 comments on commit 3a67fc6

Please sign in to comment.