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
12
# File 'lib/tex2id/converter.rb', line 6

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

Instance Method Details

#apply_char_map(s) ⇒ Object



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

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

#check_use_math(source) ⇒ Object



25
26
27
28
29
30
# File 'lib/tex2id/converter.rb', line 25

def check_use_math(source)
  source.sub(/\bUseMath:\s+(true|false)\b/m) {
    @use_math = (Regexp.last_match[1] == "true")
    ""
  }
end

#convertObject



16
17
18
19
20
21
22
23
# File 'lib/tex2id/converter.rb', line 16

def convert
  pass1 = check_use_math(@source)
  if use_math?
    convert_math(pass1)
  else
    pass1
  end
end

#convert_display_math(source) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/tex2id/converter.rb', line 38

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



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/tex2id/converter.rb', line 53

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_math(source) ⇒ Object



32
33
34
35
36
# File 'lib/tex2id/converter.rb', line 32

def convert_math(source)
  pass1 = convert_display_math(source)
  pass2 = convert_inline_math(pass1)
  pass2
end

#convert_tex_source(source) ⇒ Object



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
116
117
118
# File 'lib/tex2id/converter.rb', line 82

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



153
154
155
156
157
158
# File 'lib/tex2id/converter.rb', line 153

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

#process_macros(s) ⇒ Object



139
140
141
# File 'lib/tex2id/converter.rb', line 139

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

#process_normal_characters(s) ⇒ Object



126
127
128
129
# File 'lib/tex2id/converter.rb', line 126

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

#process_number(s) ⇒ Object



143
144
145
146
147
148
149
150
151
# File 'lib/tex2id/converter.rb', line 143

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



131
132
133
134
135
136
137
# File 'lib/tex2id/converter.rb', line 131

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

#use_math?Boolean

Returns:

  • (Boolean)


14
# File 'lib/tex2id/converter.rb', line 14

def use_math? = @use_math