Created
February 24, 2009 14:09
-
-
Save madx/69578 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Xup#:nodoc: | |
module Helpers#:nodoc: | |
module Typography | |
def parse_typography | |
# Unbreakable spaces before double punctuations | |
parse_unbreakable_punctuation ':' | |
parse_unbreakable_punctuation ';' | |
parse_unbreakable_punctuation '!' | |
parse_unbreakable_punctuation '?' | |
# Ellipsis | |
@source.gsub! /\.\.\./, '…' | |
# Quotes | |
@source.gsub!(Regexp.new("\"(#{not_char('"')})\"")) do | |
contents = $~[1].strip | |
"« #{contents} »" | |
end | |
@source.gsub!(Regexp.new("([a-zA-Z0-9])'([a-zA-Z0-9])"), '\1’\2' ) | |
# Dashes | |
@source.gsub!(/([^-])--([^-])/, '\1–\2') | |
@source.gsub!(/---/, '—') | |
# Numbers | |
@source.gsub!(/( +)(\d+)/) do | |
char = $~[1] | |
number = $~[2].to_s.reverse.split('') | |
out_num = char | |
(number.length - 1).downto(0) do |i| | |
if i % 3 == 2 && i != 0 && i != number.length - 1 then out_num += ' ' end | |
out_num += number[i] | |
end | |
out_num | |
end | |
@source.gsub!( /(\d)x(\d)/, '\1×\2') | |
@source.gsub!( /(\d)\/(\d)/, '\1÷\2') | |
end | |
private | |
def parse_unbreakable_punctuation(char) | |
@source.gsub! punctuation_regex_for(char), ' \1' | |
end | |
def punctuation_regex_for(char) | |
char = Regexp.escape(char) | |
Regexp.new("(?: +)(#{char})") | |
end | |
end | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment