Class: JayCustomItemBullet::Filter

Inherits:
HTML::Pipeline::Filter
  • Object
show all
Defined in:
lib/jay_flavored_markdown/markdown_converter.rb

Defined Under Namespace

Classes: XPathSelectorFunction

Constant Summary collapse

BulletPattern =
/\(([a-zA-Z]|\d+)\)/.freeze
ItemPattern =

Pattern used to identify all “+ (1)“ style Useful when you need iterate over all items.

/
^
(?:\s*[-+*]|(?:\d+\.))? # optional list prefix
\s*                     # optional whitespace prefix
(                       # checkbox
    #{BulletPattern}
)
(?=\s)                  # followed by whitespace
/x
ListItemSelector =
".//li[bullet_list_item(.)]".freeze
ItemParaSelector =

Selects first P tag of an LI, if present

"./p[1]".freeze

Instance Method Summary collapse

Instance Method Details

#add_css_class(node, *new_class_names) ⇒ Object

Private: adds a CSS class name to a node, respecting existing class names.



1037
1038
1039
1040
1041
1042
# File 'lib/jay_flavored_markdown/markdown_converter.rb', line 1037

def add_css_class(node, *new_class_names)
  class_names = (node['class'] || '').split(' ')
  return if new_class_names.all? { |klass| class_names.include?(klass) }
  class_names.concat(new_class_names)
  node['class'] = class_names.uniq.join(' ')
end

#bullet_list_itemsObject

List of ‘BuletList::Item` objects that were recognized in the document. This is available in the result hash as `:bullet_list_items`.

Returns an Array of BulletList::Item objects.



984
985
986
# File 'lib/jay_flavored_markdown/markdown_converter.rb', line 984

def bullet_list_items
  result[:bullet_list_items] ||= []
end

#callObject



1030
1031
1032
1033
# File 'lib/jay_flavored_markdown/markdown_converter.rb', line 1030

def call
  filter!
  doc
end

#filter!Object

Filters the source for bullet list items.

Each item is wrapped in HTML to identify, style, and layer useful behavior on top of.

Modifications apply to the parsed document directly.

Returns nothing.



1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
# File 'lib/jay_flavored_markdown/markdown_converter.rb', line 1004

def filter!
  list_items.reverse.each do |li|
    # add_css_class(li.parent, 'bullet-list')

    outer, inner =
      if p = li.xpath(ItemParaSelector)[0]
        [p, p.inner_html]
      else
        [li, li.inner_html]
      end
    if match = (inner.chomp =~ ItemPattern && $1)
      # item = Bullet::Item.new(match, inner)
      # prepend because we're iterating in reverse
      # bullet_list_items.unshift item

      add_css_class(li, 'bullet-list-item')
      outer.inner_html = render_bullet_list_item(inner)
    end
  end
end

#list_itemsObject

Public: Select all bullet lists from the ‘doc`.

Returns an Array of Nokogiri::XML::Element objects for ordered and unordered lists.



992
993
994
# File 'lib/jay_flavored_markdown/markdown_converter.rb', line 992

def list_items
  doc.xpath(ListItemSelector, XPathSelectorFunction)
end

#render_bullet_list_item(item) ⇒ Object



1025
1026
1027
1028
# File 'lib/jay_flavored_markdown/markdown_converter.rb', line 1025

def render_bullet_list_item(item)
  Nokogiri::HTML.fragment \
    item.sub(ItemPattern, '<span class="bullet-list-marker">\1</span>'), 'utf-8'
end