trenni-formatters 2.5.0 → 2.10.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,148 @@
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
+ builder.text title_for(**options)
103
+
104
+ if details = details_for(**options)
105
+ builder.inline(:small) {builder.text details}
106
+ end
107
+ end
108
+
109
+ builder.tag :input, checkbox_attributes_for(**options)
110
+
111
+ # We would like a little bit of whitespace between the checkbox and the title.
112
+ builder.text " " + title_for(**options)
113
+ end
114
+ end
115
+ end
116
+
117
+ # A submission button
118
+ def submit(**options)
119
+ options = @options.merge(**options)
120
+ options[:title] ||= submit_title_for(**options)
121
+
122
+ Builder.fragment do |builder|
123
+ builder.inline :input, submit_attributes_for(**options)
124
+ end
125
+ end
126
+
127
+ def element(klass, options = {}, &block)
128
+ options = @options.merge(**options)
129
+ buffer = Trenni::Template.buffer(block.binding)
130
+
131
+ buffer << Builder.fragment do |builder|
132
+ builder.inline(:label) do
133
+ builder.inline(:span) do
134
+ builder.text title_for(**options)
135
+
136
+ if details = details_for(**options)
137
+ builder.inline(:small) {builder.text details}
138
+ end
139
+ end
140
+
141
+ klass.call(self, options, builder, &block)
142
+ end
143
+ end
144
+ end
145
+ end
146
+ end
147
+ end
148
+ 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
@@ -41,8 +43,8 @@ module Trenni
41
43
  @builder = builder
42
44
  end
43
45
 
44
- def name_for(options)
45
- if name = @formatter.name_for(options)
46
+ def name_for(**options)
47
+ if name = @formatter.name_for(**options)
46
48
  if options[:multiple]
47
49
  name = "#{name}[]"
48
50
  end
@@ -51,26 +53,26 @@ module Trenni
51
53
  end
52
54
  end
53
55
 
54
- def raw_value_for(options)
55
- @formatter.raw_value_for(options)
56
+ def raw_value_for(**options)
57
+ @formatter.raw_value_for(**options)
56
58
  end
57
59
 
58
60
  def raw_value
59
- @raw_value ||= raw_value_for(@options)
61
+ @raw_value ||= raw_value_for(**@options)
60
62
  end
61
63
 
62
- def value_for(options)
63
- @formatter.value_for(options)
64
+ def value_for(**options)
65
+ @formatter.value_for(**options)
64
66
  end
65
67
 
66
- def title_for(options)
67
- @formatter.title_for(options)
68
+ def title_for(**options)
69
+ @formatter.title_for(**options)
68
70
  end
69
71
 
70
- def option_attributes_for(options)
72
+ def option_attributes_for(**options)
71
73
  return {
72
- :value => value_for(options),
73
- :selected => options.fetch(:selected){ raw_value == raw_value_for(options) },
74
+ :value => value_for(**options),
75
+ :selected => options.fetch(:selected) {raw_value == raw_value_for(**options)},
74
76
  :id => options[:id],
75
77
  :class => options[:class],
76
78
  :data => options[:data],
@@ -81,11 +83,11 @@ module Trenni
81
83
  options[:field] ||= 'id'
82
84
 
83
85
  Builder.fragment(builder) do |builder|
84
- builder.inline(:option, option_attributes_for(options)) { builder.text title_for(options) }
86
+ builder.inline(:option, option_attributes_for(**options)) {builder.text title_for(**options)}
85
87
  end
86
88
  end
87
89
 
88
- def optional_title_for(options)
90
+ def optional_title_for(**options)
89
91
  if options[:optional] == true
90
92
  options[:blank] || ''
91
93
  else
@@ -93,9 +95,9 @@ module Trenni
93
95
  end
94
96
  end
95
97
 
96
- def group_attributes_for(options)
98
+ def group_attributes_for(**options)
97
99
  return {
98
- :label => title_for(options),
100
+ :label => title_for(**options),
99
101
  :id => options[:id],
100
102
  :class => options[:class],
101
103
  :data => options[:data],
@@ -104,19 +106,19 @@ module Trenni
104
106
 
105
107
  def group(options = {}, &block)
106
108
  Builder.fragment(@builder) do |builder|
107
- builder.tag :optgroup, group_attributes_for(options) do
109
+ builder.tag :optgroup, group_attributes_for(**options) do
108
110
  if options[:optional]
109
- item(:title => optional_title_for(options), :value => nil, :builder => builder)
111
+ item(:title => optional_title_for(**options), :value => nil, :builder => builder)
110
112
  end
111
113
 
112
- builder.append Trenni::Template.capture(&block)
114
+ builder.capture(&block)
113
115
  end
114
116
  end
115
117
  end
116
118
 
117
- def select_attributes_for(options)
119
+ def select_attributes_for(**options)
118
120
  return {
119
- :name => name_for(options),
121
+ :name => name_for(**options),
120
122
  :id => options[:id],
121
123
  :class => options[:class],
122
124
  :multiple => options[:multiple],
@@ -126,12 +128,12 @@ module Trenni
126
128
 
127
129
  def call(options = {}, &block)
128
130
  Builder.fragment(@builder) do |builder|
129
- builder.tag :select, select_attributes_for(options) do
131
+ builder.tag :select, select_attributes_for(**options) do
130
132
  if options[:optional]
131
- item(:title => optional_title_for(options), :value => nil, :builder => builder)
133
+ item(:title => optional_title_for(**options), :value => nil, :builder => builder)
132
134
  end
133
135
 
134
- builder.append Trenni::Template.capture(self, &block)
136
+ builder.capture(self, &block)
135
137
  end
136
138
  end
137
139
  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
@@ -41,33 +43,33 @@ module Trenni
41
43
  @builder = builder
42
44
  end
43
45
 
44
- def name_for(options)
45
- @formatter.name_for(options)
46
+ def name_for(**options)
47
+ @formatter.name_for(**options)
46
48
  end
47
49
 
48
- def raw_value_for(options)
49
- @formatter.raw_value_for(options)
50
+ def raw_value_for(**options)
51
+ @formatter.raw_value_for(**options)
50
52
  end
51
53
 
52
54
  def raw_value
53
- @raw_value ||= raw_value_for(@options)
55
+ @raw_value ||= raw_value_for(**@options)
54
56
  end
55
57
 
56
- def value_for(options)
57
- @formatter.value_for(options)
58
+ def value_for(**options)
59
+ @formatter.value_for(**options)
58
60
  end
59
61
 
60
- def title_for(options)
61
- @formatter.title_for(options)
62
+ def title_for(**options)
63
+ @formatter.title_for(**options)
62
64
  end
63
65
 
64
- def radio_attributes_for(options)
66
+ def radio_attributes_for(**options)
65
67
  return {
66
68
  :type => :radio,
67
69
  :name => @field,
68
70
  # 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) },
71
+ :value => value_for(**options) || "",
72
+ :checked => options.fetch(:selected) {raw_value == raw_value_for(**options)},
71
73
  :data => options[:data],
72
74
  }
73
75
  end
@@ -76,21 +78,21 @@ module Trenni
76
78
  Builder.fragment(builder) do |builder|
77
79
  builder.tag :tr do
78
80
  builder.inline(:td, :class => :handle) do
79
- builder.tag :input, radio_attributes_for(options)
81
+ builder.tag :input, radio_attributes_for(**options)
80
82
  end
81
83
 
82
84
  builder.inline(:td, :class => :item) do
83
85
  if block_given?
84
- builder.append Trenni::Template.capture(self, &block)
86
+ builder.capture(self, &block)
85
87
  else
86
- builder.text title_for(options)
88
+ builder.text title_for(**options)
87
89
  end
88
90
  end
89
91
  end
90
92
  end >> block
91
93
  end
92
94
 
93
- def optional_title_for(options)
95
+ def optional_title_for(**options)
94
96
  if options[:optional] == true
95
97
  options[:blank] || ''
96
98
  else
@@ -103,10 +105,10 @@ module Trenni
103
105
  builder.tag :table do
104
106
  builder.tag :tbody do
105
107
  if options[:optional]
106
- item(:title => optional_title_for(options), :value => nil, :builder => builder)
108
+ item(:title => optional_title_for(**options), :value => nil, :builder => builder)
107
109
  end
108
110
 
109
- builder.append Trenni::Template.capture(self, &block)
111
+ builder.capture(self, &block)
110
112
  end
111
113
  end
112
114
  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
@@ -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
@@ -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
@@ -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
@@ -27,24 +29,24 @@ module Trenni
27
29
  def truncated_text(content, options = {})
28
30
  if content
29
31
  length = options.fetch(:length, 30)
30
-
32
+
31
33
  content = TruncatedText.truncate_text(content, length, options)
32
34
 
33
35
  return self.format(content)
34
36
  end
35
37
  end
36
-
38
+
37
39
  def self.truncate_text(text, truncate_at, options = {})
38
40
  return text.dup unless text.length > truncate_at
39
-
41
+
40
42
  options[:omission] ||= '...'
41
43
  length_with_room_for_omission = truncate_at - options[:omission].length
42
44
  stop = if options[:separator]
43
- text.rindex(options[:separator], length_with_room_for_omission) || length_with_room_for_omission
44
- else
45
- length_with_room_for_omission
46
- end
47
-
45
+ text.rindex(options[:separator], length_with_room_for_omission) || length_with_room_for_omission
46
+ else
47
+ length_with_room_for_omission
48
+ end
49
+
48
50
  "#{text[0...stop]}#{options[:omission]}"
49
51
  end
50
52
  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
@@ -20,6 +22,6 @@
20
22
 
21
23
  module Trenni
22
24
  module Formatters
23
- VERSION = "2.5.0"
25
+ VERSION = "2.10.0"
24
26
  end
25
27
  end
metadata CHANGED
@@ -1,73 +1,115 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: trenni-formatters
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.5.0
4
+ version: 2.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-03-06 00:00:00.000000000 Z
11
+ date: 2020-05-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: trenni
14
+ name: mapping
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '2.1'
19
+ version: '1.1'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '2.1'
26
+ version: '1.1'
27
27
  - !ruby/object:Gem::Dependency
28
- name: mapping
28
+ name: trenni
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '1.1'
33
+ version: '3.4'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '1.1'
40
+ version: '3.4'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bake-bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: bake-modernize
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
41
83
  - !ruby/object:Gem::Dependency
42
84
  name: bundler
43
85
  requirement: !ruby/object:Gem::Requirement
44
86
  requirements:
45
- - - "~>"
87
+ - - ">="
46
88
  - !ruby/object:Gem::Version
47
- version: '1.3'
89
+ version: '0'
48
90
  type: :development
49
91
  prerelease: false
50
92
  version_requirements: !ruby/object:Gem::Requirement
51
93
  requirements:
52
- - - "~>"
94
+ - - ">="
53
95
  - !ruby/object:Gem::Version
54
- version: '1.3'
96
+ version: '0'
55
97
  - !ruby/object:Gem::Dependency
56
- name: rspec
98
+ name: covered
57
99
  requirement: !ruby/object:Gem::Requirement
58
100
  requirements:
59
- - - "~>"
101
+ - - ">="
60
102
  - !ruby/object:Gem::Version
61
- version: '3.4'
103
+ version: '0'
62
104
  type: :development
63
105
  prerelease: false
64
106
  version_requirements: !ruby/object:Gem::Requirement
65
107
  requirements:
66
- - - "~>"
108
+ - - ">="
67
109
  - !ruby/object:Gem::Version
68
- version: '3.4'
110
+ version: '0'
69
111
  - !ruby/object:Gem::Dependency
70
- name: rake
112
+ name: rspec
71
113
  requirement: !ruby/object:Gem::Requirement
72
114
  requirements:
73
115
  - - ">="
@@ -80,44 +122,28 @@ dependencies:
80
122
  - - ">="
81
123
  - !ruby/object:Gem::Version
82
124
  version: '0'
83
- description: "\tTrenni is a templating system, and these formatters assist with the
84
- development\n\tof typical view and form based web interface. A formatter is a high-level\n\tadapter
85
- that turns model data into presentation text.\n\n\tFormatters are designed to be
86
- customised, typically per-project, for specific\n\tformatting needs.\n"
87
- email:
88
- - samuel.williams@oriontransfer.co.nz
125
+ description:
126
+ email:
89
127
  executables: []
90
128
  extensions: []
91
129
  extra_rdoc_files: []
92
130
  files:
93
- - ".gitignore"
94
- - ".rspec"
95
- - ".simplecov"
96
- - ".travis.yml"
97
- - Gemfile
98
- - README.md
99
- - Rakefile
100
131
  - lib/trenni/formatters.rb
101
132
  - lib/trenni/formatters/formatter.rb
102
133
  - lib/trenni/formatters/html/definition_list_form.rb
103
134
  - lib/trenni/formatters/html/form_formatter.rb
135
+ - lib/trenni/formatters/html/label_form.rb
104
136
  - lib/trenni/formatters/html/option_select.rb
105
137
  - lib/trenni/formatters/html/radio_select.rb
106
138
  - lib/trenni/formatters/markdown.rb
107
139
  - lib/trenni/formatters/relative_time.rb
108
140
  - lib/trenni/formatters/truncated_text.rb
109
141
  - lib/trenni/formatters/version.rb
110
- - spec/trenni/formatters/form_formatter_spec.rb
111
- - spec/trenni/formatters/formatters_spec.rb
112
- - spec/trenni/formatters/html/option_select_spec.rb
113
- - spec/trenni/formatters/html/radio_select_spec.rb
114
- - spec/trenni/formatters/markdown_spec.rb
115
- - spec/trenni/formatters/relative_time_spec.rb
116
- - spec/trenni/formatters/truncated_text_spec.rb
117
- - trenni-formatters.gemspec
118
142
  homepage: https://github.com/ioquatix/trenni-formatters
119
- licenses: []
120
- metadata: {}
143
+ licenses:
144
+ - MIT
145
+ metadata:
146
+ funding_uri: https://github.com/sponsors/ioquatix/
121
147
  post_install_message:
122
148
  rdoc_options: []
123
149
  require_paths:
@@ -126,23 +152,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
126
152
  requirements:
127
153
  - - ">="
128
154
  - !ruby/object:Gem::Version
129
- version: '0'
155
+ version: '2.5'
130
156
  required_rubygems_version: !ruby/object:Gem::Requirement
131
157
  requirements:
132
158
  - - ">="
133
159
  - !ruby/object:Gem::Version
134
160
  version: '0'
135
161
  requirements: []
136
- rubyforge_project:
137
- rubygems_version: 2.6.10
162
+ rubygems_version: 3.1.2
138
163
  signing_key:
139
164
  specification_version: 4
140
165
  summary: Formatters for Trenni, to assist with typical views and form based interfaces.
141
- test_files:
142
- - spec/trenni/formatters/form_formatter_spec.rb
143
- - spec/trenni/formatters/formatters_spec.rb
144
- - spec/trenni/formatters/html/option_select_spec.rb
145
- - spec/trenni/formatters/html/radio_select_spec.rb
146
- - spec/trenni/formatters/markdown_spec.rb
147
- - spec/trenni/formatters/relative_time_spec.rb
148
- - spec/trenni/formatters/truncated_text_spec.rb
166
+ test_files: []