8 changed files with 93 additions and 12 deletions
@ -0,0 +1,29 @@ |
|||||||
|
module Squib |
||||||
|
module Args |
||||||
|
class SmartQuotes |
||||||
|
|
||||||
|
def process(str, opt) |
||||||
|
clean_opt = opt.to_s.downcase.strip |
||||||
|
return str if clean_opt.eql? 'dumb' |
||||||
|
if clean_opt.eql? 'smart' |
||||||
|
quotify(str) # default to UTF-8 |
||||||
|
else |
||||||
|
quotify(str, opt) # supplied quotes |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
# Convert regular quotes to smart quotes by looking for |
||||||
|
# a boundary between a word character (letters, numbers, underscore) |
||||||
|
# and a quote. Replaces with the UTF-8 equivalent. |
||||||
|
# :nodoc: |
||||||
|
# @api private |
||||||
|
def quotify(str, quote_chars = ["\u201C", "\u201D"]) |
||||||
|
left_regex = /(\")(\w)/ |
||||||
|
right_regex = /(\w)(\")/ |
||||||
|
str.gsub(left_regex, quote_chars[0] + '\2') |
||||||
|
.gsub(right_regex, '\1' + quote_chars[1]) |
||||||
|
end |
||||||
|
|
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
@ -0,0 +1,46 @@ |
|||||||
|
require 'spec_helper' |
||||||
|
require 'squib/args/smart_quotes' |
||||||
|
|
||||||
|
describe Squib::Args::SmartQuotes do |
||||||
|
|
||||||
|
it 'does nothing on a non-quoted string' do |
||||||
|
expect(subject.quotify('nothing')).to eq('nothing') |
||||||
|
end |
||||||
|
|
||||||
|
it 'left quotes at the beginning' do |
||||||
|
expect(subject.quotify('"foo')).to eq("\u201Cfoo") |
||||||
|
end |
||||||
|
|
||||||
|
it 'left quotes in the middle of the string' do |
||||||
|
expect(subject.quotify('hello "foo')).to eq("hello \u201Cfoo") |
||||||
|
end |
||||||
|
|
||||||
|
it 'right quotes at the end of a string' do |
||||||
|
expect(subject.quotify('foo"')).to eq("foo\u201D") |
||||||
|
end |
||||||
|
|
||||||
|
it 'handles the entire string quoted' do |
||||||
|
expect(subject.quotify('"foo"')).to eq("\u201Cfoo\u201D") |
||||||
|
end |
||||||
|
|
||||||
|
it "quotes in the middle of the string" do |
||||||
|
expect(subject.quotify('hello "foo" world')).to eq("hello \u201Cfoo\u201D world") |
||||||
|
end |
||||||
|
|
||||||
|
it "allows custom quotes for different character sets" do |
||||||
|
expect(subject.quotify('hello "foo" world', %w({ }))).to eq("hello {foo} world") |
||||||
|
end |
||||||
|
|
||||||
|
it "processes dumb quotes" do |
||||||
|
expect(subject.process('hello "foo" world', :dumb)).to eq("hello \"foo\" world") |
||||||
|
end |
||||||
|
|
||||||
|
it "processes smart quotes" do |
||||||
|
expect(subject.process('hello "foo" world', :smart)).to eq("hello \u201Cfoo\u201D world") |
||||||
|
end |
||||||
|
|
||||||
|
it "processes custom quotes" do |
||||||
|
expect(subject.process('hello "foo" world', %w({ }))).to eq("hello {foo} world") |
||||||
|
end |
||||||
|
|
||||||
|
end |
||||||
Loading…
Reference in new issue