Skip to content

Commit

Permalink
Ignore specific element classes in the abbreviate filter. Fixes #95.
Browse files Browse the repository at this point in the history
  • Loading branch information
cdchapman committed Oct 24, 2019
1 parent cc94e19 commit e09cde4
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
10 changes: 6 additions & 4 deletions lib/filters/abbreviate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ class Abbreviate < Nanoc::Filter
include LifePreserver::Dictionaries

ABBREVIATION_REGEX ||= /([[:alnum:]]+(?:[\-;][[[:upper:]][[:digit:]]]+)*[[[:alnum:]]&&[^s]])/
IGNORE_CLASSES ||= Set.new(%w(address handle identifier prefix oldstyle tel titling uri)).freeze

identifier :abbreviate

requires 'nokogiri'
requires 'nokogiri', 'set'

def run(content, params = {})
abbreviations = params[:abbreviations] || supported_abbreviations
Expand All @@ -34,12 +35,13 @@ def abbreviate_context(content, params, abbreviations)
end

def abbreviate_html_like(content, params, abbreviations)
ignore_classes = params.fetch(:ignore, IGNORE_CLASSES)
type = params.fetch(:type)

parser = parser_for(type)
content = fix_content(content, type)

nokogiri_process(content, parser, type, abbreviations)
nokogiri_process(content, parser, type, abbreviations, ignore_classes)
end

def parser_for(type)
Expand All @@ -64,7 +66,7 @@ def fix_content(content, type)
end
end

def nokogiri_process(content, klass, type, abbreviations)
def nokogiri_process(content, klass, type, abbreviations, ignore_classes)
visited_abbreviations = []

doc = content =~ /<html[\s>]/ ? klass.parse(content) : klass.fragment(content)
Expand All @@ -75,7 +77,7 @@ def nokogiri_process(content, klass, type, abbreviations)
next unless parent.element?

abbreviated_text = node.text.dup.gsub(ABBREVIATION_REGEX) do |word|
if abbreviations.key?(word.to_sym) && parent.name != 'abbr'
if abbreviations.key?(word.to_sym) && parent.name != 'abbr' && !ignore_classes.include?(parent['class'])
if visited_abbreviations.include?(word.to_sym)
"<abbr>#{word}</abbr>"
else
Expand Down
4 changes: 3 additions & 1 deletion lib/filters/old_style_figures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

class OldStyleFigures < Nanoc::Filter

IGNORE_CLASSES ||= Set.new(%w(address handle identifier prefix oldstyle tel titling uri)).freeze

identifier :old_style_figures

IGNORE_CLASSES ||= Set.new(%w(address handle identifier prefix oldstyle tel titling uri)).freeze
requires 'nokogiri', 'set'

def run(content, params = {})
case params[:type]
Expand Down

0 comments on commit e09cde4

Please sign in to comment.