auto_html 2.0.0 → 2.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 4a15b23417dc35ee0ff8097c15b5eba4847142e2
4
- data.tar.gz: b94279092cc3fcb0f60affc58a733894e6f8993c
2
+ SHA256:
3
+ metadata.gz: f0bf51223467c7efbbf7f743e446137634deb7370ecbf73cedbfb8d434bb04f8
4
+ data.tar.gz: e5a1b45c89fdc951906e7e80ce9a9c863edeeb0cf7a5707afe2e1b9bffa96292
5
5
  SHA512:
6
- metadata.gz: aef39b00d5a58cf5b32fcdeddd42f5c5c7691dbc506ff38b0f43784339bd55eeceed67f88aefa7badd8fc61c69b922970709f4a2a2661bd49fcb39dbd2d83fb6
7
- data.tar.gz: bf49c7aa022c56fbc3b3d58b7e27d83e447dfac562c33e8bcd0a308e7180edc8c2274e326a9a88d0b83559d89af6724a650dd91e475f93091d31472e83887e2d
6
+ metadata.gz: 85b635283440e12428cfd5ad9e42fc12a90224a433c3cf31222d7321bfc4bdaffa3c39ace46bfdda54ca87808cae8e4a4a8442694c1f30666469c3017b9cb252
7
+ data.tar.gz: '08720ba6a147ab68def101baddf20c7a867ba3edaea3a13ccc6de3ee99e05f34a02f01d10c98d7a8a0e851933af6b117dce774341657002b1a59a34c9dddedff'
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # AutoHtml
2
2
 
3
- AutoHtml is a collection of filters that transform plain text into HTML code.
3
+ AutoHtml is a collection of filters that transforms plain text into HTML code.
4
4
 
5
5
  ## Installation
6
6
 
@@ -63,10 +63,32 @@ Bellow is the list of bundled filters along with their optional arguments on ini
63
63
  * `AutoHtml::Markdown`
64
64
  * `AutoHtml::SimpleFormat`
65
65
 
66
- ## Code status
66
+ ## Using AutoHtml with ActiveRecord
67
67
 
68
- [![Circle CI](https://circleci.com/gh/dejan/auto_html.svg?style=svg&circle-token=57823c8b62302106564f97b58b64643b9760ed99)](https://circleci.com/gh/dejan/auto_html)
68
+ For performance reasons it's a good idea to store the formated output in the database, in a separate column, to avoid generating the same content on each access.
69
+ This can be acomplished simply by overriding the attribute writter:
70
+
71
+ ```ruby
72
+ class Comment < ActiveRecord::Base
73
+ FORMAT = AutoHtml::Pipeline.new(
74
+ AutoHtml::HtmlEscape.new,
75
+ AutoHtml::Markdown.new
76
+ )
77
+
78
+ def text=(t)
79
+ super(t)
80
+ self[:text_html] = FORMAT.call(t)
81
+ end
82
+ end
83
+ ```
84
+
85
+ Now, every time `text` attribute is set, `text_html` will be set as well:
86
+
87
+ ```Ruby
88
+ comment = Comment.new(text: 'Hey!')
89
+ comment.text_html # => '<p>Hey!</p>'
90
+ ```
69
91
 
70
92
  ## Licence
71
93
 
72
- AutoHtml is released under the [MIT License](https://raw.githubusercontent.com/dejan/auto_html/master/MIT-LICENCE).
94
+ AutoHtml is released under the [MIT License](https://raw.githubusercontent.com/dejan/auto_html/master/MIT-LICENSE).
data/Rakefile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'bundler/gem_tasks'
2
4
  require 'rspec/core/rake_task'
3
5
  require 'rubocop/rake_task'
@@ -5,4 +7,4 @@ require 'rubocop/rake_task'
5
7
  RSpec::Core::RakeTask.new(:spec)
6
8
  RuboCop::RakeTask.new
7
9
 
8
- task default: [:rubocop, :spec]
10
+ task default: %i[rubocop spec]
@@ -1,4 +1,5 @@
1
- require 'tag_helper'
1
+ # frozen_string_literal: true
2
+
2
3
  require 'gemoji'
3
4
 
4
5
  module AutoHtml
@@ -11,7 +12,7 @@ module AutoHtml
11
12
  end
12
13
 
13
14
  def call(text)
14
- text.gsub(emoji_pattern) do
15
+ text.gsub(self.class.emoji_pattern) do
15
16
  name = Regexp.last_match(1)
16
17
  alt = ":#{name}:"
17
18
  html_options = {
@@ -27,6 +28,15 @@ module AutoHtml
27
28
  end
28
29
  end
29
30
 
31
+ def self.emoji_pattern
32
+ @emoji_pattern ||=
33
+ /:(#{emoji_names.map { |name| Regexp.escape(name) }.join('|')}):/
34
+ end
35
+
36
+ def self.emoji_names
37
+ ::Emoji.all.map(&:aliases).flatten.sort
38
+ end
39
+
30
40
  private
31
41
 
32
42
  def emoji_url(name)
@@ -37,19 +47,6 @@ module AutoHtml
37
47
  File.join('emoji', emoji_filename(name))
38
48
  end
39
49
 
40
- def self.emoji_pattern
41
- @emoji_pattern ||=
42
- /:(#{emoji_names.map { |name| Regexp.escape(name) }.join('|')}):/
43
- end
44
-
45
- def emoji_pattern
46
- self.class.emoji_pattern
47
- end
48
-
49
- def self.emoji_names
50
- ::Emoji.all.map(&:aliases).flatten.sort
51
- end
52
-
53
50
  def emoji_filename(name)
54
51
  ::Emoji.find_by_alias(name).image_filename
55
52
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'cgi'
2
4
 
3
5
  module AutoHtml
@@ -1,4 +1,4 @@
1
- require 'tag_helper'
1
+ # frozen_string_literal: true
2
2
 
3
3
  module AutoHtml
4
4
  # Image filter
@@ -17,7 +17,7 @@ module AutoHtml
17
17
  private
18
18
 
19
19
  def image_pattern
20
- %r{(?<!src=")https?:\/\/.+?\.(jpg|jpeg|bmp|gif|png)(\?\S+)?}i
20
+ %r{(?<!src=")https?://.+?\.(jpg|jpeg|bmp|gif|png)(\?\S+)?}i
21
21
  end
22
22
  end
23
23
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'uri'
2
4
  require 'rinku'
3
5
  require 'rexml/document'
@@ -11,13 +13,13 @@ module AutoHtml
11
13
  end
12
14
 
13
15
  def call(text)
14
- Rinku.auto_link(text, :all, target_attr)
16
+ Rinku.auto_link(text, :all, attributes)
15
17
  end
16
18
 
17
19
  private
18
20
 
19
21
  def attributes
20
- [target_attr, rel_attr].compact.join(' ')
22
+ [target_attr, rel_attr].compact.join(' ') unless [target_attr, rel_attr].compact.empty?
21
23
  end
22
24
 
23
25
  def rel_attr
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'redcarpet'
2
4
 
3
5
  module AutoHtml
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module AutoHtml
2
4
  # Applies collection of filters to a text
3
5
  class Pipeline
@@ -7,6 +9,7 @@ module AutoHtml
7
9
 
8
10
  def call(text)
9
11
  return '' if text.nil? || text.empty?
12
+
10
13
  @filters.inject(text) do |content, filter|
11
14
  filter.call(content)
12
15
  end
@@ -1,4 +1,4 @@
1
- require 'tag_helper'
1
+ # frozen_string_literal: true
2
2
 
3
3
  module AutoHtml
4
4
  # SimpleFormat filter
@@ -14,6 +14,7 @@ module AutoHtml
14
14
 
15
15
  def split_paragraphs(text)
16
16
  return [] if text.nil? || text.empty?
17
+
17
18
  text.to_s.gsub(/\r\n?/, "\n").split(/\n\n+/).map! do |t|
18
19
  t.gsub!(/([^\n]\n)(?=[^\n])/, '\1<br />') || t
19
20
  end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AutoHtml
4
+ # XHTML tags builder
5
+ module TagHelper
6
+ def tag(tag_name, attrs = {})
7
+ if block_given?
8
+ content_tag(tag_name, yield, attrs)
9
+ else
10
+ unary_tag(tag_name, attrs)
11
+ end
12
+ end
13
+
14
+ private
15
+
16
+ def unary_tag(tag_name, attrs = {})
17
+ "<#{tag_and_attributes(tag_name, tag_attributes(attrs))} />"
18
+ end
19
+
20
+ def content_tag(tag_name, value, attrs = {})
21
+ start_tag = "<#{tag_and_attributes(tag_name, tag_attributes(attrs))}>"
22
+ end_tag = "</#{tag_name}>"
23
+ [start_tag, value, end_tag].join
24
+ end
25
+
26
+ def tag_and_attributes(tag_name, attributes)
27
+ attributes.empty? ? tag_name : "#{tag_name} #{attributes}"
28
+ end
29
+
30
+ def tag_attributes(hash)
31
+ hash.compact
32
+ .to_a
33
+ .map { |k, v| %(#{k}="#{v}") }.join(' ')
34
+ end
35
+
36
+ extend self
37
+ end
38
+ end
data/lib/auto_html.rb CHANGED
@@ -1,7 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # AutoHtml is a collection of filters that transform plain text into HTML code.
2
4
  module AutoHtml
3
5
  autoload :Pipeline, 'auto_html/pipeline'
4
-
6
+ autoload :TagHelper, 'auto_html/tag_helper'
5
7
  autoload :Emoji, 'auto_html/emoji'
6
8
  autoload :HtmlEscape, 'auto_html/html_escape'
7
9
  autoload :Image, 'auto_html/image'
@@ -1,9 +1,12 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  RSpec.describe AutoHtml::Emoji do
4
6
  it 'converts emoji to HTML' do
5
7
  expect(subject.call(':joy:')).to eq(
6
8
  '<img src="/images/emoji/unicode/1f602.png" class="emoji" title=":joy:" '\
7
- 'alt=":joy:" height="20" witdh="20" align="absmiddle" />')
9
+ 'alt=":joy:" height="20" witdh="20" align="absmiddle" />'
10
+ )
8
11
  end
9
12
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  RSpec.describe AutoHtml::HtmlEscape do
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  RSpec.describe AutoHtml::Image do
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  RSpec.describe AutoHtml::Link do
@@ -31,9 +33,21 @@ RSpec.describe AutoHtml::Link do
31
33
  expect(result).to eq '<a href="http://www.google.com/#q=nikola+tesla&ct=tesla09&oi=ddle&fp=Xmf0jJ9P_V0">http://www.google.com/#q=nikola+tesla&ct=tesla09&oi=ddle&fp=Xmf0jJ9P_V0</a>'
32
34
  end
33
35
 
34
- it 'transforms with options' do
36
+ it 'transforms with target options' do
35
37
  filter = described_class.new(target: '_blank')
36
38
  result = filter.call('http://rors.org')
37
39
  expect(result).to eq '<a href="http://rors.org" target="_blank">http://rors.org</a>'
38
40
  end
41
+
42
+ it 'transforms with rel options' do
43
+ filter = described_class.new(rel: 'nofollow')
44
+ result = filter.call('http://rors.org')
45
+ expect(result).to eq '<a href="http://rors.org" rel="nofollow">http://rors.org</a>'
46
+ end
47
+
48
+ it 'transforms with target and rel options' do
49
+ filter = described_class.new(target: '_blank', rel: 'nofollow')
50
+ result = filter.call('http://rors.org')
51
+ expect(result).to eq '<a href="http://rors.org" target="_blank" rel="nofollow">http://rors.org</a>'
52
+ end
39
53
  end
@@ -1,7 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  RSpec.describe AutoHtml::Markdown do
4
6
  it 'formats input using simple GFM rules' do
5
- expect(subject.call '*hello*').to eq "<p><em>hello</em></p>\n"
7
+ expect(subject.call('*hello*')).to eq "<p><em>hello</em></p>\n"
6
8
  end
7
9
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  RSpec.describe AutoHtml::Pipeline do
@@ -15,10 +17,10 @@ RSpec.describe AutoHtml::Pipeline do
15
17
  end
16
18
 
17
19
  it 'is blank if input is blank' do
18
- expect(subject.call '').to eq ''
20
+ expect(subject.call('')).to eq ''
19
21
  end
20
22
 
21
23
  it 'is blank if input is nil' do
22
- expect(subject.call nil).to eq ''
24
+ expect(subject.call(nil)).to eq ''
23
25
  end
24
26
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  RSpec.describe AutoHtml::SimpleFormat do
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe AutoHtml::TagHelper do
6
+ it 'generates tag with attributes' do
7
+ result = subject.tag(:img, src: '1.png', alt: 'number one!')
8
+ expect(result).to eq '<img src="1.png" alt="number one!" />'
9
+ end
10
+
11
+ it 'generates tag without attributes' do
12
+ result = subject.tag(:br)
13
+ expect(result).to eq '<br />'
14
+ end
15
+
16
+ it 'generates content tags' do
17
+ result = subject.tag(:label, for: 'name') { 'Name' }
18
+ expect(result).to eq '<label for="name">Name</label>'
19
+ end
20
+
21
+ it 'generates content tags without attributes' do
22
+ result = subject.tag(:label) { 'Username' }
23
+ expect(result).to eq '<label>Username</label>'
24
+ end
25
+
26
+ it 'generates nested tags' do
27
+ result = subject.tag(:form) { subject.tag(:input, type: 'text') }
28
+ expect(result).to eq '<form><input type="text" /></form>'
29
+ end
30
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'auto_html'
2
4
 
3
5
  RSpec.configure do |config|
metadata CHANGED
@@ -1,85 +1,71 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: auto_html
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dejan Simic
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-12-19 00:00:00.000000000 Z
11
+ date: 2021-11-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: tag_helper
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - "~>"
18
- - !ruby/object:Gem::Version
19
- version: '0.5'
20
- type: :runtime
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - "~>"
25
- - !ruby/object:Gem::Version
26
- version: '0.5'
27
- - !ruby/object:Gem::Dependency
28
- name: rinku
14
+ name: gemoji
29
15
  requirement: !ruby/object:Gem::Requirement
30
16
  requirements:
31
17
  - - "~>"
32
18
  - !ruby/object:Gem::Version
33
- version: '1.7'
19
+ version: '2.1'
34
20
  type: :runtime
35
21
  prerelease: false
36
22
  version_requirements: !ruby/object:Gem::Requirement
37
23
  requirements:
38
24
  - - "~>"
39
25
  - !ruby/object:Gem::Version
40
- version: '1.7'
26
+ version: '2.1'
41
27
  - !ruby/object:Gem::Dependency
42
- name: gemoji
28
+ name: redcarpet
43
29
  requirement: !ruby/object:Gem::Requirement
44
30
  requirements:
45
31
  - - "~>"
46
32
  - !ruby/object:Gem::Version
47
- version: '2.1'
33
+ version: '3.5'
48
34
  type: :runtime
49
35
  prerelease: false
50
36
  version_requirements: !ruby/object:Gem::Requirement
51
37
  requirements:
52
38
  - - "~>"
53
39
  - !ruby/object:Gem::Version
54
- version: '2.1'
40
+ version: '3.5'
55
41
  - !ruby/object:Gem::Dependency
56
- name: redcarpet
42
+ name: rinku
57
43
  requirement: !ruby/object:Gem::Requirement
58
44
  requirements:
59
45
  - - "~>"
60
46
  - !ruby/object:Gem::Version
61
- version: '3.3'
47
+ version: '2.0'
62
48
  type: :runtime
63
49
  prerelease: false
64
50
  version_requirements: !ruby/object:Gem::Requirement
65
51
  requirements:
66
52
  - - "~>"
67
53
  - !ruby/object:Gem::Version
68
- version: '3.3'
54
+ version: '2.0'
69
55
  - !ruby/object:Gem::Dependency
70
56
  name: rake
71
57
  requirement: !ruby/object:Gem::Requirement
72
58
  requirements:
73
59
  - - "~>"
74
60
  - !ruby/object:Gem::Version
75
- version: '10.4'
61
+ version: 13.0.6
76
62
  type: :development
77
63
  prerelease: false
78
64
  version_requirements: !ruby/object:Gem::Requirement
79
65
  requirements:
80
66
  - - "~>"
81
67
  - !ruby/object:Gem::Version
82
- version: '10.4'
68
+ version: 13.0.6
83
69
  - !ruby/object:Gem::Dependency
84
70
  name: rspec
85
71
  requirement: !ruby/object:Gem::Requirement
@@ -114,14 +100,14 @@ dependencies:
114
100
  requirements:
115
101
  - - "~>"
116
102
  - !ruby/object:Gem::Version
117
- version: '0.33'
103
+ version: '1.23'
118
104
  type: :development
119
105
  prerelease: false
120
106
  version_requirements: !ruby/object:Gem::Requirement
121
107
  requirements:
122
108
  - - "~>"
123
109
  - !ruby/object:Gem::Version
124
- version: '0.33'
110
+ version: '1.23'
125
111
  description: Collection of filters for transforming text into HTML code
126
112
  email: desimic@gmail.com
127
113
  executables: []
@@ -138,19 +124,22 @@ files:
138
124
  - lib/auto_html/markdown.rb
139
125
  - lib/auto_html/pipeline.rb
140
126
  - lib/auto_html/simple_format.rb
141
- - spec/emoji_spec.rb
142
- - spec/html_escape_spec.rb
143
- - spec/image_spec.rb
144
- - spec/link_spec.rb
145
- - spec/markdown_spec.rb
146
- - spec/pipeline_spec.rb
147
- - spec/simple_format_spec.rb
127
+ - lib/auto_html/tag_helper.rb
128
+ - spec/auto_html/emoji_spec.rb
129
+ - spec/auto_html/html_escape_spec.rb
130
+ - spec/auto_html/image_spec.rb
131
+ - spec/auto_html/link_spec.rb
132
+ - spec/auto_html/markdown_spec.rb
133
+ - spec/auto_html/pipeline_spec.rb
134
+ - spec/auto_html/simple_format_spec.rb
135
+ - spec/auto_html/tag_helper_spec.rb
148
136
  - spec/spec_helper.rb
149
137
  homepage: https://github.com/dejan/auto_html
150
138
  licenses:
151
139
  - MIT
152
- metadata: {}
153
- post_install_message:
140
+ metadata:
141
+ rubygems_mfa_required: 'true'
142
+ post_install_message:
154
143
  rdoc_options: []
155
144
  require_paths:
156
145
  - lib
@@ -158,17 +147,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
158
147
  requirements:
159
148
  - - ">="
160
149
  - !ruby/object:Gem::Version
161
- version: '0'
150
+ version: 2.5.0
162
151
  required_rubygems_version: !ruby/object:Gem::Requirement
163
152
  requirements:
164
153
  - - ">="
165
154
  - !ruby/object:Gem::Version
166
155
  version: '0'
167
156
  requirements: []
168
- rubyforge_project:
169
- rubygems_version: 2.4.5
170
- signing_key:
157
+ rubygems_version: 3.2.22
158
+ signing_key:
171
159
  specification_version: 4
172
160
  summary: Plain text to HTML conversion
173
161
  test_files: []
174
- has_rdoc: