feat: customizable html
This commit is contained in:
@@ -11,9 +11,26 @@ class PreviewViewController: NSViewController, QLPreviewingController {
|
|||||||
return NSNib.Name("PreviewViewController")
|
return NSNib.Name("PreviewViewController")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Load resource file either from user documents dir (if exists) or app bundle (default).
|
||||||
|
func bundleFile(filename: String, ext: String) throws -> String {
|
||||||
|
if let appSupport = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
|
||||||
|
let override = appSupport.appendingPathComponent(filename + "." + ext)
|
||||||
|
if FileManager.default.fileExists(atPath: override.path) {
|
||||||
|
return try String(contentsOfFile: override.path, encoding: .utf8)
|
||||||
|
}
|
||||||
|
// else: do NOT copy! Breaks on future updates
|
||||||
|
}
|
||||||
|
// else, load bundle file
|
||||||
|
let path = Bundle.main.url(forResource: filename, withExtension: ext)
|
||||||
|
return try String(contentsOf: path!, encoding: .utf8)
|
||||||
|
}
|
||||||
|
|
||||||
func preparePreviewOfFile(at url: URL) async throws {
|
func preparePreviewOfFile(at url: URL) async throws {
|
||||||
let meta = MetaInfo(url)
|
let meta = MetaInfo(url)
|
||||||
let html = HtmlGenerator(meta).applyHtmlTemplate()
|
let html = HtmlGenerator(meta).generate(
|
||||||
|
template: try bundleFile(filename: "template", ext: "html"),
|
||||||
|
css: try bundleFile(filename: "style", ext: "css"),
|
||||||
|
)
|
||||||
// sure, we could use `WKWebView`, but that requires the `com.apple.security.network.client` entitlement
|
// sure, we could use `WKWebView`, but that requires the `com.apple.security.network.client` entitlement
|
||||||
//let web = WKWebView(frame: self.view.bounds)
|
//let web = WKWebView(frame: self.view.bounds)
|
||||||
let web = WebView(frame: self.view.bounds)
|
let web = WebView(frame: self.view.bounds)
|
||||||
|
|||||||
41
README.md
41
README.md
@@ -24,42 +24,31 @@ I merely want things to be done.
|
|||||||
Also, I've removed support for provisioning profiles (`.mobileprovision`, `.provisionprofile`) to focus on app bundles.
|
Also, I've removed support for provisioning profiles (`.mobileprovision`, `.provisionprofile`) to focus on app bundles.
|
||||||
|
|
||||||
|
|
||||||
## ToDO
|
|
||||||
|
## ToDo
|
||||||
|
|
||||||
- [ ] support for `.apk` files
|
- [ ] support for `.apk` files
|
||||||
|
|
||||||
|
|
||||||
## Development notes
|
|
||||||
|
|
||||||
|
## Features
|
||||||
|
|
||||||
|
### Customize HTML / CSS
|
||||||
|
|
||||||
|
1. Right click on the app and select "Show Package Contents"
|
||||||
|
2. Copy `Contents/Resources/template.html` (or `style.css`)
|
||||||
|
3. Open `~/Library/Containers/de.relikd.QLAppBundle.Preview/Data/Documents/`
|
||||||
|
4. Paste the previous file and modify it to your liking
|
||||||
|
5. `QLAppBundle` will use the new file from now on
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
## Development notes
|
||||||
|
|
||||||
### Debug
|
### Debug
|
||||||
|
|
||||||
You can show Console logs with `subsystem:de.relikd.QLAppBundle`
|
You can show Console logs with `subsystem:de.relikd.QLAppBundle`
|
||||||
|
|
||||||
|
|
||||||
### Compile errors
|
|
||||||
|
|
||||||
If you encounter compile errors like:
|
|
||||||
|
|
||||||
```
|
|
||||||
Command SwiftEmitModule failed with a nonzero exit code
|
|
||||||
```
|
|
||||||
|
|
||||||
or
|
|
||||||
|
|
||||||
```
|
|
||||||
Could not build Objective-C module 'ExtensionFoundation'
|
|
||||||
```
|
|
||||||
|
|
||||||
or `ThumbnailProvider` is throwing lots of errors for undefined classes:
|
|
||||||
|
|
||||||
remove the `SYSTEM_FRAMEWORK_SEARCH_PATHS` attribute from Project > Build Settings then try to compile again (it will fail).
|
|
||||||
Afterwards, restore the value in the attribute.
|
|
||||||
Now, the build index should be up-to-date and the app should compile fine.
|
|
||||||
|
|
||||||
I havent figured out the exact issue, consider it a workaround.
|
|
||||||
It should only be necessary once (or if you delete your `DerivedData` folder).
|
|
||||||
|
|
||||||
|
|
||||||
[1]: https://github.com/ealeksandrov/ProvisionQL
|
[1]: https://github.com/ealeksandrov/ProvisionQL
|
||||||
[2]: https://github.com/ealeksandrov/ProvisionQL/pull/54
|
[2]: https://github.com/ealeksandrov/ProvisionQL/pull/54
|
||||||
|
|||||||
@@ -20,9 +20,6 @@ struct HtmlGenerator {
|
|||||||
procFooterInfo()
|
procFooterInfo()
|
||||||
// App Icon (last, because the image uses a lot of memory)
|
// App Icon (last, because the image uses a lot of memory)
|
||||||
data["AppIcon"] = AppIcon(meta).extractImage(from: plistApp).withRoundCorners().asBase64()
|
data["AppIcon"] = AppIcon(meta).extractImage(from: plistApp).withRoundCorners().asBase64()
|
||||||
// insert CSS styles
|
|
||||||
let cssURL = Bundle.main.url(forResource: "style", withExtension: "css")!
|
|
||||||
data["CSS"] = try! String(contentsOf: cssURL, encoding: .utf8)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
mutating func apply(_ values: [String: String]) {
|
mutating func apply(_ values: [String: String]) {
|
||||||
@@ -39,9 +36,8 @@ struct HtmlGenerator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// prepare html, replace values
|
/// prepare html, replace values
|
||||||
func applyHtmlTemplate() -> String {
|
func generate(template html: String, css: String) -> String {
|
||||||
let templateURL = Bundle.main.url(forResource: "template", withExtension: "html")!
|
let templateValues = data.merging(["CSS": css]) { (_, new) in new }
|
||||||
let html = try! String(contentsOf: templateURL, encoding: .utf8)
|
|
||||||
|
|
||||||
// this is less efficient
|
// this is less efficient
|
||||||
// for (key, value) in templateValues {
|
// for (key, value) in templateValues {
|
||||||
@@ -58,7 +54,7 @@ struct HtmlGenerator {
|
|||||||
rv.append(contentsOf: html[prevLoc ..< start])
|
rv.append(contentsOf: html[prevLoc ..< start])
|
||||||
prevLoc = html.index(start, offsetBy: match!.range.length)
|
prevLoc = html.index(start, offsetBy: match!.range.length)
|
||||||
// append key if exists (else remove template-key)
|
// append key if exists (else remove template-key)
|
||||||
if let value = data[key] {
|
if let value = templateValues[key] {
|
||||||
rv.append(value)
|
rv.append(value)
|
||||||
} else {
|
} else {
|
||||||
// os_log(.debug, log: log, "unknown template key: %{public}@", key)
|
// os_log(.debug, log: log, "unknown template key: %{public}@", key)
|
||||||
|
|||||||
Reference in New Issue
Block a user