model_formatting 0.2.0 → 0.2.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Rakefile +3 -4
- data/bin/model_formatting +4 -0
- data/lib/model_formatting.rb +13 -8
- data/test/fixtures/mkd-extraction.txt +15 -0
- data/test/formatting_test.rb +16 -1
- data/test/model_formatting_test.rb +6 -2
- metadata +10 -8
- data/tender.patch +0 -95
data/Rakefile
CHANGED
|
@@ -3,17 +3,16 @@ require 'rake/testtask'
|
|
|
3
3
|
|
|
4
4
|
spec = Gem::Specification.new do |s|
|
|
5
5
|
s.name = "model_formatting"
|
|
6
|
-
s.version = "0.2.
|
|
6
|
+
s.version = "0.2.1.1"
|
|
7
7
|
s.author = "ENTP"
|
|
8
8
|
s.email = "company@entp.com"
|
|
9
9
|
s.homepage = "http://github.com/entp"
|
|
10
10
|
s.platform = Gem::Platform::RUBY
|
|
11
11
|
s.summary = "Automatically format model attributes using rdiscount and Tender/Lighthouse extensions."
|
|
12
|
-
s.files = FileList['[a-zA-Z]*', 'lib/**/*', 'rails/**/*', 'test/**/*']
|
|
13
|
-
s.require_path = "lib"
|
|
12
|
+
s.files = FileList['[a-zA-Z]*', 'bin/*', 'lib/**/*', 'rails/**/*', 'test/**/*']
|
|
14
13
|
s.has_rdoc = false
|
|
15
14
|
s.extra_rdoc_files = ["README"]
|
|
16
|
-
s.add_dependency("rdiscount", "~>1.
|
|
15
|
+
s.add_dependency("rdiscount", "~>1.6.5")
|
|
17
16
|
s.add_dependency("actionpack", "~>2.2.3")
|
|
18
17
|
s.add_dependency("activerecord", "~>2.2.3")
|
|
19
18
|
s.add_dependency("activesupport", "~>2.2.3")
|
data/lib/model_formatting.rb
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
require 'cgi'
|
|
2
|
+
require 'active_support' # NilClass#blank?
|
|
2
3
|
module ModelFormatting
|
|
3
4
|
class Part < Array
|
|
4
5
|
attr_reader :format
|
|
@@ -149,9 +150,15 @@ module ModelFormatting
|
|
|
149
150
|
end
|
|
150
151
|
end
|
|
151
152
|
yield text
|
|
152
|
-
#
|
|
153
|
-
|
|
154
|
-
|
|
153
|
+
# In cases where multiple tag names are provided AND the tags mismatch or
|
|
154
|
+
# overlap in non-conforming ways, it's possible for extracted sections to
|
|
155
|
+
# have extractions in them. To keep content from being eaten by the markdown
|
|
156
|
+
# extractor, loop until all of the extractions have been replaced.
|
|
157
|
+
while !extractions.keys.empty?
|
|
158
|
+
# Insert block extractions
|
|
159
|
+
text.gsub!(/\{mkd-extraction-([0-9a-f]{32})\}/) do
|
|
160
|
+
extractions.delete($1)
|
|
161
|
+
end
|
|
155
162
|
end
|
|
156
163
|
text
|
|
157
164
|
end
|
|
@@ -173,12 +180,11 @@ module ModelFormatting
|
|
|
173
180
|
end
|
|
174
181
|
|
|
175
182
|
begin
|
|
176
|
-
gem 'rdiscount', '>= 1.2.7.1'
|
|
177
183
|
require 'rdiscount'
|
|
178
184
|
def self.process_markdown(text)
|
|
179
185
|
RDiscount.new(text).to_html
|
|
180
186
|
end
|
|
181
|
-
rescue
|
|
187
|
+
rescue LoadError
|
|
182
188
|
puts "No RDiscount gem found. `gem install rdiscount`."
|
|
183
189
|
def self.process_markdown(text)
|
|
184
190
|
text
|
|
@@ -186,7 +192,6 @@ module ModelFormatting
|
|
|
186
192
|
end
|
|
187
193
|
|
|
188
194
|
begin
|
|
189
|
-
gem 'tidy'
|
|
190
195
|
require 'tidy'
|
|
191
196
|
Tidy.path = ENV['TIDY_PATH'] unless ENV['TIDY_PATH'].blank?
|
|
192
197
|
def self.process_tidy(text)
|
|
@@ -196,10 +201,10 @@ module ModelFormatting
|
|
|
196
201
|
tidy.clean(text)
|
|
197
202
|
end
|
|
198
203
|
end
|
|
199
|
-
rescue
|
|
204
|
+
rescue LoadError
|
|
200
205
|
puts "No Tidy gem found. `gem install tidy`. Don't forget to set Tidy.path."
|
|
201
206
|
def self.process_tidy(text)
|
|
202
207
|
text
|
|
203
208
|
end
|
|
204
209
|
end
|
|
205
|
-
end
|
|
210
|
+
end
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# Busted markdown!
|
|
2
|
+
|
|
3
|
+
<p><code>The missing code tag...</p>
|
|
4
|
+
|
|
5
|
+
@@@
|
|
6
|
+
will eat this code block!
|
|
7
|
+
@@@
|
|
8
|
+
|
|
9
|
+
Hard to fix but at least we can provide a warning!
|
|
10
|
+
|
|
11
|
+
EDIT: omg fixed!
|
|
12
|
+
|
|
13
|
+
<p><code>Additional code blocks below appear to trigger the bug.</code></p>
|
|
14
|
+
|
|
15
|
+
|
data/test/formatting_test.rb
CHANGED
|
@@ -47,6 +47,21 @@ module ModelFormatting
|
|
|
47
47
|
end
|
|
48
48
|
end
|
|
49
49
|
|
|
50
|
+
class BaseWithAfter < Base
|
|
51
|
+
formats :body do
|
|
52
|
+
after { |format, text, options| text }
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
describe "base with after callback" do
|
|
57
|
+
it "does not leave mkd-extraction artifacts" do
|
|
58
|
+
record = BaseWithAfter.new
|
|
59
|
+
record.body = File.read(File.dirname(__FILE__) + '/fixtures/mkd-extraction.txt')
|
|
60
|
+
record.save
|
|
61
|
+
assert_no_match /mkd-extraction/, record.formatted_body
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
50
65
|
class Post < Base
|
|
51
66
|
formats :body, :title => :title_html do
|
|
52
67
|
attributes[:bio] = :full_bio
|
|
@@ -158,4 +173,4 @@ module ModelFormatting
|
|
|
158
173
|
end
|
|
159
174
|
end
|
|
160
175
|
end
|
|
161
|
-
|
|
176
|
+
|
|
@@ -54,6 +54,10 @@ class ModelFormattingTest < Test::Unit::TestCase
|
|
|
54
54
|
ModelFormatting::Config.new(nil, nil, nil).replace_vars("a:abc/:d_e_f/:foo!", :abc => 'bc', :d_e_f => '-').should == "abc/-/!"
|
|
55
55
|
end
|
|
56
56
|
|
|
57
|
+
it "doesn't remove lines with pipes" do
|
|
58
|
+
ModelFormatting.process(:html, "Yo.\n\nhere is a | pipe\n\nfoobaz").should == %(<div><p>Yo.</p>\n\n<p>here is a | pipe</p>\n\n<p>foobaz</p></div>)
|
|
59
|
+
end
|
|
60
|
+
|
|
57
61
|
it "links and encodes urls correctly" do
|
|
58
62
|
ModelFormatting.process(:html, "a *b* \n[Whoo](http://entp.com?a=1&b=2)").should == %(<div><p>a <em>b</em><br/>\n<a href="http://entp.com?a=1&b=2">Whoo</a></p></div>)
|
|
59
63
|
end
|
|
@@ -63,11 +67,11 @@ class ModelFormattingTest < Test::Unit::TestCase
|
|
|
63
67
|
end
|
|
64
68
|
|
|
65
69
|
it "converts @@@ with params to code blocks" do
|
|
66
|
-
ModelFormatting.process(:html, "foo\n@@@ ninja\nbar\n@@@\n@@@\nbaz\n@@@\n@@@ wah wah \n \n").should == %(<div><p>foo
|
|
70
|
+
ModelFormatting.process(:html, "foo\n@@@ ninja\nbar\n@@@\n@@@\nbaz\n@@@\n@@@ wah wah \n \n").should == %(<div><p>foo</p>\n\n<pre><code class=\"ninja\">bar</code>\n</pre>\n\n\n<pre><code>baz</code>\n</pre>\n\n\n<p>@@@ wah wah</p></div>)
|
|
67
71
|
end
|
|
68
72
|
|
|
69
73
|
it "fixes irregular number of @@@'s" do
|
|
70
|
-
ModelFormatting.process(:html, "foo\n@@@\nbar\n@@@\n@@@\nbaz\n@@@\n@@@ wah wah \n \n@@@").should == %(<div><p>foo
|
|
74
|
+
ModelFormatting.process(:html, "foo\n@@@\nbar\n@@@\n@@@\nbaz\n@@@\n@@@ wah wah \n \n@@@").should == %(<div><p>foo</p>\n\n<pre><code>bar</code>\n</pre>\n\n\n<pre><code>baz</code>\n</pre>\n\n\n<p>@@@ wah wah</p>\n\n<pre><code></code>\n</pre></div>)
|
|
71
75
|
end
|
|
72
76
|
|
|
73
77
|
it "converts @@@ with params to code blocks with text format" do
|
metadata
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: model_formatting
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
hash:
|
|
4
|
+
hash: 89
|
|
5
5
|
prerelease: false
|
|
6
6
|
segments:
|
|
7
7
|
- 0
|
|
8
8
|
- 2
|
|
9
|
-
-
|
|
10
|
-
|
|
9
|
+
- 1
|
|
10
|
+
- 1
|
|
11
|
+
version: 0.2.1.1
|
|
11
12
|
platform: ruby
|
|
12
13
|
authors:
|
|
13
14
|
- ENTP
|
|
@@ -15,7 +16,7 @@ autorequire:
|
|
|
15
16
|
bindir: bin
|
|
16
17
|
cert_chain: []
|
|
17
18
|
|
|
18
|
-
date: 2010-07
|
|
19
|
+
date: 2010-11-07 01:00:00 -07:00
|
|
19
20
|
default_executable:
|
|
20
21
|
dependencies:
|
|
21
22
|
- !ruby/object:Gem::Dependency
|
|
@@ -26,12 +27,12 @@ dependencies:
|
|
|
26
27
|
requirements:
|
|
27
28
|
- - ~>
|
|
28
29
|
- !ruby/object:Gem::Version
|
|
29
|
-
hash:
|
|
30
|
+
hash: 5
|
|
30
31
|
segments:
|
|
31
32
|
- 1
|
|
33
|
+
- 6
|
|
32
34
|
- 5
|
|
33
|
-
|
|
34
|
-
version: 1.5.5
|
|
35
|
+
version: 1.6.5
|
|
35
36
|
type: :runtime
|
|
36
37
|
version_requirements: *id001
|
|
37
38
|
- !ruby/object:Gem::Dependency
|
|
@@ -141,12 +142,13 @@ extra_rdoc_files:
|
|
|
141
142
|
files:
|
|
142
143
|
- Rakefile
|
|
143
144
|
- README
|
|
144
|
-
-
|
|
145
|
+
- bin/model_formatting
|
|
145
146
|
- lib/model_formatting/config.rb
|
|
146
147
|
- lib/model_formatting/init.rb
|
|
147
148
|
- lib/model_formatting/instance_methods.rb
|
|
148
149
|
- lib/model_formatting.rb
|
|
149
150
|
- rails/init.rb
|
|
151
|
+
- test/fixtures/mkd-extraction.txt
|
|
150
152
|
- test/formatting_test.rb
|
|
151
153
|
- test/model_formatting_test.rb
|
|
152
154
|
- test/test_helper.rb
|
data/tender.patch
DELETED
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
Only in .: .git
|
|
2
|
-
diff -ur ./lib/model_formatting/instance_methods.rb ../tender/vendor/plugins/model_formatting/lib/model_formatting/instance_methods.rb
|
|
3
|
-
--- ./lib/model_formatting/instance_methods.rb 2010-07-09 10:29:14.000000000 -0700
|
|
4
|
-
+++ ../tender/vendor/plugins/model_formatting/lib/model_formatting/instance_methods.rb 2010-06-14 13:45:10.000000000 -0700
|
|
5
|
-
@@ -2,13 +2,12 @@
|
|
6
|
-
module InstanceMethods
|
|
7
|
-
protected
|
|
8
|
-
def format_content_with_model_formatting
|
|
9
|
-
- model_formatting_attributes.each do |original, formatted|
|
|
10
|
-
+ self.class.model_formatting_attributes.each do |original, formatted|
|
|
11
|
-
text = send(original)
|
|
12
|
-
data = \
|
|
13
|
-
if text.blank?
|
|
14
|
-
''
|
|
15
|
-
else
|
|
16
|
-
- text.strip!
|
|
17
|
-
options = {:white_list => model_formatting_white_list,
|
|
18
|
-
:before => model_formatting_before_callback,
|
|
19
|
-
:after => model_formatting_after_callback}
|
|
20
|
-
diff -ur ./lib/model_formatting.rb ../tender/vendor/plugins/model_formatting/lib/model_formatting.rb
|
|
21
|
-
--- ./lib/model_formatting.rb 2010-07-09 11:29:32.000000000 -0700
|
|
22
|
-
+++ ../tender/vendor/plugins/model_formatting/lib/model_formatting.rb 2010-02-19 14:49:00.000000000 -0800
|
|
23
|
-
@@ -138,11 +138,11 @@
|
|
24
|
-
parts.each { |p| p.compact! }
|
|
25
|
-
end
|
|
26
|
-
|
|
27
|
-
- def self.extract_regex(text, *regexes)
|
|
28
|
-
+ def self.extract_tag(text, *tag_names)
|
|
29
|
-
# Extract pre blocks
|
|
30
|
-
extractions = {}
|
|
31
|
-
- regexes.each do |regex|
|
|
32
|
-
- text.gsub!(regex) do |match|
|
|
33
|
-
+ tag_names.each do |tag_name|
|
|
34
|
-
+ text.gsub!(%r{<#{tag_name}[^>]*>.*?</#{tag_name}>}m) do |match|
|
|
35
|
-
md5 = Digest::MD5.hexdigest(match)
|
|
36
|
-
extractions[md5] = match
|
|
37
|
-
"{mkd-extraction-#{md5}}"
|
|
38
|
-
@@ -156,16 +156,6 @@
|
|
39
|
-
text
|
|
40
|
-
end
|
|
41
|
-
|
|
42
|
-
- def self.tag_name_to_regex(name)
|
|
43
|
-
- %r{<#{name}[^>]*>.*?</#{name}>}m
|
|
44
|
-
- end
|
|
45
|
-
-
|
|
46
|
-
- def self.extract_tag(text, *tag_names)
|
|
47
|
-
- extract_regex text, *tag_names.map { |n| tag_name_to_regex(n) } do |text|
|
|
48
|
-
- yield text
|
|
49
|
-
- end
|
|
50
|
-
- end
|
|
51
|
-
-
|
|
52
|
-
def self.gfm(text)
|
|
53
|
-
extract_tag(text, :pre) do |txt|
|
|
54
|
-
# prevent foo_bar_baz from ending up with an italic word in the middle
|
|
55
|
-
@@ -183,13 +173,13 @@
|
|
56
|
-
end
|
|
57
|
-
|
|
58
|
-
begin
|
|
59
|
-
- gem 'rdiscount', '~> 1.2.7.1'
|
|
60
|
-
+ gem 'rdiscount', '>= 1.2.7.1'
|
|
61
|
-
require 'rdiscount'
|
|
62
|
-
def self.process_markdown(text)
|
|
63
|
-
RDiscount.new(text).to_html
|
|
64
|
-
end
|
|
65
|
-
rescue Gem::LoadError
|
|
66
|
-
- puts "No RDiscount gem found. `gem install rdiscount -v '~>1.2.7.1'`."
|
|
67
|
-
+ puts "No RDiscount gem found. `gem install rdiscount`."
|
|
68
|
-
def self.process_markdown(text)
|
|
69
|
-
text
|
|
70
|
-
end
|
|
71
|
-
@@ -212,4 +202,4 @@
|
|
72
|
-
text
|
|
73
|
-
end
|
|
74
|
-
end
|
|
75
|
-
-end
|
|
76
|
-
+end
|
|
77
|
-
|
|
78
|
-
Only in .: tender.patch
|
|
79
|
-
diff -ur ./test/formatting_test.rb ../tender/vendor/plugins/model_formatting/test/formatting_test.rb
|
|
80
|
-
--- ./test/formatting_test.rb 2010-07-09 10:29:14.000000000 -0700
|
|
81
|
-
+++ ../tender/vendor/plugins/model_formatting/test/formatting_test.rb 2010-02-19 14:49:00.000000000 -0800
|
|
82
|
-
@@ -38,6 +38,13 @@
|
|
83
|
-
record.save
|
|
84
|
-
record.formatted_body.should == %(<div><p>booya</p></div>)
|
|
85
|
-
end
|
|
86
|
-
+
|
|
87
|
-
+ it "preserves leading spaces in code blocks" do
|
|
88
|
-
+ record = Simple.new
|
|
89
|
-
+ record.body = " code\n more code\n\nnot code\n\n"
|
|
90
|
-
+ record.save
|
|
91
|
-
+ record.formatted_body.should == %(<div><pre><code>code\nmore code</code></pre>\n\n<p>not code</p></div>)
|
|
92
|
-
+ end
|
|
93
|
-
end
|
|
94
|
-
|
|
95
|
-
class Post < Base
|