typography 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Changelog ADDED
@@ -0,0 +1,6 @@
1
+ ==v.0.0.1
2
+
3
+ Replace quotes
4
+ Replace dash between words to —
5
+ Add non-brace space after small words
6
+ Insert <span class="nobr"></span> around small words separated by dash
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source :gemcutter
2
+
3
+ gem "actionpack"
4
+
5
+ group :development, :test do
6
+ gem "rspec", ">=2.0.0"
7
+ gem "autotest"
8
+ end
9
+
10
+
data/README.rdoc ADDED
@@ -0,0 +1,25 @@
1
+ ==Typography
2
+
3
+ Simple plugin to make text more readable by applying some typographic rules to string. It is intended to use for russian texts, but some english typography rules are also supported.
4
+
5
+ ==Example
6
+
7
+ This plugin adds .typography method for string:
8
+
9
+ '"Читаешь -- "Прокопьев любил солянку" -- и долго не можешь понять, почему солянка написана с маленькой буквы, ведь "Солянка" -- известный московский клуб."'.typography
10
+ # => '&#171;Читаешь&nbsp;&mdash; &#132;Прокопьев любил солянку&#147;&nbsp;&mdash; и&nbsp;долго не&nbsp;можешь понять, почему солянка написана с&nbsp;маленькой буквы, ведь &#132;Солянка&#147;&nbsp;&mdash; известный московский клуб.&#187;'
11
+
12
+ It is better to use ty helper in your views:
13
+
14
+ ty 'This is a page title'
15
+ # => 'This is&nbsp;a&nbsp;page title'
16
+
17
+ Plugin also replaces h and simple_format helpers to apply typography to their results.
18
+
19
+
20
+ ==Installation
21
+
22
+ script/plugin install git://github.com/varezhka/typography.git
23
+
24
+
25
+ Copyright (c) 2008 Igor Gladkoborodov, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ require "rspec/core/rake_task"
2
+
3
+ desc "Test typography plugin."
4
+ RSpec::Core::RakeTask.new('spec')
data/init.rb ADDED
@@ -0,0 +1,4 @@
1
+ require 'typography'
2
+
3
+ ActionView::Base.send :include, TypographyHelper
4
+
data/lib/typography.rb ADDED
@@ -0,0 +1,6 @@
1
+ require "typography/helper"
2
+ require "typography/core"
3
+
4
+ module Typography
5
+ end
6
+
@@ -0,0 +1,71 @@
1
+ module Typography
2
+ class Core
3
+
4
+ def initialize(str, options = {})
5
+ @source = str
6
+ @out = str.dup
7
+ @options = options.merge({})
8
+ end
9
+
10
+ def typography
11
+ #apostrophe
12
+ @out.gsub!(/(\w)'(\w)/, '\1&#146;\2')
13
+
14
+ #russian quotes
15
+ @out = replace_quotes '&#171;', '&#187;', '&#132;', '&#147;', 'а-яА-Я'
16
+
17
+ #english quotes
18
+ @out = replace_quotes
19
+
20
+ #mdash
21
+ @out.gsub!(/--/, '&mdash;')
22
+ @out.gsub!(/(\w|;|,)\s+(—|–|-)\s*(\w)/, '\1&nbsp;&mdash; \3')
23
+ @out.gsub!(/\s+&mdash;/, '&nbsp;&mdash;')
24
+
25
+ #nobr
26
+ #around dash-separated words (что-то)
27
+ @out.gsub!(/(^|\s)((\w|0-9){1,3})-((\w|0-9){1,3})($|\s)/, '\1<span class="nobr">\2-\4</span>\6')
28
+ #1980-x почему-то
29
+ @out.gsub!(/(^|\s)((\w|0-9)+)-((\w|0-9){1,3})($|\s)/, '\1<span class="nobr">\2-\4</span>\6')
30
+
31
+ #non brake space
32
+ @out.gsub!(/(^|\s|\()(\w{1,2})\s+([^\s])/i, '\1\2&nbsp;\3')
33
+ @out.gsub!(/(^|\s|\()&([A-Za-z0-9]{2,8}|#[\d]*);(\w{1,2})\s+([^\s])/i, '\1&\2;\3&nbsp;\4') #entities
34
+ @out.gsub!(/(&nbsp;|&#161;)(\w{1,2})\s+([^\s])/i, '\1\2&nbsp;\3\4')
35
+
36
+ @out
37
+ end
38
+
39
+
40
+ # def replace_quotes!(*args)
41
+ # @source.replace self.replace_quotes(*args)
42
+ # end
43
+
44
+ def replace_quotes(left1 = '&#147;', right1 = '&#148;', left2 = '&#145;', right2 = '&#146;', letters = 'a-zA-Z')
45
+ str = @out.dup
46
+ replace_quotes = lambda do
47
+ old_str = str.dup
48
+ str.gsub!(Regexp.new("(\"|\')([#{letters}].*?[^\\s])\\1", Regexp::MULTILINE | Regexp::IGNORECASE)) do |match|
49
+ inside, before, after = $2, $`, $'
50
+ if after.match(/^([^<]+>|>)/) || before.match(/<[^>]+$/) #inside tag
51
+ match
52
+ else
53
+ "#{left1}#{inside}#{right1}"
54
+ end
55
+ end
56
+ old_str != str
57
+ end
58
+ while replace_quotes.call do end
59
+
60
+ # second level
61
+ replace_second_level_quotes = lambda do
62
+ str.gsub! Regexp.new("#{left1}(.*)#{left1}(.*)#{right1}(.*)#{right1}", Regexp::MULTILINE | Regexp::IGNORECASE) do |match|
63
+ "#{left1}#{$1}#{left2}#{$2}#{right2}#{$3}#{right1}"
64
+ end
65
+ end
66
+ while replace_second_level_quotes.call do end
67
+
68
+ str
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,8 @@
1
+ module ActionView::Helpers::TextHelper
2
+ def ty(text, options = {})
3
+ Typography::Core.new(text.html_safe, options).typography
4
+ end
5
+ def ty_simple(text, html_options={})
6
+ simple_format ty(text), html_options
7
+ end
8
+ end
@@ -0,0 +1,4 @@
1
+ module TypographyHelper
2
+ GEM_NAME = "typography"
3
+ VERSION = "0.1.0"
4
+ end
data/spec/debug.log ADDED
@@ -0,0 +1 @@
1
+ # Logfile created on Sun Sep 07 04:55:44 +0400 2008 by /
File without changes
@@ -0,0 +1,171 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe TypographyHelper, 'with typography' do
4
+ include TypographyHelper
5
+ include ActionView::Helpers::TextHelper
6
+ include ActionView::Helpers::TagHelper
7
+
8
+ it "should have t helper" do
9
+ ty('typography me please').should == 'typography me&nbsp;please'
10
+ end
11
+
12
+ it "should typography output of h helper" do
13
+ h('Typography & &amp;').should == 'Typography &amp; &amp;amp;'
14
+ end
15
+
16
+ it "should typography output of simple_format helper" do
17
+ simple_format("I'm first\n\nAnd I&nbsp;am&nbsp;second").should == "<p>I&#146;m first</p>\n\n<p>And I&nbsp;am&nbsp;second</p>"
18
+ end
19
+
20
+ it "should return '<p></p>' (simple_format of empty string) on simple_format(nil)" do
21
+ simple_format(nil).should == "<p></p>"
22
+ end
23
+ end
24
+
25
+ describe String, 'with typography' do
26
+
27
+ it "should make russian quotes for quotes with first russian letter" do
28
+ '"текст"'.typography.should == '&#171;текст&#187;'
29
+ 'Text "текст" text'.typography.should == 'Text &#171;текст&#187; text'
30
+ 'Text "текст" text "Другой текст" '.typography.should == 'Text &#171;текст&#187; text &#171;Другой текст&#187; '
31
+ end
32
+
33
+ it "should do the same with single quotes" do
34
+ '\'текст\''.typography.should == '&#171;текст&#187;'
35
+ 'Text \'текст\' text'.typography.should == 'Text &#171;текст&#187; text'
36
+ 'Text \'текст\' text \'Другой текст\' '.typography.should == 'Text &#171;текст&#187; text &#171;Другой текст&#187; '
37
+ end
38
+
39
+
40
+ it "should create second-level russian quotes" do
41
+ 'Текст "в кавычках "второго уровня""'.typography.should == 'Текст &#171;в&nbsp;кавычках &#132;второго уровня&#147;&#187;'
42
+ end
43
+
44
+
45
+ it "should make english quotes for quotes with first non-russian letter" do
46
+ '"text"'.typography.should == '&#147;text&#148;'
47
+ 'Text "text" text'.typography.should == 'Text &#147;text&#148; text'
48
+ 'Text "text" text "Another text" '.typography.should == 'Text &#147;text&#148; text &#147;Another text&#148; '
49
+ end
50
+
51
+ it "should do the same with single quotes" do
52
+ '\'text\''.typography.should == '&#147;text&#148;'
53
+ 'Text \'text\' text'.typography.should == 'Text &#147;text&#148; text'
54
+ 'Text \'text\' text \'Another text\' '.typography.should == 'Text &#147;text&#148; text &#147;Another text&#148; '
55
+ end
56
+
57
+ it "should create second-level english quotes" do
58
+ 'Text "in quotes "second level""'.typography.should == 'Text &#147;in&nbsp;quotes &#145;second level&#146;&#148;'
59
+ end
60
+
61
+
62
+ it "should not replace quotes inside html tags" do
63
+ '<a href="ссылка">ссылка</a>'.typography.should == '<a href="ссылка">ссылка</a>'
64
+ '<a href=\'ссылка\'>"ссылка"</a>'.typography.should == '<a href=\'ссылка\'>&#171;ссылка&#187;</a>'
65
+
66
+ '<a href=\'link\'>link</a>'.typography.should == '<a href=\'link\'>link</a>'
67
+ '<a href="link">"link"</a>'.typography.should == '<a href="link">&#147;link&#148;</a>'
68
+
69
+ ' one">One</a> <a href="two"></a> <a href="three" '.typography.should == ' one">One</a> <a href="two"></a> <a href="three" '
70
+ end
71
+
72
+ it "should make english and russian quotes in the same string" do
73
+ '"Кавычки" and "Quotes"'.typography.should == '&#171;Кавычки&#187; and &#147;Quotes&#148;'
74
+ '"Quotes" и "Кавычки"'.typography.should == '&#147;Quotes&#148; и&nbsp;&#171;Кавычки&#187;'
75
+
76
+ '"Кавычки "второго уровня"" and "Quotes "second level""'.typography.should == '&#171;Кавычки &#132;второго уровня&#147;&#187; and &#147;Quotes &#145;second level&#146;&#148;'
77
+ '"Quotes "second level"" и "Кавычки "второго уровня""'.typography.should == '&#147;Quotes &#145;second level&#146;&#148; и&nbsp;&#171;Кавычки &#132;второго уровня&#147;&#187;'
78
+ end
79
+
80
+ it "should replace -- to &mdash;" do
81
+ 'Replace -- to mdash please'.typography.should == 'Replace&nbsp;&mdash; to&nbsp;mdash please'
82
+ end
83
+
84
+ it "should replace \"word - word\" to \"word&nbsp;&mdash; word\"" do
85
+ 'word - word'.typography.should == 'word&nbsp;&mdash; word'
86
+ end
87
+
88
+ it "should insert &nbsp; before each &mdash; if it has empty space before" do
89
+ 'Before &mdash; after'.typography.should == 'Before&nbsp;&mdash; after'
90
+ 'Before &mdash; after'.typography.should == 'Before&nbsp;&mdash; after'
91
+ 'Before&mdash;after'.typography.should == 'Before&mdash;after'
92
+ end
93
+
94
+
95
+ it "should insert &nbsp; after small words" do
96
+ 'an apple'.typography.should == 'an&nbsp;apple'
97
+ end
98
+
99
+ it "should insert &nbsp; after small words" do
100
+ 'I want to be a scientist'.typography.should == 'I&nbsp;want to&nbsp;be&nbsp;a&nbsp;scientist'
101
+ end
102
+
103
+ it "should insert &nbsp; after small words with ( or dash before it" do
104
+ 'Apple (an orange)'.typography.should == 'Apple (an&nbsp;orange)'
105
+ end
106
+
107
+ it "should not insert &nbsp; after small words if it has not space after" do
108
+ 'Хорошо бы.'.typography.should == 'Хорошо бы.'
109
+ 'Хорошо бы'.typography.should == 'Хорошо бы'
110
+ 'Хорошо бы. Иногда'.typography.should == 'Хорошо бы. Иногда'
111
+ end
112
+
113
+ it "should insert <span class=\"nobr\"></span> around small words separated by dash" do
114
+ 'Мне фигово что-то'.typography.should == 'Мне фигово <span class="nobr">что-то</span>'
115
+ 'Как-то мне плохо'.typography.should == '<span class="nobr">Как-то</span> мне плохо'
116
+ 'хуе-мое'.typography.should == '<span class="nobr">хуе-мое</span>'
117
+ end
118
+
119
+ it "should not insert <span class=\"nobr\"></span> around words separated by dash if both of them are bigger than 3 letters" do
120
+ 'мальчик-девочка'.typography.should == 'мальчик-девочка'
121
+ end
122
+
123
+
124
+ it "should escape html if :escape_html => true is passed" do
125
+ '< & >'.typography(:escape_html => true).should == '&lt; &amp; &gt;'
126
+ end
127
+
128
+ it "should replace single quote between letters to apostrophe" do
129
+ 'I\'m here'.typography.should == 'I&#146;m here'
130
+ end
131
+
132
+
133
+ it "should typography real world examples" do
134
+ '"Читаешь -- "Прокопьев любил солянку" -- и долго не можешь понять, почему солянка написана с маленькой буквы, ведь "Солянка" -- известный московский клуб."'.typography.should == '&#171;Читаешь&nbsp;&mdash; &#132;Прокопьев любил солянку&#147;&nbsp;&mdash; и&nbsp;долго не&nbsp;можешь понять, почему солянка написана с&nbsp;маленькой буквы, ведь &#132;Солянка&#147;&nbsp;&mdash; известный московский клуб.&#187;'
135
+ end
136
+
137
+ it "should typography real world examples" do
138
+ '"Заебалоооооо" противостояние образует сет, в частности, "тюремные психозы", индуцируемые при различных психопатологических типологиях.'.typography(:escape_html => true).should == '&#171;Заебалоооооо&#187; противостояние образует сет, в&nbsp;частности, &#171;тюремные психозы&#187;, индуцируемые при различных психопатологических типологиях.'
139
+ end
140
+
141
+ it "should typography real world examples" do
142
+ '"They are the most likely habitat that we\'re going to get to in the foreseeable future," said NASA Ames Research Center\'s Aaron Zent, the lead scientist for the probe being used to look for unfrozen water.'.typography.should == '&#147;They are the most likely habitat that we&#146;re going to&nbsp;get to&nbsp;in&nbsp;the foreseeable future,&#148; said NASA Ames Research Center&#146;s Aaron Zent, the lead scientist for the probe being used to&nbsp;look for unfrozen water.'
143
+ end
144
+
145
+ it "should typography real wordl examples" do
146
+ 'Фирменный стиль: от полиграфии к интернет-решениям (в рамках выставки «Дизайн и Реклама 2009»)'.typography.should == 'Фирменный стиль: от&nbsp;полиграфии к&nbsp;интернет-решениям (в&nbsp;рамках выставки «Дизайн и&nbsp;Реклама 2009»)'
147
+ end
148
+
149
+ it "should typography real world examples" do
150
+ 'решениям (в рамках выставки'.typography.should == 'решениям (в&nbsp;рамках выставки'
151
+ end
152
+
153
+ it "should typography real world examples" do
154
+ 'Реанимация живописи: «новые дикие» и «трансавангард» в ситуации арт-рынка 1980-х'.typography.should == 'Реанимация живописи: «новые дикие» и&nbsp;«трансавангард» в&nbsp;ситуации арт-рынка <span class="nobr">1980-х</span>'
155
+ end
156
+
157
+ it "should typography real world examples" do
158
+ '«Искусство после философии&#187; – концептуальные стратегии Джозефа Кошута и Харальда Зеемана'.typography.should == '«Искусство после философии&#187;&nbsp;&mdash; концептуальные стратегии Джозефа Кошута и&nbsp;Харальда Зеемана'
159
+ end
160
+
161
+ it "should typography real world examples" do
162
+ 'Испанцы говорят, что целовать мужчину без усов, - всё равно что есть яйцо без соли'.typography.should == 'Испанцы говорят, что целовать мужчину без усов,&nbsp;&mdash; всё равно что есть яйцо без соли'
163
+ end
164
+
165
+
166
+
167
+ # it "should fail" do
168
+ # 1.should == 2
169
+ # end
170
+
171
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: typography
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Dmitry Shaposhnik
14
+ - Anton Versal
15
+ - Igor Gladkoborodov
16
+ - Pravosud Pavel
17
+ autorequire:
18
+ bindir: bin
19
+ cert_chain: []
20
+
21
+ date: 2010-11-22 00:00:00 +02:00
22
+ default_executable:
23
+ dependencies:
24
+ - !ruby/object:Gem::Dependency
25
+ name: rspec
26
+ prerelease: false
27
+ requirement: &id001 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ hash: 15
33
+ segments:
34
+ - 2
35
+ - 0
36
+ - 0
37
+ version: 2.0.0
38
+ type: :development
39
+ version_requirements: *id001
40
+ - !ruby/object:Gem::Dependency
41
+ name: actionpack
42
+ prerelease: false
43
+ requirement: &id002 !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ hash: 3
49
+ segments:
50
+ - 0
51
+ version: "0"
52
+ type: :runtime
53
+ version_requirements: *id002
54
+ description: This gem makes text more readable by applying some typographic rules to string.
55
+ email:
56
+ - dmitry@shaposhnik.name
57
+ - ant.ver@gmail.com
58
+ - igor@workisfun.ru
59
+ executables: []
60
+
61
+ extensions: []
62
+
63
+ extra_rdoc_files:
64
+ - README.rdoc
65
+ files:
66
+ - lib/typography.rb
67
+ - lib/typography/version.rb
68
+ - lib/typography/core.rb
69
+ - lib/typography/helper.rb
70
+ - spec/spec_helper.rb
71
+ - spec/debug.log
72
+ - spec/typography_spec.rb
73
+ - README.rdoc
74
+ - Rakefile
75
+ - Changelog
76
+ - Gemfile
77
+ - init.rb
78
+ has_rdoc: true
79
+ homepage: https://github.com/VerAnt/typography
80
+ licenses: []
81
+
82
+ post_install_message:
83
+ rdoc_options:
84
+ - --main
85
+ - README.rdoc
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ hash: 3
94
+ segments:
95
+ - 0
96
+ version: "0"
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ hash: 3
103
+ segments:
104
+ - 0
105
+ version: "0"
106
+ requirements: []
107
+
108
+ rubyforge_project: ""
109
+ rubygems_version: 1.3.7
110
+ signing_key:
111
+ specification_version: 3
112
+ summary: typography-0.1.0
113
+ test_files:
114
+ - spec/spec_helper.rb
115
+ - spec/debug.log
116
+ - spec/typography_spec.rb