trenni-formatters 2.8.1 → 2.12.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,146 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright, 2012, by Samuel G. D. Williams. <http://www.codeotaku.com>
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in
13
+ # all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ # THE SOFTWARE.
22
+
23
+ require 'trenni/builder'
24
+ require 'trenni/template'
25
+
26
+ require_relative 'form_formatter'
27
+
28
+ module Trenni
29
+ module Formatters
30
+ module HTML
31
+ module LabelForm
32
+ include FormFormatter
33
+
34
+ # An input field (single line text).
35
+ def input(**options)
36
+ options = @options.merge(**options)
37
+
38
+ Builder.fragment do |builder|
39
+ builder.inline(:label) do
40
+ builder.inline(:span) do
41
+ builder.text title_for(**options)
42
+
43
+ if details = details_for(**options)
44
+ builder.inline(:small) {builder.text details}
45
+ end
46
+ end
47
+
48
+ builder.inline :input, input_attributes_for(**options)
49
+ end
50
+ end
51
+ end
52
+
53
+ # An output field for the result of a computation.
54
+ def output(**options)
55
+ options = @options.merge(**options)
56
+
57
+ builder.inline(:label) do
58
+ builder.inline(:span) do
59
+ builder.text title_for(**options)
60
+
61
+ if details = details_for(**options)
62
+ builder.inline(:small) {builder.text details}
63
+ end
64
+ end
65
+
66
+ builder.inline :output, output_attributes_for(**options) do
67
+ builder.text value_for(**options)
68
+ end
69
+ end
70
+ end
71
+
72
+ # A textarea field (multi-line text).
73
+ def textarea(**options)
74
+ options = @options.merge(**options)
75
+
76
+ Builder.fragment do |builder|
77
+ builder.inline(:label) do
78
+ builder.inline(:span) do
79
+ builder.text title_for(**options)
80
+
81
+ if details = details_for(**options)
82
+ builder.inline(:small) {builder.text details}
83
+ end
84
+ end
85
+
86
+ builder.tag :textarea, textarea_attributes_for(**options) do
87
+ builder.text value_for(**options)
88
+ end
89
+ end
90
+ end
91
+ end
92
+
93
+ # A checkbox field.
94
+ def checkbox(**options)
95
+ options = @options.merge(**options)
96
+
97
+ Builder.fragment do |builder|
98
+ builder.inline(:label) do
99
+ builder.inline :input, :type => :hidden, :name => name_for(**options), :value => 'false'
100
+
101
+ builder.inline(:span) do
102
+ if details = details_for(**options)
103
+ builder.inline(:small) {builder.text details}
104
+ end
105
+ end
106
+
107
+ builder.tag :input, checkbox_attributes_for(**options)
108
+
109
+ # We would like a little bit of whitespace between the checkbox and the title:
110
+ builder.text " " + title_for(**options)
111
+ end
112
+ end
113
+ end
114
+
115
+ # A submission button
116
+ def submit(**options)
117
+ options = @options.merge(**options)
118
+ options[:title] ||= submit_title_for(**options)
119
+
120
+ Builder.fragment do |builder|
121
+ builder.inline :input, submit_attributes_for(**options)
122
+ end
123
+ end
124
+
125
+ def element(klass, **options, &block)
126
+ options = @options.merge(**options)
127
+ buffer = Trenni::Template.buffer(block.binding)
128
+
129
+ buffer << Builder.fragment do |builder|
130
+ builder.inline(:label) do
131
+ builder.inline(:span) do
132
+ builder.text title_for(**options)
133
+
134
+ if details = details_for(**options)
135
+ builder.inline(:small) {builder.text details}
136
+ end
137
+ end
138
+
139
+ klass.call(self, builder, **options, &block)
140
+ end
141
+ end
142
+ end
143
+ end
144
+ end
145
+ end
146
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Copyright, 2012, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
4
  #
3
5
  # Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -25,24 +27,20 @@ module Trenni
25
27
  module HTML
26
28
  # Standard drop-down select box:
27
29
  class OptionSelect
28
- def self.call(formatter, options, builder, &block)
29
- instance = self.new(formatter, options, builder)
30
+ def self.call(formatter, builder, **options, &block)
31
+ instance = self.new(formatter, builder, **options)
30
32
 
31
- instance.call(options, &block)
33
+ instance.call(&block)
32
34
  end
33
35
 
34
- def initialize(formatter, options, builder)
36
+ def initialize(formatter, builder, **options)
35
37
  @formatter = formatter
36
- @object = formatter.object
37
- @field = options[:field]
38
-
39
- @options = options
40
-
41
38
  @builder = builder
39
+ @options = options
42
40
  end
43
-
44
- def name_for(options)
45
- if name = @formatter.name_for(options)
41
+
42
+ def name_for(**options)
43
+ if name = @formatter.name_for(**options)
46
44
  if options[:multiple]
47
45
  name = "#{name}[]"
48
46
  end
@@ -50,85 +48,90 @@ module Trenni
50
48
  return name
51
49
  end
52
50
  end
53
-
54
- def raw_value_for(options)
55
- @formatter.raw_value_for(options)
51
+
52
+ def raw_value_for(**options)
53
+ @formatter.raw_value_for(**options)
56
54
  end
57
-
55
+
58
56
  def raw_value
59
- @raw_value ||= raw_value_for(@options)
57
+ @raw_value ||= raw_value_for(**@options)
60
58
  end
61
-
62
- def value_for(options)
63
- @formatter.value_for(options)
59
+
60
+ def value_for(**options)
61
+ @formatter.value_for(**options)
64
62
  end
65
-
66
- def title_for(options)
67
- @formatter.title_for(options)
63
+
64
+ def title_for(**options)
65
+ @formatter.title_for(**options)
68
66
  end
69
-
70
- def option_attributes_for(options)
67
+
68
+ def option_attributes_for(**options)
71
69
  return {
72
- :value => value_for(options),
73
- :selected => options.fetch(:selected){ raw_value == raw_value_for(options) },
70
+ :value => value_for(**options),
71
+ :selected => options.fetch(:selected) {raw_value == raw_value_for(**options)},
74
72
  :id => options[:id],
75
73
  :class => options[:class],
76
74
  :data => options[:data],
77
75
  }
78
76
  end
79
-
80
- def item(builder: nil, **options)
77
+
78
+ def item(builder = nil, **options)
81
79
  options[:field] ||= 'id'
82
80
 
83
81
  Builder.fragment(builder) do |builder|
84
- builder.inline(:option, option_attributes_for(options)) { builder.text title_for(options) }
82
+ builder.inline(:option, option_attributes_for(**options)) {builder.text title_for(**options)}
85
83
  end
86
84
  end
87
-
88
- def optional_title_for(options)
85
+
86
+ def optional_title_for(**options)
89
87
  if options[:optional] == true
90
88
  options[:blank] || ''
91
89
  else
92
90
  options[:optional]
93
91
  end
94
92
  end
95
-
96
- def group_attributes_for(options)
93
+
94
+ def group_attributes_for(**options)
97
95
  return {
98
- :label => title_for(options),
96
+ :label => title_for(**options),
99
97
  :id => options[:id],
100
98
  :class => options[:class],
101
99
  :data => options[:data],
102
100
  }
103
101
  end
104
-
105
- def group(options = {}, &block)
102
+
103
+ def group(**options, &block)
106
104
  Builder.fragment(@builder) do |builder|
107
- builder.tag :optgroup, group_attributes_for(options) do
105
+ builder.tag :optgroup, group_attributes_for(**options) do
108
106
  if options[:optional]
109
- item(:title => optional_title_for(options), :value => nil, :builder => builder)
107
+ item(@builder, title: optional_title_for(**options), value: nil)
110
108
  end
111
109
 
112
110
  builder.capture(&block)
113
111
  end
114
112
  end
115
113
  end
116
-
117
- def select_attributes_for(options)
114
+
115
+ def select_attributes_for(**options)
118
116
  return {
119
- :name => name_for(options),
117
+ :name => name_for(**options),
120
118
  :id => options[:id],
121
119
  :class => options[:class],
122
120
  :multiple => options[:multiple],
123
121
  :data => options[:data],
122
+ :required => options[:required],
124
123
  }
125
124
  end
126
-
127
- def call(options = {}, &block)
125
+
126
+ def optional?
127
+ @options[:optional]
128
+ end
129
+
130
+ def call(&block)
128
131
  Builder.fragment(@builder) do |builder|
129
- builder.tag :select, select_attributes_for(options) do
130
- if options[:optional]
131
- item(:title => optional_title_for(options), :value => nil, :builder => builder)
132
+ builder.tag :select, select_attributes_for(**@options) do
133
+ if self.optional?
134
+ item(builder, title: optional_title_for(**@options), value: nil)
132
135
  end
133
136
 
134
137
  builder.capture(self, &block)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Copyright, 2012, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
4
  #
3
5
  # Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -25,85 +27,87 @@ module Trenni
25
27
  module HTML
26
28
  # Table based select boxes using per-row checkboxes.
27
29
  class RadioSelect
28
- def self.call(formatter, options, builder, &block)
29
- instance = self.new(formatter, options, builder)
30
+ def self.call(formatter, builder, **options, &block)
31
+ instance = self.new(formatter, builder, **options)
30
32
 
31
- instance.call(options, &block)
33
+ instance.call(&block)
32
34
  end
33
35
 
34
- def initialize(formatter, options, builder)
36
+ def initialize(formatter, builder, **options)
35
37
  @formatter = formatter
36
- @object = formatter.object
37
-
38
+ @builder = builder
38
39
  @options = options
39
- @field = options[:field]
40
40
 
41
- @builder = builder
41
+ @field = options[:field]
42
42
  end
43
-
44
- def name_for(options)
45
- @formatter.name_for(options)
43
+
44
+ def name_for(**options)
45
+ @formatter.name_for(**options)
46
46
  end
47
47
 
48
- def raw_value_for(options)
49
- @formatter.raw_value_for(options)
48
+ def raw_value_for(**options)
49
+ @formatter.raw_value_for(**options)
50
50
  end
51
-
51
+
52
52
  def raw_value
53
- @raw_value ||= raw_value_for(@options)
53
+ @raw_value ||= raw_value_for(**@options)
54
54
  end
55
-
56
- def value_for(options)
57
- @formatter.value_for(options)
55
+
56
+ def value_for(**options)
57
+ @formatter.value_for(**options)
58
58
  end
59
-
60
- def title_for(options)
61
- @formatter.title_for(options)
59
+
60
+ def title_for(**options)
61
+ @formatter.title_for(**options)
62
62
  end
63
-
64
- def radio_attributes_for(options)
63
+
64
+ def radio_attributes_for(**options)
65
65
  return {
66
66
  :type => :radio,
67
67
  :name => @field,
68
68
  # We set a default value to empty string, otherwise it becomes "on".
69
- :value => value_for(options) || "",
70
- :checked => options.fetch(:selected){ raw_value == raw_value_for(options) },
69
+ :value => value_for(**options) || "",
70
+ :checked => options.fetch(:selected) {raw_value == raw_value_for(**options)},
71
71
  :data => options[:data],
72
72
  }
73
73
  end
74
-
75
- def item(builder: nil, **options, &block)
74
+
75
+ def item(builder = nil, **options, &block)
76
76
  Builder.fragment(builder) do |builder|
77
77
  builder.tag :tr do
78
78
  builder.inline(:td, :class => :handle) do
79
- builder.tag :input, radio_attributes_for(options)
79
+ builder.tag :input, radio_attributes_for(**options)
80
80
  end
81
81
 
82
82
  builder.inline(:td, :class => :item) do
83
83
  if block_given?
84
84
  builder.capture(self, &block)
85
85
  else
86
- builder.text title_for(options)
86
+ builder.text title_for(**options)
87
87
  end
88
88
  end
89
89
  end
90
90
  end >> block
91
91
  end
92
92
 
93
- def optional_title_for(options)
93
+ def optional_title_for(**options)
94
94
  if options[:optional] == true
95
95
  options[:blank] || ''
96
96
  else
97
97
  options[:optional]
98
98
  end
99
99
  end
100
-
101
- def call(options = {}, &block)
100
+
101
+ def optional?
102
+ @options[:optional]
103
+ end
104
+
105
+ def call(&block)
102
106
  Builder.fragment(@builder) do |builder|
103
107
  builder.tag :table do
104
108
  builder.tag :tbody do
105
- if options[:optional]
106
- item(:title => optional_title_for(options), :value => nil, :builder => builder)
109
+ if self.optional?
110
+ item(builder, title: optional_title_for(**@options), value: nil)
107
111
  end
108
112
 
109
113
  builder.capture(self, &block)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Copyright, 2012, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
4
  #
3
5
  # Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -18,20 +20,18 @@
18
20
  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
21
  # THE SOFTWARE.
20
22
 
21
- require 'sanitize'
22
- require 'kramdown'
23
+ require 'markly'
23
24
 
24
25
  require 'trenni/markup'
26
+ require 'trenni/sanitize/fragment'
25
27
 
26
28
  module Trenni
27
29
  module Formatters
28
30
  module Markdown
29
- def markdown(text)
30
- config = Sanitize::Config::BASIC.dup
31
-
32
- config[:elements] += ['h1', 'h2', 'h3']
31
+ def markdown(text, filter = Trenni::Sanitize::Fragment, **options)
32
+ root = Markly.parse(text, **options)
33
33
 
34
- html = Sanitize.clean(Kramdown::Document.new(text).to_html, config)
34
+ html = filter.parse(root.to_html).output
35
35
 
36
36
  return MarkupString.raw(html)
37
37
  end