summaryrefslogtreecommitdiff
path: root/language/hyph-utf8/source/generic/hyph-utf8/lib/tex/hyphen/converter.rb
blob: 3b966d45a408095b847b2d23648374bc05773c04 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# encoding: UTF-8

require 'scanf'
require 'byebug'

module TeX
  module Hyphen
    class Converter
      def read(conversion)
        @mapping = { }
        File.read(conversion).each_line do |line|
          next if line =~ /^#/

          eightbit, usv = line.scanf "0x%02X\tU+%04X"
          @mapping[eightbit] = usv.chr(Encoding::UTF_8)
        end
      end

      def convert(filename)
        raise "Please define the encoding mapping first with #read" unless @mapping
        doconvert = false
        File.open(filename, external_encoding: Encoding::ASCII_8BIT).each_line do |line|
          if line =~ /\\patterns{/
            doconvert = true
            next
          end
          doconvert = false if doconvert && line =~ /}/

          if doconvert
            puts (line.strip.each_byte.map do |byte|
              byebug unless @mapping[byte]
              @mapping[byte]
            end || '').join
          end
        end
      end
    end
  end
end