string_master 0.2.1 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/string_master/string_master.rb +28 -35
- data/spec/lib/string_master/string_master_spec.rb +18 -8
- data/string_master.gemspec +8 -8
- metadata +39 -16
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.3.1
|
@@ -68,41 +68,6 @@ class StringMaster
|
|
68
68
|
self
|
69
69
|
end
|
70
70
|
|
71
|
-
# Highlights code using 'uv' library.
|
72
|
-
# Make sure you have ultraviolet gem installed.
|
73
|
-
def highlight_code(options={})
|
74
|
-
begin
|
75
|
-
require 'uv'
|
76
|
-
rescue LoadError
|
77
|
-
raise LoadError, "Gem 'ultraviolet' is required to highlight code. Please install the gem. Note that it fails to build native extensions with ruby 1.9.x so it's only possible to use it with ruby 1.8.x\n\n "
|
78
|
-
end
|
79
|
-
|
80
|
-
wrap_with = options[:wrap_with] || ['','']
|
81
|
-
text = @modified_string
|
82
|
-
|
83
|
-
languages_syntax_list = File.readlines(
|
84
|
-
File.expand_path(File.dirname(__FILE__) + '/../config/languages_syntax_list')
|
85
|
-
).map { |l| l.chomp }
|
86
|
-
|
87
|
-
text.gsub!(/<code(\s*?lang=["']?(.*?)["']?)?>(.*?)<\/code>/) do
|
88
|
-
if languages_syntax_list.include?($2)
|
89
|
-
lang = $2
|
90
|
-
else
|
91
|
-
lang = 'ruby'
|
92
|
-
end
|
93
|
-
unless $3.blank?
|
94
|
-
result = Uv.parse($3.gsub('<br/>', "\n").gsub('<', '<').gsub('>', '>').gsub('"', '"'), 'xhtml', lang, false, 'active4d')
|
95
|
-
"#{wrap_with[0].gsub('$lang', lang)}#{result}#{wrap_with[1]}"
|
96
|
-
end
|
97
|
-
end
|
98
|
-
|
99
|
-
# TODO: split string longer than 80 characters
|
100
|
-
|
101
|
-
@modified_string = text
|
102
|
-
self
|
103
|
-
|
104
|
-
end
|
105
|
-
|
106
71
|
# Breaks words that are longer than 'length'
|
107
72
|
def break_long_words(length=75, &block)
|
108
73
|
@modified_string.gsub!(/<a [^>]+>|<img [^>]+>|([^\s^\n^\^^\A^\t^\r<]{#{length},}?)|<\/a>/) do |s|
|
@@ -134,6 +99,34 @@ class StringMaster
|
|
134
99
|
self
|
135
100
|
end
|
136
101
|
|
102
|
+
# Finds lines of text that satisfy a 'regexp' and wraps them into an
|
103
|
+
# opening and closing 'tag'. Best example of usage is #wrap_code.
|
104
|
+
def wrap_lines(tag, regexp)
|
105
|
+
code_open = false; result = ""
|
106
|
+
@modified_string.each_line do |line|
|
107
|
+
if line =~ /\A\s{4}/
|
108
|
+
result += "<#{tag}>" unless code_open
|
109
|
+
code_open = true
|
110
|
+
result += line.sub(regexp, '')
|
111
|
+
else
|
112
|
+
result.chomp!
|
113
|
+
result += "</#{tag}>\n" if code_open
|
114
|
+
code_open = false
|
115
|
+
result += line
|
116
|
+
end
|
117
|
+
end
|
118
|
+
@modified_string = result
|
119
|
+
end
|
120
|
+
|
121
|
+
# Finds all lines that start with 4 spaces and wraps them into <code> tags.
|
122
|
+
def wrap_code
|
123
|
+
wrap_lines("code", /\A\s{4}/)
|
124
|
+
end
|
125
|
+
|
126
|
+
def wrap_inline_code(opening_tag="<span class=\"inlineCode\">", closing_tag="</span>")
|
127
|
+
@modified_string.gsub!(/`(.+?)`/, opening_tag + '\1' + closing_tag)
|
128
|
+
end
|
129
|
+
|
137
130
|
def to_s
|
138
131
|
modified_string.html_safe
|
139
132
|
end
|
@@ -34,15 +34,25 @@ describe StringMaster do
|
|
34
34
|
parser.urls_to_links.string.should == '<a href="http://gyazo.com/a4c16e7a6009f40f29248ad4fed41bd3.png" >http://gyazo.com/a4c16e7a6009f40f29248ad4fed41bd3.png</a><br>'
|
35
35
|
end
|
36
36
|
|
37
|
-
it "
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
37
|
+
it "wraps code in <code> tags" do
|
38
|
+
code = <<CODE
|
39
|
+
I have a piece of code
|
40
|
+
puts "hello world"
|
41
|
+
exit
|
42
|
+
and here's what my code looks like.
|
43
|
+
CODE
|
44
|
+
StringMaster.new(code).wrap_code.should == <<WRAPPED_CODE
|
45
|
+
I have a piece of code
|
46
|
+
<code>puts "hello world"
|
47
|
+
exit</code>
|
48
|
+
and here's what my code looks like.
|
49
|
+
WRAPPED_CODE
|
50
|
+
end
|
45
51
|
|
52
|
+
it "wraps inline code into <span class=\"inlineCode\"></span> tags" do
|
53
|
+
code = "I have a variable called `a` and it has a `nil` value"
|
54
|
+
parser = StringMaster.new(code)
|
55
|
+
parser.wrap_inline_code.should == "I have a variable called <span class=\"inlineCode\">a</span> and it has a <span class=\"inlineCode\">nil</span> value"
|
46
56
|
end
|
47
57
|
|
48
58
|
it "breaks long words" do
|
data/string_master.gemspec
CHANGED
@@ -4,14 +4,14 @@
|
|
4
4
|
# -*- encoding: utf-8 -*-
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
|
-
s.name =
|
8
|
-
s.version = "0.
|
7
|
+
s.name = "string_master"
|
8
|
+
s.version = "0.3.1"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Roman Snitko"]
|
12
|
-
s.date =
|
13
|
-
s.description =
|
14
|
-
s.email =
|
12
|
+
s.date = "2012-05-30"
|
13
|
+
s.description = "Because every time I create a new webapp, I think about how I should process user-generated content. Should convert urls to links and images? Should I allow certain tags? Should I convert all new lines to *br* tags? Well, now all that is as simple as calling a single method."
|
14
|
+
s.email = "roman.snitko@gmail.com"
|
15
15
|
s.extra_rdoc_files = [
|
16
16
|
"LICENSE.txt",
|
17
17
|
"README.markdown"
|
@@ -34,11 +34,11 @@ Gem::Specification.new do |s|
|
|
34
34
|
"spec/spec_helper.rb",
|
35
35
|
"string_master.gemspec"
|
36
36
|
]
|
37
|
-
s.homepage =
|
37
|
+
s.homepage = "http://github.com/snitko/string_master"
|
38
38
|
s.licenses = ["MIT"]
|
39
39
|
s.require_paths = ["lib"]
|
40
|
-
s.rubygems_version =
|
41
|
-
s.summary =
|
40
|
+
s.rubygems_version = "1.8.21"
|
41
|
+
s.summary = "Most common string manipulations for a webapp"
|
42
42
|
|
43
43
|
if s.respond_to? :specification_version then
|
44
44
|
s.specification_version = 3
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: string_master
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,12 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
13
|
-
default_executable:
|
12
|
+
date: 2012-05-30 00:00:00.000000000 Z
|
14
13
|
dependencies:
|
15
14
|
- !ruby/object:Gem::Dependency
|
16
15
|
name: actionpack
|
17
|
-
requirement:
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
18
17
|
none: false
|
19
18
|
requirements:
|
20
19
|
- - ! '>='
|
@@ -22,10 +21,15 @@ dependencies:
|
|
22
21
|
version: '0'
|
23
22
|
type: :runtime
|
24
23
|
prerelease: false
|
25
|
-
version_requirements:
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
26
30
|
- !ruby/object:Gem::Dependency
|
27
31
|
name: bundler
|
28
|
-
requirement:
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
29
33
|
none: false
|
30
34
|
requirements:
|
31
35
|
- - ! '>='
|
@@ -33,10 +37,15 @@ dependencies:
|
|
33
37
|
version: '0'
|
34
38
|
type: :development
|
35
39
|
prerelease: false
|
36
|
-
version_requirements:
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
37
46
|
- !ruby/object:Gem::Dependency
|
38
47
|
name: jeweler
|
39
|
-
requirement:
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
40
49
|
none: false
|
41
50
|
requirements:
|
42
51
|
- - ! '>='
|
@@ -44,10 +53,15 @@ dependencies:
|
|
44
53
|
version: '0'
|
45
54
|
type: :development
|
46
55
|
prerelease: false
|
47
|
-
version_requirements:
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
48
62
|
- !ruby/object:Gem::Dependency
|
49
63
|
name: rcov
|
50
|
-
requirement:
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
51
65
|
none: false
|
52
66
|
requirements:
|
53
67
|
- - ! '>='
|
@@ -55,10 +69,15 @@ dependencies:
|
|
55
69
|
version: '0'
|
56
70
|
type: :development
|
57
71
|
prerelease: false
|
58
|
-
version_requirements:
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
59
78
|
- !ruby/object:Gem::Dependency
|
60
79
|
name: rspec
|
61
|
-
requirement:
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
62
81
|
none: false
|
63
82
|
requirements:
|
64
83
|
- - ! '>='
|
@@ -66,7 +85,12 @@ dependencies:
|
|
66
85
|
version: '0'
|
67
86
|
type: :development
|
68
87
|
prerelease: false
|
69
|
-
version_requirements:
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
70
94
|
description: Because every time I create a new webapp, I think about how I should
|
71
95
|
process user-generated content. Should convert urls to links and images? Should
|
72
96
|
I allow certain tags? Should I convert all new lines to *br* tags? Well, now all
|
@@ -94,7 +118,6 @@ files:
|
|
94
118
|
- spec/lib/string_master/string_master_spec.rb
|
95
119
|
- spec/spec_helper.rb
|
96
120
|
- string_master.gemspec
|
97
|
-
has_rdoc: true
|
98
121
|
homepage: http://github.com/snitko/string_master
|
99
122
|
licenses:
|
100
123
|
- MIT
|
@@ -110,7 +133,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
110
133
|
version: '0'
|
111
134
|
segments:
|
112
135
|
- 0
|
113
|
-
hash:
|
136
|
+
hash: -1214522452410579616
|
114
137
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
115
138
|
none: false
|
116
139
|
requirements:
|
@@ -119,7 +142,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
119
142
|
version: '0'
|
120
143
|
requirements: []
|
121
144
|
rubyforge_project:
|
122
|
-
rubygems_version: 1.
|
145
|
+
rubygems_version: 1.8.21
|
123
146
|
signing_key:
|
124
147
|
specification_version: 3
|
125
148
|
summary: Most common string manipulations for a webapp
|