Class: PotMarkdown::Filters::CheckboxFilter

Inherits:
HTML::Pipeline::Filter
  • Object
show all
Defined in:
lib/pot_markdown/filters/checkbox_filter.rb

Constant Summary collapse

CHECKBOX_PATTERN =
/\A\[(?<type>\s+|x)\]/

Instance Method Summary collapse

Instance Method Details

#callObject



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/pot_markdown/filters/checkbox_filter.rb', line 4

def call
  doc.xpath('.//li').each do |node|
    child = node.children.first
    next unless child.name == 'text'
    checkbox, text = checkbox_filter(child.text)
    next unless checkbox
    node.children.first.replace(text)
    node.children.first.add_previous_sibling(checkbox)
    node.set_attribute('class', "#{node.attribute('class')} #{checkbox_class}".strip)
  end
  doc
end

#checkbox_classObject



32
33
34
# File 'lib/pot_markdown/filters/checkbox_filter.rb', line 32

def checkbox_class
  context[:checkbox_class] || 'task-list-item'
end

#checkbox_filter(text) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/pot_markdown/filters/checkbox_filter.rb', line 17

def checkbox_filter(text)
  checkbox = nil
  text = text.gsub(CHECKBOX_PATTERN) do
    checked = (Regexp.last_match(1) == 'x')
    checkbox =
      "<input type=\"checkbox\"#{' checked="checked"' if checked}#{' disabled="disabled"' if disable?} />"
    ''
  end
  [checkbox, text]
end

#disable?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/pot_markdown/filters/checkbox_filter.rb', line 28

def disable?
  context[:checkbox_enable].nil? || context[:checkbox_enable]
end