markdown-viewer-rs 0.7.1

Render a Markdown file with GitHub styling and live-reload it in your browser
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
//! macOS Finder integration: `--install-finder` / `--uninstall-finder`.
//!
//! Installs, for the current user only (no admin rights needed):
//!
//! - `~/Applications/Markdown Viewer.app`: a small AppleScript app that
//!   registers for Markdown files, so they can be opened with Open With,
//!   double-click (once set as default), or by dropping them on its icon.
//!   Each file is opened with `markdown-viewer --watch --exit-when-closed`.
//! - `~/Library/Services/Open in Markdown Viewer.workflow`: a Quick Action
//!   in Finder's right-click menu that hands the selection to the app.

use std::path::{Path, PathBuf};
use std::process::Command;

use anyhow::{Context, Result, bail};

const APP_NAME: &str = "Markdown Viewer";
const BUNDLE_ID: &str = "io.github.mdellavo.markdown-viewer";
const WORKFLOW_NAME: &str = "Open in Markdown Viewer";
const ICON: &[u8] = include_bytes!("../assets/macos/AppIcon.icns");
const LSREGISTER: &str = "/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister";

/// File extensions we declare as Markdown, in case the system doesn't know
/// some of them.
const EXTENSIONS: &[&str] = &["md", "markdown", "mdown", "mkd", "mkdn"];

struct Paths {
    app: PathBuf,
    workflow: PathBuf,
    log: PathBuf,
}

fn paths() -> Result<Paths> {
    let home = PathBuf::from(std::env::var_os("HOME").context("HOME is not set")?);
    Ok(Paths {
        app: home.join("Applications").join(format!("{APP_NAME}.app")),
        workflow: home
            .join("Library/Services")
            .join(format!("{WORKFLOW_NAME}.workflow")),
        log: home.join("Library/Logs/markdown-viewer.log"),
    })
}

pub fn install() -> Result<()> {
    let p = paths()?;
    let exe = stable_exe_path()?;

    let staging = tempfile::tempdir()?;
    let built = staging.path().join(format!("{APP_NAME}.app"));
    build_app(&built, &exe, &p.log)?;
    replace_dir(&built, &p.app)?;
    run(Command::new(LSREGISTER).arg("-f").arg(&p.app))?;

    write_workflow(&p.workflow)?;
    // Make the Quick Action show up without logging out.
    let _ = Command::new("/System/Library/CoreServices/pbs")
        .arg("-update")
        .status();

    println!("Installed {}", p.app.display());
    println!("Installed {}", p.workflow.display());
    println!();
    println!("In Finder you can now:");
    println!("  - right-click a Markdown file > Open With > {APP_NAME}");
    println!("  - right-click a Markdown file > Quick Actions > {WORKFLOW_NAME}");
    println!("  - make it the default: Get Info on a .md file > Open with >");
    println!("    {APP_NAME} > Change All…");
    println!();
    println!("The app runs {}.", exe.display());
    println!("If that moves, run `markdown-viewer --install-finder` again.");
    Ok(())
}

pub fn uninstall() -> Result<()> {
    let p = paths()?;
    let mut removed = false;
    if p.app.exists() {
        let _ = Command::new(LSREGISTER).arg("-u").arg(&p.app).status();
        std::fs::remove_dir_all(&p.app)
            .with_context(|| format!("cannot remove {}", p.app.display()))?;
        println!("Removed {}", p.app.display());
        removed = true;
    }
    if p.workflow.exists() {
        std::fs::remove_dir_all(&p.workflow)
            .with_context(|| format!("cannot remove {}", p.workflow.display()))?;
        let _ = Command::new("/System/Library/CoreServices/pbs")
            .arg("-update")
            .status();
        println!("Removed {}", p.workflow.display());
        removed = true;
    }
    if !removed {
        println!("Finder integration is not installed.");
    }
    Ok(())
}

/// The path the app should run. Homebrew installs into a versioned Cellar
/// directory that changes on every upgrade, so prefer its stable
/// `<prefix>/bin` link when we're running from one.
fn stable_exe_path() -> Result<PathBuf> {
    let exe = std::env::current_exe().context("cannot find this program's path")?;
    let parts: Vec<_> = exe.components().collect();
    if let Some(i) = parts.iter().position(|c| c.as_os_str() == "Cellar") {
        let prefix: PathBuf = parts[..i].iter().collect();
        let linked = prefix.join("bin").join("markdown-viewer");
        if linked.exists() {
            return Ok(linked);
        }
    }
    Ok(exe)
}

fn build_app(app: &Path, exe: &Path, log: &Path) -> Result<()> {
    let script = app.with_extension("applescript");
    std::fs::write(&script, applescript(exe, log))?;
    run(Command::new("osacompile").arg("-o").arg(app).arg(&script))?;

    let res = app.join("Contents/Resources");
    // Swap the generic droplet icon for ours.
    let _ = std::fs::remove_file(res.join("Assets.car"));
    let _ = std::fs::remove_file(res.join("droplet.icns"));
    std::fs::write(res.join("AppIcon.icns"), ICON)?;

    let plist = app.join("Contents/Info.plist");
    let set = |args: &[&str]| run(Command::new("plutil").args(args).arg(&plist));
    let _ = set(&["-remove", "CFBundleIconName"]);
    set(&["-replace", "CFBundleIconFile", "-string", "AppIcon"])?;
    set(&["-replace", "CFBundleIdentifier", "-string", BUNDLE_ID])?;
    set(&["-replace", "CFBundleName", "-string", APP_NAME])?;
    set(&["-replace", "CFBundleDisplayName", "-string", APP_NAME])?;
    set(&[
        "-replace",
        "CFBundleShortVersionString",
        "-string",
        env!("CARGO_PKG_VERSION"),
    ])?;
    set(&[
        "-replace",
        "CFBundleDocumentTypes",
        "-json",
        &document_types(),
    ])?;
    set(&[
        "-replace",
        "UTImportedTypeDeclarations",
        "-json",
        &imported_types(),
    ])?;

    // Editing the bundle invalidated osacompile's ad-hoc signature.
    run(Command::new("codesign")
        .args(["--force", "--deep", "--sign", "-"])
        .arg(app))?;
    Ok(())
}

/// The app's script. Each file is opened in its own background viewer that
/// exits when its window is closed; output goes to a log for troubleshooting.
fn applescript(exe: &Path, log: &Path) -> String {
    format!(
        r#"-- Generated by `markdown-viewer --install-finder`.
property mdvPath : {exe}
property logPath : {log}

on open theItems
	repeat with anItem in theItems
		my viewFile(POSIX path of anItem)
	end repeat
end open

on run
	set chosen to choose file with prompt "Choose a Markdown file to view:" of type {{"net.daringfireball.markdown", "public.plain-text"}} with multiple selections allowed
	repeat with anItem in chosen
		my viewFile(POSIX path of anItem)
	end repeat
end run

on viewFile(p)
	do shell script quoted form of mdvPath & " --watch --exit-when-closed " & quoted form of p & " >> " & quoted form of logPath & " 2>&1 &"
end viewFile
"#,
        exe = applescript_string(&exe.to_string_lossy()),
        log = applescript_string(&log.to_string_lossy()),
    )
}

fn applescript_string(s: &str) -> String {
    format!("\"{}\"", s.replace('\\', "\\\\").replace('"', "\\\""))
}

fn document_types() -> String {
    format!(
        r#"[{{"CFBundleTypeName":"Markdown document","CFBundleTypeRole":"Viewer","LSHandlerRank":"Alternate","LSItemContentTypes":["net.daringfireball.markdown"],"CFBundleTypeExtensions":{exts}}}]"#,
        exts = json_list(EXTENSIONS)
    )
}

fn imported_types() -> String {
    format!(
        r#"[{{"UTTypeIdentifier":"net.daringfireball.markdown","UTTypeDescription":"Markdown document","UTTypeConformsTo":["public.plain-text"],"UTTypeReferenceURL":"https://daringfireball.net/projects/markdown/","UTTypeTagSpecification":{{"public.filename-extension":{exts},"public.mime-type":["text/markdown"]}}}}]"#,
        exts = json_list(EXTENSIONS)
    )
}

fn json_list(items: &[&str]) -> String {
    let quoted: Vec<String> = items.iter().map(|s| format!("\"{s}\"")).collect();
    format!("[{}]", quoted.join(","))
}

/// The Quick Action: a Finder service that passes the selected files to the
/// app, so all launching logic lives in one place.
fn write_workflow(dir: &Path) -> Result<()> {
    let contents = dir.join("Contents");
    if dir.exists() {
        std::fs::remove_dir_all(dir)?;
    }
    std::fs::create_dir_all(&contents)?;
    std::fs::write(contents.join("Info.plist"), workflow_info_plist())?;
    std::fs::write(contents.join("document.wflow"), workflow_document())?;
    Ok(())
}

fn workflow_info_plist() -> String {
    format!(
        r#"<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>NSServices</key>
	<array>
		<dict>
			<key>NSBackgroundColorName</key>
			<string>background</string>
			<key>NSIconName</key>
			<string>NSActionTemplate</string>
			<key>NSMenuItem</key>
			<dict>
				<key>default</key>
				<string>{WORKFLOW_NAME}</string>
			</dict>
			<key>NSMessage</key>
			<string>runWorkflowAsService</string>
			<key>NSRequiredContext</key>
			<dict>
				<key>NSApplicationIdentifier</key>
				<string>com.apple.finder</string>
			</dict>
			<key>NSSendFileTypes</key>
			<array>
				<string>net.daringfireball.markdown</string>
			</array>
		</dict>
	</array>
</dict>
</plist>
"#
    )
}

fn workflow_document() -> String {
    // `open -a` passes the files to the app as an "open documents" event.
    let command = format!("open -a \"{APP_NAME}\" \"$@\"");
    format!(
        r#"<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>AMApplicationBuild</key>
	<string>528</string>
	<key>AMApplicationVersion</key>
	<string>2.10</string>
	<key>AMDocumentVersion</key>
	<string>2</string>
	<key>actions</key>
	<array>
		<dict>
			<key>action</key>
			<dict>
				<key>AMAccepts</key>
				<dict>
					<key>Container</key>
					<string>List</string>
					<key>Optional</key>
					<true/>
					<key>Types</key>
					<array>
						<string>com.apple.cocoa.path</string>
					</array>
				</dict>
				<key>AMActionVersion</key>
				<string>2.0.3</string>
				<key>AMApplication</key>
				<array>
					<string>Automator</string>
				</array>
				<key>AMParameterProperties</key>
				<dict>
					<key>COMMAND_STRING</key>
					<dict/>
					<key>CheckedForUserDefaultShell</key>
					<dict/>
					<key>inputMethod</key>
					<dict/>
					<key>shell</key>
					<dict/>
					<key>source</key>
					<dict/>
				</dict>
				<key>AMProvides</key>
				<dict>
					<key>Container</key>
					<string>List</string>
					<key>Types</key>
					<array>
						<string>com.apple.cocoa.string</string>
					</array>
				</dict>
				<key>ActionBundlePath</key>
				<string>/System/Library/Automator/Run Shell Script.action</string>
				<key>ActionName</key>
				<string>Run Shell Script</string>
				<key>ActionParameters</key>
				<dict>
					<key>COMMAND_STRING</key>
					<string>{command}</string>
					<key>CheckedForUserDefaultShell</key>
					<true/>
					<key>inputMethod</key>
					<integer>1</integer>
					<key>shell</key>
					<string>/bin/bash</string>
					<key>source</key>
					<string></string>
				</dict>
				<key>BundleIdentifier</key>
				<string>com.apple.RunShellScript</string>
				<key>CFBundleVersion</key>
				<string>2.0.3</string>
				<key>CanShowSelectedItemsWhenRun</key>
				<false/>
				<key>CanShowWhenRun</key>
				<true/>
				<key>Category</key>
				<array>
					<string>AMCategoryUtilities</string>
				</array>
				<key>Class Name</key>
				<string>RunShellScriptAction</string>
				<key>InputUUID</key>
				<string>6B6F5A0E-3C1D-4B57-9F7E-0A1B2C3D4E51</string>
				<key>Keywords</key>
				<array>
					<string>Shell</string>
					<string>Script</string>
				</array>
				<key>OutputUUID</key>
				<string>6B6F5A0E-3C1D-4B57-9F7E-0A1B2C3D4E52</string>
				<key>UUID</key>
				<string>6B6F5A0E-3C1D-4B57-9F7E-0A1B2C3D4E53</string>
				<key>UnlocalizedApplications</key>
				<array>
					<string>Automator</string>
				</array>
				<key>arguments</key>
				<dict/>
				<key>isViewVisible</key>
				<integer>1</integer>
				<key>location</key>
				<string>309.000000:253.000000</string>
				<key>nibPath</key>
				<string>/System/Library/Automator/Run Shell Script.action/Contents/Resources/Base.lproj/main.nib</string>
			</dict>
			<key>isViewVisible</key>
			<integer>1</integer>
		</dict>
	</array>
	<key>connectors</key>
	<dict/>
	<key>workflowMetaData</key>
	<dict>
		<key>applicationBundleIDsByPath</key>
		<dict/>
		<key>applicationPaths</key>
		<array/>
		<key>inputTypeIdentifier</key>
		<string>com.apple.Automator.fileSystemObject</string>
		<key>outputTypeIdentifier</key>
		<string>com.apple.Automator.nothing</string>
		<key>presentationMode</key>
		<integer>15</integer>
		<key>processesInput</key>
		<false/>
		<key>serviceApplicationBundleID</key>
		<string>com.apple.finder</string>
		<key>serviceApplicationPath</key>
		<string>/System/Library/CoreServices/Finder.app</string>
		<key>serviceInputTypeIdentifier</key>
		<string>com.apple.Automator.fileSystemObject</string>
		<key>serviceOutputTypeIdentifier</key>
		<string>com.apple.Automator.nothing</string>
		<key>serviceProcessesInput</key>
		<false/>
		<key>systemImageName</key>
		<string>NSActionTemplate</string>
		<key>useAutomaticInputType</key>
		<false/>
		<key>workflowTypeIdentifier</key>
		<string>com.apple.Automator.servicesMenu</string>
	</dict>
</dict>
</plist>
"#
    )
}

/// Move `from` to `to`, replacing whatever is there.
fn replace_dir(from: &Path, to: &Path) -> Result<()> {
    if let Some(parent) = to.parent() {
        std::fs::create_dir_all(parent)?;
    }
    if to.exists() {
        std::fs::remove_dir_all(to).with_context(|| format!("cannot replace {}", to.display()))?;
    }
    // A rename fails across volumes (temp dir vs. home), so copy then.
    if std::fs::rename(from, to).is_err() {
        run(Command::new("ditto").arg(from).arg(to))?;
    }
    Ok(())
}

fn run(cmd: &mut Command) -> Result<()> {
    let out = cmd
        .output()
        .with_context(|| format!("cannot run {:?}", cmd.get_program()))?;
    if !out.status.success() {
        bail!(
            "{:?} failed: {}",
            cmd.get_program(),
            String::from_utf8_lossy(&out.stderr).trim()
        );
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn applescript_quotes_paths() {
        assert_eq!(applescript_string(r#"/a "b"\c"#), r#""/a \"b\"\\c""#);
        let script = applescript(
            Path::new("/opt/my bin/markdown-viewer"),
            Path::new("/l.log"),
        );
        assert!(script.contains(r#"property mdvPath : "/opt/my bin/markdown-viewer""#));
        assert!(script.contains("--watch --exit-when-closed"));
    }

    #[test]
    fn plist_fragments_are_valid_json() {
        for json in [document_types(), imported_types()] {
            serde_json::from_str::<serde_json::Value>(&json).unwrap();
        }
    }

    #[test]
    fn workflow_plists_parse() {
        let dir = tempfile::tempdir().unwrap();
        write_workflow(&dir.path().join("x.workflow")).unwrap();
        for f in ["Info.plist", "document.wflow"] {
            let path = dir.path().join("x.workflow/Contents").join(f);
            let ok = Command::new("plutil")
                .arg("-lint")
                .arg(&path)
                .status()
                .unwrap();
            assert!(ok.success(), "{f} is not a valid plist");
        }
    }
}