diff --git a/Gemfile b/Gemfile index 5f0e11b..e45e65f 100644 --- a/Gemfile +++ b/Gemfile @@ -1,10 +1,2 @@ -source :gemcutter - -gem "actionpack" - -group :development, :test do - gem "rspec", ">=2.0.0" - gem "autotest" -end - - +source :rubygems +gemspec diff --git a/Rakefile b/Rakefile index f073dc2..6195e06 100755 --- a/Rakefile +++ b/Rakefile @@ -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 \ No newline at end of file diff --git a/lib/typography/core.rb b/lib/typography/core.rb index 9dce206..bfe1338 100755 --- a/lib/typography/core.rb +++ b/lib/typography/core.rb @@ -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 '«', '»', '„', '“', 'а-яА-Я' @@ -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("(\"|\'|")((\s*<[^>]+>\s*)?[#{letters}]((<[^>]+>)|.)*?[^\\s])\\1", Regexp::MULTILINE | Regexp::IGNORECASE)) do |match| inside, before, after = $2, $`, $' if after.match(/^([^<]+>|>)/) || before.match(/<[^>]+$/) #inside tag match @@ -61,12 +61,14 @@ def replace_quotes(left1 = '“', right1 = '”', left2 = '‘', 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 diff --git a/lib/typography/helper.rb b/lib/typography/helper.rb index 88482c2..fa8c7b2 100755 --- a/lib/typography/helper.rb +++ b/lib/typography/helper.rb @@ -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 diff --git a/lib/typography/version.rb b/lib/typography/version.rb index 84bc2b4..d3216e4 100644 --- a/lib/typography/version.rb +++ b/lib/typography/version.rb @@ -1,4 +1,4 @@ module TypographyHelper GEM_NAME = "typography" - VERSION = "0.3.0" + VERSION = "0.3.1" end diff --git a/spec/debug.log b/spec/debug.log deleted file mode 100755 index 7750903..0000000 --- a/spec/debug.log +++ /dev/null @@ -1 +0,0 @@ -# Logfile created on Sun Sep 07 04:55:44 +0400 2008 by / diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 77daed8..80acab9 100755 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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) diff --git a/spec/typography_spec.rb b/spec/typography_spec.rb index 316e54d..8487f60 100755 --- a/spec/typography_spec.rb +++ b/spec/typography_spec.rb @@ -27,6 +27,15 @@ ty('Text "текст" text "Другой текст" ').should == 'Text «текст» text «Другой текст» ' end + it "should make russian quotes for quotes with first russian letter, beginning with html tag" do + ty('"текст"').should == '«текст»' + ty('Text "текст" text').should == 'Text «текст» text' + end + + it "should make russian quotes for quotes with html tag" do + ty('Текст "Начало текста текст"').should == 'Текст «Начало текста текст»' + end + it "should do the same with single quotes" do ty('\'текст\'').should == '«текст»' ty('Text \'текст\' text').should == 'Text «текст» text' @@ -158,5 +167,17 @@ ty('Испанцы говорят, что целовать мужчину без усов, - всё равно что есть яйцо без соли').should == 'Испанцы говорят, что целовать мужчину без усов, — всё равно что есть яйцо без соли' end + it "should typography nested quotes properly" do + text = %{

"Кто-то прибежал" Кто-то прибежал Кто-то прибежал Кто-то прибежал Кто-то прибежал Кто-то прибежал Кто-то прибежал Кто-то прибежал Кто-то прибежал

"Кто-то прибежал Кто-то прибежал"  Кто-то прибежал «Кто-то прибежал К»

} + expected_text = %{

«Кто-то прибежал» Кто-то прибежал Кто-то прибежал Кто-то прибежал Кто-то прибежал Кто-то прибежал Кто-то прибежал Кто-то прибежал Кто-то прибежал

«Кто-то прибежал Кто-то прибежал»  Кто-то прибежал «Кто-то прибежал К»

} + ty(text).should == expected_text + end + + it "should typography nested quotes properly on real-world examples" do + text = %{

Об очередном ляпе «налоговой библии» пишет в своей статье для «ЗН в Украине» пишет кандидат юридических наук, доцент кандидат юридических наук, доцент Данил Гетманцев.

«Даже при последующем выполнении плательщиком требований налоговой службы его счета будут находиться под арестом»

} + expected_text = %{

Об очередном ляпе «налоговой библии» пишет в своей статье для «ЗН в Украине» пишет кандидат юридических наук, доцент кандидат юридических наук, доцент Данил Гетманцев.

«Даже при последующем выполнении плательщиком требований налоговой службы его счета будут находиться под арестом»

} + ty(text).should == expected_text + end + end diff --git a/typography.gemspec b/typography.gemspec index 8275999..f5d39c8 100644 --- a/typography.gemspec +++ b/typography.gemspec @@ -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 = "" @@ -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