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
484
485
486
487
488
//! Opening the page in a browser: as a minimal app window, a new window, or
//! a tab.
//!
//! There's no cross-browser API for this, so we work out which browser will
//! be used (the one given with `--browser`, or the system default) and use
//! that browser's own mechanism:
//!
//! - App window (no tabs or address bar): `--app=<url>`, Chromium-family only.
//! - New window: `--new-window <url>` for Chromium and Firefox; AppleScript
//!   `make new document` for Safari.
//! - Each mode falls back to the next (app → window → tab) when the browser
//!   can't do it.

use std::process::{Command, Stdio};

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

/// How to show the page.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Mode {
    /// A minimal window without tabs, address bar or toolbar.
    #[default]
    App,
    /// A normal new browser window.
    Window,
    /// A tab in an existing window.
    Tab,
}

/// What actually got opened.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Opened {
    /// A window of its own (app window or new window).
    Window,
    /// A tab, possibly in a window the user was already using.
    Tab,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Kind {
    /// Chrome, Chromium, Brave, Edge, Vivaldi, Opera.
    Chromium,
    /// Firefox and LibreWolf.
    Firefox,
    Safari,
    Other,
}

/// Classify a browser by app name, bundle id, command, or `.desktop` id.
pub fn classify(name: &str) -> Kind {
    let n = name.to_ascii_lowercase();
    let has = |s: &str| n.contains(s);
    if has("chrome")
        || has("chromium")
        || has("brave")
        || has("edge")
        || has("vivaldi")
        || has("opera")
    {
        Kind::Chromium
    } else if has("firefox") || has("librewolf") {
        Kind::Firefox
    } else if has("safari") {
        Kind::Safari
    } else {
        Kind::Other
    }
}

/// The browser to use: what the user named, or the system default.
#[derive(Debug, Clone, PartialEq, Eq)]
enum Target {
    /// `--browser NAME` (macOS app name, or a command on Linux).
    Named(String),
    /// macOS default browser by bundle id.
    #[cfg_attr(not(target_os = "macos"), allow(dead_code))]
    Bundle(String),
    /// Linux default browser as a command name.
    #[cfg_attr(target_os = "macos", allow(dead_code))]
    Command(String),
    /// Couldn't tell; use the OS default opener.
    Unknown,
}

/// Chromium browsers with a working `--app` mode (Opera dropped it).
fn supports_app_mode(name: &str) -> bool {
    classify(name) == Kind::Chromium && !name.to_ascii_lowercase().contains("opera")
}

/// Open `url` (an `http://` or `file://` URL).
pub fn open(url: &str, browser: Option<&str>, mode: Mode) -> Result<Opened> {
    let target = match browser {
        Some(name) => Target::Named(name.to_string()),
        None => default_browser(),
    };
    if mode != Mode::Tab {
        match window_command(url, &target, mode) {
            Some(cmd) => match spawn(cmd) {
                Ok(()) => return Ok(Opened::Window),
                Err(err) => warn(&format!(
                    "markdown-viewer: couldn't open a separate window ({err:#}); opening in a tab"
                )),
            },
            None => warn(&format!(
                "markdown-viewer: don't know how to open a separate window in {}; opening in a tab",
                describe(&target)
            )),
        }
    }
    match tab_command(url, &target) {
        Some(cmd) => spawn(cmd)?,
        None => opener::open_browser(url).with_context(|| format!("cannot open {url}"))?,
    }
    Ok(Opened::Tab)
}

/// Print a warning without panicking if stderr has been closed.
fn warn(msg: &str) {
    use std::io::Write as _;
    let _ = writeln!(std::io::stderr(), "{msg}");
}

/// A program and its arguments.
type Cmd = (String, Vec<String>);

fn cmd(program: &str, args: &[&str]) -> Cmd {
    (
        program.to_string(),
        args.iter().map(|s| s.to_string()).collect(),
    )
}

/// Command for an app window or new window. Asking for an app window from a
/// browser without app mode gets a new window instead.
fn window_command(url: &str, target: &Target, mode: Mode) -> Option<Cmd> {
    let name = match target {
        Target::Named(n) | Target::Bundle(n) | Target::Command(n) => n,
        Target::Unknown => return None,
    };
    let app_flag = format!("--app={url}");
    let browser_args: Vec<&str> = match classify(name) {
        Kind::Chromium if mode == Mode::App && supports_app_mode(name) => vec![&app_flag],
        Kind::Chromium | Kind::Firefox => vec!["--new-window", url],
        Kind::Safari => return cfg!(target_os = "macos").then(|| safari_new_window(url)),
        Kind::Other => return None,
    };
    Some(match target {
        // `open -n` hands the arguments to the browser, which forwards them
        // to its already-running instance if there is one.
        Target::Named(app) if cfg!(target_os = "macos") => cmd(
            "open",
            &[&["-na", app, "--args"], &browser_args[..]].concat(),
        ),
        Target::Bundle(id) => cmd(
            "open",
            &[&["-nb", id, "--args"], &browser_args[..]].concat(),
        ),
        Target::Named(c) | Target::Command(c) => cmd(c, &browser_args),
        Target::Unknown => unreachable!(),
    })
}

fn tab_command(url: &str, target: &Target) -> Option<Cmd> {
    match target {
        Target::Named(app) if cfg!(target_os = "macos") => Some(cmd("open", &["-a", app, url])),
        Target::Named(c) => Some(cmd(c, &[url])),
        _ => None,
    }
}

/// Bring an existing window showing `url` (or a page under it) to the
/// front. Returns false if no such window was found or this browser can't
/// be scripted (only Chrome-family browsers and Safari on macOS can).
pub fn focus(url: &str, browser: Option<&str>) -> bool {
    if !cfg!(target_os = "macos") {
        return false;
    }
    let target = match browser {
        Some(name) => Target::Named(name.to_string()),
        None => default_browser(),
    };
    let (app, kind) = match &target {
        Target::Bundle(id) => (format!("application id \"{id}\""), classify(id)),
        Target::Named(name) => (
            format!("application \"{}\"", name.replace('"', "")),
            classify(name),
        ),
        _ => return false,
    };
    let (tabs, select) = match kind {
        Kind::Chromium => ("tabs of w", "set active tab index of w to i"),
        Kind::Safari => ("tabs of w", "set current tab of w to t"),
        _ => return false,
    };
    // The URL arrives as an argument; it's never spliced into the script.
    // `running` is checked first so this never launches the browser.
    let script = format!(
        "on run argv\n\
         set wanted to item 1 of argv\n\
         if not ({app} is running) then return \"no\"\n\
         tell {app}\n\
         repeat with w in windows\n\
         set i to 0\n\
         repeat with t in {tabs}\n\
         set i to i + 1\n\
         if (URL of t) starts with wanted then\n\
         {select}\n\
         set index of w to 1\n\
         activate\n\
         return \"yes\"\n\
         end if\n\
         end repeat\n\
         end repeat\n\
         end tell\n\
         return \"no\"\n\
         end run"
    );
    Command::new("osascript")
        .args(["-e", &script, url])
        .stderr(Stdio::null())
        .output()
        .is_ok_and(|o| String::from_utf8_lossy(&o.stdout).trim() == "yes")
}

/// The URL is passed as an argument, never spliced into the script.
fn safari_new_window(url: &str) -> Cmd {
    cmd(
        "osascript",
        &[
            "-e",
            "on run argv",
            "-e",
            "tell application \"Safari\"",
            "-e",
            "make new document with properties {URL:item 1 of argv}",
            "-e",
            "activate",
            "-e",
            "end tell",
            "-e",
            "end run",
            url,
        ],
    )
}

fn describe(target: &Target) -> String {
    match target {
        Target::Named(n) | Target::Bundle(n) | Target::Command(n) => n.clone(),
        Target::Unknown => "the default browser".into(),
    }
}

/// Run a launcher command. It's put in its own process group so Ctrl-C in
/// the terminal doesn't also kill a browser we started, and reaped on a
/// background thread so it never blocks us.
fn spawn((program, args): Cmd) -> Result<()> {
    let mut command = Command::new(&program);
    command
        .args(&args)
        .stdin(Stdio::null())
        .stdout(Stdio::null())
        .stderr(Stdio::null());
    #[cfg(unix)]
    std::os::unix::process::CommandExt::process_group(&mut command, 0);
    let mut child = command
        .spawn()
        .with_context(|| format!("cannot run {program}"))?;
    // `open` and `osascript` finish quickly and report failure via exit code.
    if program == "open" || program == "osascript" {
        let status = child.wait()?;
        if !status.success() {
            bail!("{program} exited with {status}");
        }
    } else {
        std::thread::spawn(move || child.wait());
    }
    Ok(())
}

#[cfg(target_os = "macos")]
fn default_browser() -> Target {
    // LaunchServices records the user's choice; with no entry, it's Safari.
    let plist = std::env::var_os("HOME").map(|h| {
        std::path::Path::new(&h).join(
            "Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure.plist",
        )
    });
    let Some(plist) = plist else {
        return Target::Unknown;
    };
    let output = Command::new("plutil")
        .args(["-extract", "LSHandlers", "json", "-o", "-"])
        .arg(&plist)
        .output();
    match output {
        Ok(out) if out.status.success() => {
            match handler_from_launchservices(&String::from_utf8_lossy(&out.stdout)) {
                Some(id) => Target::Bundle(id),
                None => Target::Bundle("com.apple.safari".into()),
            }
        }
        // No preferences file yet means the user never changed the default.
        _ if !plist.exists() => Target::Bundle("com.apple.safari".into()),
        _ => Target::Unknown,
    }
}

#[cfg(not(target_os = "macos"))]
fn default_browser() -> Target {
    let out = Command::new("xdg-settings")
        .args(["get", "default-web-browser"])
        .output();
    match out {
        Ok(out) if out.status.success() => {
            desktop_to_command(String::from_utf8_lossy(&out.stdout).trim())
                .map(Target::Command)
                .unwrap_or(Target::Unknown)
        }
        _ => Target::Unknown,
    }
}

/// Pick the https (else http) handler's bundle id from LaunchServices JSON.
#[cfg_attr(not(target_os = "macos"), allow(dead_code))]
fn handler_from_launchservices(json: &str) -> Option<String> {
    let handlers: Vec<serde_json::Value> = serde_json::from_str(json).ok()?;
    ["https", "http"].iter().find_map(|scheme| {
        handlers
            .iter()
            .find(|h| h["LSHandlerURLScheme"].as_str() == Some(scheme))
            .and_then(|h| h["LSHandlerRoleAll"].as_str())
            .map(str::to_string)
    })
}

/// Map a `.desktop` id (e.g. `google-chrome.desktop`, `firefox_firefox.desktop`
/// for snaps) to a command, for browsers we know how to drive.
#[cfg_attr(target_os = "macos", allow(dead_code))]
fn desktop_to_command(desktop: &str) -> Option<String> {
    let id = desktop.strip_suffix(".desktop").unwrap_or(desktop);
    let id = id.split('_').next_back().unwrap_or(id);
    const KNOWN: &[&str] = &[
        "google-chrome",
        "google-chrome-stable",
        "google-chrome-beta",
        "chromium",
        "chromium-browser",
        "brave-browser",
        "microsoft-edge",
        "microsoft-edge-stable",
        "vivaldi-stable",
        "opera",
        "firefox",
        "firefox-esr",
        "librewolf",
    ];
    KNOWN.contains(&id).then(|| id.to_string())
}

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

    #[test]
    fn classifies_browsers() {
        assert_eq!(classify("com.google.chrome"), Kind::Chromium);
        assert_eq!(classify("Google Chrome"), Kind::Chromium);
        assert_eq!(classify("com.microsoft.edgemac"), Kind::Chromium);
        assert_eq!(classify("brave-browser"), Kind::Chromium);
        assert_eq!(classify("org.mozilla.firefox"), Kind::Firefox);
        assert_eq!(classify("com.apple.Safari"), Kind::Safari);
        assert_eq!(classify("company.thebrowser.Browser"), Kind::Other);
    }

    #[test]
    fn app_mode_for_chromium() {
        let chrome = Target::Bundle("com.google.chrome".into());
        assert_eq!(
            window_command("http://x/", &chrome, Mode::App),
            Some(cmd(
                "open",
                &["-nb", "com.google.chrome", "--args", "--app=http://x/"]
            ))
        );
        let brave = Target::Command("brave-browser".into());
        assert_eq!(
            window_command("http://x/", &brave, Mode::App),
            Some(cmd("brave-browser", &["--app=http://x/"]))
        );
    }

    #[test]
    fn app_mode_falls_back_to_new_window() {
        let firefox = Target::Command("firefox".into());
        assert_eq!(
            window_command("http://x/", &firefox, Mode::App),
            Some(cmd("firefox", &["--new-window", "http://x/"]))
        );
        let opera = Target::Command("opera".into());
        assert_eq!(
            window_command("http://x/", &opera, Mode::App),
            Some(cmd("opera", &["--new-window", "http://x/"]))
        );
    }

    #[test]
    fn new_window_for_default_chrome_uses_bundle_id() {
        let c = window_command(
            "http://x/",
            &Target::Bundle("com.google.chrome".into()),
            Mode::Window,
        );
        assert_eq!(
            c,
            Some(cmd(
                "open",
                &[
                    "-nb",
                    "com.google.chrome",
                    "--args",
                    "--new-window",
                    "http://x/"
                ]
            ))
        );
    }

    #[test]
    fn new_window_for_linux_command() {
        let c = window_command(
            "http://x/",
            &Target::Command("firefox".into()),
            Mode::Window,
        );
        assert_eq!(c, Some(cmd("firefox", &["--new-window", "http://x/"])));
    }

    #[test]
    fn no_new_window_for_unknown_browsers() {
        for mode in [Mode::App, Mode::Window] {
            assert_eq!(window_command("u", &Target::Unknown, mode), None);
            assert_eq!(
                window_command("u", &Target::Command("lynx".into()), mode),
                None
            );
        }
    }

    #[cfg(target_os = "macos")]
    #[test]
    fn safari_url_is_an_argument_not_script() {
        let (prog, args) = window_command(
            "http://x/\"evil",
            &Target::Bundle("com.apple.safari".into()),
            Mode::App,
        )
        .unwrap();
        assert_eq!(prog, "osascript");
        assert_eq!(args.last().unwrap(), "http://x/\"evil");
        assert!(args[..args.len() - 1].iter().all(|a| !a.contains("evil")));
    }

    #[test]
    fn reads_launchservices_handler() {
        let json = r#"[{"LSHandlerContentType":"public.html","LSHandlerRoleAll":"x"},
                       {"LSHandlerURLScheme":"http","LSHandlerRoleAll":"org.mozilla.firefox"},
                       {"LSHandlerURLScheme":"https","LSHandlerRoleAll":"com.google.chrome"}]"#;
        assert_eq!(
            handler_from_launchservices(json).as_deref(),
            Some("com.google.chrome")
        );
        assert_eq!(handler_from_launchservices("[]"), None);
    }

    #[test]
    fn maps_desktop_ids() {
        assert_eq!(
            desktop_to_command("google-chrome.desktop").as_deref(),
            Some("google-chrome")
        );
        assert_eq!(
            desktop_to_command("firefox_firefox.desktop").as_deref(),
            Some("firefox")
        );
        assert_eq!(desktop_to_command("org.gnome.Epiphany.desktop"), None);
    }
}