Add stringex for Chinese slug generation

This commit is contained in:
Erick Guan 2014-09-16 19:15:05 +08:00
parent 968bcd5acb
commit 667758ff40
8 changed files with 39 additions and 5 deletions

View File

@ -241,6 +241,8 @@ gem 'memory_profiler', require: false, platform: :mri_21
gem 'rmmseg-cpp', require: false
gem 'stringex', require: false
gem 'logster'
# perftools only works on 1.9 atm

View File

@ -371,6 +371,7 @@ GEM
activesupport (>= 3.0)
sprockets (~> 2.8)
stackprof (0.2.7)
stringex (2.5.2)
therubyracer (0.12.1)
libv8 (~> 3.16.14.0)
ref
@ -488,6 +489,7 @@ DEPENDENCIES
sinatra
spork-rails
stackprof
stringex
therubyracer
thin
timecop

View File

@ -15,6 +15,10 @@
# http://yamllint.com/
en:
stringex:
characters:
ellipsis: ""
number: "-"
# some default transliteration rules may be missing, add them to your locale
i18n:
transliterate:

View File

@ -1,10 +1,17 @@
# encoding: utf-8
module Slug
def self.for(string)
slug = string.gsub("'", "").parameterize
slug.gsub!("_", "-")
if ['zh_CN', 'ja', 'ko'].include?(SiteSetting.default_locale)
unless defined? Stringex
require 'stringex_lite'
end
slug = string.to_url
end
slug =~ /[^\d]/ ? slug : '' # Reject slugs that only contain numbers, because they would be indistinguishable from id's.
end

View File

@ -302,10 +302,10 @@ describe Search do
it 'finds chinese topic based on title' do
SiteSetting.default_locale = 'zh_TW'
topic = Fabricate(:topic, title: 'My Title Discourse社指南')
topic = Fabricate(:topic, title: 'My Title Discourse社指南')
post = Fabricate(:post, topic: topic)
Search.execute('社指南').posts.first.id.should == post.id
Search.execute('社指南').posts.first.id.should == post.id
Search.execute('指南').posts.first.id.should == post.id
end
end

View File

@ -48,12 +48,17 @@ describe Slug do
end
it "doesn't generate slugs that are just numbers" do
Slug.for('電車男 2').should be_blank
Slug.for('2').should be_blank
end
it "doesn't keep single quotes within word" do
Slug.for("Jeff hate's this").should == "jeff-hates-this"
end
it "translate the chineses" do
SiteSetting.default_locale = 'zh_CN'
Slug.for("习近平:中企承建港口电站等助斯里兰卡发展").should == "xi-jin-ping-zhong-qi-cheng-jian-gang-kou-dian-zhan-deng-zhu-si-li-lan-qia-fa-zhan"
end
end

View File

@ -169,16 +169,22 @@ describe Category do
end
describe 'non-english characters' do
let(:category) { Fabricate(:category, name: "電車男") }
let(:category) { Fabricate(:category, name: "测试") }
it "creates a blank slug, this is OK." do
category.slug.should be_blank
category.slug_for_url.should == "#{category.id}-category"
end
it "creates a localized slug if default locale is zh_CN" do
SiteSetting.default_locale = 'zh_CN'
category.slug.should_not be_blank
category.slug_for_url.should == "ce-shi"
end
end
describe 'slug would be a number' do
let(:category) { Fabricate(:category, name: "電車男 2") }
let(:category) { Fabricate(:category, name: "2") }
it 'creates a blank slug' do
category.slug.should be_blank

View File

@ -21,6 +21,14 @@ describe Topic do
Fabricate.build(:topic, title: title).slug.should == slug
end
let(:chinese_title) { "习近平:中企承建港口电站等助斯里兰卡发展" }
let(:chinese_slug) { "xi-jin-ping-zhong-qi-cheng-jian-gang-kou-dian-zhan-deng-zhu-si-li-lan-qia-fa-zhan" }
it "returns a symbolized slug for a chinese title" do
SiteSetting.default_locale = 'zh_CN'
Fabricate.build(:topic, title: chinese_title).slug.should == chinese_slug
end
it "returns 'topic' when the slug is empty (say, non-english chars)" do
Slug.expects(:for).with(title).returns("")
Fabricate.build(:topic, title: title).slug.should == "topic"