class Mato::HtmlFilters::TaskList

Constants

CHECKED_MARK
CHECKED_MARK_FOR_EMPTY_TASK_LIST
DEFAULT_CHECKBOX_CLASS
DEFAULT_TASK_LIST_CLASS
UNCHECKED_MARK
UNCHECKED_MARK_FOR_EMPTY_TASK_LIST

Public Class Methods

new(task_list_class: DEFAULT_TASK_LIST_CLASS, checkbox_class: DEFAULT_CHECKBOX_CLASS, convert_empty_task_list: false) click to toggle source
# File lib/mato/html_filters/task_list.rb, line 14
def initialize(task_list_class: DEFAULT_TASK_LIST_CLASS, checkbox_class: DEFAULT_CHECKBOX_CLASS, convert_empty_task_list: false)
  @task_list_class = task_list_class
  @checkbox_class = checkbox_class
  @convert_empty_task_list = convert_empty_task_list
end

Public Instance Methods

build_checkbox_node(checked) click to toggle source
# File lib/mato/html_filters/task_list.rb, line 74
def build_checkbox_node(checked)
  Nokogiri::HTML4.fragment('<input type="checkbox"/>').tap do |fragment|
    checkbox = fragment.children.first
    checkbox["class"] = @checkbox_class
    checkbox["disabled"] = 'disabled'
    checkbox["checked"] = 'checked' if checked
  end
end
call(doc) click to toggle source

@param [Nokogiri::HTML4::DocumentFragment] doc

# File lib/mato/html_filters/task_list.rb, line 21
def call(doc)
  doc.search("li").each do |li|
    weave(li)
  end
end
checked_mark() click to toggle source
# File lib/mato/html_filters/task_list.rb, line 58
def checked_mark
  if @convert_empty_task_list
    CHECKED_MARK_FOR_EMPTY_TASK_LIST
  else
    CHECKED_MARK
  end
end
has_checked_mark?(text_node) click to toggle source
# File lib/mato/html_filters/task_list.rb, line 42
def has_checked_mark?(text_node)
  text_node&.content&.match?(checked_mark)
end
has_unchecked_mark?(text_node) click to toggle source
# File lib/mato/html_filters/task_list.rb, line 46
def has_unchecked_mark?(text_node)
  text_node&.content&.match?(unchecked_mark)
end
trim_mark(content, checked) click to toggle source
# File lib/mato/html_filters/task_list.rb, line 50
def trim_mark(content, checked)
  if checked
    content.sub(checked_mark, '')
  else
    content.sub(unchecked_mark, '')
  end
end
unchecked_mark() click to toggle source
# File lib/mato/html_filters/task_list.rb, line 66
def unchecked_mark
  if @convert_empty_task_list
    UNCHECKED_MARK_FOR_EMPTY_TASK_LIST
  else
    UNCHECKED_MARK
  end
end
weave(li) click to toggle source

@param [Nokogiri::XML::Node] li

# File lib/mato/html_filters/task_list.rb, line 28
def weave(li)
  text_node = li.xpath('./p[1]/text()').first || li.xpath('.//text()').first
  checked = has_checked_mark?(text_node)
  unchecked = has_unchecked_mark?(text_node)

  return unless checked || unchecked

  li["class"] = @task_list_class

  text_node.content = trim_mark(text_node.content, checked)
  checkbox = build_checkbox_node(checked)
  text_node.add_previous_sibling(checkbox)
end