Allow configuration of typographical replacements, e.g. smart quotes

If you're living in a non-UTF-8 character set, then you can configure what quotes are smartly changed.
If my regexes are messing things up, you can also turn this off with smart_quotes: false
Also updated docs.

This closes #50
This commit is contained in:
Andy Meneely
2015-04-07 21:21:00 -04:00
parent beb46a9e1c
commit 063d5156d6
13 changed files with 188 additions and 40 deletions
+50 -34
View File
@@ -8,17 +8,27 @@ module Squib
end
def process(str)
[
:left_curly,
:right_curly,
:apostraphize,
str = explicit_replacements(str)
str = smart_quotes(str) if @config['smart_quotes']
str
end
def explicit_replacements(str)
[ :left_curly, :right_curly, :apostraphize,
:ellipsificate, :em_dash, :en_dash ].each do |sym|
str = each_non_tag(str) do |token|
self.method(sym).call(token)
end
end
str
end
def smart_quotes(str)
[ :single_inside_double_quote,
:right_double_quote,
:left_double_quote,
:right_single_quote,
:left_single_quote,
:ellipsificate,
:em_dash,
:en_dash ].each do |sym|
:left_single_quote].each do |sym|
str = each_non_tag(str) do |token|
self.method(sym).call(token)
end
@@ -43,52 +53,58 @@ module Squib
# Straightforward replace
def left_curly(str)
str.gsub('``', "\u201C")
str.gsub('``', @config['ldquote'])
end
# Straightforward replace
def right_curly(str)
str.gsub(%{''}, "\u201D")
str.gsub(%{''}, @config['rdquote'])
end
# A quote between two letters is an apostraphe
def apostraphize(str)
str.gsub(/(\w)(\')(\w)/, '\1' + "\u2019" + '\3')
end
# Quote next to non-whitespace curls
def right_double_quote(str)
str.gsub(/(\S)(\")/, '\1' + "\u201D")
end
# Quote next to non-whitespace curls
def left_double_quote(str)
str.gsub(/(\")(\S)/, "\u201C" + '\2')
end
# Quote next to non-whitespace curls
def right_single_quote(str)
str.gsub(/(\S)(\')/, '\1' + "\u2019")
end
# Quote next to non-whitespace curls
def left_single_quote(str)
str.gsub(/(\')(\S)/, "\u2018" + '\2')
str.gsub(/(\w)(\')(\w)/, '\1' + @config['rsquote'] + '\3')
end
# Straightforward replace
def ellipsificate(str)
str.gsub('...', "\u2026")
str.gsub('...', @config['ellipsis'])
end
# Straightforward replace
def en_dash(str)
str.gsub('--', "\u2013")
str.gsub('--', @config['en_dash'])
end
# Straightforward replace
def em_dash(str)
str.gsub('---', "\u2014")
str.gsub('---', @config['em_dash'])
end
# Quote next to non-whitespace curls
def right_double_quote(str)
str.gsub(/(\S)(\")/, '\1' + @config['rdquote'])
end
# Quote next to non-whitespace curls
def left_double_quote(str)
str.gsub(/(\")(\S)/, @config['ldquote'] + '\2')
end
# Handle the cases where a double quote is next to a single quote
def single_inside_double_quote(str)
str.gsub(/(\")(\')(\S)/, @config['ldquote'] + @config['lsquote'] + '\3')
.gsub(/(\")(\')(\S)/, '\1' + @config['rsquote'] + @config['rdquote'])
end
# Quote next to non-whitespace curls
def right_single_quote(str)
str.gsub(/(\S)(\')/, '\1' + @config['rsquote'])
end
# Quote next to non-whitespace curls
def left_single_quote(str)
str.gsub(/(\')(\S)/, @config['lsquote'] + '\2')
end
end
+8
View File
@@ -80,6 +80,14 @@ module Squib
'hint' => :none,
'img_dir' => '.',
'progress_bar' => false,
'ldquote' => "\u201C", # UTF8 chars
'rdquote' => "\u201D",
'lsquote' => "\u2018",
'rsquote' => "\u2019",
'em_dash' => "\u2014",
'en_dash' => "\u2013",
'ellipsis' => "\u2026",
'smart_quotes' => true,
}
#Translate the hints to the methods.
+6 -1
View File
@@ -34,7 +34,7 @@ module Squib
# :nodoc:
# @api private
attr_reader :layout, :config
attr_reader :layout, :config, :quote_chars
attr_reader :dir, :prefix, :count_format
@@ -70,6 +70,7 @@ module Squib
@dir = SYSTEM_DEFAULTS[:dir]
@prefix = SYSTEM_DEFAULTS[:prefix]
@count_format = SYSTEM_DEFAULTS[:count_format]
@quote_chars = CONFIG_DEFAULTS.select {|k,v| %w(lsquote rsquote ldquote rdquote smart_quotes).include?(k) }
show_info(config, layout)
load_config(config)
@width = Args::UnitConversion.parse width, dpi
@@ -111,6 +112,10 @@ module Squib
@prefix = config['prefix']
@count_format = config['count_format']
@antialias = config['antialias']
@quote_chars ||= {}
%w(lsquote rsquote ldquote rdquote smart_quotes em_dash en_dash ellipsis).each do |key|
@quote_chars[key] = config[key]
end
end
end
+1 -1
View File
@@ -156,7 +156,7 @@ module Squib
layout.font_description = font_desc
layout.text = str
if markup
str = Args::Typographer.new(@deck.config).process(layout.text)
str = Args::Typographer.new(@deck.quote_chars).process(layout.text)
layout.markup = str
end
+13
View File
@@ -25,3 +25,16 @@
# Use a SVG cairo back end, instead of an in-memory buffer
# backend: :memory # default
# backend: :svg # can create scalable pdfs, but rendering done at the printer level is not as good as Cairo.
# Configure what text markup uses replace characters
# Below are the defaults
# lsquote: "\u2018" #note that Yaml wants double quotes here to use escape chars
# rsquote: "\u2019"
# ldquote: "\u201C"
# rdquote: "\u201D"
# em_dash: "\u2014"
# en_dash: "\u2013"
# ellipsis: "\u2026"
# We can also disallow smart quotes and only allow explicit replacements with ``LaTeX-style'' quotes.
# smart_quotes: false