Add video block for HTML5-modified

This commit is contained in:
Gabriel Arazas 2023-11-24 11:43:33 +08:00
parent 09a93481f0
commit d868c0964c
No known key found for this signature in database
GPG Key ID: ADE0C41DAB221FCC
2 changed files with 90 additions and 0 deletions

View File

@ -105,6 +105,48 @@ module Asciidoctor::Foodogsquared::Converters
html.to_html(indent: 2)
end
# A modified version of the video block except it can accept multiple
# sources with the `sources` attribute. Also, much of the built-in
# Asciidoctor capabilities such as the ability to quickly link from YouTube
# are removed.
def convert_video(node)
html = Nokogiri::HTML5::DocumentFragment.parse <<~HTML
<figure>
<video></video>
</figure>
HTML
figure = html.first_element_child
video = figure.first_element_child
add_common_attributes node, figure
add_boolean_attribute node, video, %w[loop controls muted]
add_attributes_from_node node, video, %w[width height]
if node.attr? 'sources'
_, sources = add_sources_elem node, video, 'video'
sources_download_links = sources.map do |src|
%(<a href="#{src}">#{src}</a>)
end
fallback_text = html.document.parse "Download the video at #{sources_download_links.join ', '}."
else
video['src'] = node.attr 'target'
fallback_text = html.document.parse "Download the video at #{node.attr 'target'}."
end
video.add_child fallback_text
if node.title?
html.document.create_element 'figcaption' do |block|
block.inner_html = node.captioned_title
figure.add_child block
end
end
html.to_html(indent: 2)
end
def add_common_attributes(node, html)
html['id'] = node.id if node.id
html.add_class node.role unless node.role.nil?

View File

@ -66,6 +66,54 @@ describe Asciidoctor::Foodogsquared::Converters::HTML5Modified do
(expect actual).to eq expected.chomp
end
it 'should have a video block with multiple <source>' do
input = <<~INPUT
video::hello.mp4[sources="hello.mp4,hello.webm"]
INPUT
expected = <<~HTML
<figure>
<video><source src="hello.mp4" type="video/mp4"><source src="hello.webm" type="video/webm"><p>Download the video at <a href="hello.mp4">hello.mp4</a>, <a href="hello.webm">hello.webm</a>.</p></video>
</figure>
HTML
actual = (Asciidoctor.convert input).chomp
(expect actual).to eq expected.chomp
end
it 'should have a video block with multiple <source> and a caption' do
input = <<~INPUT
.Making up stuff right now
video::./hello.mp4[sources="hello.mp4,hello.webm"]
INPUT
expected = <<~HTML
<figure>
<video><source src="hello.mp4" type="video/mp4"><source src="hello.webm" type="video/webm"><p>Download the video at <a href="hello.mp4">hello.mp4</a>, <a href="hello.webm">hello.webm</a>.</p></video>
<figcaption>Making up stuff right now</figcaption></figure>
HTML
actual = (Asciidoctor.convert input).chomp
(expect actual).to eq expected.chomp
end
it 'should have a video block with a caption, ID, and classes' do
input = <<~INPUT
[#video-sample.reverse]
.Making up stuff right now
video::./hello.mp4[sources="hello.mp4,hello.webm"]
INPUT
expected = <<~HTML
<figure id="video-sample" class="reverse">
<video><source src="hello.mp4" type="video/mp4"><source src="hello.webm" type="video/webm"><p>Download the video at <a href="hello.mp4">hello.mp4</a>, <a href="hello.webm">hello.webm</a>.</p></video>
<figcaption>Making up stuff right now</figcaption></figure>
HTML
actual = (Asciidoctor.convert input).chomp
(expect actual).to eq expected.chomp
end
it 'should have an audio block with multiple <source>' do
input = <<~INPUT
audio::hello.mp3[sources="hello.mp3,hello.webm"]