hwp_script_to_latex 1.3.5 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a3677029962b7e0d60b7a7d4d8bb30a474bde6c3cebbfd57aa61f8f5dbcbf190
4
- data.tar.gz: cb35a8ae348d5b34d7be5c25fb04a29a27c5b86744f61a21ccfbf93b1b2bfa28
3
+ metadata.gz: 8a1f7260a320ad6b3be51ded4f0a61b2da7d0143ed340715e37a0ebdcf89f2e1
4
+ data.tar.gz: 045d8409baaeff28e2803f13f7c5fe152e8f7100749d40a253fef6f2f21f6467
5
5
  SHA512:
6
- metadata.gz: 3c0e8b2f3814685a934fe5c9a10cc95cd7b7276a1d2a4b450f1dd18324238c0a0facc590dfe785f4779299193abe5b546d6c126db946aaa842c3ac74f7a33aa5
7
- data.tar.gz: 56a93a2e78d02fb63bccb150fc68a4a0aa9f500890d0798246bf94ee12a3f877b4e450a968f8a05ae8c04b0f241bbd290d154c147340e3185081f65ff3a73c5d
6
+ metadata.gz: 020d0e2857c38dbf1aa4ae5051150631bcb4033f075fef06c2229923fce601ca74c9d3058ccaa5e106b3eb2c923528af7b3993802681997a4869d29087333de5
7
+ data.tar.gz: 5addae0db39cea048c66807b8f1141a31c8206f63dd3788a995bedb3bfb59331096463924eca23f3e2c1c8ba934fab1df9cb007fa5457c185c9e7f0369627b07
@@ -14,7 +14,7 @@ module HwpScriptToLatex
14
14
 
15
15
  @rules= JSON.parse(file_content, symbolize_names: true)
16
16
  @simple_commands = @rules[:command][:simple].sort_by { |hash| -1 * hash[:script].length }
17
- @matrix_commands = @rules[:command][:matrix]
17
+ @block_commands = @rules[:command][:block]
18
18
  @meta_keywords = @rules[:keyword][:meta].sort_by { |hash| -1 * hash[:regex].inspect.length }
19
19
  @command_keywords = @rules[:keyword][:command].sort_by { |hash| -1 * hash[:regex].inspect.length }
20
20
  @symbol_keywords = @rules[:keyword][:symbol].sort_by { |hash| -1 * hash[:regex].inspect.length }
@@ -24,10 +24,17 @@ module HwpScriptToLatex
24
24
  def convert(script, math_mode: false, display_mode: false)
25
25
  # Sanitize for starting convert
26
26
  result = pre_sanitize(script)
27
- # 키워드 치환
27
+ # 단순 치환 키워드
28
28
  result = replace_keywords(result)
29
- # Simple Command 치환
29
+ # 1개의 우항을 가지는 간단한 명령어
30
30
  result = replace_simple_commands(result)
31
+ # 행렬, 케이스등 블록 명령어
32
+ # cases {...} => \begin{cases}...\end{cases}
33
+ # dmatrix {...} => \begin{vmatrix}...\end{vmatrix}
34
+ # bmatrix {...} => \begin{Bmatrix}...\end{Bmatrix}
35
+ # pmatrix {...} => \begin{pmatrix}...\end{pmatrix}
36
+ # matrix {...} => \begin{matrix}...\end{matrix}
37
+ result = replace_block_commands(result)
31
38
 
32
39
  # 특수 형태의 명령어 치환
33
40
  # Case 1. 루트 변환
@@ -35,14 +42,8 @@ module HwpScriptToLatex
35
42
  # sqrt A of B => \sqrt [A]{B}
36
43
  # Case 2. 분수 변환
37
44
  # A over B => \dfrac {A}{B}
38
- # Case 3. 행렬 변환
39
- # dmatrix {...} => \begin{vmatrix}...\end{vmatrix}
40
- # bmatrix {...} => \begin{Bmatrix}...\end{Bmatrix}
41
- # pmatrix {...} => \begin{pmatrix}...\end{pmatrix}
42
- # matrix {...} => \begin{matrix}...\end{matrix}
43
45
  result = replace_sqrt(result) # Case 1
44
46
  result = replace_fractions(result) # Case 2
45
- result = replace_matrix(result) # Case 3
46
47
 
47
48
  # 전체 수식에 디스플레이 스타일 적용
48
49
  result = decorate_displaystyle(result) if display_mode
@@ -65,8 +66,8 @@ module HwpScriptToLatex
65
66
  script = script.gsub(jokbo_regex, "")
66
67
  # 2개 이상의 공백을 하나의 공백으로 치환
67
68
  script = script.gsub(/\s+/, " ").strip
68
- # 백슬래시를 rm으로 치환
69
- script = script.gsub(/\\/, " \\rm ")
69
+ # 백슬래시(로만체)를 삭제
70
+ script = script.gsub(/\\/, "")
70
71
  # 꺽쇠 치환
71
72
  script = script.gsub(/&lt;/, "<")
72
73
  script = script.gsub(/&gt;/, ">")
@@ -103,6 +104,24 @@ module HwpScriptToLatex
103
104
  return script
104
105
  end
105
106
 
107
+ def replace_block_commands(script)
108
+ @block_commands.each do |command|
109
+ command_regex = %r((?<![a-zA-Z])(?i:#{command[:script]})\s*(?<block_content>{(?>[^{}]+|(?:\g<block_content>))*}))
110
+
111
+ match_data = script.match(command_regex)
112
+ while match_data
113
+ # 시작, 끝 중괄호 제거
114
+ block_content = remove_curly_brackets(match_data['block_content'])
115
+ # sub 메서드에 블록 문법을 사용한 이유:
116
+ # gsub, sub에서 백슬래시 문제
117
+ script = script.sub(command_regex) { " \\begin{#{command[:latex]}} %s \\end{#{command[:latex]}} " % block_content }
118
+ match_data = script.match(command_regex)
119
+ end
120
+ end
121
+
122
+ return script
123
+ end
124
+
106
125
  def replace_keywords(script)
107
126
  keywords = @meta_keywords + @command_keywords + @symbol_keywords + @reserved_keywords
108
127
  matched_count = 0
@@ -150,24 +169,6 @@ module HwpScriptToLatex
150
169
  return script
151
170
  end
152
171
 
153
- def replace_matrix(script)
154
- @matrix_commands.each do |command|
155
- matrix_regex = %r((?<![a-zA-Z])(?i:#{command[:script]})\s*(?<matrix_content>{(?>[^{}]+|(?:\\g<matrix_content>))*}))
156
-
157
- match_data = script.match(matrix_regex)
158
- while match_data
159
- # 시작, 끝 중괄호 제거
160
- matrix_content = remove_curly_brackets(match_data['matrix_content'])
161
- # sub 메서드에 블록 문법을 사용한 이유:
162
- # gsub, sub에서 백슬래시 문제
163
- script = script.sub(matrix_regex) { " \\begin{#{command[:latex]}} %s \\end{#{command[:latex]}} " % matrix_content }
164
- match_data = script.match(matrix_regex)
165
- end
166
- end
167
-
168
- return script
169
- end
170
-
171
172
  def replace_sqrt(script)
172
173
  right_term_name1 = 'rt1'
173
174
  right_term_name2 = 'rt2'
@@ -217,9 +218,9 @@ module HwpScriptToLatex
217
218
  end
218
219
 
219
220
  def decorate_displaystyle(script)
220
- script = "{#{script}}"
221
- script = script.gsub(/(?<!\\)\s*{\s*\\displaystyle/, " { ")
222
- script = script.gsub(/(?<!\\)\s*{/, " {\\displaystyle ")
221
+ script = script.gsub(/\\sum/, "\\displaystyle \\sum")
222
+ script = script.gsub(/\\int/, "\\displaystyle \\int")
223
+ script = script.gsub(/\\oint/, "\\displaystyle \\oint")
223
224
 
224
225
  return script
225
226
  end
@@ -1,3 +1,3 @@
1
1
  module HwpScriptToLatex
2
- VERSION = "1.3.5"
2
+ VERSION = "1.4.0"
3
3
  end
data/rules.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "_comments": [
3
3
  "command.simple: 1개의 우항을 가지는 간단한 명령어",
4
- "command.matrix: 행렬 명령어",
4
+ "command.block: begin/end 블록을 가지는 명령어(행렬, 케이스)",
5
5
  "keyword.meta: 공백, 줄바꿈, left right 등 특수문자",
6
6
  "keyword.command: 항이 없는 단순 치환 명령어",
7
7
  "keyword.symbol: 기호",
@@ -82,7 +82,11 @@
82
82
  "latex": "\\underline"
83
83
  }
84
84
  ],
85
- "matrix": [
85
+ "block": [
86
+ {
87
+ "script": "cases",
88
+ "latex": "cases"
89
+ },
86
90
  {
87
91
  "script": "dmatrix",
88
92
  "latex": "vmatrix"
@@ -1098,16 +1102,16 @@
1098
1102
  ],
1099
1103
  "command": [
1100
1104
  {
1101
- "regex": ["rm", "Rm", "RM"],
1102
- "latex": " \\rm "
1105
+ "regex": ["eqalign", "Eqalign", "EQALIGN"],
1106
+ "latex": ""
1103
1107
  },
1104
1108
  {
1105
- "regex": ["it", "It", "IT"],
1106
- "latex": " \\it "
1109
+ "regex": ["rm", "Rm", "RM"],
1110
+ "latex": ""
1107
1111
  },
1108
1112
  {
1109
- "regex": ["cases", "Cases", "CASES"],
1110
- "latex": " \\cases "
1113
+ "regex": ["it", "It", "IT"],
1114
+ "latex": ""
1111
1115
  },
1112
1116
  {
1113
1117
  "regex": ["sum", "Sum", "SUM"],
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hwp_script_to_latex
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.5
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - bluesh55