Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

pipe html to a browser

View 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
#!/bin/sh -e
#
# Usage: browser
# pipe html to a browser
# e.g.
# $ echo '<h1>hi mom!</h1>' | browser
# $ ron -5 man/rip.5.ron | browser
 
if [ -t 0 ]; then
if [ -n "$1" ]; then
open $1
else
cat <<usage
Usage: browser
pipe html to a browser
$ echo '<h1>hi mom!</h1>' | browser
$ ron -5 man/rip.5.ron | browser
usage
fi
else
f="/tmp/browser.$RANDOM.html"
cat /dev/stdin > $f
open $f
fi

Installation:

brew install browser

Or, if you must:

 sudo curl https://gist.github.com/raw/318247/browser -o /usr/local/bin/browser
 sudo chmod +x /usr/local/bin/browser

Just in case anyone can't figure this out:
TextMate command:
cat $TM_FILE | /usr/local/bin/browser

Small point, but "...hi mom!..." should be '...hi mom!...' in the usage example in the comments. Strong quoting is needed to avoid the dreaded "bash: !: event not found" error.

/Users/humza/homebrew/bin/browser: line 2: syntax error near unexpected token '<'
'Users/humza/homebrew/bin/browser: line 2: '

301 Moved Permanently' I get this for some reason

The URL for installation should be https:

sudo curl https://gist.github.com/raw/318247/browser -o /usr/local/bin/browser
sudo chmod +x /usr/local/bin/browser
Ps0ke commented

In bash you can do <(foo) to achieve the same as zsh's =(foo). But it doesn't work with the open(1) utility for me. How should it figure out the file type anyway?

Zeokat usefull piece of code.

Try

:; I="$(basename $0)"; [ -z $BROWSER ] && open=open || open=$BROWSER

[ $# -eq 0 ] && set -- -; [ "$1" = "-" ] && [ -t 0 ] &&
  echo "Usage: [ COMMAND | $I ] | [ $I < FILE ] | [ $I FILE ... ]" >&2 && exit

for what; do where="/tmp/$(od -N2 < /dev/urandom | openssl dgst -sha1).html"
  cat "$what" > "$where" && $open "$where"; sleep 1 && rm -f "$where" & done

Some advantages:

  • Respect $BROWSER. Can be BROWSER=lynx, BROWSER='open -a Firefox', etc.
  • No bashisms ($RANDOM) or zsh-isms (=(...)).
  • Correctly handles arguments that do not end in .html.
  • Correctly handles multiple arguments and shell globs.
  • Cleans up after itself.
  • Doesn't even need a shebang!

Or, you might prefer what I use: avoid leaving your terminal at all, and:

cat "$what" > "$where" && qlmanage -p "$where" >/dev/null 2>&1; sleep 1 ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.