-
Notifications
You must be signed in to change notification settings - Fork 205
/
Copy pathmain.swift
56 lines (47 loc) · 1.88 KB
/
main.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
/*
This source file is part of the Swift.org open source project
Copyright (c) 2021 Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception
See https://swift.org/LICENSE.txt for license information
See https://swift.org/CONTRIBUTORS.txt for Swift project authors
*/
import ArgumentParser
import Foundation
import Markdown
struct MarkdownCommand: ParsableCommand {
enum Error: LocalizedError {
case couldntDecodeInputAsUTF8
var errorDescription: String? {
switch self {
case .couldntDecodeInputAsUTF8:
return "Couldn't decode input as UTF-8"
}
}
}
static let configuration = CommandConfiguration(commandName: "markdown", shouldDisplay: false, subcommands: [
DumpTree.self,
Format.self,
])
static func parseFile(at path: String, options: ParseOptions) throws -> (source: String, parsed: Document) {
print(path)
print(Process().currentDirectoryPath)
let data = try Data(contentsOf: URL(fileURLWithPath: path))
guard let inputString = String(data: data, encoding: .utf8) else {
throw Error.couldntDecodeInputAsUTF8
}
return (inputString, Document(parsing: inputString, options: options))
}
static func parseStandardInput(options: ParseOptions) throws -> (source: String, parsed: Document) {
let stdinData: Data
if #available(macOS 10.15.4, *) {
stdinData = try FileHandle.standardInput.readToEnd() ?? Data()
} else {
stdinData = FileHandle.standardInput.readDataToEndOfFile()
}
guard let stdinString = String(data: stdinData, encoding: .utf8) else {
throw Error.couldntDecodeInputAsUTF8
}
return (stdinString, Document(parsing: stdinString, options: options))
}
}
MarkdownCommand.main()