From 36dffaf3e49bfa7f9ee2ec85dfdb1a6e81fc487f Mon Sep 17 00:00:00 2001 From: Gabriel Arazas Date: Fri, 24 Nov 2023 11:28:07 +0800 Subject: [PATCH] Add audio block for HTML5-modified --- .../converters/html5-extended.rb | 73 ++++++++++++++++++- spec/html5_extended_converter.rb | 48 ++++++++++++ 2 files changed, 120 insertions(+), 1 deletion(-) diff --git a/lib/asciidoctor/foodogsquared/converters/html5-extended.rb b/lib/asciidoctor/foodogsquared/converters/html5-extended.rb index edadee6..e07db01 100644 --- a/lib/asciidoctor/foodogsquared/converters/html5-extended.rb +++ b/lib/asciidoctor/foodogsquared/converters/html5-extended.rb @@ -67,11 +67,82 @@ module Asciidoctor::Foodogsquared::Converters html.to_html(indent: 2) end + + # A modified version of the audio node except it can accept multiple + # sources. + def convert_audio(node) + html = Nokogiri::HTML5::DocumentFragment.parse <<~HTML +
+ +
+ HTML + figure = html.first_element_child + audio = figure.first_element_child + + add_common_attributes node, figure + add_boolean_attribute node, audio, %w[loop controls muted] + + if node.attr? 'sources' + _, sources = add_sources_elem node, audio, 'audio' + + sources_download_links = sources.map do |src| + %(#{src}) + end + fallback_text = html.document.parse "Download the audio at #{sources_download_links.join ', '}." + else + audio['src'] = node.attr 'target' + fallback_text = html.document.parse "Download the audio at #{node.attr 'target'}." + end + + audio.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['class'] = node.role if node.role + html.add_class node.role unless node.role.nil? html end + + def add_attributes_from_node(node, html, options) + options.each do |option| + html[option] = node.attr option if node.attr? option + end + + html + end + + def add_boolean_attribute(node, html, options) + options.each do |option| + html[option] = option if node.option? option + end + + html + end + + def add_sources_elem(node, html, media_type) + sources = node.attr('sources', '').split(',').each do |src| + src = html.document.create_element 'source' do |block| + block['src'] = src + + type = MIME::Types.type_for(src).find do |mime| + mime.media_type == media_type + end + block['type'] = type unless media_type.nil? + end + + html.add_child src + end + + [html, sources] + end end end diff --git a/spec/html5_extended_converter.rb b/spec/html5_extended_converter.rb index 3869f72..d9fc7ba 100644 --- a/spec/html5_extended_converter.rb +++ b/spec/html5_extended_converter.rb @@ -65,4 +65,52 @@ describe Asciidoctor::Foodogsquared::Converters::HTML5Modified do 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"] + INPUT + + expected = <<~HTML +
+ +
+ HTML + + actual = (Asciidoctor.convert input).chomp + (expect actual).to eq expected.chomp + end + + it 'should have an audio block with multiple and a caption' do + input = <<~INPUT + .Making up stuff right now + audio::./hello.mp3[sources="hello.mp3,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 element with a caption, ID, and classes' do + input = <<~INPUT + [#audio-sample.reverse] + .Making up stuff right now + audio::./hello.mp3[sources="hello.mp3,hello.webm"] + INPUT + + expected = <<~HTML +
+ +
Making up stuff right now
+ HTML + + actual = (Asciidoctor.convert input).chomp + (expect actual).to eq expected.chomp + end end