mirror of
https://github.com/foo-dogsquared/asciidoctor-foodogsquared-extensions.git
synced 2025-01-30 22:57:56 +00:00
Convert to Nokogiri for HTML manipulation
This commit is contained in:
parent
66233f04e6
commit
a6283b72a1
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user