-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathOrgDocument.swift
63 lines (55 loc) · 1.59 KB
/
OrgDocument.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//
// OrgDocument.swift
// SwiftOrg
//
// Created by Xiaoxing Hu on 21/09/16.
// Copyright © 2016 Xiaoxing Hu. All rights reserved.
//
import Foundation
public struct OrgDocument: Node {
public var settings = [String: String]()
public var content = [Node]()
public var title: String? {
return settings["TITLE"]
}
public let defaultTodos: [[String]]
public var todos: [[String]] {
if let todo = settings["TODO"] {
let keywords = todo.components(separatedBy: .whitespaces)
var result: [[String]] = [[]]
for keyword in keywords {
if keyword == "|" {
result.append([])
} else {
result[result.endIndex - 1].append(keyword)
}
}
return result
}
return defaultTodos
}
public init(todos: [[String]]) {
defaultTodos = todos
}
public var description: String {
return "OrgDocument(settings: \(settings))\n - \(content)"
}
}
extension OrgParser {
func parseDocument() throws -> OrgDocument {
var index = OrgIndex([0])
while let (_, token) = tokens.peek() {
switch token {
case let .setting(key, value):
_ = tokens.dequeue()
document.settings[key] = value
default:
if let node = try parseSection(index) {
document.content.append(node)
index = index.next
}
}
}
return document
}
}