Skip to content

Commit

Permalink
Add support for more UTIs and file extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelmeuli committed Mar 22, 2020
1 parent 10f51c2 commit 2f324c4
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
19 changes: 18 additions & 1 deletion QLPlugin/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@
<dict>
<key>QLSupportedContentTypes</key>
<array>
<!--
UTIs that macOS does not allow us to handle:
- .pem (public.x509-certificate)
- .plist (com.apple.property-list)
- .ts (could be TypeScript, is identified as public.mpeg-2-transport-stream)
- .xml (public.xml)
-->

<!-- Base UTI -->
<string>public.data</string>

<!-- CSV -->
<string>dyn.ah62d4rv4ge81k2pc</string> <!-- .tab -->
<string>public.comma-separated-values-text</string> <!-- .csv -->
Expand All @@ -45,8 +57,9 @@
<string>com.adobe.jsx</string>
<string>com.apple.applescript.script</string>
<string>com.apple.applescript.text</string>
<string>com.apple.property-list</string>
<string>com.apple.property-list</string> <!-- .plist -->
<string>com.apple.rez-source</string>
<string>com.apple.xcode.entitlements-property-list</string> <!-- .entitlements -->
<string>com.barebones.bbedit.scss-source</string>
<string>com.barebones.bbedit.verilog-hdl-source</string>
<string>com.barebones.bbedit.vhdl-source</string>
Expand All @@ -70,6 +83,7 @@
<string>com.sun.javafx</string>
<string>com.sun.java-class</string>
<string>com.sun.manifest</string>
<string>com.vallettaventures.texpadm.tex</string> <!-- Texpad LaTeX (.cls, .sty, .tex) -->
<string>com.unknown.lhs</string>
<string>org.applescript.source</string>
<string>org.arduino.ino-source</string>
Expand Down Expand Up @@ -120,6 +134,7 @@
<string>org.ocaml.ocaml-source</string>
<string>org.omg.ecore</string>
<string>org.rdf.source</string>
<string>com.runningwithcrayons.alfred.appearance</string> <!-- Alfred theme (.alfredappearance) -->
<string>org.rust-lang.source</string>
<string>org.sbarex.conf</string>
<string>org.sbarex.dart</string>
Expand Down Expand Up @@ -155,13 +170,15 @@
<string>tk.tcl.tcl</string>
<string>tk.tcl.tcl-source</string>
<string>dyn.ah62d4rv4ge8007a</string>
<string>dyn.ah62d4rv4ge80255drq</string> <!-- Lockfile (.lock, e.g. for Yarn and Poetry) -->
<string>dyn.ah62d4rv4ge80c75p</string>
<string>dyn.ah62d4rv4ge80g55sq2</string>
<string>dyn.ah62d4rv4ge80g62</string>
<string>dyn.ah62d4rv4ge80s52</string>
<string>dyn.ah62d4rv4ge80s6xbqv0gn</string>
<string>dyn.ah62d4rv4ge80w5pq</string>
<string>dyn.ah62d4rv4ge80y652</string>
<string>dyn.ah62d4rv4ge81e3pxr70hq3pe</string> <!-- Lockfile (.resolved, for Swift Package Manager) -->
<string>dyn.ah62d4rv4ge81g22</string>
<string>dyn.ah62d4rv4ge81g25brvuu</string>
<string>dyn.ah62d4rv4ge81g25xsq</string>
Expand Down
32 changes: 31 additions & 1 deletion QLPlugin/Renderers/FileTypeRenderers/CodeRenderer.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,39 @@
import Foundation
import os.log

let dotfileLexers = [
".dockerignore": "bash",
".editorconfig": "ini",
".gitattributes": "bash",
".gitignore": "bash",
".npmignore": "bash",
]

let fileExtensionLexers = [
"alfredappearance": "json", // Alfred theme
"cls": "tex", // LaTeX classes file
"entitlements": "xml",
"iml": "xml",
"plist": "xml",
"resolved": "json", // Swift Package Manager lockfile (Package.resolved)
"sty": "tex", // LaTeX styles file
]

class CodeRenderer: Renderer {
private let chromaBinUrl = Bundle.main.url(forAuxiliaryExecutable: "chroma-v0.7.0")

/// Returns the name of the Chroma lexer to use for the file. This is determined based on the
/// file name/extension
private func getLexer() -> String {
if fileExtension.isEmpty {
// Dotfile
return dotfileLexers[fileUrl.lastPathComponent, default: "autodetect"]
} else {
// File with extension
return fileExtensionLexers[fileExtension, default: "autodetect"]
}
}

override func getStylesheets() -> [Stylesheet] {
var stylesheets = super.getStylesheets()
if let cssUrl = chromaCssUrl {
Expand All @@ -22,7 +52,7 @@ class CodeRenderer: Renderer {

let (status, stdout, stderr) = Shell.run(
url: binaryUrlResolved,
arguments: [fileUrl.path, "--html", "--html-only"]
arguments: [fileUrl.path, "--html", "--html-only", "--lexer", getLexer()]
)

guard status == 0 else {
Expand Down

0 comments on commit 2f324c4

Please sign in to comment.