@@ -8,12 +8,6 @@
88
99import Foundation
1010
11−typealias Lines = [String]
12−typealias Element = [String]
13−let commentChars = CharacterSet.init(charactersIn: "#/")
14−let styleCharecters = CharacterSet.init(charactersIn: "_*")
15−
16−
1711enum BracketStyle {
1812 case square
1913 case round
@@ -67,11 +61,11 @@ default:
6761 if let surl = args.popFirst() {
6862 outputUrl = URL(fileURLWithPath: surl)
6963 }
70− case "-b:s":
64+ case "-s":
7165 style = .square
72− case "-b:r":
66+ case "-r":
7367 style = .round
74− case "-b:l":
68+ case "-l":
7569 style = .line
7670 default:
7771 return
@@ -87,6 +81,7 @@ enum ElementStyle: String {
8781 case italic = "italic"
8882 case boldItalic = "bold-italic"
8983}
84+let styleCharecters = CharacterSet.init(charactersIn: "_*")
9085extension String.SubSequence {
9186 var style: ElementStyle {
9287 let num = self.prefix(while: {styleCharecters.contains($0.unicodeScalars.first!)}).count
@@ -105,209 +100,79 @@ extension String.SubSequence {
105100 return self.trimmingCharacters(in: styleCharecters)
106101 }
107102}
108−
109−func process(element: Element) -> String {
110− var out = ""
103+func process(mth: String) -> String {
104+ let whitespace = CharacterSet.whitespaces
111105
112− var inMatrix = false
113− var first = false
114106 var seperated = false
115− var afterSeparator: [Substring] = []
107+ var afterSeparator: [String.SubSequence] = []
116108
117− var small = false
118−
119− let arrows = CharacterSet.init(charactersIn: "⟺⟷⟼⟻⟶⟹⟵⟸")
120−
121− let nEl = element.map { (el) -> String in
122− switch el {
123− case let l where l.last == ">" && l.first == "<" && l.dropLast().dropFirst().filter({$0 == "="}).count > 0:
124− small = true
125− return "⟺"
126− case let l where l.last == ">" && l.first == "<" && l.dropLast().dropFirst().filter({$0 == "-"}).count > 0:
127− small = true
128− return "⟷"
129− case let l where l.last == ">" && l.first == "|" && l.dropLast().dropFirst().filter({$0 == "-"}).count > 0:
130− small = true
131− return "⟼"
132− case let l where l.last == "|" && l.first == "<" && l.dropLast().dropFirst().filter({$0 == "-"}).count > 0:
133− small = true
134− return "⟻"
135− case let l where l.last == ">" && l.dropLast().filter({$0 == "-"}).count > 0:
136− small = true
137− return "⟶"
138− case let l where l.last == ">" && l.dropLast().filter({$0 == "="}).count > 0:
139− small = true
140− return "⟹"
141− case let l where l.first == "<" && l.dropFirst().filter({$0 == "-"}).count > 0:
142− small = true
143− return "⟵"
144− case let l where l.first == "<" && l.dropFirst().filter({$0 == "="}).count > 0:
145− small = true
146− return "⟸"
147− default:
148− return el
149− }
150− }
151−
152− func process(lineElement: Substring, withAttributes attributes: String = "", style: ElementStyle = .none) -> Substring {
153− func extractNextElement(from line: Substring) -> (element: Substring, left: Substring) {
154− if line.first != "(" {
155− var i = line.startIndex
156− var el: Substring = ""
157− while i < line.endIndex && CharacterSet.alphanumerics.contains(line[i].unicodeScalars.first!) {
158− el.append(line[i])
159− i = line.index(after: i)
160− }
161− return (el, line[i...])
162− } else {
163− var brackets = 0
164− var i = line.startIndex
165− var el: Substring = ""
166− while i < line.endIndex {
167− switch el[i] {
168− case "(":
169− brackets += 1
170− case ")":
171− brackets -= 1
172− default:
173− break
174− }
175− i = line.index(after: i)
176− if brackets == 0 {
177− break
178− }
179− el.append(el[i])
180− }
181− if brackets != 0 {
182− print("Mismatched brackets")
183− exit(0)
184− }
185− return (el.dropFirst(), line[i...])
186− }
187− }
188− var i = lineElement.startIndex
189− while i < lineElement.endIndex {
190− switch lineElement[i] {
191− case "^":
192− let parts = extractNextElement(from: lineElement[lineElement.index(after: i)...])
193− return "<mo mathvariant=\"\(style.rawValue)\" \(attributes)>\(lineElement[..<i].filter({$0 != "\\"}))</mo>" + "<msup><mo></mo><mn>\(parts.element)</mn></msup>" + process(lineElement: parts.left, withAttributes: attributes, style: style)
194− case "!":
195− let parts = extractNextElement(from: lineElement[lineElement.index(after: i)...])
196− return "<mo mathvariant=\"\(style.rawValue)\" \(attributes)>\(lineElement[..<i].filter({$0 != "\\"}))</mo>" + "<mover accent=\"true\"><mrow><mo>\(parts.element)</mo></mrow><mo>_</mo></mover>" + process(lineElement: parts.left, withAttributes: attributes, style: style)
197− case "\\":
198− i = lineElement.index(after: i)
199− case "/":
200− let parts = extractNextElement(from: lineElement[lineElement.index(after: i)...])
201− return "<mo mathvariant=\"\(style.rawValue)\" \(attributes)>\(lineElement[..<i].filter({$0 != "\\"}))</mo>" + "<msub><mo></mo><mn>\(parts.element)</mn></msub>" + process(lineElement: parts.left, withAttributes: attributes, style: style)
202− default:
203− break
204− }
205− i = lineElement.index(after: i)
206− }
207− return Substring("<mo mathvariant=\"\(style.rawValue)\" \(attributes)>\(lineElement.filter({$0 != "\\"}))</mo>")
208− }
209− func process(line: Substring) {
109+ var out = "<math>"
110+ let lines = mth.split(separator: "\n", omittingEmptySubsequences: false)
111+ .map {$0.trimmingCharacters(in: .whitespaces)}
112+ var insideMatrix = false
113+ func pureProcess(line: Substring) {
210114 out.append("<mtr>")
211115 for element in line.split(
212− whereSeparator: {c in CharacterSet.whitespaces.contains(c.unicodeScalars.first!)}) {
116+ whereSeparator: {c in whitespace.contains(c.unicodeScalars.first!)}) {
213117 switch element {
214118 case "_":
215119 out.append("<mtd><mspace height=\"0.8em\"/></mtd>")
216120 default:
217121 let eStyle = element.style
218− out.append("<mtd>\(process(lineElement: Substring(element.trimmingStyleCharecters()), withAttributes: "height=\"0.8em\"", style: eStyle))</mtd>")
122+ out.append("<mtd><mn mathvariant=\"\(eStyle.rawValue)\" height=\"0.8em\">\(element.trimmingStyleCharecters())</mn></mtd>")
219123 }
220124 }
221125 out.append("</mtr>")
222126 }
223− func process(comment: String) {
224− if comment.hasPrefix("#") {
225− out.append("<!-- \(comment.trimmingCharacters(in: commentChars).trimmingCharacters(in: .whitespaces)) -->")
127+ func checkInsideMatrix() {
128+ if insideMatrix {
129+ if seperated {
130+ out.append("</mtable><mo>|</mo><mtable>")
131+ for line in afterSeparator {
132+ pureProcess(line: line)
133+ }
134+ }
135+ out.append("</mtable><mo>\(style.closing)</mo></mrow>")
136+ insideMatrix = false
137+ seperated = false
138+ afterSeparator = []
226139 }
227140 }
228−
229− for line in nEl {
230− if inMatrix {
231− if first && line.contains("|") {
141+ func process(line: String.SubSequence) {
142+ switch line {
143+ case "":
144+ checkInsideMatrix()
145+ default:
146+ if !insideMatrix {
147+ out.append("<mrow><mo>\(style.opening)</mo><mtable>")
148+ insideMatrix = true
149+ }
150+ pureProcess(line: line)
151+ }
152+
153+ }
154+ for line in lines {
155+ if !insideMatrix {
156+ if lines.first?.contains("|") == true {
232157 seperated = true
233158 }
234− switch line {
235− case "]":
236− if seperated {
237− out.append("</mtable><mo stretchy=\"true\">|</mo><mtable>")
238− for line in afterSeparator {
239− process(line: line)
240− }
241− }
242− out.append("</mtable><mo stretchy=\"true\">\(style.closing)</mo></mrow></mtd></mtr>")
243− inMatrix = false
244− seperated = false
245− afterSeparator = []
246− default:
247− if seperated {
248− let split = line.split(separator: "|", maxSplits: 1, omittingEmptySubsequences: false)
249− if split.count != 2 {
250− print("Inconsistent syntax. The `|` seperator isn't consistent within a matrix.")
251− exit(0)
252− }
253− process(line: split[0])
254− afterSeparator.append(split[1])
255− } else {
256− process(line: Substring(line))
257− }
159+ }
160+ if line == "" {
161+ process(line: Substring(line))
162+ } else if seperated {
163+ let split = line.split(separator: "|", maxSplits: 1, omittingEmptySubsequences: false)
164+ if split.count != 2 {
165+ print("Inconsistent syntax")
166+ exit(0)
258167 }
168+ process(line: split[0])
169+ afterSeparator.append(split[1])
259170 } else {
260− switch line {
261− case let l where l.hasPrefix("//"):
262− break
263− case let l where l.hasPrefix("#"):
264− out.append("</mtable></math>\n\n")
265− process(comment: l)
266− out.append("\n\n<math><mtable>")
267− break // TODO: Figure out what to do
268− case "[":
269− out.append("<mtr><mtd><mrow><mo stretchy=\"true\">\(style.opening)</mo><mtable>")
270− inMatrix = true
271− first = true
272− continue
273− case "":
274− if !first {
275− out.append("</mtable><mtable>")
276− }
277− first = true
278− continue
279− case let l where arrows.contains(l.first!.unicodeScalars.first!):
280− out.append("<mtr><mtd><mo>\(line)</mo></mtd></mtr>")
281− default:
282− out.append("<mtr><mtd>\(small ? "<mstyle scriptlevel=\"1\">" : "")\(process(lineElement: Substring(line)))\(small ? "</mstyle>" : "")</mtd></mtr>")
283− }
171+ process(line: Substring(line))
284172 }
285− first = false
286− }
287− return out
288−}
289−
290−func process(mth: String) -> String {
291− let elements = mth
292− .split(separator: "\n", omittingEmptySubsequences: false)
293− .map({$0.trimmingCharacters(in: .whitespaces)})
294− .map({ l in
295− var i = l.startIndex
296− for u in 0..<max(0, l.count - 1) {
297− if l[i] == "/" && l[l.index(after: i)] == "/" {
298− return String(l.dropLast(l.count - u))
299− }
300− i = l.index(after: i)
301− }
302− return l
303− })
304− .split(separator: "")
305− var out = "<math><mtable>"
306− for el in elements {
307− out.append(process(element: Array(el)))
308− out.append("</mtable><mtable>")
309173 }
310− out.append("</mtable></math>")
174+ checkInsideMatrix()
175+ out.append("</math>")
311176 return out
312177}
313178