Class: Banzai::Filter::AttributesFilter
- Inherits:
-
HTML::Pipeline::Filter
- Object
- HTML::Pipeline::Filter
- Banzai::Filter::AttributesFilter
- Includes:
- Concerns::PipelineTimingCheck
- Defined in:
- lib/banzai/filter/attributes_filter.rb
Overview
Looks for attributes that are specified for an element. Follows the basic syntax laid out in github.com/jgm/commonmark-hs/blob/master/commonmark-extensions/test/attributes.md For example,
![](http://example.com/image.jpg){width=50%}
However we currently have the following limitations:
-
only support images
-
only support the ‘width` and `height` attributes
-
attributes can not span multiple lines
-
unsupported attributes are thrown away
Constant Summary collapse
- CSS =
'img'
- XPATH =
Gitlab::Utils::Nokogiri.css_to_xpath(CSS).freeze
- ATTRIBUTES_PATTERN =
%r{\A(?<matched>\{(?<attributes>.{1,100})\})}
- WIDTH_HEIGHT_REGEX =
%r{\A(?<name>height|width)="?(?<size>[\w%]{1,10})"?\z}
- VALID_SIZE_REGEX =
%r{\A\d{1,4}(%|px)?\z}
Constants included from Concerns::PipelineTimingCheck
Concerns::PipelineTimingCheck::MAX_PIPELINE_SECONDS
Instance Method Summary collapse
Methods included from Concerns::PipelineTimingCheck
Instance Method Details
#call ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/banzai/filter/attributes_filter.rb', line 25 def call doc.xpath(XPATH).each do |img| sibling = img.next next unless sibling && sibling.text? && sibling.content.first == '{' match = sibling.content.match(ATTRIBUTES_PATTERN) next unless match && match[:attributes] match[:attributes].split(' ').each do |attribute| next unless attribute.match?(WIDTH_HEIGHT_REGEX) attribute_match = attribute.match(WIDTH_HEIGHT_REGEX) img[attribute_match[:name].to_sym] = attribute_match[:size] if valid_size?(attribute_match[:size]) end sibling.content = sibling.content.sub(match[:matched], '') end doc end |