rad_kit 0.0.6 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/components/captcha.rb +24 -0
- data/lib/components/captcha.yml +2 -0
- data/lib/kit/{http_controller → controller}/authorized.rb +1 -1
- data/lib/kit/controller/captcha.rb +23 -0
- data/lib/kit/{http_controller → controller}/localized.rb +1 -1
- data/lib/kit/controller.rb +5 -0
- data/lib/kit/gems.rb +10 -6
- data/lib/kit/i18n/locales/ru/pluralization.rb +62 -0
- data/lib/kit/i18n.rb +16 -0
- data/lib/kit/kit.rb +10 -7
- data/lib/kit/kit_text_utils.rb +30 -0
- data/lib/kit/misc/prepare_model.rb +16 -0
- data/lib/kit/misc/user_error.rb +12 -0
- data/lib/kit/models/attachments_uploader_helper.rb +1 -1
- data/lib/kit/models/authorized.rb +6 -8
- data/lib/kit/models/authorized_object.rb +3 -4
- data/lib/kit/models/{micelaneous.rb → miscellaneous.rb} +0 -0
- data/lib/kit/models_after.rb +1 -1
- data/lib/kit/mongoid/{rad_micelaneous.rb → rad_miscellaneous.rb} +1 -1
- data/lib/kit/mongoid/text_processor.rb +1 -1
- data/lib/kit/mongoid.rb +2 -2
- data/lib/kit/spec.rb +1 -2
- data/lib/kit/tasks.rb +1 -1
- data/lib/text_utils/code_highlighter.rb +75 -0
- data/lib/text_utils/custom_markdown.rb +64 -0
- data/lib/text_utils/ensure_utf.rb +14 -0
- data/lib/text_utils/format_qualifier.rb +13 -0
- data/lib/{kit/text_utils → text_utils}/html_sanitizer.rb +8 -6
- data/lib/text_utils/markdown.rb +33 -0
- data/lib/text_utils/pipe.rb +16 -0
- data/lib/text_utils/processor.rb +14 -0
- data/lib/text_utils/support.rb +15 -0
- data/lib/text_utils/truncate.rb +30 -0
- data/lib/text_utils.rb +23 -0
- data/readme.md +1 -8
- data/spec/controller/authorization_spec.rb +1 -1
- data/spec/controller/captcha_spec.rb +66 -0
- data/spec/i18n/i18n_spec/locales/en/general.yml +5 -0
- data/spec/i18n/i18n_spec/locales/ru/general.yml +7 -0
- data/spec/i18n/i18n_spec.rb +32 -0
- data/spec/misc/kit_text_utils_spec.rb +29 -0
- data/spec/misc/prepare_model_spec.rb +37 -0
- data/spec/misc/user_error_spec.rb +38 -0
- data/spec/models/authorized_object_spec.rb +10 -3
- data/spec/spec_helper/factories.rb +4 -0
- data/spec/spec_helper/user.rb +2 -3
- data/spec/spec_helper.rb +0 -5
- data/spec/text_utils/code_highlighter_spec.rb +38 -0
- data/spec/text_utils/custom_markdown_spec.rb +82 -0
- data/spec/text_utils/format_qualifier_spec.rb +37 -0
- data/spec/text_utils/html_sanitizer_spec.rb +88 -0
- data/spec/text_utils/markdown_spec.rb +114 -0
- data/spec/text_utils/pipe_spec.rb +35 -0
- data/spec/text_utils/spec_helper.rb +26 -0
- data/spec/text_utils/text_processor_shared.rb +9 -0
- data/spec/text_utils/truncate_spec.rb +22 -0
- metadata +57 -47
- data/lib/kit/http_controller.rb +0 -4
- data/lib/kit/text_utils/code_highlighter.rb +0 -58
- data/lib/kit/text_utils/custom_markdown.rb +0 -90
- data/lib/kit/text_utils/ensure_utf.rb +0 -8
- data/lib/kit/text_utils/github_flavoured_markdown.rb +0 -32
- data/lib/kit/text_utils/image_box.rb +0 -35
- data/lib/kit/text_utils/markup.rb +0 -43
- data/lib/kit/text_utils/processor.rb +0 -25
- data/lib/kit/text_utils/tag_shortcuts.rb +0 -14
- data/lib/kit/text_utils/truncate.rb +0 -29
- data/lib/kit/text_utils/truncator.rb +0 -15
- data/lib/kit/text_utils/urls.rb +0 -13
- data/lib/kit/text_utils.rb +0 -43
- data/spec/utils/text_utils_spec.rb +0 -280
@@ -0,0 +1,114 @@
|
|
1
|
+
require 'text_utils/spec_helper'
|
2
|
+
|
3
|
+
describe "Markdown" do
|
4
|
+
include RSpec::TextUtilsHelper
|
5
|
+
|
6
|
+
before do
|
7
|
+
@processor = TextUtils::Markdown.new
|
8
|
+
@options = {format: :markdown}
|
9
|
+
end
|
10
|
+
|
11
|
+
it_should_behave_like 'text processor'
|
12
|
+
|
13
|
+
it "should not touch single underscores inside words" do
|
14
|
+
process("foo_bar").should include("foo_bar")
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should correctly guess links (from error)" do
|
18
|
+
to_doc("http://some_domain.com http://some_domain.com").css('a').size == 2
|
19
|
+
end
|
20
|
+
|
21
|
+
it "basic test" do
|
22
|
+
text = <<MARKDOWN
|
23
|
+
# Title
|
24
|
+
|
25
|
+
**text**
|
26
|
+
MARKDOWN
|
27
|
+
|
28
|
+
doc = to_doc text
|
29
|
+
doc.css('h1').first.content.should == 'Title'
|
30
|
+
doc.css("p b, p strong").first.content.should == 'text'
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should guess urls" do
|
34
|
+
doc = to_doc "This is a http://www.some.com/some link"
|
35
|
+
doc.content.strip.should == "This is a http://www.some.com/some link"
|
36
|
+
doc.css("p a").first.to_fuzzy_hash.should == {href: "http://www.some.com/some"}
|
37
|
+
|
38
|
+
# from error
|
39
|
+
doc = to_doc "http://www.some.com/some"
|
40
|
+
doc.content.strip.should == "http://www.some.com/some"
|
41
|
+
doc.css("p a").first.to_fuzzy_hash.should == {href: "http://www.some.com/some"}
|
42
|
+
end
|
43
|
+
|
44
|
+
it "code blocks" do
|
45
|
+
text = <<MARKDOWN
|
46
|
+
``` ruby
|
47
|
+
puts "Hello World"
|
48
|
+
```
|
49
|
+
MARKDOWN
|
50
|
+
|
51
|
+
to_doc(text).css('code').first.to_fuzzy_hash.should == {class: 'ruby'}
|
52
|
+
end
|
53
|
+
|
54
|
+
it "definitions" do
|
55
|
+
text = <<MARKDOWN
|
56
|
+
Ruby IoC [Micon][micon]
|
57
|
+
|
58
|
+
[micon]: https://github.com/alexeypetrushin/micon
|
59
|
+
MARKDOWN
|
60
|
+
|
61
|
+
to_doc(text).css('a').first.to_fuzzy_hash.should == {href: 'https://github.com/alexeypetrushin/micon'}
|
62
|
+
end
|
63
|
+
|
64
|
+
describe "new lines" do
|
65
|
+
it "should correctly insert newline (from error)" do
|
66
|
+
html = <<HTML
|
67
|
+
![img] Open Design.
|
68
|
+
http://oomps.com
|
69
|
+
|
70
|
+
[img]: /some_link
|
71
|
+
HTML
|
72
|
+
|
73
|
+
to_doc(html).css('br').size.should == 1
|
74
|
+
end
|
75
|
+
|
76
|
+
it "shouldn't add new lines after image (from error)" do
|
77
|
+
html = <<HTML
|
78
|
+
a ![img] b
|
79
|
+
|
80
|
+
[img]: /some_link
|
81
|
+
HTML
|
82
|
+
|
83
|
+
to_doc(html).css('br').size.should == 0
|
84
|
+
end
|
85
|
+
|
86
|
+
it "d" do
|
87
|
+
html = <<HTML
|
88
|
+
a
|
89
|
+
![img]
|
90
|
+
b
|
91
|
+
|
92
|
+
[img]: /some_link
|
93
|
+
HTML
|
94
|
+
|
95
|
+
to_doc(html).css('br').size.should == 2
|
96
|
+
end
|
97
|
+
|
98
|
+
it "should convert \n to <br/>" do
|
99
|
+
to_doc("foo\nbar").css('br').size.should == 1
|
100
|
+
end
|
101
|
+
|
102
|
+
it "shouldn't create newline after > sign (from error)" do
|
103
|
+
html = <<HTML
|
104
|
+
<div>text</div> text
|
105
|
+
HTML
|
106
|
+
|
107
|
+
to_doc(html).css('br').size.should == 0
|
108
|
+
end
|
109
|
+
|
110
|
+
it "shouldn't add empty <p> before first line (from error)" do
|
111
|
+
process("<span>text</span>text").should_not =~ /<p>\s*<\/p>/
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'text_utils/spec_helper'
|
2
|
+
|
3
|
+
describe "FormatQualifier" do
|
4
|
+
before :all do
|
5
|
+
class TextProcA < TextUtils::Processor
|
6
|
+
def call data, env
|
7
|
+
call_next "#{data} a", env
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
class TextProcB < TextUtils::Processor
|
12
|
+
def initialize processor, text
|
13
|
+
super(processor)
|
14
|
+
@text = text
|
15
|
+
end
|
16
|
+
|
17
|
+
def call data, env
|
18
|
+
call_next "#{data} #{@text}", env
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
after :all do
|
23
|
+
remove_constants :TextProcA, :TextProcB
|
24
|
+
end
|
25
|
+
|
26
|
+
before do
|
27
|
+
@pipe = TextUtils::Pipe.new \
|
28
|
+
TextProcA,
|
29
|
+
[TextProcB, 'b']
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should qualify :html format" do
|
33
|
+
@pipe.call('text').should == "text a b"
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'rspec_ext'
|
2
|
+
require 'rspec_ext/xhtml'
|
3
|
+
|
4
|
+
require 'text_utils'
|
5
|
+
require 'ruby_ext'
|
6
|
+
|
7
|
+
|
8
|
+
require 'text_utils/text_processor_shared'
|
9
|
+
|
10
|
+
|
11
|
+
class String
|
12
|
+
def to_doc
|
13
|
+
Nokogiri::HTML(self)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
module RSpec::TextUtilsHelper
|
19
|
+
def process data
|
20
|
+
@processor.call data, @options
|
21
|
+
end
|
22
|
+
|
23
|
+
def to_doc data
|
24
|
+
process(data).to_doc
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'text_utils/spec_helper'
|
2
|
+
|
3
|
+
describe "Truncate" do
|
4
|
+
before do
|
5
|
+
@processor = TextUtils::Truncate.new nil, 20
|
6
|
+
end
|
7
|
+
|
8
|
+
it_should_behave_like 'text processor'
|
9
|
+
|
10
|
+
it "should truncate text" do
|
11
|
+
text = %{Hi there, I have a page that will list news articles}
|
12
|
+
@processor.call(text, {format: :text}).should == "Hi there, I have a ..."
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should truncate html" do
|
16
|
+
text = %{Hi <div><b>there</b>, I have a page that will list news articles</div>}
|
17
|
+
@processor.call(text, {format: :html}).should == "Hi there, I have a ..."
|
18
|
+
|
19
|
+
text = %{a<br/>b}
|
20
|
+
@processor.call(text, {format: :html}).should == "a b" # from error
|
21
|
+
end
|
22
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rad_kit
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,72 +10,72 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-07-
|
13
|
+
date: 2011-07-30 00:00:00 +04:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
|
-
name:
|
17
|
+
name: i18n
|
18
18
|
requirement: &id001 !ruby/object:Gem::Requirement
|
19
19
|
none: false
|
20
20
|
requirements:
|
21
21
|
- - "="
|
22
22
|
- !ruby/object:Gem::Version
|
23
|
-
version:
|
23
|
+
version: 0.5.0
|
24
24
|
type: :runtime
|
25
25
|
prerelease: false
|
26
26
|
version_requirements: *id001
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
28
|
+
name: redcarpet
|
29
29
|
requirement: &id002 !ruby/object:Gem::Requirement
|
30
30
|
none: false
|
31
31
|
requirements:
|
32
32
|
- - "="
|
33
33
|
- !ruby/object:Gem::Version
|
34
|
-
version: 1.2
|
34
|
+
version: 1.17.2
|
35
35
|
type: :runtime
|
36
36
|
prerelease: false
|
37
37
|
version_requirements: *id002
|
38
38
|
- !ruby/object:Gem::Dependency
|
39
|
-
name:
|
39
|
+
name: sanitize
|
40
40
|
requirement: &id003 !ruby/object:Gem::Requirement
|
41
41
|
none: false
|
42
42
|
requirements:
|
43
43
|
- - "="
|
44
44
|
- !ruby/object:Gem::Version
|
45
|
-
version: 1.2.
|
45
|
+
version: 1.2.1
|
46
46
|
type: :runtime
|
47
47
|
prerelease: false
|
48
48
|
version_requirements: *id003
|
49
49
|
- !ruby/object:Gem::Dependency
|
50
|
-
name:
|
50
|
+
name: stringex
|
51
51
|
requirement: &id004 !ruby/object:Gem::Requirement
|
52
52
|
none: false
|
53
53
|
requirements:
|
54
54
|
- - "="
|
55
55
|
- !ruby/object:Gem::Version
|
56
|
-
version:
|
56
|
+
version: 1.2.0
|
57
57
|
type: :runtime
|
58
58
|
prerelease: false
|
59
59
|
version_requirements: *id004
|
60
60
|
- !ruby/object:Gem::Dependency
|
61
|
-
name:
|
61
|
+
name: state_machine
|
62
62
|
requirement: &id005 !ruby/object:Gem::Requirement
|
63
63
|
none: false
|
64
64
|
requirements:
|
65
65
|
- - "="
|
66
66
|
- !ruby/object:Gem::Version
|
67
|
-
version:
|
67
|
+
version: 0.10.4
|
68
68
|
type: :runtime
|
69
69
|
prerelease: false
|
70
70
|
version_requirements: *id005
|
71
71
|
- !ruby/object:Gem::Dependency
|
72
|
-
name:
|
72
|
+
name: recaptcha
|
73
73
|
requirement: &id006 !ruby/object:Gem::Requirement
|
74
74
|
none: false
|
75
75
|
requirements:
|
76
76
|
- - "="
|
77
77
|
- !ruby/object:Gem::Version
|
78
|
-
version: 0.
|
78
|
+
version: 0.3.1
|
79
79
|
type: :runtime
|
80
80
|
prerelease: false
|
81
81
|
version_requirements: *id006
|
@@ -91,7 +91,7 @@ dependencies:
|
|
91
91
|
prerelease: false
|
92
92
|
version_requirements: *id007
|
93
93
|
- !ruby/object:Gem::Dependency
|
94
|
-
name:
|
94
|
+
name: rad_common_interface
|
95
95
|
requirement: &id008 !ruby/object:Gem::Requirement
|
96
96
|
none: false
|
97
97
|
requirements:
|
@@ -102,7 +102,7 @@ dependencies:
|
|
102
102
|
prerelease: false
|
103
103
|
version_requirements: *id008
|
104
104
|
- !ruby/object:Gem::Dependency
|
105
|
-
name:
|
105
|
+
name: rad_assets
|
106
106
|
requirement: &id009 !ruby/object:Gem::Requirement
|
107
107
|
none: false
|
108
108
|
requirements:
|
@@ -113,7 +113,7 @@ dependencies:
|
|
113
113
|
prerelease: false
|
114
114
|
version_requirements: *id009
|
115
115
|
- !ruby/object:Gem::Dependency
|
116
|
-
name:
|
116
|
+
name: mongoid_misc
|
117
117
|
requirement: &id010 !ruby/object:Gem::Requirement
|
118
118
|
none: false
|
119
119
|
requirements:
|
@@ -123,17 +123,6 @@ dependencies:
|
|
123
123
|
type: :runtime
|
124
124
|
prerelease: false
|
125
125
|
version_requirements: *id010
|
126
|
-
- !ruby/object:Gem::Dependency
|
127
|
-
name: mongoid_misc
|
128
|
-
requirement: &id011 !ruby/object:Gem::Requirement
|
129
|
-
none: false
|
130
|
-
requirements:
|
131
|
-
- - ">="
|
132
|
-
- !ruby/object:Gem::Version
|
133
|
-
version: "0"
|
134
|
-
type: :runtime
|
135
|
-
prerelease: false
|
136
|
-
version_requirements: *id011
|
137
126
|
description:
|
138
127
|
email:
|
139
128
|
executables: []
|
@@ -145,26 +134,34 @@ extra_rdoc_files: []
|
|
145
134
|
files:
|
146
135
|
- Rakefile
|
147
136
|
- readme.md
|
137
|
+
- lib/components/captcha.rb
|
138
|
+
- lib/components/captcha.yml
|
148
139
|
- lib/components/kit.rb
|
149
140
|
- lib/components/kit.yml
|
150
141
|
- lib/components/models.rb
|
142
|
+
- lib/kit/controller/authorized.rb
|
143
|
+
- lib/kit/controller/captcha.rb
|
144
|
+
- lib/kit/controller/localized.rb
|
145
|
+
- lib/kit/controller.rb
|
151
146
|
- lib/kit/factories.rb
|
152
147
|
- lib/kit/gems.rb
|
153
|
-
- lib/kit/
|
154
|
-
- lib/kit/
|
155
|
-
- lib/kit/http_controller.rb
|
148
|
+
- lib/kit/i18n/locales/ru/pluralization.rb
|
149
|
+
- lib/kit/i18n.rb
|
156
150
|
- lib/kit/kit.rb
|
151
|
+
- lib/kit/kit_text_utils.rb
|
152
|
+
- lib/kit/misc/prepare_model.rb
|
153
|
+
- lib/kit/misc/user_error.rb
|
157
154
|
- lib/kit/models/attachment_uploader.rb
|
158
155
|
- lib/kit/models/attachments_uploader_helper.rb
|
159
156
|
- lib/kit/models/authorized.rb
|
160
157
|
- lib/kit/models/authorized_object.rb
|
161
158
|
- lib/kit/models/default_permissions.yml
|
162
159
|
- lib/kit/models/file_uploader.rb
|
163
|
-
- lib/kit/models/
|
160
|
+
- lib/kit/models/miscellaneous.rb
|
164
161
|
- lib/kit/models/role.rb
|
165
162
|
- lib/kit/models.rb
|
166
163
|
- lib/kit/models_after.rb
|
167
|
-
- lib/kit/mongoid/
|
164
|
+
- lib/kit/mongoid/rad_miscellaneous.rb
|
168
165
|
- lib/kit/mongoid/text_processor.rb
|
169
166
|
- lib/kit/mongoid.rb
|
170
167
|
- lib/kit/spec/items_controller_crud.rb
|
@@ -172,22 +169,27 @@ files:
|
|
172
169
|
- lib/kit/support/string.rb
|
173
170
|
- lib/kit/support.rb
|
174
171
|
- lib/kit/tasks.rb
|
175
|
-
- lib/
|
176
|
-
- lib/
|
177
|
-
- lib/
|
178
|
-
- lib/
|
179
|
-
- lib/
|
180
|
-
- lib/
|
181
|
-
- lib/
|
182
|
-
- lib/
|
183
|
-
- lib/
|
184
|
-
- lib/
|
185
|
-
- lib/
|
186
|
-
- lib/kit/text_utils/urls.rb
|
187
|
-
- lib/kit/text_utils.rb
|
172
|
+
- lib/text_utils/code_highlighter.rb
|
173
|
+
- lib/text_utils/custom_markdown.rb
|
174
|
+
- lib/text_utils/ensure_utf.rb
|
175
|
+
- lib/text_utils/format_qualifier.rb
|
176
|
+
- lib/text_utils/html_sanitizer.rb
|
177
|
+
- lib/text_utils/markdown.rb
|
178
|
+
- lib/text_utils/pipe.rb
|
179
|
+
- lib/text_utils/processor.rb
|
180
|
+
- lib/text_utils/support.rb
|
181
|
+
- lib/text_utils/truncate.rb
|
182
|
+
- lib/text_utils.rb
|
188
183
|
- spec/controller/authorization_spec.rb
|
184
|
+
- spec/controller/captcha_spec.rb
|
189
185
|
- spec/controller/comments_spec.rb
|
190
186
|
- spec/controller/items_spec.rb
|
187
|
+
- spec/i18n/i18n_spec/locales/en/general.yml
|
188
|
+
- spec/i18n/i18n_spec/locales/ru/general.yml
|
189
|
+
- spec/i18n/i18n_spec.rb
|
190
|
+
- spec/misc/kit_text_utils_spec.rb
|
191
|
+
- spec/misc/prepare_model_spec.rb
|
192
|
+
- spec/misc/user_error_spec.rb
|
191
193
|
- spec/models/attachments_spec/a.txt
|
192
194
|
- spec/models/attachments_spec.rb
|
193
195
|
- spec/models/attachments_uploader_helper_spec/v1/a.txt
|
@@ -207,7 +209,15 @@ files:
|
|
207
209
|
- spec/spec_helper/factories.rb
|
208
210
|
- spec/spec_helper/user.rb
|
209
211
|
- spec/spec_helper.rb
|
210
|
-
- spec/
|
212
|
+
- spec/text_utils/code_highlighter_spec.rb
|
213
|
+
- spec/text_utils/custom_markdown_spec.rb
|
214
|
+
- spec/text_utils/format_qualifier_spec.rb
|
215
|
+
- spec/text_utils/html_sanitizer_spec.rb
|
216
|
+
- spec/text_utils/markdown_spec.rb
|
217
|
+
- spec/text_utils/pipe_spec.rb
|
218
|
+
- spec/text_utils/spec_helper.rb
|
219
|
+
- spec/text_utils/text_processor_shared.rb
|
220
|
+
- spec/text_utils/truncate_spec.rb
|
211
221
|
has_rdoc: true
|
212
222
|
homepage: http://github.com/alexeypetrushin/rad_kit
|
213
223
|
licenses: []
|
data/lib/kit/http_controller.rb
DELETED
@@ -1,58 +0,0 @@
|
|
1
|
-
class Rad::TextUtils::CodeHighlighter < Rad::TextUtils::Processor
|
2
|
-
|
3
|
-
# highlights code inside of <code lang/language='java'> ... code ... </code>
|
4
|
-
def process html, env
|
5
|
-
require 'coderay'
|
6
|
-
require 'nokogiri'
|
7
|
-
|
8
|
-
snippets, unique_id = {}, 0
|
9
|
-
|
10
|
-
# Highlighting
|
11
|
-
html = html.gsub /(<code.*?>)(.+?)(<\/code\s*>)/im do
|
12
|
-
begin
|
13
|
-
node = Nokogiri::HTML($1 + $3).css('code').first
|
14
|
-
language = node.attributes['lang'].try(:value) || node.attributes['language'].try(:value)
|
15
|
-
code = $2
|
16
|
-
|
17
|
-
if language.present? and code.present?
|
18
|
-
attributes = []
|
19
|
-
node.attributes.each do |name, value|
|
20
|
-
attributes << %(#{name}="#{value.value}")
|
21
|
-
end
|
22
|
-
|
23
|
-
code = CodeRay.scan(code, language.downcase.to_sym).div :css => :class
|
24
|
-
|
25
|
-
snippet = "<code #{attributes.join(' ')}>\n#{code}\n</code>"
|
26
|
-
|
27
|
-
# adding prefix 'hl_' to all CodeRay classes
|
28
|
-
node = Nokogiri::HTML(snippet).css('code').first
|
29
|
-
node.css("*").each do |e|
|
30
|
-
classes = e.attribute 'class'
|
31
|
-
if classes.present? and classes.value.present?
|
32
|
-
classes = classes.value.strip.split(/\s+/).collect{|c| "hl_#{c}"}.join(' ')
|
33
|
-
e['class'] = classes
|
34
|
-
end
|
35
|
-
end
|
36
|
-
snippet = node.to_s
|
37
|
-
end
|
38
|
-
# rescue StandardError => e
|
39
|
-
# rad.logger.warn "CodeHighlighter: #{e.message}"
|
40
|
-
end
|
41
|
-
|
42
|
-
# temporarilly removing all highlighted code from html to prevent it's beed damaged by next processors
|
43
|
-
unique_id += 1
|
44
|
-
id = "CODE#{unique_id}CODE"
|
45
|
-
snippets[id] = snippet
|
46
|
-
id
|
47
|
-
end
|
48
|
-
|
49
|
-
html = call_next html, env
|
50
|
-
|
51
|
-
# inserting highlighted code back to html
|
52
|
-
html = html.gsub /(CODE[0-9]+CODE)/ do |id|
|
53
|
-
snippets[id]
|
54
|
-
end
|
55
|
-
|
56
|
-
html
|
57
|
-
end
|
58
|
-
end
|
@@ -1,90 +0,0 @@
|
|
1
|
-
class Rad::TextUtils::CustomMarkdown < Rad::TextUtils::Processor
|
2
|
-
def process markdown, env
|
3
|
-
markdown = github_flavoured_markdown markdown
|
4
|
-
markdown = image_box markdown
|
5
|
-
text = custom_markdown markdown
|
6
|
-
|
7
|
-
call_next text, env
|
8
|
-
end
|
9
|
-
|
10
|
-
protected
|
11
|
-
def custom_markdown markdown
|
12
|
-
# BlueCloth doesn't apply inside of HTML tags, so we temporarilly replacing it
|
13
|
-
markdown = markdown.gsub('<', 'HTML_BEGIN').gsub('>', 'HTML_END')
|
14
|
-
|
15
|
-
markdown = markdown.gsub(" \n", "<br/>\n")
|
16
|
-
|
17
|
-
text = BlueCloth.new(markdown).to_html
|
18
|
-
|
19
|
-
text = text.gsub(/\A<.+?>/){"#{$&} "}.gsub(/<\/.+?>\Z/){" #{$&}"}
|
20
|
-
|
21
|
-
text = text.gsub('HTML_BEGIN', '<').gsub('HTML_END', '>')
|
22
|
-
|
23
|
-
text.gsub(/[\n]+/, "\n")
|
24
|
-
end
|
25
|
-
|
26
|
-
def github_flavoured_markdown markdown
|
27
|
-
# Extract pre blocks
|
28
|
-
extractions = {}
|
29
|
-
markdown.gsub!(%r{<pre>.*?</pre>}m) do |match|
|
30
|
-
md5 = Digest::MD5.hexdigest(match)
|
31
|
-
extractions[md5] = match
|
32
|
-
"{gfm-extraction-#{md5}}"
|
33
|
-
end
|
34
|
-
|
35
|
-
# prevent foo_bar_baz from ending up with an italic word in the middle
|
36
|
-
markdown.gsub!(/(^(?! {4}|\t)\w+_\w+_\w[\w_]*)/) do |x|
|
37
|
-
x.gsub('_', '\_') if x.split('').sort.to_s[0..1] == '__'
|
38
|
-
end
|
39
|
-
|
40
|
-
# in very clear cases, let newlines become <br /> tags
|
41
|
-
markdown.gsub!(/^[\w\<\!][^\n]*\n+/) do |x|
|
42
|
-
if x =~ /\>[\n\r]*/
|
43
|
-
x
|
44
|
-
else
|
45
|
-
x =~ /\n{2}/ ? x : (x.strip!; x << " \n")
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
# Insert pre block extractions
|
50
|
-
markdown.gsub!(/\{gfm-extraction-([0-9a-f]{32})\}/) do
|
51
|
-
"\n\n" + extractions[$1]
|
52
|
-
end
|
53
|
-
|
54
|
-
markdown
|
55
|
-
end
|
56
|
-
|
57
|
-
# !![img] => [![img_thumb]][img]
|
58
|
-
def image_box markdown
|
59
|
-
img_urls = {}
|
60
|
-
markdown = markdown.gsub(/!!\[(.+?)\]/) do
|
61
|
-
img_key = $1 || ''
|
62
|
-
if url = markdown.scan(/\[#{img_key}\]:\s*([^\s]+)$/).first.try(:first)
|
63
|
-
unless url =~ /\.[^\.]+\.[^\.]+$/ # image.png
|
64
|
-
thumb_img_key = "#{img_key}_thumb"
|
65
|
-
|
66
|
-
# building url with thumb (foo.png => foo.thumb.png)
|
67
|
-
img_urls[thumb_img_key] = url.sub(/\.([^\.]+)$/){".thumb.#{$1}"}
|
68
|
-
|
69
|
-
"[![][#{thumb_img_key}]][#{img_key}]"
|
70
|
-
else # image.(icon|thumb|...).png
|
71
|
-
img_key_full = "#{img_key}_full"
|
72
|
-
|
73
|
-
# building url with thumb (foo.png => foo.thumb.png)
|
74
|
-
img_urls[img_key_full] = url.sub(/\.([^\.]+)\.([^\.]+)$/){".#{$2}"}
|
75
|
-
|
76
|
-
"[![][#{img_key}]][#{img_key_full}]"
|
77
|
-
end
|
78
|
-
else
|
79
|
-
$& || ''
|
80
|
-
end
|
81
|
-
end
|
82
|
-
|
83
|
-
unless img_urls.blank?
|
84
|
-
markdown << "\n"
|
85
|
-
markdown << img_urls.to_a.collect{|k, v| "[#{k}]: #{v}"}.join("\n")
|
86
|
-
end
|
87
|
-
|
88
|
-
markdown
|
89
|
-
end
|
90
|
-
end
|
@@ -1,8 +0,0 @@
|
|
1
|
-
class Rad::TextUtils::EnsureUtf < Rad::TextUtils::Processor
|
2
|
-
def process data, env
|
3
|
-
data = call_next data, env
|
4
|
-
|
5
|
-
# Escape all non-word unicode symbols, otherwise it will raise error when converting to BSON
|
6
|
-
Iconv.conv('UTF-8//IGNORE//TRANSLIT', 'UTF-8', data)
|
7
|
-
end
|
8
|
-
end
|
@@ -1,32 +0,0 @@
|
|
1
|
-
class Rad::TextUtils::GithubFlavouredMarkdown < Rad::TextUtils::Processor
|
2
|
-
def process markdown, env
|
3
|
-
# Extract pre blocks
|
4
|
-
extractions = {}
|
5
|
-
markdown.gsub!(%r{<pre>.*?</pre>}m) do |match|
|
6
|
-
md5 = Digest::MD5.hexdigest(match)
|
7
|
-
extractions[md5] = match
|
8
|
-
"{gfm-extraction-#{md5}}"
|
9
|
-
end
|
10
|
-
|
11
|
-
# prevent foo_bar_baz from ending up with an italic word in the middle
|
12
|
-
markdown.gsub!(/(^(?! {4}|\t)\w+_\w+_\w[\w_]*)/) do |x|
|
13
|
-
x.gsub('_', '\_') if x.split('').sort.to_s[0..1] == '__'
|
14
|
-
end
|
15
|
-
|
16
|
-
# in very clear cases, let newlines become <br /> tags
|
17
|
-
markdown.gsub!(/^[\w\<\!][^\n]*\n+/) do |x|
|
18
|
-
if x =~ /\>[\n\r]*/
|
19
|
-
x
|
20
|
-
else
|
21
|
-
x =~ /\n{2}/ ? x : (x.strip!; x << " \n")
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
# Insert pre block extractions
|
26
|
-
markdown.gsub!(/\{gfm-extraction-([0-9a-f]{32})\}/) do
|
27
|
-
"\n\n" + extractions[$1]
|
28
|
-
end
|
29
|
-
|
30
|
-
call_next markdown, env
|
31
|
-
end
|
32
|
-
end
|
@@ -1,35 +0,0 @@
|
|
1
|
-
class Rad::TextUtils::ImageBox < Rad::TextUtils::Processor
|
2
|
-
# !![img] => [![img_thumb]][img]
|
3
|
-
def process markdown, env
|
4
|
-
img_urls = {}
|
5
|
-
markdown = markdown.gsub(/!!\[(.+?)\]/) do
|
6
|
-
img_key = $1 || ''
|
7
|
-
if url = markdown.scan(/\[#{img_key}\]:\s*([^\s]+)$/).first.try(:first)
|
8
|
-
unless url =~ /\.[^\.]+\.[^\.]+$/ # image.png
|
9
|
-
thumb_img_key = "#{img_key}_thumb"
|
10
|
-
|
11
|
-
# building url with thumb (foo.png => foo.thumb.png)
|
12
|
-
img_urls[thumb_img_key] = url.sub(/\.([^\.]+)$/){".thumb.#{$1}"}
|
13
|
-
|
14
|
-
"[![][#{thumb_img_key}]][#{img_key}]"
|
15
|
-
else # image.(icon|thumb|...).png
|
16
|
-
img_key_full = "#{img_key}_full"
|
17
|
-
|
18
|
-
# building url with thumb (foo.png => foo.thumb.png)
|
19
|
-
img_urls[img_key_full] = url.sub(/\.([^\.]+)\.([^\.]+)$/){".#{$2}"}
|
20
|
-
|
21
|
-
"[![][#{img_key}]][#{img_key_full}]"
|
22
|
-
end
|
23
|
-
else
|
24
|
-
$& || ''
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
unless img_urls.blank?
|
29
|
-
markdown << "\n"
|
30
|
-
markdown << img_urls.to_a.collect{|k, v| "[#{k}]: #{v}"}.join("\n")
|
31
|
-
end
|
32
|
-
|
33
|
-
call_next markdown, env
|
34
|
-
end
|
35
|
-
end
|