-
Notifications
You must be signed in to change notification settings - Fork 16
/
CodePreview.swift
136 lines (122 loc) Β· 3.04 KB
/
CodePreview.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import Foundation
import os.log
let dotfileLexers = [
// Files with syntax supported by Chroma
".bashrc": ".bashrc",
".vimrc": ".vimrc",
".zprofile": "zsh",
".zshrc": ".zshrc",
"dockerfile": "Dockerfile",
"gemfile": "Gemfile",
"gnumakefile": "Makefile",
"makefile": "Makefile",
"pkgbuild": "PKGBUILD",
"rakefile": "Rakefile",
// Files for which a different, similar syntax is used
".dockerignore": "bash",
".editorconfig": "ini",
".gitattributes": "bash",
".gitconfig": "ini",
".gitignore": "bash",
".npmignore": "bash",
".zsh_history": "txt",
]
let fileExtensionLexers = [
// Files with syntax supported by Chroma
"alfredappearance": "json",
"cls": "tex",
"csproj": "xml",
"entitlements": "xml",
"hbs": "handlebars",
"iml": "xml",
"plist": "xml",
"props": "xml",
"resolved": "json",
"scpt": "applescript",
"scptd": "applescript",
"spf": "xml",
"spTheme": "xml",
"storyboard": "xml",
"stringsdict": "xml",
"sty": "tex",
"targets": "xml",
"webmanifest": "json",
"xcscheme": "xml",
"xib": "xml",
// Files for which a different, similar syntax is used
"liquid": "twig",
"modulemap": "hcl",
"njk": "twig",
"pbxproj": "txt",
"sln": "txt",
"strings": "c",
]
class CodePreview: Preview {
private let chromaStylesheetURL = Bundle.main.url(
forResource: "shared-chroma",
withExtension: "css"
)
required init() {}
/// Returns the name of the Chroma lexer to use for the file. This is determined based on the
/// file name/extension.
private func getLexer(fileURL: URL) -> String {
if fileURL.pathExtension.isEmpty {
// Dotfile
return dotfileLexers[fileURL.lastPathComponent.lowercased(), default: "autodetect"]
} else if fileURL.pathExtension.lowercased() == "dist" {
// .dist file
return getLexer(fileURL: fileURL.deletingPathExtension())
} else {
// File with extension
return fileExtensionLexers[
fileURL.pathExtension.lowercased(),
default: fileURL.pathExtension
]
}
}
func getSource(file: File) throws -> String {
try file.read()
}
private func getHTML(file: File) throws -> String {
var source: String
do {
source = try getSource(file: file)
} catch {
os_log(
"Could not read code file: %{public}s",
log: Log.parse,
type: .error,
error.localizedDescription
)
throw error
}
let lexer = getLexer(fileURL: file.url)
do {
return try HTMLRenderer.renderCode(source, lexer: lexer)
} catch {
os_log(
"Could not generate code HTML: %{public}s",
log: Log.render,
type: .error,
error.localizedDescription
)
throw error
}
}
private func getStylesheets() -> [Stylesheet] {
var stylesheets = [Stylesheet]()
// Chroma stylesheet (for code syntax highlighting)
if let chromaStylesheetURL = chromaStylesheetURL {
stylesheets.append(Stylesheet(url: chromaStylesheetURL))
} else {
os_log("Could not find Chroma stylesheet", log: Log.render, type: .error)
}
return stylesheets
}
func createPreviewVC(file: File) throws -> PreviewVC {
WebPreviewVC(
html: try getHTML(file: file),
stylesheets: getStylesheets()
)
}
}