Class: Mato::HtmlFilters::TaskList
- Inherits:
-
Object
- Object
- Mato::HtmlFilters::TaskList
- Defined in:
- lib/mato/html_filters/task_list.rb
Constant Summary collapse
- CHECKED_MARK =
/\A\[x\] /
- UNCHECKED_MARK =
/\A\[ \] /
- CHECKED_MARK_FOR_EMPTY_TASK_LIST =
/\A\[x\] ?/
- UNCHECKED_MARK_FOR_EMPTY_TASK_LIST =
/\A\[ \] ?/
- DEFAULT_TASK_LIST_CLASS =
"task-list-item"
- DEFAULT_CHECKBOX_CLASS =
"task-list-item-checkbox"
Instance Method Summary collapse
- #build_checkbox_node(checked) ⇒ Object
- #call(doc) ⇒ Object
- #checked_mark ⇒ Object
- #has_checked_mark?(text_node) ⇒ Boolean
- #has_unchecked_mark?(text_node) ⇒ Boolean
-
#initialize(task_list_class: DEFAULT_TASK_LIST_CLASS, checkbox_class: DEFAULT_CHECKBOX_CLASS, convert_empty_task_list: false) ⇒ TaskList
constructor
A new instance of TaskList.
- #trim_mark(content, checked) ⇒ Object
- #unchecked_mark ⇒ Object
- #weave(li) ⇒ Object
Constructor Details
#initialize(task_list_class: DEFAULT_TASK_LIST_CLASS, checkbox_class: DEFAULT_CHECKBOX_CLASS, convert_empty_task_list: false) ⇒ TaskList
Returns a new instance of TaskList.
14 15 16 17 18 |
# 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 |
Instance Method Details
#build_checkbox_node(checked) ⇒ Object
74 75 76 77 78 79 80 81 |
# 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) ⇒ Object
21 22 23 24 25 |
# 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 ⇒ Object
58 59 60 61 62 63 64 |
# 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) ⇒ Boolean
42 43 44 |
# 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) ⇒ Boolean
46 47 48 |
# 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) ⇒ Object
50 51 52 53 54 55 56 |
# 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 ⇒ Object
66 67 68 69 70 71 72 |
# 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) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 |
# 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 |