Convert to Nokogiri for HTML manipulation

This commit is contained in:
Gabriel Arazas 2023-11-23 10:11:20 +08:00
parent 66233f04e6
commit a6283b72a1
No known key found for this signature in database
GPG Key ID: ADE0C41DAB221FCC

View File

@ -1,5 +1,6 @@
# frozen_string_literal: true
require 'nokogiri'
require 'mime/types'
module Asciidoctor::Foodogsquared::Converters
@ -11,29 +12,30 @@ module Asciidoctor::Foodogsquared::Converters
# Take note this is only intended for the author. The user has to explicitly
# require them somewhere to make use of this.
class HTML5Modified < Asciidoctor::Foodogsquared::Converter::Html5Extended
register_for 'html5'
def convert_paragraph(node)
attributes = html_attributes node
html = Nokogiri::XML::DocumentFragment.parse '<p></p>'
paragraph = html.first_element_child
add_common_attributes node, paragraph
if node.title?
<<~HTML
<p#{attributes.join ' '}>
<strong class="title">#{node.captioned_title}</strong>
#{node.content}
</p>
HTML
else
<<~HTML
<p#{attributes.join ' '}>#{node.content}</p>
HTML
title = html.document.create_element 'strong', class: 'title'
title.content = node.captioned_title
paragraph.add_child title
end
paragraph.inner_html += node.content
html.to_html
end
def html_attributes(node)
attributes = []
attributes << %(id="#{node.id}") if node.id
attributes << %(class="#{node.role}") if node.role
attributes
def add_common_attributes(node, html)
html['id'] = node.id if node.id
html['class'] = node.role if node.role
html
end
end
end