-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSciMatEngine.bas
More file actions
347 lines (318 loc) · 11.8 KB
/
Copy pathSciMatEngine.bas
File metadata and controls
347 lines (318 loc) · 11.8 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
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
81
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
Attribute VB_Name = "SciMatEngine"
Sub ConvertMarkdownMath()
Dim rng As Range
Dim i As Integer
Dim monoIons() As String
Dim diatomicGases() As String
' ==============================================================================
' ⚙️ USER CONFIGURATION: THE EXCEPTION WHITELISTS
' ==============================================================================
monoIons = Split("O2-,S2-,P3-,N3-,C4-,H+,H-,K+,F-,I-,V2+,V3+,V4+,V5+,Y3+,U4+,U6+", ",")
diatomicGases = Split("H2,N2,O2,O3,P4,S8,C60", ",")
' ==============================================================================
' PART 1: MARKDOWN MATH CONVERTER
' ==============================================================================
Application.Dialogs(2844).Execute
DoEvents
Set rng = ActiveDocument.Content
With rng.Find
.Text = "$$[!$]@$$"
.MatchWildcards = True
Do While .Execute
rng.Text = Mid(rng.Text, 3, Len(rng.Text) - 4)
ActiveDocument.OMaths.Add rng
rng.OMaths(1).BuildUp
rng.Collapse 0
Loop
End With
Set rng = ActiveDocument.Content
With rng.Find
.Text = "$[!$]@$"
.MatchWildcards = True
Do While .Execute
rng.Text = Mid(rng.Text, 2, Len(rng.Text) - 2)
ActiveDocument.OMaths.Add rng
rng.OMaths(1).BuildUp
rng.Collapse 0
Loop
End With
' ==============================================================================
' PART 2: THE CHEMISTRY ENGINE (V1.5.6)
' ==============================================================================
' ---------------------------------------------------------
' PASS 0: TEXT SANITIZER (Normalizes notation before formatting)
' ---------------------------------------------------------
' 0A. Caret Sweeper (Word requires ^^ to find a literal caret!)
Dim caretCharges() As String
caretCharges = Split("^^1+,^^1-,^^2+,^^2-,^^3+,^^3-,^^4+,^^4-,^^+,^^-", ",")
For i = LBound(caretCharges) To UBound(caretCharges)
Set rng = ActiveDocument.Content
With rng.Find
.Text = caretCharges(i)
.MatchWildcards = False
.MatchWholeWord = False
Do While .Execute
rng.Text = Replace(rng.Text, "^", "")
rng.Collapse 0
Loop
End With
Next i
' 0B. Notation Normalizer (Converts -- to 2-, ++ to 2+)
Set rng = ActiveDocument.Content
With rng.Find
.Text = "([A-Za-z0-9])\-\-\-"
.MatchWildcards = True
Do While .Execute
rng.Text = Left(rng.Text, 1) & "3-"
rng.Collapse 0
Loop
End With
Set rng = ActiveDocument.Content
With rng.Find
.Text = "([A-Za-z0-9])\-\-"
.MatchWildcards = True
Do While .Execute
rng.Text = Left(rng.Text, 1) & "2-"
rng.Collapse 0
Loop
End With
Set rng = ActiveDocument.Content
With rng.Find
.Text = "([A-Za-z0-9])\+\+"
.MatchWildcards = True
Do While .Execute
rng.Text = Left(rng.Text, 1) & "2+"
rng.Collapse 0
Loop
End With
' ---------------------------------------------------------
' PASS 1: WHITELISTS & EDGE CASES
' ---------------------------------------------------------
' 1A. Polyatomic Edge Cases (Fixes O3-, OH-, CN- without hitting AB-)
Dim polyExceptions() As String
polyExceptions = Split("O3-,I3-,OH-,CN-", ",")
For i = LBound(polyExceptions) To UBound(polyExceptions)
Set rng = ActiveDocument.Content
With rng.Find
.Text = polyExceptions(i)
.MatchWildcards = False
.MatchWholeWord = False
.MatchCase = True
Do While .Execute
If polyExceptions(i) = "O3-" Or polyExceptions(i) = "I3-" Then
ActiveDocument.Range(rng.Start + 1, rng.Start + 2).Font.Subscript = True
ActiveDocument.Range(rng.Start + 2, rng.End).Font.Superscript = True
Else
ActiveDocument.Range(rng.Start + 2, rng.End).Font.Superscript = True
End If
rng.Collapse 0
Loop
End With
Next i
' 1B. Mono-Ions
For i = LBound(monoIons) To UBound(monoIons)
Set rng = ActiveDocument.Content
With rng.Find
.Text = monoIons(i)
.MatchWildcards = False
.MatchWholeWord = False
.MatchCase = True
Do While .Execute
ActiveDocument.Range(rng.Start + 1, rng.End).Font.Superscript = True
rng.Collapse 0
Loop
End With
Next i
' 1C. Gases
For i = LBound(diatomicGases) To UBound(diatomicGases)
Set rng = ActiveDocument.Content
With rng.Find
.Text = diatomicGases(i)
.MatchWildcards = False
.MatchWholeWord = False
.MatchCase = True
Do While .Execute
ActiveDocument.Range(rng.Start + 1, rng.End).Font.Subscript = True
rng.Collapse 0
Loop
End With
Next i
' ---------------------------------------------------------
' PASS 1.5: Polyatomic Charge Splitters
' ---------------------------------------------------------
Set rng = ActiveDocument.Content
With rng.Find
.Text = "[A-Za-z][0-9][0-9][-+]"
.MatchWildcards = True
Do While .Execute
If rng.Font.Superscript = False And rng.Font.Subscript = False Then
ActiveDocument.Range(rng.Start + 1, rng.Start + 2).Font.Subscript = True
ActiveDocument.Range(rng.Start + 2, rng.End).Font.Superscript = True
End If
rng.Collapse 0
Loop
End With
Set rng = ActiveDocument.Content
With rng.Find
.Text = "[A-Z][0-9][-+]"
.MatchWildcards = True
Do While .Execute
If rng.Font.Superscript = False And rng.Font.Subscript = False Then
ActiveDocument.Range(rng.Start + 1, rng.Start + 2).Font.Subscript = True
ActiveDocument.Range(rng.Start + 2, rng.End).Font.Superscript = True
End If
rng.Collapse 0
Loop
End With
' ---------------------------------------------------------
' PASS 2: MONO-IONS FIRST
' ---------------------------------------------------------
Set rng = ActiveDocument.Content
With rng.Find
.Text = "[A-Z][a-z][0-9]{1,2}[-+]"
.MatchWildcards = True
Do While .Execute
If rng.Font.Subscript = False Then
rng.MoveStart wdCharacter, 2
rng.Font.Superscript = True
End If
rng.Collapse 0
Loop
End With
Set rng = ActiveDocument.Content
With rng.Find
.Text = "[A-Z][a-z][-+]"
.MatchWildcards = True
Do While .Execute
If rng.Font.Subscript = False Then
rng.MoveStart wdCharacter, 2
rng.Font.Superscript = True
End If
rng.Collapse 0
Loop
End With
Set rng = ActiveDocument.Content
With rng.Find
.Text = "[\)\]][0-9]{1,2}[-+]"
.MatchWildcards = True
Do While .Execute
If rng.Font.Subscript = False Then
rng.MoveStart wdCharacter, 1
rng.Font.Superscript = True
End If
rng.Collapse 0
Loop
End With
Set rng = ActiveDocument.Content
With rng.Find
.Text = "[\)\]][-+]"
.MatchWildcards = True
Do While .Execute
If rng.Font.Subscript = False Then
rng.MoveStart wdCharacter, 1
rng.Font.Superscript = True
End If
rng.Collapse 0
Loop
End With
' ---------------------------------------------------------
' PASS 3: BASE ELEMENTS LAST
' ---------------------------------------------------------
Set rng = ActiveDocument.Content
With rng.Find
.Text = "[a-z][A-Z][0-9]{1,2}"
.MatchWildcards = True
Do While .Execute
If rng.Font.Superscript = False And rng.Font.Subscript = False Then
rng.MoveStart wdCharacter, 2
rng.Font.Subscript = True
End If
rng.Collapse 0
Loop
End With
Set rng = ActiveDocument.Content
With rng.Find
.Text = "[A-Z][A-Z][0-9]{1,2}"
.MatchWildcards = True
Do While .Execute
If rng.Font.Superscript = False And rng.Font.Subscript = False Then
rng.MoveStart wdCharacter, 2
rng.Font.Subscript = True
End If
rng.Collapse 0
Loop
End With
' THE ORGANIC PATCH (With Standalone Gatekeeper)
Set rng = ActiveDocument.Content
With rng.Find
.Text = "[CHONPSK][0-9]{1,2}"
.MatchWildcards = True
Do While .Execute
Dim isSafe As Boolean
isSafe = True
' If isolated by spaces or punctuation, ignore it
If rng.Start > 0 And rng.End < ActiveDocument.Content.End - 1 Then
Dim pChar As String, nChar As String
pChar = ActiveDocument.Range(rng.Start - 1, rng.Start).Text
nChar = ActiveDocument.Range(rng.End, rng.End + 1).Text
If (pChar = " " Or pChar = Chr(13) Or pChar = Chr(9)) And _
(nChar = " " Or nChar = "," Or nChar = "." Or nChar = Chr(13) Or nChar = Chr(9)) Then
isSafe = False
End If
End If
If isSafe And rng.Font.Superscript = False And rng.Font.Subscript = False Then
rng.MoveStart wdCharacter, 1
rng.Font.Subscript = True
End If
rng.Collapse 0
Loop
End With
Set rng = ActiveDocument.Content
With rng.Find
.Text = "[A-Z][a-z][0-9]{1,2}"
.MatchWildcards = True
Do While .Execute
If rng.Font.Superscript = False And rng.Font.Subscript = False Then
rng.MoveStart wdCharacter, 2
rng.Font.Subscript = True
End If
rng.Collapse 0
Loop
End With
Set rng = ActiveDocument.Content
With rng.Find
.Text = "[\)\]][0-9]{1,2}"
.MatchWildcards = True
Do While .Execute
If rng.Font.Superscript = False And rng.Font.Subscript = False Then
rng.MoveStart wdCharacter, 1
rng.Font.Subscript = True
End If
rng.Collapse 0
Loop
End With
' ---------------------------------------------------------
' PASS 4: THE ISOTOPE SWEEPER
' ---------------------------------------------------------
Set rng = ActiveDocument.Content
With rng.Find
.Text = "<[0-9]{1,3}[A-Z]"
.MatchWildcards = True
Do While .Execute
Dim isSafeIso As Boolean
isSafeIso = True
If rng.Start > 0 Then
Dim prevChar As String
prevChar = ActiveDocument.Range(rng.Start - 1, rng.Start).Text
If prevChar = ")" Or prevChar = "]" Or prevChar = "}" Then
isSafeIso = False
End If
End If
If isSafeIso Then
rng.MoveEnd wdCharacter, -1
rng.Font.Superscript = True
End If
rng.Collapse 0
Loop
End With
End Sub