-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Decompose Markdown.swift; Add simple CLI
- Loading branch information
Showing
7 changed files
with
178 additions
and
146 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Markdown lexer implementation | ||
// (c) Kyrylo Khlopko | ||
|
||
internal enum MarkdownToken { | ||
case newline | ||
case line(String) | ||
case header(HeaderLevel) | ||
} | ||
|
||
internal struct Lexer { | ||
private let contents: [Character] | ||
private var lastPos = 0 | ||
|
||
init(contents: String) { | ||
self.contents = Array(contents) | ||
} | ||
|
||
init(contents: [Character]) { | ||
self.contents = contents | ||
} | ||
|
||
mutating func nextTok() -> MarkdownToken? { | ||
guard lastPos < contents.count else { | ||
return nil | ||
} | ||
let start = lastPos | ||
switch contents[lastPos] { | ||
case "\n": | ||
lastPos += 1 | ||
return .newline | ||
case "#": | ||
return header(start: start) | ||
default: | ||
return line(start: start) | ||
} | ||
} | ||
|
||
private mutating func header(start: Int) -> MarkdownToken { | ||
var level = 0 | ||
while lastPos < contents.count && contents[lastPos] == "#" { | ||
level += 1 | ||
lastPos += 1 | ||
} | ||
guard | ||
let headerLevel = HeaderLevel(rawValue: level), | ||
contents[lastPos] == " " | ||
else { | ||
return line(start: start) | ||
} | ||
lastPos += 1 | ||
return .header(headerLevel) | ||
} | ||
|
||
private mutating func line(start: Int) -> MarkdownToken { | ||
guard start < contents.count else { | ||
return .line(String(contents[start...])) | ||
} | ||
while lastPos < contents.count && contents[lastPos] != "\n" { | ||
lastPos += 1 | ||
} | ||
return .line(String(contents[start..<lastPos])) | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Markdown parser implementation | ||
// (c) Kyrylo Khlopko | ||
|
||
internal struct Parser { | ||
private var lexer: Lexer | ||
|
||
init(contents: String) { | ||
lexer = Lexer(contents: contents) | ||
} | ||
|
||
init(contents: [Character]) { | ||
lexer = Lexer(contents: contents) | ||
} | ||
|
||
private var blocks: [Block] = [] | ||
private var paragraphValue: String = "" | ||
private var readingParagraph = false | ||
|
||
mutating func parse() -> [Block] { | ||
setup() | ||
while let tok = lexer.nextTok() { | ||
switch tok { | ||
case .newline: | ||
checkParagraph() | ||
case let .line(value): | ||
parseLine(value: value) | ||
case let .header(level): | ||
checkParagraph() | ||
parseHeader(level: level) | ||
} | ||
} | ||
checkParagraph() | ||
return blocks | ||
} | ||
|
||
private mutating func setup() { | ||
blocks = [] | ||
paragraphValue = "" | ||
readingParagraph = false | ||
} | ||
|
||
private mutating func parseLine(value: String) { | ||
if readingParagraph { | ||
paragraphValue += "\n" | ||
paragraphValue += value | ||
} else { | ||
readingParagraph = true | ||
paragraphValue = value | ||
} | ||
_ = lexer.nextTok() // consume newline | ||
} | ||
|
||
private mutating func parseHeader(level: HeaderLevel) { | ||
let nextTok = lexer.nextTok() | ||
var headerValue: Block = .p([.text("", .regular)]) | ||
if case let .line(value) = nextTok { | ||
headerValue = .p([.text(value, .regular)]) | ||
} | ||
blocks.append(.h(level, headerValue)) | ||
_ = lexer.nextTok() // consume newline | ||
} | ||
|
||
private mutating func checkParagraph() { | ||
if readingParagraph { | ||
blocks.append(.p([.text(paragraphValue, .regular)])) | ||
readingParagraph = false | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters