Class: Kramdown::Parser::Formdown

Inherits:
Kramdown
  • Object
show all
Defined in:
lib/kramdown/parser/formdown.rb

Constant Summary collapse

TEXT_FIELD_START =
/(_+)([@\#]?)\((.+?)\)/
TEXT_AREA_START =
/(_+)(\/+)\((.+?)\)/
CHECKBOX_FIELD_START =
/\[([\sxX])?\]/
RADIO_BUTTON_FIELD_START =
/\(([\sxX])?\)/
BUTTON_START =
/\[\s(.+)\s\]([\!])?/

Instance Method Summary collapse

Constructor Details

#initialize(source, options) ⇒ Formdown

Returns a new instance of Formdown.



14
15
16
17
# File 'lib/kramdown/parser/formdown.rb', line 14

def initialize(source, options)
  super
  @span_parsers.unshift :text_fields, :text_areas, :checkboxes, :buttons, :radio_buttons
end

Instance Method Details

#parse_buttonsObject



63
64
65
66
67
68
69
70
71
72
# File 'lib/kramdown/parser/formdown.rb', line 63

def parse_buttons
  @src.pos += @src.matched_size
  value, type = BUTTON_START.match(@src.matched).captures
  type = case type
  when '!' then :submit
  # else :button
  else :submit # TODO - Should we even support other types of buttons? Probably not... just make it a submit.
  end
  @tree.children << Element.new(:html_element, :input, type: type, value: value)
end

#parse_checkboxesObject



47
48
49
50
# File 'lib/kramdown/parser/formdown.rb', line 47

def parse_checkboxes
  @src.pos += @src.matched_size
  @tree.children << Element.new(:html_element, :input, type: :checkbox)
end

#parse_radio_buttonsObject



55
56
57
58
# File 'lib/kramdown/parser/formdown.rb', line 55

def parse_radio_buttons
  @src.pos += @src.matched_size
  @tree.children << Element.new(:html_element, :input, type: :radio)
end

#parse_text_areasObject



37
38
39
40
41
# File 'lib/kramdown/parser/formdown.rb', line 37

def parse_text_areas
  @src.pos += @src.matched_size
  col, rows, placeholder = TEXT_AREA_START.match(@src.matched).captures
  @tree.children << Element.new(:html_element, :textarea, placeholder: placeholder, name: placeholder, cols: col.size, rows: rows.size)
end

#parse_text_fieldsObject



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/kramdown/parser/formdown.rb', line 21

def parse_text_fields
  @src.pos += @src.matched_size
  line, type, placeholder = TEXT_FIELD_START.match(@src.matched).captures
  # Optionally pull out email or number types.
  type = case type
  when '@' then :email
  when '#' then :number
  else :text
  end
  @tree.children << Element.new(:html_element, :input, type: type, placeholder: placeholder, name: placeholder, size: line.size)
end