trenni-formatters 2.1.1 → 2.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +1 -0
- data/Gemfile +3 -0
- data/lib/trenni/formatters/{format/markdown.rb → markdown.rb} +14 -8
- data/lib/trenni/formatters/{format/time.rb → relative_time.rb} +13 -11
- data/lib/trenni/formatters/{format/text.rb → truncated_text.rb} +21 -19
- data/lib/trenni/formatters/version.rb +1 -1
- data/spec/trenni/formatters/html/radio_select_spec.rb +1 -1
- data/spec/trenni/formatters/markdown_spec.rb +34 -0
- data/spec/trenni/formatters/relative_time_spec.rb +38 -0
- data/spec/trenni/formatters/truncated_text_spec.rb +32 -0
- metadata +11 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5e2814d2a4811d2a066fd96b9cc0c9bc78f7dbe4
|
4
|
+
data.tar.gz: 525e508d5b65000040c12b3c09b5452c2f899ef3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5fbaeb44cb3ad9a3d6f3b499043f2a8e2703b03dd59b998349423384e32be1c927a89d2ec662f13edfe5eeb3f5e2621160231df9d32ae82aa506e0ab118a7c41
|
7
|
+
data.tar.gz: 1fe8760cbe647bc9ab1266666bdde4bc6e6b771f86bfc874af30b29cab7d3b4f1815610612b3e9222c2f007434ff0d3fb84832b03b54395f20d54e524ac7d41b
|
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
@@ -21,14 +21,20 @@
|
|
21
21
|
require 'sanitize'
|
22
22
|
require 'kramdown'
|
23
23
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
24
|
+
require 'trenni/markup'
|
25
|
+
|
26
|
+
module Trenni
|
27
|
+
module Formatters
|
28
|
+
module Markdown
|
29
|
+
def markdown(text)
|
30
|
+
config = Sanitize::Config::BASIC.dup
|
31
|
+
|
32
|
+
config[:elements] += ['h1', 'h2', 'h3']
|
33
|
+
|
34
|
+
html = Sanitize.clean(Kramdown::Document.new(text).to_html, config)
|
35
|
+
|
36
|
+
return MarkupString.raw(html)
|
37
|
+
end
|
32
38
|
end
|
33
39
|
end
|
34
40
|
end
|
@@ -21,19 +21,21 @@
|
|
21
21
|
require 'trenni/strings'
|
22
22
|
require 'mapping/model'
|
23
23
|
|
24
|
-
module Trenni
|
25
|
-
module
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
object.localtime.
|
24
|
+
module Trenni
|
25
|
+
module Formatters
|
26
|
+
module RelativeTime
|
27
|
+
def self.included(base)
|
28
|
+
base.map(Time) do |object, options|
|
29
|
+
current_time = options.fetch(:current_time) {Time.now}
|
30
|
+
|
31
|
+
# Ensure we display the time in localtime, and show the year if it is different:
|
32
|
+
if object.localtime.year != current_time.year
|
33
|
+
object.localtime.strftime("%B %-d, %-l:%M%P, %Y")
|
34
|
+
else
|
35
|
+
object.localtime.strftime("%B %-d, %-l:%M%P")
|
36
|
+
end
|
33
37
|
end
|
34
38
|
end
|
35
|
-
end
|
36
39
|
end
|
37
40
|
end
|
38
41
|
end
|
39
|
-
|
@@ -21,30 +21,32 @@
|
|
21
21
|
require 'trenni/strings'
|
22
22
|
require 'mapping/model'
|
23
23
|
|
24
|
-
module Trenni
|
25
|
-
module
|
26
|
-
|
27
|
-
|
28
|
-
|
24
|
+
module Trenni
|
25
|
+
module Formatters
|
26
|
+
module TruncatedText
|
27
|
+
def truncated_text(content, options = {})
|
28
|
+
if content
|
29
|
+
length = options.fetch(:length, 30)
|
29
30
|
|
30
|
-
|
31
|
-
|
32
|
-
|
31
|
+
content = TruncatedText.truncate_text(content, length, options)
|
32
|
+
|
33
|
+
return self.format(content)
|
34
|
+
end
|
33
35
|
end
|
34
|
-
end
|
35
36
|
|
36
|
-
|
37
|
-
|
37
|
+
def self.truncate_text(text, truncate_at, options = {})
|
38
|
+
return text.dup unless text.length > truncate_at
|
38
39
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
40
|
+
options[:omission] ||= '...'
|
41
|
+
length_with_room_for_omission = truncate_at - options[:omission].length
|
42
|
+
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
|
46
47
|
|
47
|
-
|
48
|
+
"#{text[0...stop]}#{options[:omission]}"
|
49
|
+
end
|
48
50
|
end
|
49
51
|
end
|
50
52
|
end
|
@@ -43,7 +43,7 @@ module Trenni::Formatters::HTML::RadioSelectSpec
|
|
43
43
|
end
|
44
44
|
end
|
45
45
|
|
46
|
-
expect(captured).to be == %Q{<dt>Bar</dt>\n<dd>\n\t<table>\n\t\t<tbody>\n\t\t\t<tr>\n\t\t\t\t<td class="handle"><input type="radio" name="bar" value="0"/></td>\n\t\t\t\t<td class="item">A</td>\n\t\t\t</tr><tr>\n\t\t\t\t<td class="handle"><input type="radio" name="bar" value="10"/></td>\n\t\t\t\t<td class="item">B</td>\n\t\t\t</tr>\n\t\t</tbody>\n\t</table>\n</dd>}
|
46
|
+
expect(captured).to be == %Q{<dt>Bar</dt>\n<dd>\n\t<table>\n\t\t<tbody>\n\t\t\t<tr>\n\t\t\t\t<td class="handle"><input type="radio" name="bar" value="0"/></td>\n\t\t\t\t<td class="item">A</td>\n\t\t\t</tr><tr>\n\t\t\t\t<td class="handle"><input type="radio" name="bar" value="10" checked/></td>\n\t\t\t\t<td class="item">B</td>\n\t\t\t</tr>\n\t\t</tbody>\n\t</table>\n</dd>}
|
47
47
|
end
|
48
48
|
end
|
49
49
|
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# Copyright, 2017, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
2
|
+
#
|
3
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
# of this software and associated documentation files (the "Software"), to deal
|
5
|
+
# in the Software without restriction, including without limitation the rights
|
6
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
# copies of the Software, and to permit persons to whom the Software is
|
8
|
+
# furnished to do so, subject to the following conditions:
|
9
|
+
#
|
10
|
+
# The above copyright notice and this permission notice shall be included in
|
11
|
+
# all copies or substantial portions of the Software.
|
12
|
+
#
|
13
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
# THE SOFTWARE.
|
20
|
+
|
21
|
+
require 'trenni/formatters/formatter'
|
22
|
+
require 'trenni/formatters/markdown'
|
23
|
+
|
24
|
+
describe Trenni::Formatters::Markdown do
|
25
|
+
subject {Class.new(Trenni::Formatters::Formatter).include(Trenni::Formatters::Markdown).new}
|
26
|
+
|
27
|
+
let(:sample_text) {"# Heading\n\nParagraph\n"}
|
28
|
+
|
29
|
+
it "should format markdown" do
|
30
|
+
result = subject.markdown(sample_text)
|
31
|
+
|
32
|
+
expect(result).to include('<h1>', '<p>')
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# Copyright, 2017, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
2
|
+
#
|
3
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
# of this software and associated documentation files (the "Software"), to deal
|
5
|
+
# in the Software without restriction, including without limitation the rights
|
6
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
# copies of the Software, and to permit persons to whom the Software is
|
8
|
+
# furnished to do so, subject to the following conditions:
|
9
|
+
#
|
10
|
+
# The above copyright notice and this permission notice shall be included in
|
11
|
+
# all copies or substantial portions of the Software.
|
12
|
+
#
|
13
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
# THE SOFTWARE.
|
20
|
+
|
21
|
+
require 'trenni/formatters/formatter'
|
22
|
+
require 'trenni/formatters/relative_time'
|
23
|
+
|
24
|
+
describe Trenni::Formatters::RelativeTime do
|
25
|
+
subject {Class.new(Trenni::Formatters::Formatter).include(Trenni::Formatters::RelativeTime).new}
|
26
|
+
|
27
|
+
let(:now) {Time.now}
|
28
|
+
let(:time_this_year) {Time.mktime(now.year, 1, 1)}
|
29
|
+
let(:time_last_year) {Time.mktime(now.year - 1, 1, 1)}
|
30
|
+
|
31
|
+
it "should format without year" do
|
32
|
+
expect(subject.format(time_this_year, current_time: now)).to be == "January 1, 12:00am"
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should format with year" do
|
36
|
+
expect(subject.format(time_last_year, current_time: now)).to be == "January 1, 12:00am, #{time_last_year.year}"
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# Copyright, 2017, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
2
|
+
#
|
3
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
# of this software and associated documentation files (the "Software"), to deal
|
5
|
+
# in the Software without restriction, including without limitation the rights
|
6
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
# copies of the Software, and to permit persons to whom the Software is
|
8
|
+
# furnished to do so, subject to the following conditions:
|
9
|
+
#
|
10
|
+
# The above copyright notice and this permission notice shall be included in
|
11
|
+
# all copies or substantial portions of the Software.
|
12
|
+
#
|
13
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
# THE SOFTWARE.
|
20
|
+
|
21
|
+
require 'trenni/formatters/formatter'
|
22
|
+
require 'trenni/formatters/truncated_text'
|
23
|
+
|
24
|
+
describe Trenni::Formatters::TruncatedText do
|
25
|
+
subject {Class.new(Trenni::Formatters::Formatter).include(Trenni::Formatters::TruncatedText).new}
|
26
|
+
|
27
|
+
let(:sample_text) {"The quick brown fox jumped over the lazy dog!"}
|
28
|
+
|
29
|
+
it "should truncate text" do
|
30
|
+
expect(subject.truncated_text(sample_text)).to be == "The quick brown fox jumped ..."
|
31
|
+
end
|
32
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: trenni-formatters
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.2.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-01-
|
11
|
+
date: 2017-01-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: trenni
|
@@ -98,19 +98,22 @@ files:
|
|
98
98
|
- README.md
|
99
99
|
- Rakefile
|
100
100
|
- lib/trenni/formatters.rb
|
101
|
-
- lib/trenni/formatters/format/markdown.rb
|
102
|
-
- lib/trenni/formatters/format/text.rb
|
103
|
-
- lib/trenni/formatters/format/time.rb
|
104
101
|
- lib/trenni/formatters/formatter.rb
|
105
102
|
- lib/trenni/formatters/html/definition_list_form.rb
|
106
103
|
- lib/trenni/formatters/html/form_formatter.rb
|
107
104
|
- lib/trenni/formatters/html/option_select.rb
|
108
105
|
- lib/trenni/formatters/html/radio_select.rb
|
106
|
+
- lib/trenni/formatters/markdown.rb
|
107
|
+
- lib/trenni/formatters/relative_time.rb
|
108
|
+
- lib/trenni/formatters/truncated_text.rb
|
109
109
|
- lib/trenni/formatters/version.rb
|
110
110
|
- spec/trenni/formatters/form_formatter_spec.rb
|
111
111
|
- spec/trenni/formatters/formatters_spec.rb
|
112
112
|
- spec/trenni/formatters/html/option_select_spec.rb
|
113
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
|
114
117
|
- trenni-formatters.gemspec
|
115
118
|
homepage: https://github.com/ioquatix/trenni-formatters
|
116
119
|
licenses: []
|
@@ -140,3 +143,6 @@ test_files:
|
|
140
143
|
- spec/trenni/formatters/formatters_spec.rb
|
141
144
|
- spec/trenni/formatters/html/option_select_spec.rb
|
142
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
|