Class: TaskList::Filter

Inherits:
HTML::Pipeline::Filter
  • Object
show all
Defined in:
lib/task_list/filter.rb

Overview

TaskList filter replaces task list item markers (‘[ ]` and `[x]`) with checkboxes, marked up with metadata and behavior.

This should be run on the HTML generated by the Markdown filter, after the SanitizationFilter.

Syntax


Task list items must be in a list format:

“‘

  • incomplete

  • x

    complete

“‘

Results


The following keys are written to the result hash:

:task_list_items - An array of TaskList::Item objects.

Constant Summary collapse

Incomplete =
"[ ]".freeze
Complete =
"[x]".freeze
IncompletePattern =

matches all whitespace

/\[[[:space:]]\]/.freeze
CompletePattern =

matches any capitalization

/\[[xX]\]/.freeze
ItemPattern =

Pattern used to identify all task list items. Useful when you need iterate over all items.

/
  ^
  (?:\s*[-+*]|(?:\d+\.))? # optional list prefix
  \s*                     # optional whitespace prefix
  (                       # checkbox
    #{CompletePattern}|
    #{IncompletePattern}
  )
  (?=\s)                  # followed by whitespace
/x

Instance Method Summary collapse

Instance Method Details

#callObject



108
109
110
111
# File 'lib/task_list/filter.rb', line 108

def call
  filter!
  doc
end

#filter!Object

Filters the source for task 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.



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/task_list/filter.rb', line 95

def filter!
  doc.xpath(".//li").reverse.each do |li|
    container = li.xpath("./p[1]")[0] || li
    next if not container.inner_html =~ ItemPattern

    item = TaskList::Item.new($1, container.inner_html)
    task_list_items.unshift item
    container.inner_html = render_task_list_item(item)
    li.parent.add_class('task-list')
    li.add_class('task-list-item')
  end
end

#render_item_checkbox(item) ⇒ Object

Renders the item checkbox in a span including the item state.

Returns an HTML-safe String.



64
65
66
67
68
69
70
# File 'lib/task_list/filter.rb', line 64

def render_item_checkbox(item)
  %(<input type="checkbox"
    class="task-list-item-checkbox"
    #{'checked="checked"' if item.complete?}
    disabled="disabled"
  />)
end

#render_task_list_item(item) ⇒ Object

Public: Marks up the task list item checkbox with metadata and behavior.

NOTE: produces a string that, when assigned to a Node’s ‘inner_html`, will corrupt the string contents’ encodings. Instead, we parse the rendered HTML and explicitly set its encoding so that assignment will not change the encodings.

See [this pull](github.com/github/github/pull/8505) for details.

Returns the marked up task list item Nokogiri::XML::NodeSet object.



82
83
84
85
# File 'lib/task_list/filter.rb', line 82

def render_task_list_item(item)
  Nokogiri::HTML.fragment \
    item.source.sub(ItemPattern, render_item_checkbox(item)), 'utf-8'
end

#task_list_itemsObject

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

Returns an Array of TaskList::Item objects.



57
58
59
# File 'lib/task_list/filter.rb', line 57

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