Class: Typogrowth::Parser
- Inherits:
-
Object
- Object
- Typogrowth::Parser
- Defined in:
- lib/typogrowth.rb
Overview
Parses and corrects the typography in strings. It supports different language rules and easy user rules customization.
Instance Attribute Summary collapse
-
#yaml ⇒ Object
readonly
Returns the value of attribute yaml.
Class Method Summary collapse
-
.merge(custom) ⇒ Object
Recursively merges the initial settings with custom.
-
.parse(str, lang = :default) ⇒ Object
Inplace version of string typographying.
-
.parse!(str, lang = :default) ⇒ Object
Out-of-place version of
Stringtypographing.
Instance Attribute Details
#yaml ⇒ Object (readonly)
Returns the value of attribute yaml.
34 35 36 |
# File 'lib/typogrowth.rb', line 34 def yaml @yaml end |
Class Method Details
.merge(custom) ⇒ Object
Recursively merges the initial settings with custom.
To supply your own rules to processing:
-
create a
hashof additional rules in the same form as in the
standard typogrowth.yaml file shipped with a project
-
merge the hash with the standard one using this function
For instance, to add french rules one is to merge in the following yaml:
:quotes :
:punctuation :
:fr : "\\k<quote>\\k<punct>"
…
52 53 54 |
# File 'lib/typogrowth.rb', line 52 def self.merge custom instance.yaml.rmerge!(custom) end |
.parse(str, lang = :default) ⇒ Object
Inplace version of string typographying.
Retrieves the string and changes all the typewriters quotes (doubles and sigles), to inches, minutes, seconds, proper quotation signs.
While the input strings are e.g.
And God said "Baz heard "Bar" once" , and there was light.
That's a 6.3" man, he sees sunsets at 10°20'30" E.
It will produce:
And God said “Baz heard ‘Bar’ once,” and there was light.
That’s a 6.3″ man, he sees sunsets at 10°20′30″ E.
The utility also handles dashes as well.
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/typogrowth.rb', line 77 def self.parse str, lang = :default lang = lang.to_sym str.split(/\R{2,}/).map { |para| para.gsub(URI.regexp) { |m| "⚓#{Base64.encode64 m}⚓" } instance.yaml.each { |k, values| values.each { |k, v| if !!v[:re] v[lang] = v[:default] if (!v[lang] || v[lang].size.zero?) raise MalformedRulesFile.new "Malformed rules file (no subst for #{v})" \ if !v[lang] || v[lang].size.zero? substituted = !!v[:pattern] ? para.gsub!(/#{v[:re]}/) { |m| m.gsub(/#{v[:pattern]}/, v[lang].first) } : para.gsub!(/#{v[:re]}/, v[lang].first) # logger.warn "Unsafe substitutions were made to source:\n# ⇒ #{para}"\ # if v[:alert] && substituted if v[lang].size > 1 para.gsub!(/#{v[lang].first}/) { |m| prev = $` obsoletes = prev.count(v[lang].join) compliants = values[v[:compliant].to_sym][lang] || values[v[:compliant].to_sym][:default] obsoletes -= prev.count(compliants.join) \ if !!v[:compliant] !!v[:slave] ? obsoletes -= prev.count(v[:original]) + 1 : obsoletes += prev.count(v[:original]) v[lang][obsoletes % v[lang].size] } end end } } para }.join(%Q( )) .gsub(/⚓(.*)⚓/m) { |m| Base64.decode64 m } end |
.parse!(str, lang = :default) ⇒ Object
Out-of-place version of String typographing. See #parse!
118 119 120 |
# File 'lib/typogrowth.rb', line 118 def self.parse! str, lang = :default str.replace self.parse(str, lang) end |