formdown 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -89,15 +89,25 @@ In the rails Gemfile, include the formdown gem via:
89
89
  gem "formdown", require: "formdown/rails"
90
90
  ```
91
91
 
92
- Rails will pick up files with the extension `.fmd` and render them as HTML. For example, `app/views/user/my_form.fmd.html` would render the formdown document.
92
+ Rails will pick up files with the extension `.fmd` and render them as HTML. For example, `app/views/user/my_form.html.fmd` would render the formdown document.
93
93
 
94
94
  Its recommended to use a layout around the Formdown file to handle the form submission action and surrounding HTML content.
95
95
 
96
96
  **Note**: There's currently no simple way of mapping Formdown fields to ActiveRecord model attributes.
97
97
 
98
+ ### Middleman
99
+
100
+ In the Gemfile, include the formdown gem via:
101
+
102
+ ```ruby
103
+ gem "formdown", require: "formdown/tilt"
104
+ ```
105
+
106
+ Place your Formdown file in the [Middleman](http://middlemanapp.com/) source directory and the form will render (e.g. `source/my_form.html.fmd` will render the form at `/my_form.html`)
107
+
98
108
  ## Contributing
99
109
 
100
- 1. Fork it ( https://github.com/[my-github-username]/formdown/fork )
110
+ 1. Fork it ( https://github.com/bradgessler/formdown/fork )
101
111
  2. Create your feature branch (`git checkout -b my-new-feature`)
102
112
  3. Commit your changes (`git commit -am 'Add some feature'`)
103
113
  4. Push to the branch (`git push origin my-new-feature`)
@@ -16,4 +16,4 @@ module ActionView
16
16
  end
17
17
 
18
18
  # Register in the rails stack.
19
- ActionView::Template.register_template_handler(:fmd, ActionView::Template::Handlers::Formdown.new)
19
+ ActionView::Template.register_template_handler(*Formdown::FILE_EXTENSIONS, ActionView::Template::Handlers::Formdown.new)
@@ -0,0 +1,15 @@
1
+ require 'kramdown'
2
+ require 'kramdown/parser/formdown'
3
+ require 'kramdown/document'
4
+
5
+ module Formdown
6
+ class Renderer
7
+ def initialize(content)
8
+ @document = ::Kramdown::Document.new(content, input: 'Formdown')
9
+ end
10
+
11
+ def to_html
12
+ @document.to_html
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,6 @@
1
+ require 'tilt'
2
+ require 'tilt/formdown'
3
+
4
+ # TODO - Support lazy registration when Middleman supports Tilt 2.x.
5
+ # Tilt.register_lazy :FormdownTemplate, 'tilt/formdown', 'formdown', 'fmd'
6
+ Tilt.register Tilt::FormdownTemplate, *Formdown::FILE_EXTENSIONS
@@ -1,3 +1,3 @@
1
1
  module Formdown
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/formdown.rb CHANGED
@@ -1,95 +1,8 @@
1
- require "formdown/version"
2
-
3
- # Formdown extends the Markdown language with practical HTML form builder syntax.
4
- #
5
- # TODO
6
- # - name: parameters should be applied at a different layer, NOT within this file. This will
7
- # make sure that we don't clobber the users form submissions.
8
- # - DRY up the methods below into more of a macro language. The constants, extractions, etc.
9
- # are a common pattern.
10
-
11
- require 'kramdown'
12
- require 'kramdown/parser/kramdown'
13
- require 'kramdown/document'
14
-
15
- module Formdown
16
- class Parser < Kramdown::Parser::Kramdown
17
- def initialize(source, options)
18
- super
19
- @span_parsers.unshift :text_fields, :text_areas, :checkboxes, :buttons, :radio_buttons
20
- end
21
-
22
- TEXT_FIELD_START = /(_+)([@\#]?)\((.+?)\)/
23
-
24
- def parse_text_fields
25
- @src.pos += @src.matched_size
26
- line, type, placeholder = TEXT_FIELD_START.match(@src.matched).captures
27
- # Optionally pull out email or number types.
28
- type = case type
29
- when '@' then :email
30
- when '#' then :number
31
- else :text
32
- end
33
- @tree.children << Element.new(:html_element, :input, type: type, placeholder: placeholder, name: placeholder, size: line.size)
34
- end
35
- define_parser(:text_fields, TEXT_FIELD_START, '_{')
36
-
37
-
38
- TEXT_AREA_START = /(_+)(\/+)\((.+?)\)/
39
-
40
- def parse_text_areas
41
- @src.pos += @src.matched_size
42
- col, rows, placeholder = TEXT_AREA_START.match(@src.matched).captures
43
- @tree.children << Element.new(:html_element, :textarea, placeholder: placeholder, name: placeholder, cols: col.size, rows: rows.size)
44
- end
45
- define_parser(:text_areas, TEXT_AREA_START, '_/')
46
-
47
-
48
- CHECKBOX_FIELD_START = /\[([\sxX])?\]/
49
-
50
- def parse_checkboxes
51
- @src.pos += @src.matched_size
52
- @tree.children << Element.new(:html_element, :input, type: :checkbox)
53
- end
54
- define_parser(:checkboxes, CHECKBOX_FIELD_START, '\[')
55
-
56
- RADIO_BUTTON_FIELD_START = /\(([\sxX])?\)/
57
-
58
- def parse_radio_buttons
59
- @src.pos += @src.matched_size
60
- @tree.children << Element.new(:html_element, :input, type: :radio)
61
- end
62
- define_parser(:radio_buttons, RADIO_BUTTON_FIELD_START, '\(')
63
-
64
- BUTTON_START = /\[\s(.+)\s\]([\!])?/
65
-
66
- def parse_buttons
67
- @src.pos += @src.matched_size
68
- value, type = BUTTON_START.match(@src.matched).captures
69
- type = case type
70
- when '!' then :submit
71
- # else :button
72
- else :submit # TODO - Should we even support other types of buttons? Probably not... just make it a submit.
73
- end
74
- @tree.children << Element.new(:html_element, :input, type: type, value: value)
75
- end
76
- define_parser(:buttons, BUTTON_START, '\[')
77
- end
78
- end
79
-
80
- # Work around to get this build passing for Ruby 1.9 since the kramdown Parser.const_get()
81
- # TODO - Patch kramdown to work with nested namespaces in Ruby 1.9.
82
- # Details about the issue noted at https://github.com/gettalong/kramdown/commit/cd5423f23eb783a2c435e8545a98625cb280f286#diff-1f46a84d29e6ac9751ae5d75bafc25aeR88
83
- FormdownParser = Formdown::Parser
1
+ require 'formdown/version'
84
2
 
85
3
  module Formdown
86
- class Renderer
87
- def initialize(content)
88
- @document = ::Kramdown::Document.new(content, input: 'FormdownParser')
89
- end
4
+ # File extensions for various template handlers.
5
+ FILE_EXTENSIONS = %w[formdown fmd].freeze
90
6
 
91
- def to_html
92
- @document.to_html
93
- end
94
- end
7
+ autoload :Renderer, 'formdown/renderer'
95
8
  end
@@ -0,0 +1,76 @@
1
+ require 'kramdown/parser/kramdown'
2
+
3
+ # Formdown extends the Markdown language with practical HTML form builder syntax.
4
+ #
5
+ # TODO
6
+ # - name: Form values should be applied at a different layer, NOT within this file. This will
7
+ # make sure that we don't clobber the users form submissions.
8
+ # - DRY up the methods below into more of a macro language. The constants, extractions, etc.
9
+ # are a common pattern.
10
+
11
+ module Kramdown
12
+ module Parser
13
+ class Formdown < Kramdown::Parser::Kramdown
14
+ def initialize(source, options)
15
+ super
16
+ @span_parsers.unshift :text_fields, :text_areas, :checkboxes, :buttons, :radio_buttons
17
+ end
18
+
19
+ TEXT_FIELD_START = /(_+)([@\#]?)\((.+?)\)/
20
+
21
+ def parse_text_fields
22
+ @src.pos += @src.matched_size
23
+ line, type, placeholder = TEXT_FIELD_START.match(@src.matched).captures
24
+ # Optionally pull out email or number types.
25
+ type = case type
26
+ when '@' then :email
27
+ when '#' then :number
28
+ else :text
29
+ end
30
+ @tree.children << Element.new(:html_element, :input, type: type, placeholder: placeholder, name: placeholder, size: line.size)
31
+ end
32
+ define_parser(:text_fields, TEXT_FIELD_START, '_{')
33
+
34
+
35
+ TEXT_AREA_START = /(_+)(\/+)\((.+?)\)/
36
+
37
+ def parse_text_areas
38
+ @src.pos += @src.matched_size
39
+ col, rows, placeholder = TEXT_AREA_START.match(@src.matched).captures
40
+ @tree.children << Element.new(:html_element, :textarea, placeholder: placeholder, name: placeholder, cols: col.size, rows: rows.size)
41
+ end
42
+ define_parser(:text_areas, TEXT_AREA_START, '_/')
43
+
44
+
45
+ CHECKBOX_FIELD_START = /\[([\sxX])?\]/
46
+
47
+ def parse_checkboxes
48
+ @src.pos += @src.matched_size
49
+ @tree.children << Element.new(:html_element, :input, type: :checkbox)
50
+ end
51
+ define_parser(:checkboxes, CHECKBOX_FIELD_START, '\[')
52
+
53
+ RADIO_BUTTON_FIELD_START = /\(([\sxX])?\)/
54
+
55
+ def parse_radio_buttons
56
+ @src.pos += @src.matched_size
57
+ @tree.children << Element.new(:html_element, :input, type: :radio)
58
+ end
59
+ define_parser(:radio_buttons, RADIO_BUTTON_FIELD_START, '\(')
60
+
61
+ BUTTON_START = /\[\s(.+)\s\]([\!])?/
62
+
63
+ def parse_buttons
64
+ @src.pos += @src.matched_size
65
+ value, type = BUTTON_START.match(@src.matched).captures
66
+ type = case type
67
+ when '!' then :submit
68
+ # else :button
69
+ else :submit # TODO - Should we even support other types of buttons? Probably not... just make it a submit.
70
+ end
71
+ @tree.children << Element.new(:html_element, :input, type: type, value: value)
72
+ end
73
+ define_parser(:buttons, BUTTON_START, '\[')
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,31 @@
1
+ require 'formdown'
2
+ require 'tilt/template'
3
+
4
+ module Tilt
5
+ class FormdownTemplate < Template
6
+ # DUMB_QUOTES = [39, 39, 34, 34]
7
+
8
+ def self.engine_initialized?
9
+ defined? ::Formdown
10
+ end
11
+
12
+ def initialize_engine
13
+ require_template_library 'formdown'
14
+ end
15
+
16
+ def prepare
17
+ # options[:smart_quotes] = DUMB_QUOTES unless options[:smartypants]
18
+ # @engine = Formdown::Document.new(data, options)
19
+ @engine = Formdown::Renderer.new(data)
20
+ @output = nil
21
+ end
22
+
23
+ def evaluate(scope, locals, &block)
24
+ @output ||= @engine.to_html
25
+ end
26
+
27
+ def allows_script?
28
+ false
29
+ end
30
+ end
31
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: formdown
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-08-02 00:00:00.000000000 Z
12
+ date: 2014-08-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: kramdown
@@ -111,7 +111,11 @@ files:
111
111
  - lib/formdown.rb
112
112
  - lib/formdown/cli.rb
113
113
  - lib/formdown/rails.rb
114
+ - lib/formdown/renderer.rb
115
+ - lib/formdown/tilt.rb
114
116
  - lib/formdown/version.rb
117
+ - lib/kramdown/parser/formdown.rb
118
+ - lib/tilt/formdown.rb
115
119
  - spec/fixtures/kitchen_sink.fmd
116
120
  - spec/formdown_spec.rb
117
121
  - spec/spec_helper.rb