| 1 |
require 'psych' |
| 2 |
require 'hydra' |
| 3 |
require 'byebug' |
| 4 |
require 'date' |
| 5 |
|
| 6 |
require_relative 'path' |
| 7 |
|
| 8 |
class String |
| 9 |
def superstrip |
| 10 |
strip.gsub /%.*$/, '' |
| 11 |
end |
| 12 |
|
| 13 |
def supersplit |
| 14 |
strip.gsub(/\s+/m,"\n").split("\n") |
| 15 |
end |
| 16 |
|
| 17 |
def safe |
| 18 |
gsub /[\s-]/, '' |
| 19 |
end |
| 20 |
|
| 21 |
def titlecase |
| 22 |
split.map(&:capitalize).join(' ') |
| 23 |
end |
| 24 |
end |
| 25 |
|
| 26 |
module TeX |
| 27 |
module Hyphen |
| 28 |
class InvalidMetadata < StandardError; end |
| 29 |
class NoAuthor < InvalidMetadata; end |
| 30 |
class NoLicence < InvalidMetadata; end |
| 31 |
|
| 32 |
include PATH |
| 33 |
|
| 34 |
class Author |
| 35 |
def initialize(name,surname,email,contacted1,contacted2) |
| 36 |
@name = name |
| 37 |
@surname = surname |
| 38 |
@email = email |
| 39 |
# this mostly means if email has been recently checked |
| 40 |
@contacted1 = contacted1 |
| 41 |
# this means if we made more cooperation with author, |
| 42 |
# exchanging patches etc. |
| 43 |
@contacted2 = contacted2 |
| 44 |
end |
| 45 |
|
| 46 |
attr_reader :name, :surname, :email |
| 47 |
|
| 48 |
def self.authors |
| 49 |
@@authors ||= Psych::safe_load(File.read(File.expand_path('authors.yml', __dir__)), permitted_classes: [Author, Date]) |
| 50 |
end |
| 51 |
|
| 52 |
def self.all |
| 53 |
authors.values |
| 54 |
end |
| 55 |
|
| 56 |
def self.[] a |
| 57 |
authors[a] |
| 58 |
end |
| 59 |
end |
| 60 |
|
| 61 |
class Language |
| 62 |
# TODO .get! |
| 63 |
attr_reader :bcp47 |
| 64 |
|
| 65 |
@@eohmarker = '=' * 42 |
| 66 |
|
| 67 |
def iso639 |
| 68 |
@bcp47.split('-').first |
| 69 |
end |
| 70 |
|
| 71 |
# FIXME Find something better than all that extract_metadata unless @blah nonsens |
| 72 |
def babelname |
| 73 |
extract_metadata unless @babelname |
| 74 |
@babelname |
| 75 |
end |
| 76 |
|
| 77 |
def description |
| 78 |
extract_metadata unless @description |
| 79 |
@description |
| 80 |
end |
| 81 |
|
| 82 |
def use_old_loader |
| 83 |
extract_metadata unless @use_old_loader |
| 84 |
@use_old_loader |
| 85 |
end |
| 86 |
|
| 87 |
def use_old_patterns_comment |
| 88 |
extract_metadata unless @use_old_patterns_comment |
| 89 |
@use_old_patterns_comment |
| 90 |
end |
| 91 |
|
| 92 |
def legacy_patterns |
| 93 |
extract_metadata unless @legacy_patterns |
| 94 |
@legacy_patterns |
| 95 |
end |
| 96 |
|
| 97 |
def luaspecial |
| 98 |
extract_metadata unless @luaspecial |
| 99 |
@luaspecial |
| 100 |
end |
| 101 |
|
| 102 |
def message |
| 103 |
extract_metadata unless @message |
| 104 |
@message |
| 105 |
end |
| 106 |
|
| 107 |
def known_bugs |
| 108 |
extract_metadata unless @known_bugs |
| 109 |
@known_bugs |
| 110 |
end |
| 111 |
|
| 112 |
def initialize(bcp47 = nil) |
| 113 |
# byebug if bcp47 == 'mn-cyrl-x-lmc' |
| 114 |
@bcp47 = bcp47 |
| 115 |
end |
| 116 |
|
| 117 |
def self.languages |
| 118 |
@@languages ||= Dir.glob(File.join(PATH::TEX, 'hyph-*.tex')).inject [] do |languages, texfile| |
| 119 |
bcp47 = texfile.gsub /^.*\/hyph-(.*)\.tex$/, '\1' |
| 120 |
# languages << [bcp47, Language.new(bcp47)] |
| 121 |
languages << [bcp47, nil] |
| 122 |
end.to_h |
| 123 |
end |
| 124 |
@@languages = Language.languages |
| 125 |
|
| 126 |
def self.all |
| 127 |
@@all ||= languages.map do |bcp47, language| |
| 128 |
next if bcp47 == 'sr-cyrl' # FIXME Remove later |
| 129 |
@@languages[bcp47] ||= Language.new(bcp47) |
| 130 |
end.compact |
| 131 |
end |
| 132 |
|
| 133 |
def self.all_by_iso639 |
| 134 |
all.inject(Hash.new) do |maintags, language| |
| 135 |
(maintags[language.iso639] ||= Array.new) << language |
| 136 |
maintags |
| 137 |
end |
| 138 |
end |
| 139 |
|
| 140 |
def self.find_by_bcp47(bcp47) |
| 141 |
languages[bcp47] |
| 142 |
end |
| 143 |
|
| 144 |
def private_use? |
| 145 |
@bcp47.length == 3 || @bcp47[3] == '-' and @bcp47[0] == 'q' and ('a'..'t').include? @bcp47[1] |
| 146 |
end |
| 147 |
|
| 148 |
# TODO This should probably become “macrolanguage name” or something similar |
| 149 |
# @@displaynames = { |
| 150 |
# 'el' => 'Greek', |
| 151 |
# 'nb' => 'Norwegian', |
| 152 |
# 'nn' => 'Norwegian', |
| 153 |
# 'sh' => 'Serbian', |
| 154 |
# } |
| 155 |
|
| 156 |
def licences |
| 157 |
extract_metadata unless @licences |
| 158 |
@licences |
| 159 |
end |
| 160 |
|
| 161 |
def lefthyphenmin |
| 162 |
extract_metadata unless @lefthyphenmin |
| 163 |
@lefthyphenmin |
| 164 |
end |
| 165 |
|
| 166 |
def righthyphenmin |
| 167 |
extract_metadata unless @righthyphenmin |
| 168 |
@righthyphenmin |
| 169 |
end |
| 170 |
|
| 171 |
# Strictly speaking a misnomer, because grc-x-ibycus should also return true. |
| 172 |
# But useful for a number of apostrophe-related routines |
| 173 |
def isgreek? |
| 174 |
['grc', 'el-polyton', 'el-monoton', 'grc-x-ibycus'].include? @bcp47 |
| 175 |
end |
| 176 |
|
| 177 |
def serbian? |
| 178 |
@bcp47 =~ /^sh-/ |
| 179 |
end |
| 180 |
|
| 181 |
def italic? |
| 182 |
['it', 'pms'].include? @bcp47 |
| 183 |
end |
| 184 |
|
| 185 |
def has_apostrophes? |
| 186 |
begin |
| 187 |
!isgreek? && patterns.any? { |p| p =~ /'/ } |
| 188 |
rescue Errno::ENOENT |
| 189 |
false |
| 190 |
end |
| 191 |
end |
| 192 |
|
| 193 |
def has_hyphens? |
| 194 |
begin |
| 195 |
patterns.any? { |p| p =~ /-/ } |
| 196 |
rescue Errno::ENOENT |
| 197 |
false |
| 198 |
end |
| 199 |
end |
| 200 |
|
| 201 |
def extract_apostrophes |
| 202 |
plain, with_apostrophe = Array.new, nil |
| 203 |
|
| 204 |
patterns.each do |pattern| |
| 205 |
plain << pattern |
| 206 |
if pattern =~ /'/ && !isgreek? |
| 207 |
pattern_with_apostrophe = pattern.gsub(/'/,"’") |
| 208 |
plain << pattern_with_apostrophe |
| 209 |
(with_apostrophe ||= []) << pattern_with_apostrophe |
| 210 |
end |
| 211 |
end |
| 212 |
|
| 213 |
{ plain: plain, with_apostrophe: with_apostrophe } |
| 214 |
end |
| 215 |
|
| 216 |
def extract_characters # FIXME Turkish and others iİ; check Church Slavonic |
| 217 |
characters = Array.new |
| 218 |
|
| 219 |
characters_indexes = patterns.join.gsub(/[.0-9]/,'').unpack('U*').sort.uniq |
| 220 |
characters_indexes.each do |c| |
| 221 |
ch = [c].pack('U') |
| 222 |
characters << ch + ch.upcase |
| 223 |
characters << "’’" if ch == "'" && !isgreek? |
| 224 |
end |
| 225 |
|
| 226 |
characters |
| 227 |
end |
| 228 |
|
| 229 |
def comments_and_licence # Major TODO extract everything into YAML, and write .yml |
| 230 |
@comments_and_licence ||= readtexfile.gsub(/(.*)\\patterns.*/m,'\1') |
| 231 |
end |
| 232 |
|
| 233 |
def authors |
| 234 |
extract_metadata unless @authors |
| 235 |
@authors |
| 236 |
end |
| 237 |
|
| 238 |
def github_link |
| 239 |
sprintf 'https://github.com/hyphenation/tex-hyphen/tree/master/hyph-utf8/tex/generic/hyph-utf8/patterns/tex/hyph-%s.tex', @bcp47 |
| 240 |
end |
| 241 |
|
| 242 |
def <=>(other) |
| 243 |
# TODO Remove when practical |
| 244 |
a = self.babelname |
| 245 |
b = other.babelname |
| 246 |
|
| 247 |
if a == 'serbian' && b == 'serbianc' |
| 248 |
return -1 |
| 249 |
elsif a == 'serbianc' && b == 'serbian' |
| 250 |
return 1 |
| 251 |
end |
| 252 |
|
| 253 |
self.bcp47 <=> other.bcp47 |
| 254 |
end |
| 255 |
|
| 256 |
@@texfile = Hash.new |
| 257 |
def readtexfile(bcp47 = @bcp47) |
| 258 |
begin |
| 259 |
@@texfile[bcp47] ||= File.read(File.join(PATH::TEX, sprintf('hyph-%s.tex', bcp47))) |
| 260 |
rescue Errno::ENOENT |
| 261 |
@@texfile[bcp47] = "" |
| 262 |
end |
| 263 |
end |
| 264 |
|
| 265 |
def patterns |
| 266 |
@patterns ||= if @bcp47 == 'eo' then |
| 267 |
readtexfile.superstrip. |
| 268 |
gsub(/.*\\patterns\s*\{(.*)\}.*/m,'\1'). |
| 269 |
# |
| 270 |
gsub(/\\adj\{(.*?)\}/m,'\1a. \1aj. \1ajn. \1an. \1e.'). |
| 271 |
gsub(/\\nom\{(.*?)\}/m,'\1a. \1aj. \1ajn. \1an. \1e. \1o. \1oj. \1ojn. \1on.'). |
| 272 |
gsub(/\\ver\{(.*?)\}/m,'\1as. \1i. \1is. \1os. \1u. \1us.'). |
| 273 |
# |
| 274 |
supersplit |
| 275 |
else |
| 276 |
readtexfile(if ['nb', 'nn'].include? @bcp47 then 'no' else @bcp47 end).superstrip. |
| 277 |
gsub(/.*\\patterns\s*\{(.*?)\}.*/m,'\1'). |
| 278 |
supersplit |
| 279 |
end |
| 280 |
end |
| 281 |
|
| 282 |
def exceptions |
| 283 |
unless @exceptions |
| 284 |
if readtexfile.superstrip.index('\hyphenation') |
| 285 |
@exceptions = readtexfile.superstrip.gsub(/.*\\hyphenation\s*\{(.*?)\}.*/m,'\1').supersplit |
| 286 |
if @exceptions != "" |
| 287 |
@hyphenation = @exceptions.inject [] do |exceptions, exception| |
| 288 |
exceptions << [exception.gsub('-', ''), exception] |
| 289 |
end.to_h |
| 290 |
else |
| 291 |
@hyphenation = { } |
| 292 |
end |
| 293 |
else |
| 294 |
@exceptions = "" |
| 295 |
@hyphenation = { } |
| 296 |
end |
| 297 |
end |
| 298 |
|
| 299 |
@exceptions |
| 300 |
end |
| 301 |
|
| 302 |
def hyphenate(word) |
| 303 |
exceptions |
| 304 |
return @hyphenation[word] if @hyphenation[word] # FIXME Better name |
| 305 |
|
| 306 |
unless @hydra |
| 307 |
begin |
| 308 |
metadata = extract_metadata |
| 309 |
@hydra = Hydra.new patterns, :lax, '', metadata |
| 310 |
rescue InvalidMetadata |
| 311 |
@hydra = Hydra.new patterns |
| 312 |
end |
| 313 |
end |
| 314 |
@hydra.showhyphens(word) |
| 315 |
end |
| 316 |
|
| 317 |
def extract_metadata |
| 318 |
header = "" |
| 319 |
File.read(File.join(PATH::TEX, sprintf('hyph-%s.tex', @bcp47))).each_line do |line| |
| 320 |
break if line =~ /\\patterns|#{@@eohmarker}/ |
| 321 |
header += line.gsub(/^% /, '').gsub(/%.*/, '') |
| 322 |
end |
| 323 |
begin |
| 324 |
metadata = Psych::safe_load(header, permitted_classes: [Date]) |
| 325 |
raise InvalidMetadata unless metadata.is_a? Hash |
| 326 |
rescue Psych::SyntaxError |
| 327 |
raise InvalidMetadata |
| 328 |
end |
| 329 |
|
| 330 |
@name = metadata.dig('language', 'name') |
| 331 |
@lefthyphenmin = metadata.dig('hyphenmins', 'typesetting', 'left') || metadata.dig('hyphenmins', 'generation', 'left') |
| 332 |
@righthyphenmin = metadata.dig('hyphenmins', 'typesetting', 'right') || metadata.dig('hyphenmins', 'generation', 'right') |
| 333 |
# FIXME That’s not nice |
| 334 |
if respond_to? :synonyms |
| 335 |
@synonyms = metadata.dig('texlive', 'synonyms') || [] |
| 336 |
@encoding = metadata.dig('texlive', 'encoding') |
| 337 |
end |
| 338 |
@message = metadata.dig('texlive', 'message') |
| 339 |
@legacy_patterns = metadata.dig('texlive', 'legacy_patterns') |
| 340 |
@use_old_loader = metadata.dig('texlive', 'use_old_loader') |
| 341 |
@use_old_patterns_comment = metadata.dig('texlive', 'use_old_patterns_comment') |
| 342 |
@luaspecial = metadata.dig('texlive', 'luaspecial') |
| 343 |
@description = metadata.dig('texlive', 'description') |
| 344 |
@babelname = metadata.dig('texlive', 'babelname') |
| 345 |
@package = metadata.dig('texlive', 'package') |
| 346 |
licences = metadata.dig('licence') |
| 347 |
raise NoLicence unless licences |
| 348 |
licences = [licences] unless licences.is_a? Array |
| 349 |
@licences = licences.map do |licence| |
| 350 |
raise bcp47 if licence.is_a? String |
| 351 |
next if licence.values == [nil] |
| 352 |
licence.dig('name') || 'custom' |
| 353 |
end.compact |
| 354 |
authors = metadata.dig('authors') |
| 355 |
if authors |
| 356 |
@authors = authors.map do |author| |
| 357 |
author['name'] |
| 358 |
end |
| 359 |
else |
| 360 |
@authors = nil |
| 361 |
raise NoAuthor.new # FIXME |
| 362 |
end |
| 363 |
@known_bugs = metadata.dig('known_bugs') |
| 364 |
|
| 365 |
# raise NoAuthor unless @authors && @authors.count > 0 # TODO Later ;-) AR 2018-09-13 |
| 366 |
|
| 367 |
metadata |
| 368 |
end |
| 369 |
end |
| 370 |
end |
| 371 |
end |