2023-04-03 15:35:18 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class ChatBlock < Asciidoctor::Extensions::BlockProcessor
|
|
|
|
use_dsl
|
|
|
|
|
|
|
|
named :chat
|
|
|
|
on_context :example
|
|
|
|
name_positional_attributes 'avatar', 'state'
|
|
|
|
default_attributes 'state' => 'default', 'avatarstype' => 'webp'
|
|
|
|
|
|
|
|
def process(parent, reader, attrs)
|
|
|
|
block = create_block parent, :pass, nil, attrs, content_model: :compound
|
|
|
|
block.add_role('dialogblock')
|
|
|
|
|
|
|
|
# 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_html_fragment block, %(
|
2023-05-12 12:05:04 +00:00
|
|
|
<div role="figure" class="dialogblock dialogblock__box dialogblock__avatar--#{attrs['avatar']} #{attrs['role']}">
|
2023-04-03 15:35:18 +00:00
|
|
|
<div class="dialogblock dialogblock__avatar">
|
|
|
|
))
|
|
|
|
|
|
|
|
attrs['avatarsdir'] ||= File.expand_path('./avatars', attrs['iconsdir'])
|
|
|
|
|
2023-04-06 22:25:41 +00:00
|
|
|
avatar_sticker = "#{attrs['avatar'].to_kebab}/#{attrs['state'].to_kebab}.#{attrs['avatarstype']}"
|
2023-04-03 15:35:18 +00:00
|
|
|
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, %(
|
|
|
|
</div>
|
|
|
|
<div class="dialogblock dialogblock__text">
|
2023-04-17 08:22:03 +00:00
|
|
|
<small class="dialogblock dialogblock__avatar-name">#{attrs['name']}</small>
|
2023-04-03 15:35:18 +00:00
|
|
|
))
|
|
|
|
|
|
|
|
parse_content block, reader
|
|
|
|
|
|
|
|
block << (create_html_fragment block, %(
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
))
|
|
|
|
|
|
|
|
block
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def create_html_fragment(parent, html, attributes = nil)
|
|
|
|
create_block parent, :pass, html, attributes
|
|
|
|
end
|
|
|
|
end
|