Skip to content

Commit

Permalink
Fix #2304 -- align options for youtube, soundcloud, vimeo directives
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Warrick <kwpolska@gmail.com>
  • Loading branch information
Kwpolska committed Apr 29, 2016
1 parent b7b884b commit 78adaf3
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 11 deletions.
2 changes: 2 additions & 0 deletions CHANGES.txt
Expand Up @@ -9,6 +9,8 @@ Features
Bugfixes
--------

* Add ``align`` options for ``youtube``, ``vimeo``, ``soundcloud``
reST directives (Issue #2304)
* Update ``FILE_METADATA_REGEXP`` example in docs (Issue #2296)
* Show “tags too similar” error instead of cryptic doit crash (Issue #2325)
* Fix crashes when tag appears multiple times in a post (Issue #2315)
Expand Down
9 changes: 9 additions & 0 deletions docs/manual.txt
Expand Up @@ -1662,6 +1662,9 @@ Once you have that, all you need to do is:

.. youtube:: 8N_tupPBtWQ

Supported options: ``height``, ``width``, ``align`` (one of ``left``,
``center``, ``right``) — all are optional.

Vimeo
~~~~~

Expand All @@ -1684,6 +1687,9 @@ You can override this if you wish:
:height: 240
:width: 320

Supported options: ``height``, ``width``, ``align`` (one of ``left``,
``center``, ``right``) — all are optional.

Soundcloud
~~~~~~~~~~

Expand All @@ -1705,6 +1711,9 @@ You can also embed playlists, via the `soundcloud_playlist` directive which work

.. soundcloud_playlist:: 9411706

Supported options: ``height``, ``width``, ``align`` (one of ``left``,
``center``, ``right``) — all are optional.

Code
~~~~

Expand Down
2 changes: 1 addition & 1 deletion nikola/data/themes/base/assets/css/rst.css
Expand Up @@ -191,7 +191,7 @@ img.align-center, .figure.align-center, object.align-center {
text-align: right }

/* reset inner alignment in figures */
div.align-right {
.figure.align-right {
text-align: inherit }

/* div.align-center * { */
Expand Down
8 changes: 8 additions & 0 deletions nikola/plugins/compile/rest/__init__.py
Expand Up @@ -36,6 +36,7 @@
import docutils.io
import docutils.readers.standalone
import docutils.writers.html4css1
import docutils.parsers.rst.directives

from nikola.plugin_categories import PageCompiler
from nikola.utils import unicode_str, get_logger, makedirs, write_metadata, STDERR_HANDLER
Expand Down Expand Up @@ -276,3 +277,10 @@ def rst2html(source, source_path=None, source_class=docutils.io.StringInput,
pub.publish(enable_exit_status=enable_exit_status)

return pub.writer.parts['docinfo'] + pub.writer.parts['fragment'], pub.document.reporter.max_level, pub.settings.record_dependencies

# Alignment helpers for extensions
_align_options_base = ('left', 'center', 'right')


def _align_choice(argument):
return docutils.parsers.rst.directives.choice(argument, _align_options_base + ("none", ""))
17 changes: 12 additions & 5 deletions nikola/plugins/compile/rest/soundcloud.py
Expand Up @@ -4,7 +4,7 @@

from docutils import nodes
from docutils.parsers.rst import Directive, directives

from nikola.plugins.compile.rest import _align_choice, _align_options_base

from nikola.plugin_categories import RestExtension

Expand All @@ -22,11 +22,13 @@ def set_site(self, site):
return super(Plugin, self).set_site(site)


CODE = ("""<iframe width="{width}" height="{height}"
CODE = """\
<div class="soundcloud-player{align}">
<iframe width="{width}" height="{height}"
scrolling="no" frameborder="no"
src="https://w.soundcloud.com/player/?url=http://api.soundcloud.com/{preslug}/"""
"""{sid}">
</iframe>""")
src="https://w.soundcloud.com/player/?url=http://api.soundcloud.com/{preslug}/{sid}">
</iframe>
</div>"""


class SoundCloud(Directive):
Expand All @@ -44,6 +46,7 @@ class SoundCloud(Directive):
option_spec = {
'width': directives.positive_int,
'height': directives.positive_int,
"align": _align_choice
}
preslug = "tracks"

Expand All @@ -57,6 +60,10 @@ def run(self):
'preslug': self.preslug,
}
options.update(self.options)
if self.options.get('align') in _align_options_base:
options['align'] = ' align-' + self.options['align']
else:
options['align'] = ''
return [nodes.raw('', CODE.format(**options), format='html')]

def check_content(self):
Expand Down
10 changes: 9 additions & 1 deletion nikola/plugins/compile/rest/vimeo.py
Expand Up @@ -28,6 +28,7 @@

from docutils import nodes
from docutils.parsers.rst import Directive, directives
from nikola.plugins.compile.rest import _align_choice, _align_options_base

import requests
import json
Expand All @@ -48,10 +49,12 @@ def set_site(self, site):
return super(Plugin, self).set_site(site)


CODE = """<iframe src="https://player.vimeo.com/video/{vimeo_id}"
CODE = """<div class="vimeo-video{align}">
<iframe src="https://player.vimeo.com/video/{vimeo_id}"
width="{width}" height="{height}"
frameborder="0" webkitAllowFullScreen="webkitAllowFullScreen" mozallowfullscreen="mozallowfullscreen" allowFullScreen="allowFullScreen">
</iframe>
</div>
"""

VIDEO_DEFAULT_HEIGHT = 500
Expand All @@ -73,6 +76,7 @@ class Vimeo(Directive):
option_spec = {
"width": directives.positive_int,
"height": directives.positive_int,
"align": _align_choice
}

# set to False for not querying the vimeo api for size
Expand All @@ -92,6 +96,10 @@ def run(self):
return err
self.set_video_size()
options.update(self.options)
if self.options.get('align') in _align_options_base:
options['align'] = ' align-' + self.options['align']
else:
options['align'] = ''
return [nodes.raw('', CODE.format(**options), format='html')]

def check_modules(self):
Expand Down
14 changes: 10 additions & 4 deletions nikola/plugins/compile/rest/youtube.py
Expand Up @@ -28,7 +28,7 @@

from docutils import nodes
from docutils.parsers.rst import Directive, directives

from nikola.plugins.compile.rest import _align_choice, _align_options_base

from nikola.plugin_categories import RestExtension

Expand All @@ -46,10 +46,11 @@ def set_site(self, site):


CODE = """\
<iframe width="{width}"
height="{height}"
<div class="youtube-video{align}">
<iframe width="{width}" height="{height}"
src="https://www.youtube.com/embed/{yid}?rel=0&amp;hd=1&amp;wmode=transparent"
></iframe>"""
></iframe>
</div>"""


class Youtube(Directive):
Expand All @@ -67,6 +68,7 @@ class Youtube(Directive):
option_spec = {
"width": directives.positive_int,
"height": directives.positive_int,
"align": _align_choice
}

def run(self):
Expand All @@ -78,6 +80,10 @@ def run(self):
'height': 344,
}
options.update(self.options)
if self.options.get('align') in _align_options_base:
options['align'] = ' align-' + self.options['align']
else:
options['align'] = ''
return [nodes.raw('', CODE.format(**options), format='html')]

def check_content(self):
Expand Down

0 comments on commit 78adaf3

Please sign in to comment.