Class: Formdown::Parser

Inherits:
Kramdown::Parser::Kramdown
  • Object
show all
Defined in:
lib/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) ⇒ Parser

Returns a new instance of Parser.



17
18
19
20
# File 'lib/formdown.rb', line 17

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

Instance Method Details

#parse_buttonsObject



66
67
68
69
70
71
72
73
74
75
# File 'lib/formdown.rb', line 66

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



50
51
52
53
# File 'lib/formdown.rb', line 50

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

#parse_radio_buttonsObject



58
59
60
61
# File 'lib/formdown.rb', line 58

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

#parse_text_areasObject



40
41
42
43
44
# File 'lib/formdown.rb', line 40

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



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/formdown.rb', line 24

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