model_formatting 0.5 → 0.6.6
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/Gemfile +3 -3
- data/Gemfile.lock +43 -35
- data/README +7 -1
- data/Rakefile +7 -10
- data/lib/model_formatting.rb +6 -12
- data/lib/model_formatting/init.rb +1 -1
- data/lib/redcarpet_custom.rb +27 -0
- data/test/formatting_test.rb +1 -1
- data/test/model_formatting_test.rb +7 -2
- metadata +47 -48
data/Gemfile
CHANGED
@@ -3,9 +3,9 @@ source "http://gem.entp.com"
|
|
3
3
|
|
4
4
|
gem 'rake'
|
5
5
|
gem 'redcarpet', '~> 2.2'
|
6
|
-
gem 'actionpack', '~> 3.
|
7
|
-
gem 'activerecord', '~> 3.
|
8
|
-
gem 'activesupport', '~> 3.
|
6
|
+
gem 'actionpack', '~> 3.2'
|
7
|
+
gem 'activerecord', '~> 3.2'
|
8
|
+
gem 'activesupport', '~> 3.2'
|
9
9
|
gem 'tidy_ffi', '~> 0.1.2'
|
10
10
|
|
11
11
|
group :development, :test do
|
data/Gemfile.lock
CHANGED
@@ -2,55 +2,63 @@ GEM
|
|
2
2
|
remote: http://rubygems.org/
|
3
3
|
remote: http://gem.entp.com/
|
4
4
|
specs:
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
rack (~> 1.2
|
13
|
-
rack-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
arel (
|
27
|
-
builder (
|
5
|
+
actionpack (3.2.15)
|
6
|
+
activemodel (= 3.2.15)
|
7
|
+
activesupport (= 3.2.15)
|
8
|
+
builder (~> 3.0.0)
|
9
|
+
erubis (~> 2.7.0)
|
10
|
+
journey (~> 1.0.4)
|
11
|
+
rack (~> 1.4.5)
|
12
|
+
rack-cache (~> 1.2)
|
13
|
+
rack-test (~> 0.6.1)
|
14
|
+
sprockets (~> 2.2.1)
|
15
|
+
activemodel (3.2.15)
|
16
|
+
activesupport (= 3.2.15)
|
17
|
+
builder (~> 3.0.0)
|
18
|
+
activerecord (3.2.15)
|
19
|
+
activemodel (= 3.2.15)
|
20
|
+
activesupport (= 3.2.15)
|
21
|
+
arel (~> 3.0.2)
|
22
|
+
tzinfo (~> 0.3.29)
|
23
|
+
activesupport (3.2.15)
|
24
|
+
i18n (~> 0.6, >= 0.6.4)
|
25
|
+
multi_json (~> 1.0)
|
26
|
+
arel (3.0.2)
|
27
|
+
builder (3.0.4)
|
28
28
|
context (0.5.5)
|
29
|
-
erubis (2.
|
30
|
-
abstract (>= 1.0.0)
|
29
|
+
erubis (2.7.0)
|
31
30
|
ffi (1.0.10)
|
32
|
-
|
31
|
+
hike (1.2.3)
|
32
|
+
i18n (0.6.5)
|
33
|
+
journey (1.0.4)
|
33
34
|
matchy (0.4.0)
|
34
|
-
|
35
|
-
rack
|
36
|
-
|
37
|
-
|
35
|
+
multi_json (1.8.2)
|
36
|
+
rack (1.4.5)
|
37
|
+
rack-cache (1.2)
|
38
|
+
rack (>= 0.4)
|
39
|
+
rack-test (0.6.2)
|
38
40
|
rack (>= 1.0)
|
39
41
|
rake (0.9.2)
|
40
42
|
redcarpet (2.2.2)
|
43
|
+
sprockets (2.2.2)
|
44
|
+
hike (~> 1.2)
|
45
|
+
multi_json (~> 1.0)
|
46
|
+
rack (~> 1.0)
|
47
|
+
tilt (~> 1.1, != 1.3.0)
|
41
48
|
tidy_ffi (0.1.3)
|
42
49
|
ffi (>= 0.3.5)
|
43
|
-
|
50
|
+
tilt (1.4.1)
|
51
|
+
tzinfo (0.3.38)
|
44
52
|
|
45
53
|
PLATFORMS
|
46
54
|
ruby
|
47
55
|
|
48
56
|
DEPENDENCIES
|
49
|
-
actionpack (~> 3.
|
50
|
-
activerecord (~> 3.
|
51
|
-
activesupport (~> 3.
|
57
|
+
actionpack (~> 3.2)
|
58
|
+
activerecord (~> 3.2)
|
59
|
+
activesupport (~> 3.2)
|
52
60
|
context (= 0.5.5)
|
53
61
|
matchy (~> 0.4.0)
|
54
62
|
rake
|
55
|
-
redcarpet
|
63
|
+
redcarpet (~> 2.2)
|
56
64
|
tidy_ffi (~> 0.1.2)
|
data/README
CHANGED
@@ -1,3 +1,9 @@
|
|
1
|
-
This is a plugin for Rails
|
1
|
+
This is a plugin for Rails that performs automatic text-to-html formatting.
|
2
|
+
|
3
|
+
For rails 2.3, please use the 'rails23' branch (you can specify ~> 0.2 in your gemfile)
|
4
|
+
|
5
|
+
It uses the 'redcarpet' markdown library with a few other pre-processing steps.
|
6
|
+
Note: tests do not run on 1.9.x series due to a bug in the test frameworks we use,
|
7
|
+
but the code works fine.
|
2
8
|
|
3
9
|
It is extracted from and used by [Tender](http://tenderapp.com/) and [Lighthouse](http://lighthouseapp.com/).
|
data/Rakefile
CHANGED
@@ -1,21 +1,22 @@
|
|
1
1
|
require 'rubygems/package_task'
|
2
|
-
require 'rake/testtask'
|
2
|
+
# require 'rake/testtask'
|
3
3
|
|
4
4
|
spec = Gem::Specification.new do |s|
|
5
5
|
s.name = "model_formatting"
|
6
|
-
s.version = "0.
|
6
|
+
s.version = "0.6.6"
|
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
|
-
s.summary = "Automatically format model attributes using
|
11
|
+
s.summary = "Automatically format model attributes using Redcarpet (markdown) and Tender/Lighthouse extensions."
|
12
|
+
s.description = s.summary
|
12
13
|
s.files = FileList['[a-zA-Z]*', 'bin/*', 'lib/**/*', 'rails/**/*', 'test/**/*']
|
13
14
|
s.has_rdoc = false
|
14
15
|
s.extra_rdoc_files = ["README"]
|
15
16
|
s.add_dependency("redcarpet", "~>2.2")
|
16
|
-
s.add_dependency("actionpack", "~>3.
|
17
|
-
s.add_dependency("activerecord", "~>3.
|
18
|
-
s.add_dependency("activesupport", "~>3.
|
17
|
+
s.add_dependency("actionpack", "~>3.2")
|
18
|
+
s.add_dependency("activerecord", "~>3.2")
|
19
|
+
s.add_dependency("activesupport", "~>3.2")
|
19
20
|
s.add_dependency("tidy_ffi", "~>0.1.2")
|
20
21
|
s.add_development_dependency("jeremymcanally-context", "~>0.5.5")
|
21
22
|
s.add_development_dependency("jeremymcanally-matchy", "~>0.1.0")
|
@@ -27,7 +28,3 @@ Gem::PackageTask.new(spec) do |pkg|
|
|
27
28
|
pkg.gem_spec = spec
|
28
29
|
end
|
29
30
|
|
30
|
-
Rake::TestTask.new do |t|
|
31
|
-
t.test_files = FileList['test/*_test.rb']
|
32
|
-
end
|
33
|
-
|
data/lib/model_formatting.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'cgi'
|
2
2
|
require 'active_support' # NilClass#blank?
|
3
|
+
require 'redcarpet_custom'
|
3
4
|
module ModelFormatting
|
4
5
|
class Part < Array
|
5
6
|
attr_reader :format
|
@@ -97,7 +98,7 @@ module ModelFormatting
|
|
97
98
|
format == :html ? "<div>#{string}</div>" : string
|
98
99
|
end
|
99
100
|
|
100
|
-
CODEBLOCK_RE =
|
101
|
+
CODEBLOCK_RE = /^(@@@|```)( ([a-z]+)\s*)?$/
|
101
102
|
|
102
103
|
# Parse a string into a given array of [CodeBlcok, FormattedBlock, CodeBlock, FormattedBlock]
|
103
104
|
#
|
@@ -122,7 +123,7 @@ module ModelFormatting
|
|
122
123
|
current_part = nil
|
123
124
|
else
|
124
125
|
if current_part then parts << current_part end
|
125
|
-
current_part = CodePart.new(format, $
|
126
|
+
current_part = CodePart.new(format, $3)
|
126
127
|
end
|
127
128
|
in_code_block = !in_code_block
|
128
129
|
else
|
@@ -178,11 +179,6 @@ module ModelFormatting
|
|
178
179
|
|
179
180
|
def self.gfm(text)
|
180
181
|
extract_tag(text, :pre) do |txt|
|
181
|
-
# prevent foo_bar_baz from ending up with an italic word in the middle
|
182
|
-
text.gsub!(/(^(?! {4}|\t)\w+_\w+_\w[\w_]*)/) do |x|
|
183
|
-
x.gsub('_', '\_') if x.split('').sort.to_s[0..1] == '__'
|
184
|
-
end
|
185
|
-
|
186
182
|
# in very clear cases, let newlines become <br /> tags
|
187
183
|
#text.gsub!(/(\A|^$\n)(^\w[^\n]*\n)(^\w[^\n]*$)+/m) do |x|
|
188
184
|
# x.gsub(/^(.+)$/, "\\1 ")
|
@@ -195,14 +191,12 @@ module ModelFormatting
|
|
195
191
|
begin
|
196
192
|
require 'redcarpet'
|
197
193
|
def self.process_markdown(text)
|
198
|
-
markdown = Redcarpet::Markdown.new(Redcarpet::Render::
|
194
|
+
markdown = Redcarpet::Markdown.new(Redcarpet::Render::Custom,
|
199
195
|
:no_intra_emphasis => true,
|
200
196
|
:tables => true,
|
201
197
|
:fenced_code_blocks => true,
|
202
|
-
:space_after_headers => true
|
203
|
-
|
204
|
-
# :with_toc_data => true,
|
205
|
-
:autolink => true).render(text)
|
198
|
+
:space_after_headers => true
|
199
|
+
).render(text)
|
206
200
|
end
|
207
201
|
rescue LoadError
|
208
202
|
puts "No Redcarpet gem found. `gem install redcarpet`."
|
@@ -31,7 +31,7 @@ module ModelFormatting::Init
|
|
31
31
|
unless respond_to?(:model_formatting_attributes)
|
32
32
|
# use all these attributes instead of a single ModelFormatting::Config because
|
33
33
|
# it's easier to support subclassing.
|
34
|
-
|
34
|
+
class_attribute :model_formatting_attributes,
|
35
35
|
:model_formatting_white_list, :model_formatting_context,
|
36
36
|
:model_formatting_before_callback, :model_formatting_after_callback
|
37
37
|
send :include, ModelFormatting::InstanceMethods
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'redcarpet'
|
2
|
+
module Redcarpet::Render
|
3
|
+
class Custom < HTML
|
4
|
+
# include Redcarpet::Render::HTML
|
5
|
+
def header(title, level)
|
6
|
+
@headers ||= []
|
7
|
+
# you can use this permalink style: 1-foo-bar with the level in it
|
8
|
+
# permalink = "#{level}-#{title.gsub(/\W+/, "-")}"
|
9
|
+
|
10
|
+
# .. or just use title. you might like a better regex here, too
|
11
|
+
permalink = title.gsub(/\W+/, "-").downcase
|
12
|
+
|
13
|
+
# for extra credit: implement this as its own method
|
14
|
+
if @headers.include?(permalink)
|
15
|
+
permalink += "_1"
|
16
|
+
# my brain hurts
|
17
|
+
loop do
|
18
|
+
break if !@headers.include?(permalink)
|
19
|
+
# generate titles like foo-bar_1, foo-bar_2
|
20
|
+
permalink.gsub!(/\_(\d+)$/, "_#{$1.to_i + 1}")
|
21
|
+
end
|
22
|
+
end
|
23
|
+
@headers << permalink
|
24
|
+
%(\n<h#{level} id=\"#{permalink}\"><a name="#{permalink}" class="anchor" href="##{permalink}"></a>#{title}</h#{level}>\n)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/test/formatting_test.rb
CHANGED
@@ -5,7 +5,7 @@ module ModelFormatting
|
|
5
5
|
class Test < Test::Unit::TestCase
|
6
6
|
class Base < Struct.new(:body, :formatted_body, :title, :title_html, :bio, :full_bio)
|
7
7
|
ModelFormatting::Init.setup_on self
|
8
|
-
|
8
|
+
class_attribute :before_save_callback
|
9
9
|
|
10
10
|
def self.before_save(method = nil)
|
11
11
|
if method
|
@@ -7,6 +7,11 @@ class ModelFormattingTest < Test::Unit::TestCase
|
|
7
7
|
parts.first.class.should == ModelFormatting::FormattedPart
|
8
8
|
parts.first.should == 'foo'
|
9
9
|
end
|
10
|
+
|
11
|
+
it "generates TOC titles" do
|
12
|
+
ModelFormatting.process(:html, "# Title One\n\n# Title Two\n\n## Title One").should == \
|
13
|
+
"<div><h1 id=\"title-one\"><a name=\"title-one\" class=\"anchor\" href=\n\"#title-one\" id=\"title-one\"></a>Title One</h1>\n<h1 id=\"title-two\"><a name=\"title-two\" class=\"anchor\" href=\n\"#title-two\" id=\"title-two\"></a>Title Two</h1>\n<h2 id=\"title-one_1\"><a name=\"title-one_1\" class=\"anchor\" href=\n\"#title-one_1\" id=\"title-one_1\"></a>Title One</h2></div>"
|
14
|
+
end
|
10
15
|
|
11
16
|
it "parses empty code block into array of single CodeBlock" do
|
12
17
|
parts = ModelFormatting.parse_text_parts(nil, nil, "@@@ foo\n@@@")
|
@@ -105,8 +110,8 @@ class ModelFormattingTest < Test::Unit::TestCase
|
|
105
110
|
assert_equal "<pre>\nfoo_bar_baz\n</pre>", ModelFormatting.gfm("<pre>\nfoo_bar_baz\n</pre>")
|
106
111
|
end
|
107
112
|
|
108
|
-
it "
|
109
|
-
assert_equal "
|
113
|
+
it "does not touch underscores inside words" do
|
114
|
+
assert_equal "foo_bar_baz", ModelFormatting.gfm("foo_bar_baz")
|
110
115
|
end
|
111
116
|
|
112
117
|
it "turns newlines into br tags in simple cases" do
|
metadata
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: model_formatting
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 11
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
|
8
|
+
- 6
|
9
|
+
- 6
|
10
|
+
version: 0.6.6
|
10
11
|
platform: ruby
|
11
12
|
authors:
|
12
13
|
- ENTP
|
@@ -14,12 +15,10 @@ autorequire:
|
|
14
15
|
bindir: bin
|
15
16
|
cert_chain: []
|
16
17
|
|
17
|
-
date: 2013-
|
18
|
+
date: 2013-10-31 00:00:00 Z
|
18
19
|
dependencies:
|
19
20
|
- !ruby/object:Gem::Dependency
|
20
|
-
|
21
|
-
prerelease: false
|
22
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
21
|
+
version_requirements: &id001 !ruby/object:Gem::Requirement
|
23
22
|
none: false
|
24
23
|
requirements:
|
25
24
|
- - ~>
|
@@ -29,60 +28,57 @@ dependencies:
|
|
29
28
|
- 2
|
30
29
|
- 2
|
31
30
|
version: "2.2"
|
31
|
+
requirement: *id001
|
32
|
+
prerelease: false
|
33
|
+
name: redcarpet
|
32
34
|
type: :runtime
|
33
|
-
version_requirements: *id001
|
34
35
|
- !ruby/object:Gem::Dependency
|
35
|
-
|
36
|
-
prerelease: false
|
37
|
-
requirement: &id002 !ruby/object:Gem::Requirement
|
36
|
+
version_requirements: &id002 !ruby/object:Gem::Requirement
|
38
37
|
none: false
|
39
38
|
requirements:
|
40
39
|
- - ~>
|
41
40
|
- !ruby/object:Gem::Version
|
42
|
-
hash:
|
41
|
+
hash: 3
|
43
42
|
segments:
|
44
43
|
- 3
|
45
|
-
-
|
46
|
-
|
47
|
-
|
44
|
+
- 2
|
45
|
+
version: "3.2"
|
46
|
+
requirement: *id002
|
47
|
+
prerelease: false
|
48
|
+
name: actionpack
|
48
49
|
type: :runtime
|
49
|
-
version_requirements: *id002
|
50
50
|
- !ruby/object:Gem::Dependency
|
51
|
-
|
52
|
-
prerelease: false
|
53
|
-
requirement: &id003 !ruby/object:Gem::Requirement
|
51
|
+
version_requirements: &id003 !ruby/object:Gem::Requirement
|
54
52
|
none: false
|
55
53
|
requirements:
|
56
54
|
- - ~>
|
57
55
|
- !ruby/object:Gem::Version
|
58
|
-
hash:
|
56
|
+
hash: 3
|
59
57
|
segments:
|
60
58
|
- 3
|
61
|
-
-
|
62
|
-
|
63
|
-
|
59
|
+
- 2
|
60
|
+
version: "3.2"
|
61
|
+
requirement: *id003
|
62
|
+
prerelease: false
|
63
|
+
name: activerecord
|
64
64
|
type: :runtime
|
65
|
-
version_requirements: *id003
|
66
65
|
- !ruby/object:Gem::Dependency
|
67
|
-
|
68
|
-
prerelease: false
|
69
|
-
requirement: &id004 !ruby/object:Gem::Requirement
|
66
|
+
version_requirements: &id004 !ruby/object:Gem::Requirement
|
70
67
|
none: false
|
71
68
|
requirements:
|
72
69
|
- - ~>
|
73
70
|
- !ruby/object:Gem::Version
|
74
|
-
hash:
|
71
|
+
hash: 3
|
75
72
|
segments:
|
76
73
|
- 3
|
77
|
-
-
|
78
|
-
|
79
|
-
|
74
|
+
- 2
|
75
|
+
version: "3.2"
|
76
|
+
requirement: *id004
|
77
|
+
prerelease: false
|
78
|
+
name: activesupport
|
80
79
|
type: :runtime
|
81
|
-
version_requirements: *id004
|
82
80
|
- !ruby/object:Gem::Dependency
|
83
|
-
|
84
|
-
prerelease: false
|
85
|
-
requirement: &id005 !ruby/object:Gem::Requirement
|
81
|
+
version_requirements: &id005 !ruby/object:Gem::Requirement
|
86
82
|
none: false
|
87
83
|
requirements:
|
88
84
|
- - ~>
|
@@ -93,12 +89,12 @@ dependencies:
|
|
93
89
|
- 1
|
94
90
|
- 2
|
95
91
|
version: 0.1.2
|
92
|
+
requirement: *id005
|
93
|
+
prerelease: false
|
94
|
+
name: tidy_ffi
|
96
95
|
type: :runtime
|
97
|
-
version_requirements: *id005
|
98
96
|
- !ruby/object:Gem::Dependency
|
99
|
-
|
100
|
-
prerelease: false
|
101
|
-
requirement: &id006 !ruby/object:Gem::Requirement
|
97
|
+
version_requirements: &id006 !ruby/object:Gem::Requirement
|
102
98
|
none: false
|
103
99
|
requirements:
|
104
100
|
- - ~>
|
@@ -109,12 +105,12 @@ dependencies:
|
|
109
105
|
- 5
|
110
106
|
- 5
|
111
107
|
version: 0.5.5
|
108
|
+
requirement: *id006
|
109
|
+
prerelease: false
|
110
|
+
name: jeremymcanally-context
|
112
111
|
type: :development
|
113
|
-
version_requirements: *id006
|
114
112
|
- !ruby/object:Gem::Dependency
|
115
|
-
|
116
|
-
prerelease: false
|
117
|
-
requirement: &id007 !ruby/object:Gem::Requirement
|
113
|
+
version_requirements: &id007 !ruby/object:Gem::Requirement
|
118
114
|
none: false
|
119
115
|
requirements:
|
120
116
|
- - ~>
|
@@ -125,9 +121,11 @@ dependencies:
|
|
125
121
|
- 1
|
126
122
|
- 0
|
127
123
|
version: 0.1.0
|
124
|
+
requirement: *id007
|
125
|
+
prerelease: false
|
126
|
+
name: jeremymcanally-matchy
|
128
127
|
type: :development
|
129
|
-
|
130
|
-
description:
|
128
|
+
description: Automatically format model attributes using Redcarpet (markdown) and Tender/Lighthouse extensions.
|
131
129
|
email: company@entp.com
|
132
130
|
executables: []
|
133
131
|
|
@@ -138,13 +136,14 @@ extra_rdoc_files:
|
|
138
136
|
files:
|
139
137
|
- Gemfile
|
140
138
|
- Gemfile.lock
|
141
|
-
- README
|
142
139
|
- Rakefile
|
140
|
+
- README
|
143
141
|
- bin/model_formatting
|
144
|
-
- lib/model_formatting.rb
|
145
142
|
- lib/model_formatting/config.rb
|
146
143
|
- lib/model_formatting/init.rb
|
147
144
|
- lib/model_formatting/instance_methods.rb
|
145
|
+
- lib/model_formatting.rb
|
146
|
+
- lib/redcarpet_custom.rb
|
148
147
|
- rails/init.rb
|
149
148
|
- test/fixtures/mkd-extraction.txt
|
150
149
|
- test/formatting_test.rb
|
@@ -182,6 +181,6 @@ rubyforge_project:
|
|
182
181
|
rubygems_version: 1.8.24
|
183
182
|
signing_key:
|
184
183
|
specification_version: 3
|
185
|
-
summary: Automatically format model attributes using
|
184
|
+
summary: Automatically format model attributes using Redcarpet (markdown) and Tender/Lighthouse extensions.
|
186
185
|
test_files: []
|
187
186
|
|