Class: Tex2id::Converter

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/tex2id/converter.rb

Defined Under Namespace

Modules: Constants

Constant Summary

Constants included from Constants

Constants::CHAR_MAP, Constants::CHAR_MAP_PATTERN, Constants::MACROS, Constants::MACROS_IN_SUPERSCRIPT, Constants::TOKEN_PATTERN

Instance Method Summary collapse

Constructor Details

#initialize(source, only_fix_md2inao: false) ⇒ Converter

Returns a new instance of Converter.



6
7
8
9
10
11
# File 'lib/tex2id/converter.rb', line 6

def initialize(source, only_fix_md2inao: false)
  @source = source
  @only_fix_md2inao = only_fix_md2inao
  @state_stack = []
  @token_stack = []
end

Instance Method Details

#apply_char_map(s) ⇒ Object



101
102
103
104
105
# File 'lib/tex2id/converter.rb', line 101

def apply_char_map(s)
  s = s.gsub(CHAR_MAP_PATTERN) do |matched|
    CHAR_MAP[matched] || matched
  end
end

#convertObject



13
14
15
16
17
# File 'lib/tex2id/converter.rb', line 13

def convert
  pass1 = convert_display_math(@source)
  pass2 = convert_inline_math(pass1)
  pass2
end

#convert_display_math(source) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/tex2id/converter.rb', line 19

def convert_display_math(source)
  source.gsub(/\$\$(.*?)\$\$/m) {
    tex_source = fix_md2inao($1.strip)
    next "$$#{tex_source}$$" if @only_fix_md2inao

    if (m = /%filename:\s*(\S+)\s*\z/.match(tex_source))
      "<CharStyle:赤字>画像を挿入:#{m[1]}<CharStyle:>"
    else
      [ "<pstyle:半行アキ>",
        "<pstyle:数式>" + convert_tex_source(tex_source),
      ].join("\n")
    end
  }.gsub(/<ParaStyle:本文>(<pstyle:半行アキ>)/) { $1 }
end

#convert_inline_math(source) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/tex2id/converter.rb', line 34

def convert_inline_math(source)
  source.each_line.map { |line|
    case line
    when /\A<ParaStyle:リスト>/,
         /\A<ParaStyle:リスト白文字>/
      # do nothing
    else
      inline_commands = []
      line.gsub!(/<CharStyle:(?:コマンド|コード(文字単位))>[^<]+<CharStyle:>/) do |matched|
        id = inline_commands.length
        inline_commands << matched
        "<tex2id_inline_commands[#{id}]>"
      end
      line.gsub!(/\$(.*?)\$/) do
        tex_source = fix_md2inao($1.strip)
        if @only_fix_md2inao
          "$" + tex_source + "$"
        else
          convert_tex_source(tex_source)
        end
      end
      line.gsub!(/<tex2id_inline_commands\[(\d+)\]>/) do
        inline_commands[$1.to_i]
      end
    end
    line
  }.join('')
end

#convert_tex_source(source) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/tex2id/converter.rb', line 63

def convert_tex_source(source)
  buf = ""
  result = ""

  source.scan(TOKEN_PATTERN) do |token|
    if buf.length > 0 && token[12].nil?
      result << process_normal_characters(buf)
      buf.clear
    end

    case
    when (tok = token[0])
      result << "<cstyle:数式ローマン>" + tok + "<cstyle:>"
    when (tok = token[1])
      result << process_macros(tok)
    when (tok = token[2])
      result << tok
    when (tok = token[3] || token[4] || token[5])
      tok = process_macros(tok) if token[5]
      if (tok2 = token[6] || token[7])
        result << process_superscript_on_subscript(tok2, tok)
      else
        result << "<cstyle:数式下付き>" + tok + "<cstyle:>"
      end
    when (tok = token[8] || token[9] || token[10])
      result << "<cstyle:数式上付き>" + apply_char_map(tok) + "<cstyle:>"
    when (tok = token[11])
      result << process_number(tok)
    when (tok = token[12])
      buf << tok
    end
  end

  result << process_normal_characters(buf) if buf.length > 0

  result
end

#fix_md2inao(source) ⇒ Object



134
135
136
137
138
139
# File 'lib/tex2id/converter.rb', line 134

def fix_md2inao(source)
  source.dup.tap do |s|
    s.gsub!(/<CharStyle:(?:イタリック(?:(変形斜体))?)?>/, '_')
    s.gsub!(/(?:<005C>){2}/, '\\')
  end
end

#process_macros(s) ⇒ Object



120
121
122
# File 'lib/tex2id/converter.rb', line 120

def process_macros(s)
  MACROS[s] || "\\#{s}"
end

#process_normal_characters(s) ⇒ Object



107
108
109
110
# File 'lib/tex2id/converter.rb', line 107

def process_normal_characters(s)
  s = apply_char_map(s)
  "<cstyle:数式>" + s + "<cstyle:>"
end

#process_number(s) ⇒ Object



124
125
126
127
128
129
130
131
132
# File 'lib/tex2id/converter.rb', line 124

def process_number(s)
  # TODO: generalize the following
  s.gsub!(/[-\u{2212}]\\infty/) {|x| "<ctk:-150>\u{2212}<ctk:><F031>" }
  s.gsub!(/\\infty/) {|x| '<F031>' }
  if s[0] == '-' || s[0] == "\u{2212}"
    s[0,1] = "<ctk:-300>\u{2212}<ctk:>"
  end
  "<cstyle:数式>#{s}<cstyle:>"
end

#process_superscript_on_subscript(superscript, subscript) ⇒ Object



112
113
114
115
116
117
118
# File 'lib/tex2id/converter.rb', line 112

def process_superscript_on_subscript(superscript, subscript)
  converted_superscript = superscript.dup
  MACROS_IN_SUPERSCRIPT.each do |key, value|
    converted_superscript.gsub!(key, value)
  end
  "<cstyle:数式下付き><cr:1><crstr:#{converted_superscript}>#{subscript}<cr:><crstr:><cstyle:>"
end