Module: Truty::General
- Included in:
- Truty
- Defined in:
- lib/truty/general.rb
Overview
Module with general typography fixes for all the languages. The fixes in here should not be language specific.
Instance Method Summary collapse
-
#brackets_whitespace(input) ⇒ String
Fixes spaces around various brackets.
-
#ellipsis(input) ⇒ String
Converts three or more periods (dots, points) into ellipsis.
-
#emdash(input) ⇒ String
Adds thin spaces to emdash from both sides.
-
#endash(input) ⇒ String
Adds non-breaking space before endash.
-
#fix(input, lang = :general) ⇒ String
Improves the typography of the large plain text with paragraphs.
-
#general(input) ⇒ String
Improves basic non-language specific issues in typography.
-
#multicharacters(input) ⇒ String
Tries to substitute more characters which should be one, like “©”, “™”, etc.
-
#multiplication_sign(input) ⇒ String
Adds multiplication sign between numbers instead of X.
-
#name_abbreviations(input) ⇒ String
Adds non-breaking space after one-character name abbreviation (“A.”, “J.”, etc.).
-
#punctuation_whitespace(input) ⇒ String
Fixes spaces around punctuation.
-
#quotes(input, type = '"“”„', start_quotes = "“", end_quotes = "”") ⇒ String
Converts quotes to the typograhic ones.
-
#soft_hyphens(input, lang = "en_us", left = 2, right = 2, char = "") ⇒ String
Adds soft hyphens to the input.
-
#space_between_numbers(input) ⇒ String
Adds thin non-breaking space between numbers.
-
#trailing_spaces(input) ⇒ String
Removes whitespace after the end of the paragraph.
-
#units(input) ⇒ String
Fixes non-breaking spaces betwwen number and unit.
-
#widows(input) ⇒ String
Adds non-breaking space before the last word in the paragraph.
Instance Method Details
#brackets_whitespace(input) ⇒ String
Fixes spaces around various brackets.
115 116 117 118 119 120 |
# File 'lib/truty/general.rb', line 115 def brackets_whitespace(input) output = input.gsub(/([\(\[\{])\s*/, '\1') output = output.gsub(/\s*([\]\)\}])/, '\1') output = output.gsub(/\s+([\(\[\{])\s*/, ' \1') output = output.gsub(/\s*([\]\)\}])\s+/, '\1 ') end |
#ellipsis(input) ⇒ String
Converts three or more periods (dots, points) into ellipsis.
41 42 43 |
# File 'lib/truty/general.rb', line 41 def ellipsis(input) input.gsub(/\.{3,}/, "…") end |
#emdash(input) ⇒ String
Adds thin spaces to emdash from both sides. Also converts two or three hyphens to emdash.
49 50 51 |
# File 'lib/truty/general.rb', line 49 def emdash(input) input.gsub(/\s+(—|-{2,3})\s+/, " — ") end |
#endash(input) ⇒ String
Adds non-breaking space before endash.
57 58 59 |
# File 'lib/truty/general.rb', line 57 def endash(input) input.gsub(/\s+(–|-)\s+/, " – ") end |
#fix(input, lang = :general) ⇒ String
Improves the typography of the large plain text with paragraphs. Adds non-breaking spaces, hyphenation, fixes dashes, etc.
13 14 15 16 17 18 |
# File 'lib/truty/general.rb', line 13 def fix(input, lang = :general) if not Truty.respond_to? lang then lang = :general end input.split("\n").collect { |p| Truty.send lang, p }.join("\n") end |
#general(input) ⇒ String
Improves basic non-language specific issues in typography.
24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/truty/general.rb', line 24 def general(input) input = ellipsis(input) input = multicharacters(input) input = brackets_whitespace(input) input = emdash(input) input = endash(input) input = name_abbreviations(input) input = multiplication_sign(input) input = space_between_numbers(input) input = units(input) input = widows(input) end |
#multicharacters(input) ⇒ String
Tries to substitute more characters which should be one, like “©”, “™”, etc.
126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/truty/general.rb', line 126 def multicharacters(input) output = input.gsub(/\([Cc]\)/, "©") output = output.gsub(/\([Pp]\)/, "℗") output = output.gsub(/\([Rr]\)/, "®") output = output.gsub(/\((SM|sm|Sm)\)/, "℠") output = output.gsub(/\((TM|tm|Tm)\)/, "™") output = output.gsub(/\+-/, "±") output = output.gsub(/-\+/, "∓") output = output.gsub(/N[oO]\.?\s*(\d+)/, '№\1') output = output.gsub(/°C/, '℃') output = output.gsub(/°F/, '℉') end |
#multiplication_sign(input) ⇒ String
Adds multiplication sign between numbers instead of X.
98 99 100 101 |
# File 'lib/truty/general.rb', line 98 def multiplication_sign(input) output = input.gsub(/(\d+)\s{0,1}[Xx]\s{0,1}(\d+)/, '\1 × \2') output = output.gsub(/(\d+)[Xx]/, '\1×') end |
#name_abbreviations(input) ⇒ String
Adds non-breaking space after one-character name abbreviation (“A.”, “J.”, etc.)
176 177 178 |
# File 'lib/truty/general.rb', line 176 def name_abbreviations(input) input.gsub(/(\s|^)(([A-Z]\.\s+)+)/) { $1 + $2.gsub(/ +/, " ")} end |
#punctuation_whitespace(input) ⇒ String
Fixes spaces around punctuation.
143 144 145 |
# File 'lib/truty/general.rb', line 143 def punctuation_whitespace(input) input.gsub(/\s*([\!\?\.,;:…]+)\s*/, '\1 ') end |
#quotes(input, type = '"“”„', start_quotes = "“", end_quotes = "”") ⇒ String
Converts quotes to the typograhic ones.
89 90 91 92 |
# File 'lib/truty/general.rb', line 89 def quotes(input, type = '"“”„', start_quotes = "“", end_quotes = "”") regexp = Regexp.new('[' + type + '][^' + type + ']*[' + type + ']') input.gsub(regexp) { |s| start_quotes + s[1..-2].gsub(/(^[\s ]+|[\s ]+$)/, "") + end_quotes } end |
#soft_hyphens(input, lang = "en_us", left = 2, right = 2, char = "") ⇒ String
Adds soft hyphens to the input.
69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/truty/general.rb', line 69 def soft_hyphens(input, lang = "en_us", left = 2, right = 2, char = "") l = Text::Hyphen.new(:language => lang, :left => left, :right => right) words = input.split(/[ ]+/m) result = [] words.each_with_index do |w, n| if !(w.length < 6 || n == words.size - 1 || w =~ URI::regexp || w =~ /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/i) w = l.visualise(w, char) end result << w end result.join(" ") end |
#space_between_numbers(input) ⇒ String
Adds thin non-breaking space between numbers.
107 108 109 |
# File 'lib/truty/general.rb', line 107 def space_between_numbers(input) input.gsub(/(\d)\s+(\d)/, '\1 \2') end |
#trailing_spaces(input) ⇒ String
Removes whitespace after the end of the paragraph.
168 169 170 |
# File 'lib/truty/general.rb', line 168 def trailing_spaces(input) input.gsub(/\s*($|\z)/, '') end |
#units(input) ⇒ String
Fixes non-breaking spaces betwwen number and unit.
151 152 153 154 |
# File 'lib/truty/general.rb', line 151 def units(input) output = input.gsub(/(\d+)\s+(%|‰|‱|℃|℉|°|€|Kč|(Y|Z|E|P|T|G|M|k|h|da|d|m|µ|n|p|f|a|z|y)?(m(²|³)?|g|s|h|A|K|cd|mol|Ω|℃|℉))/, '\1 \2') output.gsub(/(\*|§|#|†)\s+(\d+)/, '\1 \2') end |
#widows(input) ⇒ String
Adds non-breaking space before the last word in the paragraph.
160 161 162 |
# File 'lib/truty/general.rb', line 160 def widows(input) input.gsub(/(\s)(\S+(\$|\z))/, ' \2') end |