typography 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Changelog +6 -0
- data/Gemfile +10 -0
- data/README.rdoc +25 -0
- data/Rakefile +4 -0
- data/init.rb +4 -0
- data/lib/typography.rb +6 -0
- data/lib/typography/core.rb +71 -0
- data/lib/typography/helper.rb +8 -0
- data/lib/typography/version.rb +4 -0
- data/spec/debug.log +1 -0
- data/spec/spec_helper.rb +0 -0
- data/spec/typography_spec.rb +171 -0
- metadata +116 -0
data/Changelog
ADDED
data/Gemfile
ADDED
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
|
+
# => '«Читаешь — „Прокопьев любил солянку“ — и долго не можешь понять, почему солянка написана с маленькой буквы, ведь „Солянка“ — известный московский клуб.»'
|
11
|
+
|
12
|
+
It is better to use ty helper in your views:
|
13
|
+
|
14
|
+
ty 'This is a page title'
|
15
|
+
# => 'This is a 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
data/init.rb
ADDED
data/lib/typography.rb
ADDED
@@ -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’\2')
|
13
|
+
|
14
|
+
#russian quotes
|
15
|
+
@out = replace_quotes '«', '»', '„', '“', 'а-яА-Я'
|
16
|
+
|
17
|
+
#english quotes
|
18
|
+
@out = replace_quotes
|
19
|
+
|
20
|
+
#mdash
|
21
|
+
@out.gsub!(/--/, '—')
|
22
|
+
@out.gsub!(/(\w|;|,)\s+(—|–|-)\s*(\w)/, '\1 — \3')
|
23
|
+
@out.gsub!(/\s+—/, ' —')
|
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 \3')
|
33
|
+
@out.gsub!(/(^|\s|\()&([A-Za-z0-9]{2,8}|#[\d]*);(\w{1,2})\s+([^\s])/i, '\1&\2;\3 \4') #entities
|
34
|
+
@out.gsub!(/( |¡)(\w{1,2})\s+([^\s])/i, '\1\2 \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 = '“', right1 = '”', left2 = '‘', right2 = '’', 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
|
data/spec/debug.log
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# Logfile created on Sun Sep 07 04:55:44 +0400 2008 by /
|
data/spec/spec_helper.rb
ADDED
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 please'
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should typography output of h helper" do
|
13
|
+
h('Typography & &').should == 'Typography & &amp;'
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should typography output of simple_format helper" do
|
17
|
+
simple_format("I'm first\n\nAnd I am second").should == "<p>I’m first</p>\n\n<p>And I am 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 == '«текст»'
|
29
|
+
'Text "текст" text'.typography.should == 'Text «текст» text'
|
30
|
+
'Text "текст" text "Другой текст" '.typography.should == 'Text «текст» text «Другой текст» '
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should do the same with single quotes" do
|
34
|
+
'\'текст\''.typography.should == '«текст»'
|
35
|
+
'Text \'текст\' text'.typography.should == 'Text «текст» text'
|
36
|
+
'Text \'текст\' text \'Другой текст\' '.typography.should == 'Text «текст» text «Другой текст» '
|
37
|
+
end
|
38
|
+
|
39
|
+
|
40
|
+
it "should create second-level russian quotes" do
|
41
|
+
'Текст "в кавычках "второго уровня""'.typography.should == 'Текст «в кавычках „второго уровня“»'
|
42
|
+
end
|
43
|
+
|
44
|
+
|
45
|
+
it "should make english quotes for quotes with first non-russian letter" do
|
46
|
+
'"text"'.typography.should == '“text”'
|
47
|
+
'Text "text" text'.typography.should == 'Text “text” text'
|
48
|
+
'Text "text" text "Another text" '.typography.should == 'Text “text” text “Another text” '
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should do the same with single quotes" do
|
52
|
+
'\'text\''.typography.should == '“text”'
|
53
|
+
'Text \'text\' text'.typography.should == 'Text “text” text'
|
54
|
+
'Text \'text\' text \'Another text\' '.typography.should == 'Text “text” text “Another text” '
|
55
|
+
end
|
56
|
+
|
57
|
+
it "should create second-level english quotes" do
|
58
|
+
'Text "in quotes "second level""'.typography.should == 'Text “in quotes ‘second level’”'
|
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=\'ссылка\'>«ссылка»</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">“link”</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 == '«Кавычки» and “Quotes”'
|
74
|
+
'"Quotes" и "Кавычки"'.typography.should == '“Quotes” и «Кавычки»'
|
75
|
+
|
76
|
+
'"Кавычки "второго уровня"" and "Quotes "second level""'.typography.should == '«Кавычки „второго уровня“» and “Quotes ‘second level’”'
|
77
|
+
'"Quotes "second level"" и "Кавычки "второго уровня""'.typography.should == '“Quotes ‘second level’” и «Кавычки „второго уровня“»'
|
78
|
+
end
|
79
|
+
|
80
|
+
it "should replace -- to —" do
|
81
|
+
'Replace -- to mdash please'.typography.should == 'Replace — to mdash please'
|
82
|
+
end
|
83
|
+
|
84
|
+
it "should replace \"word - word\" to \"word — word\"" do
|
85
|
+
'word - word'.typography.should == 'word — word'
|
86
|
+
end
|
87
|
+
|
88
|
+
it "should insert before each — if it has empty space before" do
|
89
|
+
'Before — after'.typography.should == 'Before — after'
|
90
|
+
'Before — after'.typography.should == 'Before — after'
|
91
|
+
'Before—after'.typography.should == 'Before—after'
|
92
|
+
end
|
93
|
+
|
94
|
+
|
95
|
+
it "should insert after small words" do
|
96
|
+
'an apple'.typography.should == 'an apple'
|
97
|
+
end
|
98
|
+
|
99
|
+
it "should insert after small words" do
|
100
|
+
'I want to be a scientist'.typography.should == 'I want to be a scientist'
|
101
|
+
end
|
102
|
+
|
103
|
+
it "should insert after small words with ( or dash before it" do
|
104
|
+
'Apple (an orange)'.typography.should == 'Apple (an orange)'
|
105
|
+
end
|
106
|
+
|
107
|
+
it "should not insert 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 == '< & >'
|
126
|
+
end
|
127
|
+
|
128
|
+
it "should replace single quote between letters to apostrophe" do
|
129
|
+
'I\'m here'.typography.should == 'I’m here'
|
130
|
+
end
|
131
|
+
|
132
|
+
|
133
|
+
it "should typography real world examples" do
|
134
|
+
'"Читаешь -- "Прокопьев любил солянку" -- и долго не можешь понять, почему солянка написана с маленькой буквы, ведь "Солянка" -- известный московский клуб."'.typography.should == '«Читаешь — „Прокопьев любил солянку“ — и долго не можешь понять, почему солянка написана с маленькой буквы, ведь „Солянка“ — известный московский клуб.»'
|
135
|
+
end
|
136
|
+
|
137
|
+
it "should typography real world examples" do
|
138
|
+
'"Заебалоооооо" противостояние образует сет, в частности, "тюремные психозы", индуцируемые при различных психопатологических типологиях.'.typography(:escape_html => true).should == '«Заебалоооооо» противостояние образует сет, в частности, «тюремные психозы», индуцируемые при различных психопатологических типологиях.'
|
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 == '“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.'
|
143
|
+
end
|
144
|
+
|
145
|
+
it "should typography real wordl examples" do
|
146
|
+
'Фирменный стиль: от полиграфии к интернет-решениям (в рамках выставки «Дизайн и Реклама 2009»)'.typography.should == 'Фирменный стиль: от полиграфии к интернет-решениям (в рамках выставки «Дизайн и Реклама 2009»)'
|
147
|
+
end
|
148
|
+
|
149
|
+
it "should typography real world examples" do
|
150
|
+
'решениям (в рамках выставки'.typography.should == 'решениям (в рамках выставки'
|
151
|
+
end
|
152
|
+
|
153
|
+
it "should typography real world examples" do
|
154
|
+
'Реанимация живописи: «новые дикие» и «трансавангард» в ситуации арт-рынка 1980-х'.typography.should == 'Реанимация живописи: «новые дикие» и «трансавангард» в ситуации арт-рынка <span class="nobr">1980-х</span>'
|
155
|
+
end
|
156
|
+
|
157
|
+
it "should typography real world examples" do
|
158
|
+
'«Искусство после философии» – концептуальные стратегии Джозефа Кошута и Харальда Зеемана'.typography.should == '«Искусство после философии» — концептуальные стратегии Джозефа Кошута и Харальда Зеемана'
|
159
|
+
end
|
160
|
+
|
161
|
+
it "should typography real world examples" do
|
162
|
+
'Испанцы говорят, что целовать мужчину без усов, - всё равно что есть яйцо без соли'.typography.should == 'Испанцы говорят, что целовать мужчину без усов, — всё равно что есть яйцо без соли'
|
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
|