@@ -8,6 +8,12 @@
9import Foundation
9import Foundation
11+typealias Lines = [String]
12+typealias Element = [String]
13+let commentChars = CharacterSet.init(charactersIn: "#/")
14+let styleCharecters = CharacterSet.init(charactersIn: "_*")
11enum BracketStyle {
17enum BracketStyle {
12 case square
18 case square
13 case round
19 case round
@@ -61,11 +67,11 @@ default:
61 if let surl = args.popFirst() {
67 if let surl = args.popFirst() {
62 outputUrl = URL(fileURLWithPath: surl)
68 outputUrl = URL(fileURLWithPath: surl)
64− case "-s":
70+ case "-b:s":
65 style = .square
71 style = .square
66− case "-r":
72+ case "-b:r":
67 style = .round
73 style = .round
68− case "-l":
74+ case "-b:l":
69 style = .line
75 style = .line
@@ -81,7 +87,6 @@ enum ElementStyle: String {
81 case italic = "italic"
87 case italic = "italic"
82 case boldItalic = "bold-italic"
88 case boldItalic = "bold-italic"
84−let styleCharecters = CharacterSet.init(charactersIn: "_*")
85extension String.SubSequence {
90extension String.SubSequence {
86 var style: ElementStyle {
91 var style: ElementStyle {
87 let num = self.prefix(while: {styleCharecters.contains($0.unicodeScalars.first!)}).count
92 let num = self.prefix(while: {styleCharecters.contains($0.unicodeScalars.first!)}).count
@@ -100,20 +105,55 @@ extension String.SubSequence {
100 return self.trimmingCharacters(in: styleCharecters)
105 return self.trimmingCharacters(in: styleCharecters)
103−func process(mth: String) -> String {
109+func process(element: Element) -> String {
104− let whitespace = CharacterSet.whitespaces
110+ var out = ""
112+ var inMatrix = false
106 var seperated = false
114 var seperated = false
107− var afterSeparator: [String.SubSequence] = []
115+ var afterSeparator: [Substring] = []
119+ let arrows = CharacterSet.init(charactersIn: "⟺⟷⟼⟻⟶⟹⟵⟸")
121+ let nEl = element.map { (el) -> String in
123+ case let l where l.last == ">" && l.first == "<" && l.dropLast().dropFirst().filter({$0 == "="}).count > 0:
126+ case let l where l.last == ">" && l.first == "<" && l.dropLast().dropFirst().filter({$0 == "-"}).count > 0:
129+ case let l where l.last == ">" && l.first == "|" && l.dropLast().dropFirst().filter({$0 == "-"}).count > 0:
132+ case let l where l.last == "|" && l.first == "<" && l.dropLast().dropFirst().filter({$0 == "-"}).count > 0:
135+ case let l where l.last == ">" && l.dropLast().filter({$0 == "-"}).count > 0:
138+ case let l where l.last == ">" && l.dropLast().filter({$0 == "="}).count > 0:
141+ case let l where l.first == "<" && l.dropFirst().filter({$0 == "-"}).count > 0:
144+ case let l where l.first == "<" && l.dropFirst().filter({$0 == "="}).count > 0:
109− var out = "<math>"
152+
110− let lines = mth.split(separator: "\n", omittingEmptySubsequences: false)
111− .map {$0.trimmingCharacters(in: .whitespaces)}
112− var insideMatrix = false
113− func pureProcess(line: Substring) {
153+ func process(line: Substring) {
114 out.append("<mtr>")
154 out.append("<mtr>")
115 for element in line.split(
155 for element in line.split(
116− whereSeparator: {c in whitespace.contains(c.unicodeScalars.first!)}) {
156+ whereSeparator: {c in CharacterSet.whitespaces.contains(c.unicodeScalars.first!)}) {
117 switch element {
157 switch element {
118 case "_":
158 case "_":
119 out.append("<mtd><mspace height=\"0.8em\"/></mtd>")
159 out.append("<mtd><mspace height=\"0.8em\"/></mtd>")
@@ -124,55 +164,99 @@ func process(mth: String) -> String {
125 out.append("</mtr>")
165 out.append("</mtr>")
127− func checkInsideMatrix() {
167+ func process(comment: String) {
128− if insideMatrix {
168+ if comment.hasPrefix("#") {
129− if seperated {
169+ out.append("<!-- \(comment.trimmingCharacters(in: commentChars).trimmingCharacters(in: .whitespaces)) -->")
130− out.append("</mtable><mo>|</mo><mtable>")
131− for line in afterSeparator {
132− pureProcess(line: line)
135− out.append("</mtable><mo>\(style.closing)</mo></mrow>")
136− insideMatrix = false
141− func process(line: String.SubSequence) {
172+
147− out.append("<mrow><mo>\(style.opening)</mo><mtable>")
150− pureProcess(line: line)
154− for line in lines {
173+ for line in nEl {
155− if !insideMatrix {
174+ if inMatrix {
156− if lines.first?.contains("|") == true {
175+ if first && line.contains("|") {
157 seperated = true
176 seperated = true
160− if line == "" {
179+ case "]":
181+ out.append("</mtable><mo stretchy=\"true\">|</mo><mtable>")
182+ for line in afterSeparator {
161− process(line: Substring(line))
183+ process(line: line)
186+ out.append("</mtable><mo stretchy=\"true\">\(style.closing)</mo></mrow></mtd></mtr>")
162− } else if seperated {
191+ if seperated {
163− let split = line.split(separator: "|", maxSplits: 1, omittingEmptySubsequences: false)
192+ let split = line.split(separator: "|", maxSplits: 1, omittingEmptySubsequences: false)
164− if split.count != 2 {
193+ if split.count != 2 {
165− print("Inconsistent syntax")
194+ print("Inconsistent syntax. The `|` seperator isn't consistent within a matrix.")
197+ process(line: split[0])
198+ afterSeparator.append(split[1])
200+ process(line: Substring(line))
168− process(line: split[0])
169− afterSeparator.append(split[1])
171− process(line: Substring(line))
204+ switch line {
205+ case let l where l.hasPrefix("//"):
207+ case let l where l.hasPrefix("#"):
208+ out.append("</mtable></math>\n\n")
210+ out.append("\n\n<math><mtable>")
211+ break // TODO: Figure out what to do
214+ out.append("<mtr><mtd><mrow><mo stretchy=\"true\">\(style.opening)</mo><mtable>")
219+ print("Inconsistent syntax. Opening a matrix from within a matrix.")
224+ out.append("</mtable><mtable>")
228+ case let l where arrows.contains(l.first!.unicodeScalars.first!):
229+ out.append("<mtr><mtd><mo>\(line)</mo></mtd></mtr>")
231+ out.append("<mtr><mtd>\(small ? "<mstyle scriptlevel=\"1\">" : "")<mo>\(line)</mo>\(small ? "</mstyle>" : "")</mtd></mtr>")
239+func process(mth: String) -> String {
241+ .split(separator: "\n", omittingEmptySubsequences: false)
242+ .map({$0.trimmingCharacters(in: .whitespaces)})
244+ var i = l.startIndex
245+ for u in 0..<max(0, l.count - 1) {
246+ if l[i] == "/" && l[l.index(after: i)] == "/" {
247+ return String(l.dropLast(l.count - u))
249+ i = l.index(after: i)
253+ .split(separator: "")
254+ var out = "<math><mtable>"
255+ for el in elements {
256+ out.append(process(element: Array(el)))
257+ out.append("</mtable><mtable>")
175− out.append("</math>")
259+ out.append("</mtable></math>")
176 return out
260 return out