@@ -11,12 +11,15 @@ import Foundation
1111enum BracketStyle {
1212 case square
1313 case round
14+ case line
1415 var opening: Character {
1516 switch self {
1617 case .round:
1718 return "("
1819 case .square:
1920 return "["
21+ case .line:
22+ return "|"
2023 }
2124 }
2225 var closing: Character {
@@ -25,6 +28,8 @@ enum BracketStyle {
2528 return ")"
2629 case .square:
2730 return "]"
31+ case .line:
32+ return "|"
2833 }
2934 }
3035}
@@ -60,6 +65,8 @@ default:
6065 style = .square
6166 case "-r":
6267 style = .round
68+ case "-l":
69+ style = .line
6370 default:
6471 return
6572 }
@@ -96,34 +103,75 @@ extension String.SubSequence {
96103func process(mth: String) -> String {
97104 let whitespace = CharacterSet.whitespaces
98105
106+ var seperated = false
107+ var afterSeparator: [String.SubSequence] = []
108+
99109 var out = "<math>"
100110 let lines = mth.split(separator: "\n", omittingEmptySubsequences: false)
101111 .map {$0.trimmingCharacters(in: .whitespaces)}
102112 var insideMatrix = false
103− for line in lines {
113+ func pureProcess(line: Substring) {
114+ out.append("<mtr>")
115+ for element in line.split(
116+ whereSeparator: {c in whitespace.contains(c.unicodeScalars.first!)}) {
117+ switch element {
118+ case "_":
119+ out.append("<mtd><mspace height=\"0.8em\"/></mtd>")
120+ default:
121+ let eStyle = element.style
122+ out.append("<mtd><mn mathvariant=\"\(eStyle.rawValue)\" height=\"0.8em\">\(element.trimmingStyleCharecters())</mn></mtd>")
123+ }
124+ }
125+ out.append("</mtr>")
126+ }
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 = []
139+ }
140+ }
141+ func process(line: String.SubSequence) {
104142 switch line {
105143 case "":
106− if insideMatrix {
107− out.append("</mtable><mo>\(style.closing)</mo></mrow>")
108− insideMatrix = false
109− }
144+ checkInsideMatrix()
110145 default:
111146 if !insideMatrix {
112147 out.append("<mrow><mo>\(style.opening)</mo><mtable>")
113148 insideMatrix = true
114149 }
115− out.append("<mtr>")
116− for element in line.split(
117− whereSeparator: {c in whitespace.contains(c.unicodeScalars.first!)}) {
118− let eStyle = element.style
119− out.append("<mtd><mn mathvariant=\"\(eStyle.rawValue)\">\(element.trimmingStyleCharecters())</mn></mtd>")
120− }
121− out.append("</mtr>")
150+ pureProcess(line: line)
122151 }
152+
123153 }
124− if insideMatrix {
125− out.append("</mtable><mo>\(style.closing)</mo></mrow>")
154+ for line in lines {
155+ if !insideMatrix {
156+ if lines.first?.contains("|") == true {
157+ seperated = true
158+ }
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)
167+ }
168+ process(line: split[0])
169+ afterSeparator.append(split[1])
170+ } else {
171+ process(line: Substring(line))
172+ }
126173 }
174+ checkInsideMatrix()
127175 out.append("</math>")
128176 return out
129177}