Class: HwpScriptToLatex::Converter

Inherits:
Object
  • Object
show all
Defined in:
lib/hwp_script_to_latex/converter.rb

Overview

한글 수식스크립트를 LaTeX 문법으로 변환

Constant Summary collapse

LEFT_TERM_REGEX =

명령어에 사용되는 좌, 우항 정규표현식

"(?:[^{}\\s`]{9})*?\\K(?<%s>{(?>[^{}]+|(?:\\g<%s>))*}|(?:[^{}\\s`]{0,9}))"
RIGHT_TERM_REGEX =
"(?<%s>{(?>[^{}]+|(?:\\g<%s>))*}|(?:[^{}\\s`]{0,9}))"

Instance Method Summary collapse

Constructor Details

#initializeConverter

Returns a new instance of Converter.



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/hwp_script_to_latex/converter.rb', line 10

def initialize
  f = File.open(File.join(File.dirname(__FILE__), '..', '..', 'rules.json'), 'r')
  file_content = f.read
  f.close

  @rules= JSON.parse(file_content, symbolize_names: true)
  @simple_commands = @rules[:command][:simple].sort_by { |hash| -1 * hash[:script].length }
  @matrix_commands = @rules[:command][:matrix]
  @meta_keywords = @rules[:keyword][:meta].sort_by { |hash| -1 * hash[:regex].inspect.length }
  @command_keywords = @rules[:keyword][:command].sort_by { |hash| -1 * hash[:regex].inspect.length }
  @symbol_keywords = @rules[:keyword][:symbol].sort_by { |hash| -1 * hash[:regex].inspect.length }
  @reserved_keywords = @rules[:keyword][:reserved].sort_by { |hash| -1 * hash[:regex].inspect.length }
end

Instance Method Details

#convert(script, math_mode: false, display_mode: false) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/hwp_script_to_latex/converter.rb', line 24

def convert(script, math_mode: false, display_mode: false)
  # Sanitize for starting convert
  result = pre_sanitize(script)
  # 키워드 치환
  result = replace_keywords(result)
  # Simple Command 치환
  result = replace_simple_commands(result)

  # 특수 형태의 명령어 치환
  # Case 1. 루트 변환
  #   sqrt A => \sqrt {A}
  #   sqrt A of B => \sqrt [A]{B}
  # Case 2. 분수 변환
  #   A over B => \dfrac {A}{B}
  # Case 3. 행렬 변환
  #   dmatrix {...} => \begin{vmatrix}...\end{vmatrix}
  #   bmatrix {...} => \begin{Bmatrix}...\end{Bmatrix}
  #   pmatrix {...} => \begin{pmatrix}...\end{pmatrix}
  #   matrix {...} => \begin{matrix}...\end{matrix}
  result = replace_sqrt(result) # Case 1
  result = replace_fractions(result) # Case 2
  result = replace_matrix(result) # Case 3

  # 전체 수식에 디스플레이 스타일 적용
  result = decorate_displaystyle(result) if display_mode

  # Sanitize result for removing dirty spaces
  result = post_sanitize(result)

  # Math mode
  result = "$#{result}$" if math_mode

  return result
end