-
Notifications
You must be signed in to change notification settings - Fork 81
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Torna carregamento de imagens "lazy" por padrão (conversor customizado asciidoctor) #28
Open
arturo32
wants to merge
1
commit into
pythonfluente:main
Choose a base branch
from
arturo32:addLazyLoadingImagesConverter
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
#!/bin/bash | ||
set -e # exit when any command fails | ||
asciidoctor livro.adoc -o index.html | ||
asciidoctor -r ./ferramentas/addLazyLoadingImages.rb livro.adoc -o index.html | ||
open index.html |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
class AddLazyLoadingImages < (Asciidoctor::Converter.for 'html5') | ||
register_for 'html5' | ||
|
||
def convert_image node | ||
target = node.attr 'target' | ||
width_attr = (node.attr? 'width') ? %( width="#{node.attr 'width'}") : '' | ||
height_attr = (node.attr? 'height') ? %( height="#{node.attr 'height'}") : '' | ||
|
||
# https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/loading | ||
loading_attr_value = (node.attr 'loading') || (node.document.attr 'image-loading') | ||
if loading_attr_value && loading_attr_value != 'eager' && loading_attr_value != 'lazy' | ||
logger.warn 'valid values for attribute "loading" are "eager" and "lazy". found: ' + loading_attr_value | ||
end | ||
loading_attr = loading_attr_value ? %( loading="#{loading_attr_value}") : '' | ||
|
||
if ((node.attr? 'format', 'svg') || (target.include? '.svg')) && node.document.safe < SafeMode::SECURE | ||
if node.option? 'inline' | ||
img = (read_svg_contents node, target) || %(<span class="alt">#{node.alt}</span>) | ||
elsif node.option? 'interactive' | ||
fallback = (node.attr? 'fallback') ? %(<img src="#{node.image_uri node.attr 'fallback'}" alt="#{encode_attribute_value node.alt}"#{width_attr}#{height_attr}#{loading_attr}#{@void_element_slash}>) : %(<span class="alt">#{node.alt}</span>) | ||
img = %(<object type="image/svg+xml" data="#{src = node.image_uri target}"#{width_attr}#{height_attr}>#{fallback}</object>) | ||
else | ||
img = %(<img src="#{src = node.image_uri target}" alt="#{encode_attribute_value node.alt}"#{width_attr}#{height_attr}#{loading_attr}#{@void_element_slash}>) | ||
end | ||
else | ||
img = %(<img src="#{src = node.image_uri target}" alt="#{encode_attribute_value node.alt}"#{width_attr}#{height_attr}#{loading_attr}#{@void_element_slash}>) | ||
end | ||
if (node.attr? 'link') && ((href_attr_val = node.attr 'link') != 'self' || (href_attr_val = src)) | ||
img = %(<a class="image" href="#{href_attr_val}"#{(append_link_constraint_attrs node).join}>#{img}</a>) | ||
end | ||
id_attr = node.id ? %( id="#{node.id}") : '' | ||
classes = ['imageblock'] | ||
classes << (node.attr 'float') if node.attr? 'float' | ||
classes << %(text-#{node.attr 'align'}) if node.attr? 'align' | ||
classes << node.role if node.role | ||
class_attr = %( class="#{classes.join ' '}") | ||
title_el = node.title? ? %(\n<div class="title">#{node.captioned_title}</div>) : '' | ||
%(<div#{id_attr}#{class_attr}> | ||
<div class="content"> | ||
#{img} | ||
</div>#{title_el} | ||
</div>) | ||
end | ||
|
||
def convert_inline_image node | ||
target = node.target | ||
if (type = node.type || 'image') == 'icon' | ||
if (icons = node.document.attr 'icons') == 'font' | ||
i_class_attr_val = %(fa fa-#{target}) | ||
i_class_attr_val = %(#{i_class_attr_val} fa-#{node.attr 'size'}) if node.attr? 'size' | ||
if node.attr? 'flip' | ||
i_class_attr_val = %(#{i_class_attr_val} fa-flip-#{node.attr 'flip'}) | ||
elsif node.attr? 'rotate' | ||
i_class_attr_val = %(#{i_class_attr_val} fa-rotate-#{node.attr 'rotate'}) | ||
end | ||
attrs = (node.attr? 'title') ? %( title="#{node.attr 'title'}") : '' | ||
img = %(<i class="#{i_class_attr_val}"#{attrs}></i>) | ||
elsif icons | ||
attrs = (node.attr? 'width') ? %( width="#{node.attr 'width'}") : '' | ||
attrs = %(#{attrs} height="#{node.attr 'height'}") if node.attr? 'height' | ||
attrs = %(#{attrs} title="#{node.attr 'title'}") if node.attr? 'title' | ||
img = %(<img src="#{src = node.icon_uri target}" alt="#{encode_attribute_value node.alt}"#{attrs}#{@void_element_slash}>) | ||
else | ||
img = %([#{node.alt}]) | ||
end | ||
else | ||
attrs = (node.attr? 'width') ? %( width="#{node.attr 'width'}") : '' | ||
attrs = %(#{attrs} height="#{node.attr 'height'}") if node.attr? 'height' | ||
attrs = %(#{attrs} title="#{node.attr 'title'}") if node.attr? 'title' | ||
|
||
# https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/loading | ||
loading_attr_value = (node.attr 'loading') || (node.document.attr 'image-loading') | ||
if loading_attr_value && loading_attr_value != 'eager' && loading_attr_value != 'lazy' | ||
logger.warn 'valid values for attribute "loading" are "eager" and "lazy". found: ' + loading_attr_value | ||
end | ||
attrs = %(#{attrs} loading="#{loading_attr_value}") if loading_attr_value | ||
Comment on lines
+71
to
+76
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pedaço de código que é diferente do código fonte da função |
||
|
||
if ((node.attr? 'format', 'svg') || (target.include? '.svg')) && node.document.safe < SafeMode::SECURE | ||
if node.option? 'inline' | ||
img = (read_svg_contents node, target) || %(<span class="alt">#{node.alt}</span>) | ||
elsif node.option? 'interactive' | ||
fallback = (node.attr? 'fallback') ? %(<img src="#{node.image_uri node.attr 'fallback'}" alt="#{encode_attribute_value node.alt}"#{attrs}#{@void_element_slash}>) : %(<span class="alt">#{node.alt}</span>) | ||
img = %(<object type="image/svg+xml" data="#{src = node.image_uri target}"#{attrs}>#{fallback}</object>) | ||
else | ||
img = %(<img src="#{src = node.image_uri target}" alt="#{encode_attribute_value node.alt}"#{attrs}#{@void_element_slash}>) | ||
end | ||
else | ||
img = %(<img src="#{src = node.image_uri target}" alt="#{encode_attribute_value node.alt}"#{attrs}#{@void_element_slash}>) | ||
end | ||
end | ||
if (node.attr? 'link') && ((href_attr_val = node.attr 'link') != 'self' || (href_attr_val = src)) | ||
img = %(<a class="image" href="#{href_attr_val}"#{(append_link_constraint_attrs node).join}>#{img}</a>) | ||
end | ||
class_attr_val = type | ||
if (role = node.role) | ||
class_attr_val = (node.attr? 'float') ? %(#{class_attr_val} #{node.attr 'float'} #{role}) : %(#{class_attr_val} #{role}) | ||
elsif node.attr? 'float' | ||
class_attr_val = %(#{class_attr_val} #{node.attr 'float'}) | ||
end | ||
%(<span class="#{class_attr_val}">#{img}</span>) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pedaço de código que é diferente do código fonte da função
convert_image
do asciidoctor.