|
| 1 | +# encoding: utf-8 |
| 2 | +require 'html/pipeline' |
| 3 | + |
| 4 | +class TaskList |
| 5 | + # Returns a `Nokogiri::DocumentFragment` object. |
| 6 | + def self.filter(*args) |
| 7 | + Filter.call(*args) |
| 8 | + end |
| 9 | + |
| 10 | + # TaskList filter replaces task list item markers (`[ ]` and `[x]`) with |
| 11 | + # checkboxes, marked up with metadata and behavior. |
| 12 | + # |
| 13 | + # This should be run on the HTML generated by the Markdown filter, after the |
| 14 | + # SanitizationFilter. |
| 15 | + # |
| 16 | + # Syntax |
| 17 | + # ------ |
| 18 | + # |
| 19 | + # Task list items must be in a list format: |
| 20 | + # |
| 21 | + # ``` |
| 22 | + # - [ ] incomplete |
| 23 | + # - [x] complete |
| 24 | + # ``` |
| 25 | + # |
| 26 | + # Results |
| 27 | + # ------- |
| 28 | + # |
| 29 | + # The following keys are written to the result hash: |
| 30 | + # :task_list_items - An array of TaskList::Item objects. |
| 31 | + class Filter < HTML::Pipeline::Filter |
| 32 | + |
| 33 | + ListSelector = [ |
| 34 | + # select UL/OL |
| 35 | + ".//li[starts-with(text(),'[ ]')]/..", |
| 36 | + ".//li[starts-with(text(),'[x]')]/..", |
| 37 | + # and those wrapped in Ps |
| 38 | + ".//li/p[1][starts-with(text(),'[ ]')]/../..", |
| 39 | + ".//li/p[1][starts-with(text(),'[x]')]/../.." |
| 40 | + ].join(' | ').freeze |
| 41 | + |
| 42 | + # Selects all LIs from a TaskList UL/OL |
| 43 | + ItemSelector = ".//li".freeze |
| 44 | + |
| 45 | + # Selects first P tag of an LI, if present |
| 46 | + ItemParaSelector = ".//p[1]".freeze |
| 47 | + |
| 48 | + attr_accessor :index |
| 49 | + |
| 50 | + def initialize(*) |
| 51 | + @index = 0 |
| 52 | + super |
| 53 | + end |
| 54 | + |
| 55 | + # Private: increments and returns the next item index Integer. |
| 56 | + def next_index |
| 57 | + @index += 1 |
| 58 | + end |
| 59 | + |
| 60 | + # List of `TaskList::Item` objects that were recognized in the document. |
| 61 | + # This is available in the result hash as `:task_list_items`. |
| 62 | + # |
| 63 | + # Returns an Array of TaskList::Item objects. |
| 64 | + def task_list_items |
| 65 | + result[:task_list_items] ||= [] |
| 66 | + end |
| 67 | + |
| 68 | + # Renders the item checkbox in a span including the item index and state. |
| 69 | + # |
| 70 | + # Returns an HTML-safe String. |
| 71 | + def render_item_checkbox(item) |
| 72 | + %(<input type="checkbox" |
| 73 | + class="task-list-item-checkbox" |
| 74 | + data-item-index="#{item.index}" |
| 75 | + #{'checked="checked"' if item.complete?} |
| 76 | + data-item-complete="#{item.complete? ? 1 : 0}" |
| 77 | + disabled="disabled" |
| 78 | + />) |
| 79 | + end |
| 80 | + |
| 81 | + # Public: Marks up the task list item checkbox with metadata and behavior. |
| 82 | + # |
| 83 | + # NOTE: produces a string that, when assigned to a Node's `inner_html`, |
| 84 | + # will corrupt the string contents' encodings. Instead, we parse the |
| 85 | + # rendered HTML and explicitly set its encoding so that assignment will |
| 86 | + # not change the encodings. |
| 87 | + # |
| 88 | + # See [this pull](https://github.com/github/github/pull/8505) for details. |
| 89 | + # |
| 90 | + # Returns the marked up task list item Nokogiri::XML::NodeSet object. |
| 91 | + def render_task_list_item(item) |
| 92 | + Nokogiri::HTML.fragment <<-html, 'utf-8' |
| 93 | + <label>#{ |
| 94 | + item.source.sub(TaskList::ItemPattern, render_item_checkbox(item)) |
| 95 | + }</label> |
| 96 | + html |
| 97 | + end |
| 98 | + |
| 99 | + # Public: Select all task lists from the `doc`. |
| 100 | + # |
| 101 | + # Returns an Array of Nokogiri::XML::Element objects for ordered and |
| 102 | + # unordered lists. |
| 103 | + def task_lists |
| 104 | + doc.xpath(ListSelector) |
| 105 | + end |
| 106 | + |
| 107 | + # Public: filters a Nokogiri::XML::Element ordered/unordered list, marking |
| 108 | + # up the list items in order to add behavior and include metadata. |
| 109 | + # |
| 110 | + # Modifies the provided node. |
| 111 | + # |
| 112 | + # Returns nothing. |
| 113 | + def filter_list(node) |
| 114 | + add_css_class(node, 'task-list') |
| 115 | + |
| 116 | + node.xpath(ItemSelector).each do |li| |
| 117 | + outer, inner = |
| 118 | + if p = li.xpath(ItemParaSelector)[0] |
| 119 | + [p, p.inner_html] |
| 120 | + else |
| 121 | + [li, li.inner_html] |
| 122 | + end |
| 123 | + if match = TaskList.item?(inner) |
| 124 | + item = TaskList::Item.new(next_index, match, inner) |
| 125 | + task_list_items << item |
| 126 | + |
| 127 | + add_css_class(li, 'task-list-item') |
| 128 | + outer.inner_html = render_task_list_item(item) |
| 129 | + end |
| 130 | + end |
| 131 | + end |
| 132 | + |
| 133 | + # Filters the source for task list items. |
| 134 | + # |
| 135 | + # Each item is wrapped in HTML to identify, style, and layer |
| 136 | + # useful behavior on top of. |
| 137 | + # |
| 138 | + # Modifications apply to the parsed document directly. |
| 139 | + # |
| 140 | + # Returns nothing. |
| 141 | + def filter! |
| 142 | + task_lists.each do |node| |
| 143 | + filter_list node |
| 144 | + end |
| 145 | + end |
| 146 | + |
| 147 | + def call |
| 148 | + filter! |
| 149 | + doc |
| 150 | + end |
| 151 | + |
| 152 | + # Private: adds a CSS class name to a node, respecting existing class |
| 153 | + # names. |
| 154 | + def add_css_class(node, *new_class_names) |
| 155 | + class_names = (node['class'] || '').split(' ') |
| 156 | + class_names.concat(new_class_names) |
| 157 | + node['class'] = class_names.uniq.join(' ') |
| 158 | + end |
| 159 | + end |
| 160 | +end |
0 commit comments