evil-front 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: df26866b8b19e4d3f42c79c980310ff044a36f93
4
- data.tar.gz: 1707febaae47a5ba06ec9a5b55d19c4839859223
3
+ metadata.gz: 5c642832df3cebbd5ff200d7c90df281731b9db7
4
+ data.tar.gz: 27841ed12430bba30e6aee3ffe3f15491ab29bab
5
5
  SHA512:
6
- metadata.gz: 2b2bf25a92aff66e2b38759e21ca0eb6a60f3ef04c1f2120577671dd9636298e59cebc6f39344e720f469bed21ca9710dffda75c223dc4789628959e4d32067d
7
- data.tar.gz: a307a12384256e1625d99b8ebf92403c65d608e13217b43ade5bf57a4b963f311b28314f2c809d2463da74c8f4adb7c598868440e151fbb76a64a42bcb78860a
6
+ metadata.gz: 7e1ce451ab3e68f39af7dcfbc072791d25c0970122b7804f702da59e659d7668e174c7cd0789cb9703cb78eb8ea956ed79e864b6287b6b7d2eb14e593a37b92e
7
+ data.tar.gz: 8f0ed46f7385518a09bc468e58770f704ae1df55c1eeb249537292c5f50a010b739d69c7a42ac3f35f886aabf05deaa37a535ba4aba5c56351462274c5b79d1a
data/ChangeLog.md CHANGED
@@ -1,3 +1,10 @@
1
+ ## 0.2 “Ptolemaeus Crater”
2
+
3
+ * Add `english_typograph` helper.
4
+ * Add `typograph_by_locale` helper to use typographer for current I18n locale.
5
+ * Increase `english_typograph` speed.
6
+ * Increase `evil-front/links.js` speed.
7
+
1
8
  ## 0.1 “Hellespontus Montes”
2
9
 
3
10
  * Initial release.
data/evil-front.gemspec CHANGED
@@ -22,6 +22,7 @@ Gem::Specification.new do |s|
22
22
  s.add_dependency 'nokogiri', '>= 1'
23
23
  s.add_dependency 'sprockets', '>= 1'
24
24
  s.add_dependency 'unicode_utils', '>= 1.4'
25
+ s.add_dependency 'rubypants-unicode', '>= 0'
25
26
  s.add_dependency 'rails-sass-images', '>= 0.3'
26
27
  s.add_dependency 'standalone_typograf', '>= 3.0.1'
27
28
  end
@@ -3,7 +3,7 @@
3
3
 
4
4
  evil.doc.ready(function() {
5
5
  evil.body.on('click', 'a', function (e) {
6
- if ( $(this).attr('href') == '#' ) {
6
+ if ( this.getAttribute('href') == '#' ) {
7
7
  e.preventDefault();
8
8
  }
9
9
  });
@@ -0,0 +1,31 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubypants-unicode'
4
+
5
+ module EvilFront
6
+ # Helpers to work with English text.
7
+ module English
8
+ extend Typograph
9
+
10
+ private
11
+
12
+ # Replace symbols to right ones, like m-dash, quotes, etc.
13
+ def self.use_right_symbols(text)
14
+ RubyPants.new(text).to_html
15
+ end
16
+
17
+ # Small words to insert non-break space before them
18
+ def self.tiny_words
19
+ @tiny_words ||= begin
20
+ tiny = %w(to is be the a an no if at on of in or and)
21
+ tiny += tiny.map(&:capitalize)
22
+ tiny.map { |i| Regexp.new("( | )(#{Regexp.quote i}) ") }
23
+ end
24
+ end
25
+
26
+ # Apply all typograph methods to text
27
+ def self.typograph_all(text)
28
+ typograph(text)
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,21 @@
1
+ module EvilFront::Helpers
2
+ # Insert non-break spaces and mark quotes to have nice text.
3
+ # Work only with English language.
4
+ #
5
+ # = english_typograph user.description
6
+ #
7
+ # You can send block:
8
+ #
9
+ # = english_typograph do
10
+ # = user.name
11
+ # = user.profession
12
+ def english_typograph(text = nil, &block)
13
+ text = if block_given?
14
+ capture(&block)
15
+ else
16
+ EvilFront.escape(text)
17
+ end
18
+ text = EvilFront::English.typograph_html(text)
19
+ EvilFront.html_safe(text)
20
+ end
21
+ end
@@ -1,12 +1,12 @@
1
1
  module EvilFront::Helpers
2
- # Insert non-break spaces and mark quotes to have nice text. Work only with
3
- # Russian language.
2
+ # Insert non-break spaces and mark quotes to have nice text.
3
+ # Work only with Russian language.
4
4
  #
5
- # = typograph user.description
5
+ # = russian_typograph user.description
6
6
  #
7
7
  # You can send block:
8
8
  #
9
- # = typograph do
9
+ # = russian_typograph do
10
10
  # = user.name
11
11
  # = user.profession
12
12
  def russian_typograph(text = nil, &block)
@@ -0,0 +1,16 @@
1
+ module EvilFront::Helpers
2
+ # Add typograph rules to text by language in current locale.
3
+ #
4
+ # typograph_by_locale(post.body)
5
+ def typograph_by_locale(text = nil, &block)
6
+ text = capture(&block) if block_given?
7
+
8
+ if I18n.locale == :ru
9
+ russian_typograph(text)
10
+ elsif I18n.locale == :en
11
+ english_typograph(text)
12
+ else
13
+ text
14
+ end
15
+ end
16
+ end
@@ -1,12 +1,13 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  require 'unicode_utils'
4
- require 'nokogiri'
5
4
  require 'standalone_typograf'
6
5
 
7
6
  module EvilFront
8
7
  # Helpers to work with Russian text.
9
8
  module Russian
9
+ extend Typograph
10
+
10
11
  # Capitalize only first letter (like titles in Russian).
11
12
  #
12
13
  # = EvilFront::Russian.capitalize_first(title)
@@ -14,44 +15,6 @@ module EvilFront
14
15
  UnicodeUtils.upcase(text[0]) + text[1..-1]
15
16
  end
16
17
 
17
- # Insert non-break spaces and mark quotes to have nice text. Work only with
18
- # Russian language.
19
- #
20
- # EvilFront::Russian.typograph(article)
21
- def self.typograph(text)
22
- return text if text.nil? or text.empty?
23
-
24
- text = StandaloneTypograf::Typograf.new(text).prepare
25
-
26
- tiny = %w(ни не и но а или да как из-за про по за для
27
- на до при меж о у в во с со от ото из без
28
- безо к ко об обо под подо над перед передо)
29
- tiny += tiny.map { |i| capitalize_first(i) }
30
- tiny.each do |word|
31
- regexp = Regexp.new(" #{Regexp.quote word} ") # fix JRuby issue
32
- text.gsub! regexp, " #{word} " # non-break space
33
- end
34
-
35
- text.gsub!(/([^\s" ]+)-([^\s" ]+)/, '\1‑\2')
36
-
37
- text
38
- end
39
-
40
- # Like `typograph`, but process only text nodes in HTML.
41
- #
42
- # EvilFront::Russian.typograph_html(article.html)
43
- def self.typograph_html(html)
44
- return html if html.nil? or html.empty?
45
-
46
- if html.include? '<'
47
- nodes = Nokogiri::HTML::DocumentFragment.parse(html)
48
- typograph_node! nodes
49
- nodes.to_html
50
- else
51
- auto_flying_quotes(typograph(html))
52
- end
53
- end
54
-
55
18
  # Find quotes in text and make them flying
56
19
  def self.auto_flying_quotes(text)
57
20
  text.gsub(/\s«[^»]+»/) { |i| flying_quotes i[2..-2], space: i[0] }.
@@ -67,18 +30,25 @@ module EvilFront
67
30
 
68
31
  private
69
32
 
70
- # Recursively apply typography to Nokogiri nodes
71
- def self.typograph_node!(node)
72
- return if node.name == 'code'
73
-
74
- node.children.each do |child|
75
- if child.is_a? Nokogiri::XML::Text
76
- text = EvilFront.escape(child.content)
77
- child.replace( auto_flying_quotes(typograph(text)) )
78
- else
79
- typograph_node! child
80
- end
33
+ # Small words to insert non-break space before them
34
+ def self.tiny_words
35
+ @tiny_words ||= begin
36
+ tiny = %w(ни не и но а или да как из-за про по за для
37
+ на до при меж о у в во с со от ото из без
38
+ безо к ко об обо под подо над перед передо)
39
+ tiny += tiny.map { |i| capitalize_first(i) }
40
+ tiny.map { |i| Regexp.new("()(#{Regexp.quote i}) ") }
81
41
  end
82
42
  end
43
+
44
+ # Replace symbols to right ones, like m-dash, quotes, etc.
45
+ def self.use_right_symbols(text)
46
+ StandaloneTypograf::Typograf.new(text).prepare
47
+ end
48
+
49
+ # Apply all typograph methods to text
50
+ def self.typograph_all(text)
51
+ auto_flying_quotes(typograph(text))
52
+ end
83
53
  end
84
54
  end
@@ -0,0 +1,53 @@
1
+ # encoding: utf-8
2
+
3
+ require 'nokogiri'
4
+
5
+ module EvilFront
6
+ # Abstract module to be used in `Russian` and `English` typographs.
7
+ module Typograph
8
+ # Like `typograph`, but process only text nodes in HTML.
9
+ #
10
+ # EvilFront::Russian.typograph_html(article.html)
11
+ def typograph_html(html)
12
+ return html if html.nil? or html.empty?
13
+
14
+ if html.include? '<'
15
+ nodes = Nokogiri::HTML::DocumentFragment.parse(html)
16
+ typograph_node! nodes
17
+ nodes.to_html
18
+ else
19
+ typograph_all(html)
20
+ end
21
+ end
22
+
23
+ # Insert non-break spaces and mark quotes to have nice text.
24
+ #
25
+ # EvilFront::Russian.typograph(article)
26
+ def typograph(text)
27
+ return text if text.nil? or text.empty?
28
+
29
+ text.gsub! '&quot;', '"'
30
+ text = use_right_symbols(text)
31
+ text.gsub!(/([^\s" ]+)-([^\s" ]+)/, '\1‑\2')
32
+ tiny_words.each { |regexp| text.gsub! regexp, "\\1\\2 " }
33
+
34
+ text
35
+ end
36
+
37
+ private
38
+
39
+ # Recursively apply typography to Nokogiri nodes
40
+ def typograph_node!(node)
41
+ return if %w(pre code kbd script style math).include? node.name
42
+
43
+ node.children.each do |child|
44
+ if child.is_a? Nokogiri::XML::Text
45
+ text = EvilFront.escape(child.content)
46
+ child.replace( typograph_all(text) )
47
+ else
48
+ typograph_node! child
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -1,3 +1,3 @@
1
1
  module EvilFront
2
- VERSION = '0.1.0' unless defined? EvilFront::VERSION
2
+ VERSION = '0.2.0' unless defined? EvilFront::VERSION
3
3
  end
data/lib/evil-front.rb CHANGED
@@ -6,7 +6,9 @@ require 'sprockets'
6
6
  require 'rails-sass-images'
7
7
 
8
8
  module EvilFront
9
- autoload :Russian, 'evil-front/russian'
9
+ autoload :Typograph, 'evil-front/typograph'
10
+ autoload :Russian, 'evil-front/russian'
11
+ autoload :English, 'evil-front/english'
10
12
 
11
13
  if defined?(::Rails)
12
14
  # Enable `lib/assets/` and `vendor/assets/` in Rails app.
@@ -0,0 +1,27 @@
1
+ # encoding: utf-8
2
+
3
+ require_relative 'spec_helper'
4
+
5
+ describe EvilFront::English do
6
+
7
+ describe 'typograph' do
8
+
9
+ it 'changes quotes' do
10
+ EvilFront::English.typograph('"a".').should == '“a”.'
11
+ end
12
+
13
+ it 'changes dashes' do
14
+ EvilFront::English.typograph('a - b').should == 'a - b'
15
+ end
16
+
17
+ it 'changes ellipsis' do
18
+ EvilFront::English.typograph('a...').should == 'a…'
19
+ end
20
+
21
+ it 'inserts non-break spaces' do
22
+ EvilFront::English.typograph('he is a hero').should == 'he is a hero'
23
+ end
24
+
25
+ end
26
+
27
+ end
data/spec/helpers_spec.rb CHANGED
@@ -47,7 +47,7 @@ describe EvilFront::Helpers do
47
47
 
48
48
  describe 'russian_typograph' do
49
49
 
50
- it 'typograps text inside tags' do
50
+ it 'typographs text inside tags' do
51
51
  tag = '<a title="а - б">а - б</a>'.html_safe
52
52
  russian_typograph(tag).should == '<a title="а - б">а — б</a>'
53
53
  end
@@ -58,6 +58,39 @@ describe EvilFront::Helpers do
58
58
 
59
59
  end
60
60
 
61
+ describe 'english_typograph' do
62
+
63
+ it 'typographs text inside tags' do
64
+ tag = '<a title="a...">a...</a>'.html_safe
65
+ english_typograph(tag).should == '<a title="a...">a…</a>'
66
+ end
67
+
68
+ it 'escapes HTML' do
69
+ english_typograph('<a>').should == '&lt;a&gt;'
70
+ end
71
+
72
+ end
73
+
74
+ describe 'typograph_by_locale' do
75
+ after do
76
+ I18n.locale = :en
77
+ end
78
+
79
+ it 'typographs by current locale' do
80
+ I18n.locale = :en
81
+ typograph_by_locale('"a"').should == '“a”'
82
+
83
+ I18n.locale = :ru
84
+ typograph_by_locale('"a"').should == '<span class="quotes">«a»</span>'
85
+ end
86
+
87
+ it 'returns origin text on unknown locale' do
88
+ I18n.locale = :fr
89
+ typograph_by_locale('"a"').should == '"a"'
90
+ end
91
+
92
+ end
93
+
61
94
  describe 'title' do
62
95
  after do
63
96
  I18n.locale = :en
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: evil-front
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrey Sitnik
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-09 00:00:00.000000000 Z
11
+ date: 2014-01-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: i18n
@@ -94,6 +94,20 @@ dependencies:
94
94
  - - ">="
95
95
  - !ruby/object:Gem::Version
96
96
  version: '1.4'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubypants-unicode
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
97
111
  - !ruby/object:Gem::Dependency
98
112
  name: rails-sass-images
99
113
  requirement: !ruby/object:Gem::Requirement
@@ -164,9 +178,11 @@ files:
164
178
  - lib/assets/stylesheets/evil-front/styled-taps.sass
165
179
  - lib/assets/stylesheets/evil-front/to-px.sass
166
180
  - lib/evil-front.rb
181
+ - lib/evil-front/english.rb
167
182
  - lib/evil-front/helpers.rb
168
183
  - lib/evil-front/helpers/capitalize_first.rb
169
184
  - lib/evil-front/helpers/disable_mobile_zoom.rb
185
+ - lib/evil-front/helpers/english_typograph.rb
170
186
  - lib/evil-front/helpers/flying_quote.rb
171
187
  - lib/evil-front/helpers/head_content.rb
172
188
  - lib/evil-front/helpers/head_tag.rb
@@ -176,9 +192,12 @@ files:
176
192
  - lib/evil-front/helpers/tel.rb
177
193
  - lib/evil-front/helpers/title.rb
178
194
  - lib/evil-front/helpers/title_tag.rb
195
+ - lib/evil-front/helpers/typograph_by_locale.rb
179
196
  - lib/evil-front/russian.rb
180
197
  - lib/evil-front/slim.rb
198
+ - lib/evil-front/typograph.rb
181
199
  - lib/evil-front/version.rb
200
+ - spec/english_spec.rb
182
201
  - spec/helpers_spec.rb
183
202
  - spec/russian_spec.rb
184
203
  - spec/sass/empty.txt