Class: SlackMarkdown::Filters::ItalicFilter

Inherits:
HTML::Pipeline::Filter
  • Object
show all
Includes:
IgnorableAncestorTags
Defined in:
lib/slack_markdown/filters/italic_filter.rb

Constant Summary collapse

ITALIC_PATTERN =
/(?<=^|\W)_(.+)_(?=\W|$)/

Constants included from IgnorableAncestorTags

SlackMarkdown::Filters::IgnorableAncestorTags::DEFAULT_IGNORED_ANCESTOR_TAGS

Instance Method Summary collapse

Methods included from IgnorableAncestorTags

#ignored_ancestor_tags

Instance Method Details

#callObject



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/slack_markdown/filters/italic_filter.rb', line 11

def call
  doc.search(".//text()").each do |node|
    content = node.to_html
    next if has_ancestor?(node, ignored_ancestor_tags)
    next unless content.include?('_')
    html = italic_filter(content)
    next if html == content
    node.replace(html)
  end
  doc
end

#italic_filter(text) ⇒ Object



23
24
25
26
27
# File 'lib/slack_markdown/filters/italic_filter.rb', line 23

def italic_filter(text)
  text.gsub(ITALIC_PATTERN) do
    "<i>#{$1}</i>"
  end
end