Skip to content

Commit

Permalink
Youtube plugin add start at option (#3604)
Browse files Browse the repository at this point in the history
* add start_at option to youtube plugin

* add test for youtube start_at directive option

* add youtube start_at option to CHANGES.txt

* update youtube directive docs to include start_at option

* fix lint error
  • Loading branch information
marianoguerra committed Mar 8, 2022
1 parent 1bc90dc commit 09fb875
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Features
--------

* Gallery index pages support the `status` flag (Issue #3598)
* Add ``start_at`` option to youtube directive (Issue #3603)


New in v8.2.0
Expand Down
3 changes: 2 additions & 1 deletion docs/manual.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2622,13 +2622,14 @@ Once you have that, all you need to do is:
.. youtube:: 8N_tupPBtWQ
Supported options: ``height``, ``width``, ``align`` (one of ``left``,
Supported options: ``height``, ``width``, ``start_at``, ``align`` (one of ``left``,
``center``, ``right``) — all are optional. Example:

.. code:: restructuredtext
.. youtube:: 8N_tupPBtWQ
:align: center
:start_at: 4
Vimeo
~~~~~
Expand Down
13 changes: 11 additions & 2 deletions nikola/plugins/compile/rest/youtube.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def set_site(self, site):
CODE = """\
<div class="youtube-video{align}">
<iframe width="{width}" height="{height}"
src="https://www.youtube-nocookie.com/embed/{yid}?rel=0&wmode=transparent"
src="https://www.youtube-nocookie.com/embed/{yid}?rel=0&wmode=transparent{start_at}"
frameborder="0" allow="encrypted-media" allowfullscreen
></iframe>
</div>"""
Expand All @@ -69,7 +69,8 @@ class Youtube(Directive):
option_spec = {
"width": directives.unchanged,
"height": directives.unchanged,
"align": _align_choice
"align": _align_choice,
"start_at": directives.unchanged
}

def run(self):
Expand All @@ -85,6 +86,14 @@ def run(self):
options['align'] = ' align-' + self.options['align']
else:
options['align'] = ''

start_at = options.get('start_at')

if start_at:
options['start_at'] = '&start=' + start_at
else:
options['start_at'] = ''

return [nodes.raw('', CODE.format(**options), format='html')]

def check_content(self):
Expand Down
23 changes: 23 additions & 0 deletions tests/test_rst_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,29 @@ def test_youtube_iframe():
)


def test_youtube_iframe_start_at():
"""Test Youtube iframe tag generation with start_at attribute"""

sample = ".. youtube:: YID\n :height: 400\n :width: 600\n :start_at: 60"
html = get_html_from_rst(sample)
assert_html_contains(
html,
"iframe",
attributes={
"src": (
"https://www.youtube-nocookie.com"
"/embed/YID?rel=0&"
"wmode=transparent&start=60"
),
"height": "400",
"width": "600",
"frameborder": "0",
"allowfullscreen": "",
"allow": "encrypted-media",
},
)


def test_vimeo(disable_vimeo_api_query):
"""Test Vimeo iframe tag generation"""

Expand Down

0 comments on commit 09fb875

Please sign in to comment.