Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 2 additions & 10 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,2 @@
source :gemcutter

gem "actionpack"

group :development, :test do
gem "rspec", ">=2.0.0"
gem "autotest"
end


source :rubygems
gemspec
4 changes: 3 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
require "rspec/core/rake_task"

desc "Test typography plugin."
RSpec::Core::RakeTask.new('spec')
RSpec::Core::RakeTask.new(:spec)

task :default => :spec
12 changes: 7 additions & 5 deletions lib/typography/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ def initialize(str, options = {})
def typography
#apostrophe
@out.gsub!(/(\w)'(\w)/, '\1’\2')
# @out.gsub!(/\“/,'„')
# @out.gsub!(/\”/,'“')
@out.gsub!(/\“/,'„')
@out.gsub!(/\”/,'“')
#russian quotes
@out = replace_quotes '«', '»', '„', '“', 'а-яА-Я'

Expand Down Expand Up @@ -48,7 +48,7 @@ def replace_quotes(left1 = '“', right1 = '”', left2 = '‘', right2

replace_quotes = lambda do
old_str = str.dup
str.gsub!(Regexp.new("(\"|\'|")([#{letters}].*?[^\\s])\\1", Regexp::MULTILINE | Regexp::IGNORECASE)) do |match|
str.gsub!(Regexp.new("(\"|\'|&quot;)((\s*<[^>]+>\s*)?[#{letters}]((<[^>]+>)|.)*?[^\\s])\\1", Regexp::MULTILINE | Regexp::IGNORECASE)) do |match|
inside, before, after = $2, $`, $'
if after.match(/^([^<]+>|>)/) || before.match(/<[^>]+$/) #inside tag
match
Expand All @@ -61,12 +61,14 @@ def replace_quotes(left1 = '&#147;', right1 = '&#148;', left2 = '&#145;', right2
while replace_quotes.call do end

# second level
str.gsub!('&', "\0&")
replace_second_level_quotes = lambda do
str.gsub! Regexp.new("#{left1}(.*)#{left1}(.*)#{right1}(.*)#{right1}", Regexp::MULTILINE | Regexp::IGNORECASE) do |match|
"#{left1}#{$1}#{left2}#{$2}#{right2}#{$3}#{right1}"
str.gsub! Regexp.new("#{left1}([^\0`]*)\0#{left1}([^\0]*)\0#{right1}", Regexp::MULTILINE | Regexp::IGNORECASE) do
"#{left1}#{$1}#{left2}#{$2}#{right2}"
end
end
while replace_second_level_quotes.call do end
str.gsub!("\0", '')

str
end
Expand Down
4 changes: 3 additions & 1 deletion lib/typography/helper.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# encoding: utf-8
require 'action_view'

module ActionView::Helpers::TextHelper
def ty(text, options = {})
TypographyHelper::Core.new(text.html_safe, options).typography
TypographyHelper::Core.new(text.to_s.html_safe, options).typography
end
def ty_simple(text, html_options={})
simple_format ty(text), html_options
Expand Down
2 changes: 1 addition & 1 deletion lib/typography/version.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module TypographyHelper
GEM_NAME = "typography"
VERSION = "0.3.0"
VERSION = "0.3.1"
end
1 change: 0 additions & 1 deletion spec/debug.log

This file was deleted.

6 changes: 2 additions & 4 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
$KCODE = 'u'
require 'rubygems'
require 'action_pack'
require 'action_view'
require File.join File.dirname(__FILE__), '../lib/typography'

require 'bundler'
Bundler.require(:default, :development)
21 changes: 21 additions & 0 deletions spec/typography_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@
ty('Text "текст" text "Другой текст" ').should == 'Text &laquo;текст&raquo; text &laquo;Другой текст&raquo; '
end

it "should make russian quotes for quotes with first russian letter, beginning with html tag" do
ty('"<a href="#link">те</a>кст"').should == '&laquo;<a href="#link">те</a>кст&raquo;'
ty('Text "<b>те</b>кст" text').should == 'Text &laquo;<b>те</b>кст&raquo; text'
end

it "should make russian quotes for quotes with html tag" do
ty('Текст "Начало текста <a href="#link">те</a>кст"').should == 'Текст &laquo;Начало текста <a href="#link">те</a>кст&raquo;'
end

it "should do the same with single quotes" do
ty('\'текст\'').should == '&laquo;текст&raquo;'
ty('Text \'текст\' text').should == 'Text &laquo;текст&raquo; text'
Expand Down Expand Up @@ -158,5 +167,17 @@
ty('Испанцы говорят, что целовать мужчину без усов, - всё равно что есть яйцо без соли').should == 'Испанцы говорят, что целовать мужчину без усов,&nbsp;&mdash; всё равно что есть яйцо без соли'
end

it "should typography nested quotes properly" do
text = %{<p>&quot;Кто-то прибежал&quot; Кто-то прибежал Кто-то прибежал Кто-то прибежал Кто-то прибежал Кто-то прибежал Кто-то прибежал Кто-то прибежал Кто-то прибежал</p><p>&quot;Кто-то прибежал Кто-то прибежал&quot; &nbsp;Кто-то прибежал &laquo;Кто-то прибежал К&raquo;</p>}
expected_text = %{<p>&laquo;Кто-то прибежал&raquo; <span class=\"nobr\">Кто-то</span> прибежал <span class=\"nobr\">Кто-то</span> прибежал <span class=\"nobr\">Кто-то</span> прибежал <span class=\"nobr\">Кто-то</span> прибежал <span class=\"nobr\">Кто-то</span> прибежал <span class=\"nobr\">Кто-то</span> прибежал <span class=\"nobr\">Кто-то</span> прибежал <span class=\"nobr\">Кто-то</span> прибежал</p><p>&laquo;Кто-то прибежал <span class=\"nobr\">Кто-то</span> прибежал&raquo; &nbsp;Кто-то прибежал &laquo;Кто-то прибежал К&raquo;</p>}
ty(text).should == expected_text
end

it "should typography nested quotes properly on real-world examples" do
text = %{<p>Об очередном ляпе &laquo;налоговой библии&raquo; пишет в своей статье для &laquo;ЗН в Украине&raquo; пишет кандидат юридических наук, доцент кандидат юридических наук, доцент Данил Гетманцев.</p><p>&laquo;Даже при последующем выполнении плательщиком требований налоговой службы его счета будут находиться под арестом&raquo;</p>}
expected_text = %{<p>Об очередном ляпе &laquo;налоговой библии&raquo; пишет в&nbsp;своей статье для &laquo;ЗН&nbsp;в&nbsp;Украине&raquo; пишет кандидат юридических наук, доцент кандидат юридических наук, доцент Данил Гетманцев.</p><p>&laquo;Даже при последующем выполнении плательщиком требований налоговой службы его счета будут находиться под арестом&raquo;</p>}
ty(text).should == expected_text
end

end

7 changes: 4 additions & 3 deletions typography.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ $:.unshift lib unless $:.include?(lib)

require 'typography/version'

spec = Gem::Specification.new do |s|
Gem::Specification.new do |s|
s.authors = ["Dmitry Shaposhnik", "Anton Versal", "Igor Gladkoborodov", "Pravosud Pavel"]
s.email = ["dmitry@shaposhnik.name", "ant.ver@gmail.com", "igor@workisfun.ru" ]
s.email = ["dmitry@shaposhnik.name", "ant.ver@gmail.com", "igor@workisfun.ru"]
s.date = "2010-11-22"
s.homepage = "https://github.com/VerAnt/typography"
s.rubyforge_project = ""
Expand All @@ -22,8 +22,9 @@ spec = Gem::Specification.new do |s|
s.extra_rdoc_files = ["README.rdoc"]
s.rdoc_options = ["--main", "README.rdoc"]

s.add_development_dependency "rspec", ">= 2.0.0"
s.add_dependency "actionpack"
s.add_development_dependency "rspec", ">= 2.0.0"
s.add_development_dependency "autotest"
end