on master help sign in
Added separator line
0b4ea8filyakooo0
Mathdown/Mathdown/main.swift+62−14
@@ -11,12 +11,15 @@ import Foundation
11enum BracketStyle {
11enum BracketStyle {
12 case square
12 case square
13 case round
13 case round
14+ case line
14 var opening: Character {
15 var opening: Character {
15 switch self {
16 switch self {
16 case .round:
17 case .round:
17 return "("
18 return "("
18 case .square:
19 case .square:
19 return "["
20 return "["
21+ case .line:
22+ return "|"
20 }
23 }
21 }
24 }
22 var closing: Character {
25 var closing: Character {
@@ -25,6 +28,8 @@ enum BracketStyle {
25 return ")"
28 return ")"
26 case .square:
29 case .square:
27 return "]"
30 return "]"
31+ case .line:
32+ return "|"
28 }
33 }
29 }
34 }
30}
35}
@@ -60,6 +65,8 @@ default:
60 style = .square
65 style = .square
61 case "-r":
66 case "-r":
62 style = .round
67 style = .round
68+ case "-l":
69+ style = .line
63 default:
70 default:
64 return
71 return
65 }
72 }
@@ -96,34 +103,75 @@ extension String.SubSequence {
96func process(mth: String) -> String {
103func process(mth: String) -> String {
97 let whitespace = CharacterSet.whitespaces
104 let whitespace = CharacterSet.whitespaces
98
105
106+ var seperated = false
107+ var afterSeparator: [String.SubSequence] = []
108+
99 var out = "<math>"
109 var out = "<math>"
100 let lines = mth.split(separator: "\n", omittingEmptySubsequences: false)
110 let lines = mth.split(separator: "\n", omittingEmptySubsequences: false)
101 .map {$0.trimmingCharacters(in: .whitespaces)}
111 .map {$0.trimmingCharacters(in: .whitespaces)}
102 var insideMatrix = false
112 var insideMatrix = false
113+ func pureProcess(line: Substring) {
114+ out.append("<mtr>")
103− for line in lines {
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) {
104 switch line {
142 switch line {
105 case "":
143 case "":
106− if insideMatrix {
144+ checkInsideMatrix()
107− out.append("</mtable><mo>\(style.closing)</mo></mrow>")
108− insideMatrix = false
109− }
110 default:
145 default:
111 if !insideMatrix {
146 if !insideMatrix {
112 out.append("<mrow><mo>\(style.opening)</mo><mtable>")
147 out.append("<mrow><mo>\(style.opening)</mo><mtable>")
113 insideMatrix = true
148 insideMatrix = true
114 }
149 }
115− out.append("<mtr>")
150+ pureProcess(line: line)
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>")
122 }
151 }
152+
123 }
153 }
154+ for line in lines {
124− if insideMatrix {
155+ if !insideMatrix {
125− out.append("</mtable><mo>\(style.closing)</mo></mrow>")
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+ }
126 }
173 }
174+ checkInsideMatrix()
127 out.append("</math>")
175 out.append("</math>")
128 return out
176 return out
129}
177}
README.md+10−1
@@ -16,10 +16,18 @@ You can also use [Markdown](https://en.wikipedia.org/wiki/Markdown)-style text e
16
16
17```
17```
18***1*** 2 __3__
18***1*** 2 __3__
19−3 _2_ 8
19+ 3 _2_ 8
20```
20```
21![](images/mat2.png)
21![](images/mat2.png)
22
22
23+You can use `_` to leave an empty space in the matrix
24+
25+```
26+***1*** 2 __3__ | _
27+ _ _2_ 8 | 4
28+```
29+![](images/mat3.png)
30+
23## Usage
31## Usage
24`mathdown -r -i input.mth -o output.xml`
32`mathdown -r -i input.mth -o output.xml`
25
33
@@ -28,3 +36,4 @@ You can also use [Markdown](https://en.wikipedia.org/wiki/Markdown)-style text e
28- `-o <file path>` Specifies the output pathfile
36- `-o <file path>` Specifies the output pathfile
29- `-s` Uses square brackets
37- `-s` Uses square brackets
30- `-r` Uses round brackets
38- `-r` Uses round brackets
39+- `-l` Uses line brackets
images/mat1.png

Binary file not shown.

images/mat2.png

Binary file not shown.

images/mat3.pngnew file

Binary file not shown.