cleanup module definitions
parent
77a694f797
commit
21bee3a3cb
|
|
@ -1,12 +1,7 @@
|
|||
module Squib
|
||||
# @api private
|
||||
module Args
|
||||
module ColorValidator
|
||||
module Squib::Args::ColorValidator
|
||||
|
||||
def colorify(color, custom_colors = {})
|
||||
custom_colors[color.to_s] || color.to_s
|
||||
end
|
||||
|
||||
end
|
||||
def colorify(color, custom_colors = {})
|
||||
custom_colors[color.to_s] || color.to_s
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,35 +1,35 @@
|
|||
require_relative 'arg_loader'
|
||||
|
||||
module Squib
|
||||
# @api private
|
||||
module Args
|
||||
module Squib::Args
|
||||
module_function def extract_coord(opts, deck)
|
||||
Box.new.extract!(opts, deck)
|
||||
end
|
||||
|
||||
class Coords
|
||||
include ArgLoader
|
||||
class Coords
|
||||
include ArgLoader
|
||||
|
||||
def self.parameters
|
||||
{ x: 0, y: 0,
|
||||
x1: 100, y1: 100,
|
||||
x2: 150, y2: 150,
|
||||
x3: 100, y3: 150,
|
||||
cx1: 0 , cy1: 0,
|
||||
cx2: 0 , cy2: 0,
|
||||
inner_radius: 50, outer_radius: 100,
|
||||
radius: 100,
|
||||
n: 5,
|
||||
arc_start: 0, arc_end: 2 * Math::PI, arc_direction: :clockwise, arc_close: false,
|
||||
}
|
||||
end
|
||||
def self.parameters
|
||||
{ x: 0, y: 0,
|
||||
x1: 100, y1: 100,
|
||||
x2: 150, y2: 150,
|
||||
x3: 100, y3: 150,
|
||||
cx1: 0 , cy1: 0,
|
||||
cx2: 0 , cy2: 0,
|
||||
inner_radius: 50, outer_radius: 100,
|
||||
radius: 100,
|
||||
n: 5,
|
||||
arc_start: 0, arc_end: 2 * Math::PI, arc_direction: :clockwise, arc_close: false,
|
||||
}
|
||||
end
|
||||
|
||||
def self.expanding_parameters
|
||||
parameters.keys # all of them
|
||||
end
|
||||
|
||||
def self.params_with_units
|
||||
parameters.keys # all of them
|
||||
end
|
||||
def self.expanding_parameters
|
||||
parameters.keys # all of them
|
||||
end
|
||||
|
||||
def self.params_with_units
|
||||
parameters.keys # all of them
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,25 +1,22 @@
|
|||
require 'csv'
|
||||
|
||||
module Squib
|
||||
# @api private
|
||||
module Args
|
||||
class CSV_Opts
|
||||
|
||||
def initialize(opts)
|
||||
opts = opts.keep_if { |k, _v| CSV::DEFAULT_OPTIONS.key? k}
|
||||
@hash = CSV::DEFAULT_OPTIONS.merge(opts).merge(required)
|
||||
end
|
||||
|
||||
def to_hash
|
||||
@hash
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def required
|
||||
{ headers: true, converters: :numeric }
|
||||
end
|
||||
module Squib::Args
|
||||
class CSV_Opts
|
||||
|
||||
def initialize(opts)
|
||||
opts = opts.keep_if { |k, _v| CSV::DEFAULT_OPTIONS.key? k}
|
||||
@hash = CSV::DEFAULT_OPTIONS.merge(opts).merge(required)
|
||||
end
|
||||
|
||||
def to_hash
|
||||
@hash
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def required
|
||||
{ headers: true, converters: :numeric }
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,16 +1,11 @@
|
|||
module Squib
|
||||
# @api private
|
||||
module Args
|
||||
module DirValidator
|
||||
|
||||
def ensure_dir_created(dir)
|
||||
unless Dir.exists?(dir)
|
||||
Squib.logger.warn "Dir '#{dir}' does not exist, creating it."
|
||||
FileUtils.mkdir_p dir
|
||||
end
|
||||
return dir
|
||||
end
|
||||
module Squib::Args::DirValidator
|
||||
|
||||
def ensure_dir_created(dir)
|
||||
unless Dir.exists?(dir)
|
||||
Squib.logger.warn "Dir '#{dir}' does not exist, creating it."
|
||||
FileUtils.mkdir_p dir
|
||||
end
|
||||
return dir
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,25 +1,22 @@
|
|||
require_relative 'arg_loader'
|
||||
|
||||
module Squib
|
||||
# @api private
|
||||
module Args
|
||||
module Squib::Args
|
||||
|
||||
class EmbedAdjust
|
||||
include ArgLoader
|
||||
class EmbedAdjust
|
||||
include ArgLoader
|
||||
|
||||
def self.parameters
|
||||
{ dx: 0, dy: 0 }
|
||||
end
|
||||
def self.parameters
|
||||
{ dx: 0, dy: 0 }
|
||||
end
|
||||
|
||||
def self.expanding_parameters
|
||||
parameters.keys # all of them
|
||||
end
|
||||
|
||||
def self.params_with_units
|
||||
parameters.keys # all of them
|
||||
end
|
||||
def self.expanding_parameters
|
||||
parameters.keys # all of them
|
||||
end
|
||||
|
||||
def self.params_with_units
|
||||
parameters.keys # all of them
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,17 +1,12 @@
|
|||
require_relative 'arg_loader'
|
||||
|
||||
module Squib
|
||||
# @api private
|
||||
module Args
|
||||
|
||||
class EmbedKey
|
||||
|
||||
# Validate the embed lookup key
|
||||
def validate_key(str)
|
||||
str.to_s
|
||||
end
|
||||
module Squib::Args
|
||||
class EmbedKey
|
||||
|
||||
# Validate the embed lookup key
|
||||
def validate_key(str)
|
||||
str.to_s
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,37 +1,34 @@
|
|||
require 'cairo'
|
||||
|
||||
module Squib
|
||||
# @api private
|
||||
module Args
|
||||
module Squib::Args
|
||||
|
||||
class HandSpecial
|
||||
include ArgLoader
|
||||
class HandSpecial
|
||||
include ArgLoader
|
||||
|
||||
def initialize(card_height)
|
||||
@card_height = card_height
|
||||
end
|
||||
def initialize(card_height)
|
||||
@card_height = card_height
|
||||
end
|
||||
|
||||
def self.parameters
|
||||
{
|
||||
angle_range: (Math::PI / -4.0)..(Math::PI / 4),
|
||||
radius: :auto
|
||||
}
|
||||
end
|
||||
def self.parameters
|
||||
{
|
||||
angle_range: (Math::PI / -4.0)..(Math::PI / 4),
|
||||
radius: :auto
|
||||
}
|
||||
end
|
||||
|
||||
def self.expanding_parameters
|
||||
[] # none of them
|
||||
end
|
||||
def self.expanding_parameters
|
||||
[] # none of them
|
||||
end
|
||||
|
||||
def self.params_with_units
|
||||
[ :radius ]
|
||||
end
|
||||
|
||||
def validate_radius(arg)
|
||||
return 0.3 * @card_height if arg.to_s.downcase.strip == 'auto'
|
||||
arg
|
||||
end
|
||||
def self.params_with_units
|
||||
[ :radius ]
|
||||
end
|
||||
|
||||
def validate_radius(arg)
|
||||
return 0.3 * @card_height if arg.to_s.downcase.strip == 'auto'
|
||||
arg
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,40 +1,37 @@
|
|||
require_relative 'arg_loader'
|
||||
|
||||
module Squib
|
||||
# @api private
|
||||
module Args
|
||||
module Squib::Args
|
||||
|
||||
class Import
|
||||
include ArgLoader
|
||||
class Import
|
||||
include ArgLoader
|
||||
|
||||
def self.parameters
|
||||
{ strip: true,
|
||||
explode: 'qty'
|
||||
}
|
||||
end
|
||||
def self.parameters
|
||||
{ strip: true,
|
||||
explode: 'qty'
|
||||
}
|
||||
end
|
||||
|
||||
def self.expanding_parameters
|
||||
[] # none of them
|
||||
end
|
||||
def self.expanding_parameters
|
||||
[] # none of them
|
||||
end
|
||||
|
||||
def self.params_with_units
|
||||
[] # none of them
|
||||
end
|
||||
def self.params_with_units
|
||||
[] # none of them
|
||||
end
|
||||
|
||||
def validate_strip(arg)
|
||||
raise 'Strip must be true or false' unless arg == true || arg == false
|
||||
arg
|
||||
end
|
||||
def validate_strip(arg)
|
||||
raise 'Strip must be true or false' unless arg == true || arg == false
|
||||
arg
|
||||
end
|
||||
|
||||
def validate_explode(arg)
|
||||
arg
|
||||
end
|
||||
|
||||
def strip?
|
||||
strip
|
||||
end
|
||||
def validate_explode(arg)
|
||||
arg
|
||||
end
|
||||
|
||||
def strip?
|
||||
strip
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ module Squib::Args
|
|||
module_function def extract_paint(opts, deck)
|
||||
Paint.new(deck.custom_colors).extract!(opts, deck)
|
||||
end
|
||||
|
||||
class Paint
|
||||
include ArgLoader
|
||||
include ColorValidator
|
||||
|
|
|
|||
|
|
@ -1,118 +1,114 @@
|
|||
require_relative '../constants'
|
||||
require_relative 'arg_loader'
|
||||
|
||||
module Squib
|
||||
# @api private
|
||||
module Args
|
||||
module Squib::Args
|
||||
|
||||
class Paragraph
|
||||
include ArgLoader
|
||||
class Paragraph
|
||||
include ArgLoader
|
||||
|
||||
def self.parameters
|
||||
{ align: :left,
|
||||
str: 'Hello, World!',
|
||||
font: :use_set,
|
||||
font_size: nil,
|
||||
markup: false,
|
||||
justify: false,
|
||||
wrap: true,
|
||||
ellipsize: :end,
|
||||
spacing: nil,
|
||||
valign: :top,
|
||||
hint: :off
|
||||
}
|
||||
end
|
||||
|
||||
def self.parameters
|
||||
{ align: :left,
|
||||
str: 'Hello, World!',
|
||||
font: :use_set,
|
||||
font_size: nil,
|
||||
markup: false,
|
||||
justify: false,
|
||||
wrap: true,
|
||||
ellipsize: :end,
|
||||
spacing: nil,
|
||||
valign: :top,
|
||||
hint: :off
|
||||
}
|
||||
def self.expanding_parameters
|
||||
parameters.keys # all of them
|
||||
end
|
||||
|
||||
def self.params_with_units
|
||||
[] # none of them
|
||||
end
|
||||
|
||||
def initialize(deck_font)
|
||||
@deck_font = deck_font
|
||||
end
|
||||
|
||||
def validate_str(arg, _i)
|
||||
arg.to_s
|
||||
end
|
||||
|
||||
def validate_font(arg, _i)
|
||||
arg = @deck_font if arg == :use_set
|
||||
arg = Squib::DEFAULT_FONT if arg == :default
|
||||
arg
|
||||
end
|
||||
|
||||
def validate_align(arg, _i)
|
||||
case arg.to_s.downcase.strip
|
||||
when 'left'
|
||||
Pango::Alignment::LEFT
|
||||
when 'right'
|
||||
Pango::Alignment::RIGHT
|
||||
when 'center'
|
||||
Pango::Alignment::CENTER
|
||||
else
|
||||
raise ArgumentError, 'align must be one of: center, left, right'
|
||||
end
|
||||
end
|
||||
|
||||
def self.expanding_parameters
|
||||
parameters.keys # all of them
|
||||
def validate_wrap(arg, _i)
|
||||
case arg.to_s.downcase.strip
|
||||
when 'word'
|
||||
Pango::WrapMode::WORD
|
||||
when 'char', 'false'
|
||||
Pango::WrapMode::CHAR
|
||||
when 'word_char', 'true'
|
||||
Pango::WrapMode::WORD_CHAR
|
||||
else
|
||||
raise ArgumentError, 'wrap must be one of: word, char, word_char, true, or false'
|
||||
end
|
||||
end
|
||||
|
||||
def self.params_with_units
|
||||
[] # none of them
|
||||
def validate_ellipsize(arg, _i)
|
||||
case arg.to_s.downcase.strip
|
||||
when 'none', 'false'
|
||||
Pango::EllipsizeMode::NONE
|
||||
when 'start'
|
||||
Pango::EllipsizeMode::START
|
||||
when 'middle'
|
||||
Pango::EllipsizeMode::MIDDLE
|
||||
when 'end', 'true'
|
||||
Pango::EllipsizeMode::END
|
||||
when 'autoscale'
|
||||
:autoscale
|
||||
else
|
||||
raise ArgumentError, 'ellipsize must be one of: none, start, middle, end, true, false or autoscale'
|
||||
end
|
||||
end
|
||||
|
||||
def initialize(deck_font)
|
||||
@deck_font = deck_font
|
||||
end
|
||||
|
||||
def validate_str(arg, _i)
|
||||
arg.to_s
|
||||
end
|
||||
|
||||
def validate_font(arg, _i)
|
||||
arg = @deck_font if arg == :use_set
|
||||
arg = DEFAULT_FONT if arg == :default
|
||||
def validate_justify(arg, _i)
|
||||
case arg
|
||||
when nil, true, false
|
||||
arg
|
||||
else
|
||||
raise ArgumentError, 'justify must be one of: nil, true, or false'
|
||||
end
|
||||
end
|
||||
|
||||
def validate_align(arg, _i)
|
||||
case arg.to_s.downcase.strip
|
||||
when 'left'
|
||||
Pango::Alignment::LEFT
|
||||
when 'right'
|
||||
Pango::Alignment::RIGHT
|
||||
when 'center'
|
||||
Pango::Alignment::CENTER
|
||||
else
|
||||
raise ArgumentError, 'align must be one of: center, left, right'
|
||||
end
|
||||
def validate_spacing(arg, _i)
|
||||
return nil if arg.nil?
|
||||
raise ArgumentError, 'spacing must be a number or nil' unless arg.respond_to? :to_f
|
||||
arg.to_f * Pango::SCALE
|
||||
end
|
||||
|
||||
def validate_valign(arg, _i)
|
||||
if %w(top middle bottom).include? arg.to_s.downcase
|
||||
arg.to_s.downcase
|
||||
else
|
||||
raise ArgumentError, 'valign must be one of: top, middle, bottom'
|
||||
end
|
||||
|
||||
def validate_wrap(arg, _i)
|
||||
case arg.to_s.downcase.strip
|
||||
when 'word'
|
||||
Pango::WrapMode::WORD
|
||||
when 'char', 'false'
|
||||
Pango::WrapMode::CHAR
|
||||
when 'word_char', 'true'
|
||||
Pango::WrapMode::WORD_CHAR
|
||||
else
|
||||
raise ArgumentError, 'wrap must be one of: word, char, word_char, true, or false'
|
||||
end
|
||||
end
|
||||
|
||||
def validate_ellipsize(arg, _i)
|
||||
case arg.to_s.downcase.strip
|
||||
when 'none', 'false'
|
||||
Pango::EllipsizeMode::NONE
|
||||
when 'start'
|
||||
Pango::EllipsizeMode::START
|
||||
when 'middle'
|
||||
Pango::EllipsizeMode::MIDDLE
|
||||
when 'end', 'true'
|
||||
Pango::EllipsizeMode::END
|
||||
when 'autoscale'
|
||||
:autoscale
|
||||
else
|
||||
raise ArgumentError, 'ellipsize must be one of: none, start, middle, end, true, false or autoscale'
|
||||
end
|
||||
end
|
||||
|
||||
def validate_justify(arg, _i)
|
||||
case arg
|
||||
when nil, true, false
|
||||
arg
|
||||
else
|
||||
raise ArgumentError, 'justify must be one of: nil, true, or false'
|
||||
end
|
||||
end
|
||||
|
||||
def validate_spacing(arg, _i)
|
||||
return nil if arg.nil?
|
||||
raise ArgumentError, 'spacing must be a number or nil' unless arg.respond_to? :to_f
|
||||
arg.to_f * Pango::SCALE
|
||||
end
|
||||
|
||||
def validate_valign(arg, _i)
|
||||
if %w(top middle bottom).include? arg.to_s.downcase
|
||||
arg.to_s.downcase
|
||||
else
|
||||
raise ArgumentError, 'valign must be one of: top, middle, bottom'
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,63 +1,60 @@
|
|||
require_relative 'arg_loader'
|
||||
require_relative 'dir_validator'
|
||||
|
||||
module Squib
|
||||
# @api private
|
||||
module Args
|
||||
class SaveBatch
|
||||
include ArgLoader
|
||||
include DirValidator
|
||||
|
||||
def initialize
|
||||
end
|
||||
|
||||
def self.parameters
|
||||
{
|
||||
angle: 0,
|
||||
count_format: '%02d',
|
||||
dir: '_output',
|
||||
prefix: 'card_',
|
||||
rotate: false,
|
||||
trim_radius: 0,
|
||||
trim: 0,
|
||||
}
|
||||
end
|
||||
|
||||
def self.expanding_parameters
|
||||
self.parameters.keys # all of them
|
||||
end
|
||||
|
||||
def self.params_with_units
|
||||
[:trim, :trim_radius]
|
||||
end
|
||||
|
||||
def validate_dir(arg, _i)
|
||||
ensure_dir_created(arg)
|
||||
end
|
||||
|
||||
def validate_rotate(arg, i)
|
||||
case arg
|
||||
when true, :clockwise
|
||||
angle[i] = 0.5 * Math::PI
|
||||
return true
|
||||
when :counterclockwise
|
||||
angle[i] = 1.5 * Math::PI
|
||||
return true
|
||||
when false
|
||||
false
|
||||
else
|
||||
raise 'invalid option to rotate: only [true, false, :clockwise, :counterclockwise]'
|
||||
end
|
||||
end
|
||||
|
||||
def full_filename(i)
|
||||
"#{dir[i]}/#{prefix[i]}#{count_format[i] % i}.png"
|
||||
end
|
||||
|
||||
def summary
|
||||
"#{dir[0]}/#{prefix[0]}_*"
|
||||
end
|
||||
module Squib::Args
|
||||
class SaveBatch
|
||||
include ArgLoader
|
||||
include DirValidator
|
||||
|
||||
def initialize
|
||||
end
|
||||
|
||||
def self.parameters
|
||||
{
|
||||
angle: 0,
|
||||
count_format: '%02d',
|
||||
dir: '_output',
|
||||
prefix: 'card_',
|
||||
rotate: false,
|
||||
trim_radius: 0,
|
||||
trim: 0,
|
||||
}
|
||||
end
|
||||
|
||||
def self.expanding_parameters
|
||||
self.parameters.keys # all of them
|
||||
end
|
||||
|
||||
def self.params_with_units
|
||||
[:trim, :trim_radius]
|
||||
end
|
||||
|
||||
def validate_dir(arg, _i)
|
||||
ensure_dir_created(arg)
|
||||
end
|
||||
|
||||
def validate_rotate(arg, i)
|
||||
case arg
|
||||
when true, :clockwise
|
||||
angle[i] = 0.5 * Math::PI
|
||||
return true
|
||||
when :counterclockwise
|
||||
angle[i] = 1.5 * Math::PI
|
||||
return true
|
||||
when false
|
||||
false
|
||||
else
|
||||
raise 'invalid option to rotate: only [true, false, :clockwise, :counterclockwise]'
|
||||
end
|
||||
end
|
||||
|
||||
def full_filename(i)
|
||||
"#{dir[i]}/#{prefix[i]}#{count_format[i] % i}.png"
|
||||
end
|
||||
|
||||
def summary
|
||||
"#{dir[0]}/#{prefix[0]}_*"
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,44 +1,41 @@
|
|||
require_relative 'arg_loader'
|
||||
|
||||
module Squib
|
||||
# @api private
|
||||
module Args
|
||||
class SprueFile
|
||||
include ArgLoader
|
||||
module Squib::Args
|
||||
class SprueFile
|
||||
include ArgLoader
|
||||
|
||||
def initialize(dsl_method_default = {})
|
||||
@dsl_method_default = dsl_method_default
|
||||
end
|
||||
def initialize(dsl_method_default = {})
|
||||
@dsl_method_default = dsl_method_default
|
||||
end
|
||||
|
||||
def self.parameters
|
||||
{
|
||||
sprue: nil
|
||||
}
|
||||
end
|
||||
def self.parameters
|
||||
{
|
||||
sprue: nil
|
||||
}
|
||||
end
|
||||
|
||||
def self.expanding_parameters
|
||||
[]
|
||||
end
|
||||
def self.expanding_parameters
|
||||
[]
|
||||
end
|
||||
|
||||
def self.params_with_units
|
||||
[] # none of them
|
||||
end
|
||||
def self.params_with_units
|
||||
[] # none of them
|
||||
end
|
||||
|
||||
def validate_template_file(arg)
|
||||
return nil if arg.nil?
|
||||
def validate_template_file(arg)
|
||||
return nil if arg.nil?
|
||||
|
||||
thefile = File.exist?(arg) ? arg : builtin(arg)
|
||||
raise "File #{File.expand_path(arg)} does not exist!" unless
|
||||
File.exist? thefile
|
||||
thefile = File.exist?(arg) ? arg : builtin(arg)
|
||||
raise "File #{File.expand_path(arg)} does not exist!" unless
|
||||
File.exist? thefile
|
||||
|
||||
File.expand_path(thefile)
|
||||
end
|
||||
File.expand_path(thefile)
|
||||
end
|
||||
|
||||
private
|
||||
private
|
||||
|
||||
def builtin(file)
|
||||
"#{File.dirname(__FILE__)}/../builtin/sprues/#{file}"
|
||||
end
|
||||
def builtin(file)
|
||||
"#{File.dirname(__FILE__)}/../builtin/sprues/#{file}"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,119 +1,115 @@
|
|||
require_relative '../constants'
|
||||
|
||||
module Squib
|
||||
# @api private
|
||||
module Args
|
||||
# Internal class for handling arguments
|
||||
# @api private
|
||||
class Typographer
|
||||
module Squib::Args
|
||||
# Internal class for handling arguments
|
||||
class Typographer
|
||||
|
||||
def initialize(config = Conf::DEFAULTS)
|
||||
%w(lsquote ldquote rsquote rdquote smart_quotes
|
||||
em_dash en_dash ellipsis).each do |var|
|
||||
instance_variable_set("@#{var}", config[var])
|
||||
end
|
||||
def initialize(config = Squib::Conf::DEFAULTS)
|
||||
%w(lsquote ldquote rsquote rdquote smart_quotes
|
||||
em_dash en_dash ellipsis).each do |var|
|
||||
instance_variable_set("@#{var}", config[var])
|
||||
end
|
||||
|
||||
def process(str)
|
||||
str = explicit_replacements(str.to_s)
|
||||
str = smart_quotes(str) if @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].each do |sym|
|
||||
str = each_non_tag(str) do |token|
|
||||
self.method(sym).call(token)
|
||||
end
|
||||
end
|
||||
str
|
||||
end
|
||||
|
||||
# Iterate over each non-tag for processing
|
||||
# Allows us to ignore anything inside < and >
|
||||
def each_non_tag(str)
|
||||
full_str = ''
|
||||
tag_delimit = /(<(?:(?!<).)*>)/ # use non-capturing group w/ negative lookahead
|
||||
str.split(tag_delimit).each do |token|
|
||||
if token.start_with? '<'
|
||||
full_str << token # don't process tags
|
||||
else
|
||||
full_str << yield(token)
|
||||
end
|
||||
end
|
||||
return full_str
|
||||
end
|
||||
|
||||
# Straightforward replace
|
||||
def left_curly(str)
|
||||
str.gsub('``', @ldquote)
|
||||
end
|
||||
|
||||
# Straightforward replace
|
||||
def right_curly(str)
|
||||
str.gsub(%{''}, @rdquote)
|
||||
end
|
||||
|
||||
# A quote between two letters is an apostraphe
|
||||
def apostraphize(str)
|
||||
str.gsub(/(\w)(\')(\w)/, '\1' + @rsquote + '\3')
|
||||
end
|
||||
|
||||
# Straightforward replace
|
||||
def ellipsificate(str)
|
||||
str.gsub('...', @ellipsis)
|
||||
end
|
||||
|
||||
# Straightforward replace
|
||||
def en_dash(str)
|
||||
str.gsub('--', @en_dash)
|
||||
end
|
||||
|
||||
# Straightforward replace
|
||||
def em_dash(str)
|
||||
str.gsub('---', @em_dash)
|
||||
end
|
||||
|
||||
# Quote next to non-whitespace curls
|
||||
def right_double_quote(str)
|
||||
str.gsub(/(\S)(\")/, '\1' + @rdquote)
|
||||
end
|
||||
|
||||
# Quote next to non-whitespace curls
|
||||
def left_double_quote(str)
|
||||
str.gsub(/(\")(\S)/, @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)/, @ldquote + @lsquote + '\3')
|
||||
.gsub(/(\")(\')(\S)/, '\1' + @rsquote + @rdquote)
|
||||
end
|
||||
|
||||
# Quote next to non-whitespace curls
|
||||
def right_single_quote(str)
|
||||
str.gsub(/(\S)(\')/, '\1' + @rsquote)
|
||||
end
|
||||
|
||||
# Quote next to non-whitespace curls
|
||||
def left_single_quote(str)
|
||||
str.gsub(/(\')(\S)/, @lsquote + '\2')
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
def process(str)
|
||||
str = explicit_replacements(str.to_s)
|
||||
str = smart_quotes(str) if @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].each do |sym|
|
||||
str = each_non_tag(str) do |token|
|
||||
self.method(sym).call(token)
|
||||
end
|
||||
end
|
||||
str
|
||||
end
|
||||
|
||||
# Iterate over each non-tag for processing
|
||||
# Allows us to ignore anything inside < and >
|
||||
def each_non_tag(str)
|
||||
full_str = ''
|
||||
tag_delimit = /(<(?:(?!<).)*>)/ # use non-capturing group w/ negative lookahead
|
||||
str.split(tag_delimit).each do |token|
|
||||
if token.start_with? '<'
|
||||
full_str << token # don't process tags
|
||||
else
|
||||
full_str << yield(token)
|
||||
end
|
||||
end
|
||||
return full_str
|
||||
end
|
||||
|
||||
# Straightforward replace
|
||||
def left_curly(str)
|
||||
str.gsub('``', @ldquote)
|
||||
end
|
||||
|
||||
# Straightforward replace
|
||||
def right_curly(str)
|
||||
str.gsub(%{''}, @rdquote)
|
||||
end
|
||||
|
||||
# A quote between two letters is an apostraphe
|
||||
def apostraphize(str)
|
||||
str.gsub(/(\w)(\')(\w)/, '\1' + @rsquote + '\3')
|
||||
end
|
||||
|
||||
# Straightforward replace
|
||||
def ellipsificate(str)
|
||||
str.gsub('...', @ellipsis)
|
||||
end
|
||||
|
||||
# Straightforward replace
|
||||
def en_dash(str)
|
||||
str.gsub('--', @en_dash)
|
||||
end
|
||||
|
||||
# Straightforward replace
|
||||
def em_dash(str)
|
||||
str.gsub('---', @em_dash)
|
||||
end
|
||||
|
||||
# Quote next to non-whitespace curls
|
||||
def right_double_quote(str)
|
||||
str.gsub(/(\S)(\")/, '\1' + @rdquote)
|
||||
end
|
||||
|
||||
# Quote next to non-whitespace curls
|
||||
def left_double_quote(str)
|
||||
str.gsub(/(\")(\S)/, @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)/, @ldquote + @lsquote + '\3')
|
||||
.gsub(/(\")(\')(\S)/, '\1' + @rsquote + @rdquote)
|
||||
end
|
||||
|
||||
# Quote next to non-whitespace curls
|
||||
def right_single_quote(str)
|
||||
str.gsub(/(\S)(\')/, '\1' + @rsquote)
|
||||
end
|
||||
|
||||
# Quote next to non-whitespace curls
|
||||
def left_single_quote(str)
|
||||
str.gsub(/(\')(\S)/, @lsquote + '\2')
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Reference in New Issue