From d868c0964cf67556d50214dfeb7c52451bd2cc12 Mon Sep 17 00:00:00 2001 From: Gabriel Arazas Date: Fri, 24 Nov 2023 11:43:33 +0800 Subject: [PATCH] Add video block for HTML5-modified --- .../converters/html5-extended.rb | 42 ++++++++++++++++ spec/html5_extended_converter.rb | 48 +++++++++++++++++++ 2 files changed, 90 insertions(+) diff --git a/lib/asciidoctor/foodogsquared/converters/html5-extended.rb b/lib/asciidoctor/foodogsquared/converters/html5-extended.rb index e07db01..40e3cff 100644 --- a/lib/asciidoctor/foodogsquared/converters/html5-extended.rb +++ b/lib/asciidoctor/foodogsquared/converters/html5-extended.rb @@ -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 +
+ +
+ 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| + %(#{src}) + 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? diff --git a/spec/html5_extended_converter.rb b/spec/html5_extended_converter.rb index d9fc7ba..36544bf 100644 --- a/spec/html5_extended_converter.rb +++ b/spec/html5_extended_converter.rb @@ -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 ' do + input = <<~INPUT + video::hello.mp4[sources="hello.mp4,hello.webm"] + INPUT + + expected = <<~HTML +
+ +
+ HTML + + actual = (Asciidoctor.convert input).chomp + (expect actual).to eq expected.chomp + end + + it 'should have a video block with multiple and a caption' do + input = <<~INPUT + .Making up stuff right now + video::./hello.mp4[sources="hello.mp4,hello.webm"] + INPUT + + expected = <<~HTML +
+ +
Making up stuff right now
+ 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 +
+ +
Making up stuff right now
+ HTML + + actual = (Asciidoctor.convert input).chomp + (expect actual).to eq expected.chomp + end + it 'should have an audio block with multiple ' do input = <<~INPUT audio::hello.mp3[sources="hello.mp3,hello.webm"]