diff --git a/lib/asciidoctor-foodogsquared-extensions.rb b/lib/asciidoctor-foodogsquared-extensions.rb index f0ce2cd..c170f1d 100644 --- a/lib/asciidoctor-foodogsquared-extensions.rb +++ b/lib/asciidoctor-foodogsquared-extensions.rb @@ -1,3 +1,4 @@ # frozen_string_literal: true require 'asciidoctor/foodogsquared/extensions' +require 'asciidoctor/foodogsquared/converter' diff --git a/lib/asciidoctor/foodogsquared/chat-block/extension.rb b/lib/asciidoctor/foodogsquared/chat-block/extension.rb index 5214bdf..08244e7 100644 --- a/lib/asciidoctor/foodogsquared/chat-block/extension.rb +++ b/lib/asciidoctor/foodogsquared/chat-block/extension.rb @@ -10,54 +10,17 @@ module Asciidoctor::Foodogsquared::Extensions default_attributes 'state' => 'default' def process(parent, reader, attrs) - # You can think of this section as a pipeline constructing the HTML - # component for this block. Specifically, we're building one component that - # contains two output: the dialog image of our avatar and its content. attrs['name'] ||= attrs['avatar'] - block = create_block parent, :pass, nil, attrs, content_model: :compound - block.add_role('dialogblock') - - block << (create_html_fragment block, <<~HTML -
-
- HTML - - ) - + # Configuring the avatar-related attributes. attrs['avatarsdir'] ||= File.expand_path('./avatars', attrs['iconsdir']) attrs['avatarstype'] ||= parent.attributes['avatarstype'] || 'avif' + attrs['avatarsticker'] = "#{attrs['avatar'].to_kebab}/#{attrs['state'].to_kebab}.#{attrs['avatarstype']}" - avatar_sticker = "#{attrs['avatar'].to_kebab}/#{attrs['state'].to_kebab}.#{attrs['avatarstype']}" - avatar_img_attrs = { - 'target' => parent.image_uri(avatar_sticker, 'avatarsdir'), - 'alt' => attrs['name'] - } - avatar_imgblock = create_image_block block, avatar_img_attrs - - block << avatar_imgblock - block << (create_html_fragment block, <<~HTML -
-
- #{attrs['name']} - HTML - ) - - parse_content block, reader - - block << (create_html_fragment block, <<~HTML -
-
- HTML - ) + block = create_block parent, :chat, nil, attrs, content_model: :compound + block.add_role 'dialogblock' block end - - private - - def create_html_fragment(parent, html, attributes = nil) - create_block parent, :pass, html, attributes - end end end diff --git a/lib/asciidoctor/foodogsquared/converter.rb b/lib/asciidoctor/foodogsquared/converter.rb new file mode 100644 index 0000000..1c6f835 --- /dev/null +++ b/lib/asciidoctor/foodogsquared/converter.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +# foodogsquared's custom converter that is an extended version of the built-in +# HTML5 converter. It features custom blocks as well as their preferred output +# for certain blocks. +module Asciidoctor::Foodogsquared::Converter + class Html5Custom < (Asciidoctor::Converter.for 'html5') + register_for 'html5' + + def convert_chat(node) + attributes = [] + attributes << %(id="#{node.id}") if node.id + attributes << %(class="#{node.role}") if node.role + + avatar_sticker = node.attributes['avatarsticker'] + avatar_uri = node.parent.image_uri avatar_sticker, 'avatarsdir' + + <<~HTML +
+
+ #{node.attributes['name']} +
+
+ #{node.attributes['name']} + #{node.content} +
+
+ HTML + end + end +end diff --git a/spec/chat_block_processor_spec.rb b/spec/chat_block_processor_spec.rb index 80f9864..3843e98 100644 --- a/spec/chat_block_processor_spec.rb +++ b/spec/chat_block_processor_spec.rb @@ -10,17 +10,13 @@ describe ChatBlock do INPUT expected = <<~RESULT -
-
-
-
- foodogsquared -
-
-
-
- foodogsquared -
+
+
+ foodogsquared +
+
+ foodogsquared +

Hello there!

@@ -28,7 +24,7 @@ describe ChatBlock do RESULT actual = (Asciidoctor.convert input).tr_s '\n', '\n' - (expect actual).to eq expected.chomp + (expect actual).to include expected.chomp end it 'should create a basic chat block with non-default values with document attributes' do @@ -43,17 +39,13 @@ describe ChatBlock do INPUT expected = <<~RESULT -
-
-
-
- foodogsquared -
-
-
-
- foodogsquared -
+
+
+ foodogsquared +
+
+ foodogsquared +

Hello there!

@@ -61,7 +53,7 @@ describe ChatBlock do RESULT actual = (Asciidoctor.convert input).tr_s '\n', '\n' - (expect actual).to eq expected.chomp + (expect actual).to include expected.chomp end it 'should create a basic chat block with non-default values' do @@ -72,28 +64,29 @@ describe ChatBlock do [chat, foodogsquared, state=nervous, role=shake] ==== Hello there! + + *wow* ==== INPUT expected = <<~RESULT -
-
-
-
- foodogsquared -
-
-
-
- foodogsquared -
+
+
+ foodogsquared +
+
+ foodogsquared +

Hello there!

+
+
+

wow

RESULT actual = (Asciidoctor.convert input).tr_s '\n', '\n' - (expect actual).to eq expected.chomp + (expect actual).to include expected.chomp end end