Snippets

Kirk Duncan polar-bookshelf.sh

Created by Kirk Duncan last modified
#! /usr/bin/env bash

# DISCLAIMER: THE SCRIPT IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND.
# BECAUSE I HAVEN'T USED IT IN AGES.
# -------------------------------------------------------------------------------------------------- #
function print_help() {
cat << EOF

Usage: ${0##*/} [prince|pandoc] [http|html] name

URI = scheme:[//authority]path[?query][#fragment]

Requirements:
  pandoc : https://pandoc.org/
  prince : https://www.princexml.com/
  polar  : https://github.com/burtonator/polar-bookshelf

Errors:
  Could not convert svg:
  rsvg-convert  : https://github.com/miyako/console-rsvg-convert
  pandoc-svg.py : https://gist.github.com/jeromerobert/3996eca3acd12e4c3d40#file-pandoc-svg-py

Security:
  The HTML generated by pandoc is not guaranteed to be safe.
  To be safe, you should run all the generated HTML through an HTML sanitizer.

Example as at 2020-05-22:
$ html-to-pdf.sh prince https://ucsd-progsys.github.io/liquidhaskell-blog/ Liquid-Haskell-Blog
$ html-to-pdf.sh pandoc https://ucsd-progsys.github.io/liquidhaskell-blog/ Liquid-Haskell-Blog

EOF
}
# -------------------------------------------------------------------------------------------------- #
if [ $# -eq 0 ]; then print_help; exit; fi
# -------------------------------------------------------------------------------------------------- #
function check_dependency() {
  if ! (type "$1" > /dev/null 2>& 1); then
    echo "html-to-pdf: missing dependency: can't find $1" 1>& 2
    exit 1
  fi
}
# -------------------------------------------------------------------------------------------------- #
function input() {
  local ENGINES=(prince pandoc)

  if [ "$ENGINE" = "${ENGINES[0]}" ]; then
    prince --input=html --page-size=A4 --output="$PDF" "$HTTP"
  else
    pandoc --standalone --from=html --to=pdf --output="$PDF" "$HTTP"
  fi
}
# -------------------------------------------------------------------------------------------------- #
function output() { polar "$PDF"; }
# -------------------------------------------------------------------------------------------------- #
function cleanup() { rm "$PDF"; }
# -------------------------------------------------------------------------------------------------- #
function main() {
  check_dependency pandoc
  check_dependency prince
  input
  output
  cleanup
}
# -------------------------------------------------------------------------------------------------- #
ENGINE="$1"
HTTP="$2"
PDF="$3.pdf"
# -------------------------------------------------------------------------------------------------- #
main
exit $?
#! /usr/bin/env bash

# DISCLAIMER: THE SCRIPT IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND.
# BECAUSE I HAVEN'T USED IT IN AGES.
# -------------------------------------------------------------------------------------------------- #
function print_help() {
cat << EOF

Usage: ${0##*/} [prince|pandoc] [http|md] name

URI = scheme:[//authority]path[?query][#fragment]

Requirements:
  pandoc : https://pandoc.org/
  prince : https://www.princexml.com/
  polar  : https://github.com/burtonator/polar-bookshelf

Errors:
  Could not convert svg:
  rsvg-convert  : https://github.com/miyako/console-rsvg-convert
  pandoc-svg.py : https://gist.github.com/jeromerobert/3996eca3acd12e4c3d40#file-pandoc-svg-py

Security:
  The HTML generated by pandoc is not guaranteed to be safe.
  To be safe, you should run all the generated HTML through an HTML sanitizer.

Example as at 2020-05-22:
$ md-to-pdf.sh pandoc README.md local-readme
$ md-to-pdf.sh prince https://github.com/jgm/pandoc/blob/master/README.md github-readme
$ md-to-pdf.sh pandoc https://bitbucket.org/cloudrepo-examples/maven-library-example/raw/72aa7e3eed2c014e1a2b37b650a940c495abe11b/README.md maven-library-example-readme

EOF
}
# -------------------------------------------------------------------------------------------------- #
if [ $# -eq 0 ]; then print_help; exit; fi
# -------------------------------------------------------------------------------------------------- #
function check_dependency() {
  if ! (type "$1" > /dev/null 2>& 1); then
    echo "md-to-pdf: missing dependency: can't find $1" 1>& 2
    exit 1
  fi
}
# -------------------------------------------------------------------------------------------------- #
function input() {
  local ENGINES=(prince pandoc)

  if [ "$ENGINE" = "${ENGINES[0]}" ]; then
    prince --input=auto --page-size=A4 --output="$PDF" "$MD"
  else
    pandoc --standalone --from=markdown --to=pdf --output="$PDF" "$MD"
  fi
}
# -------------------------------------------------------------------------------------------------- #
function output() { polar "$PDF"; }
# -------------------------------------------------------------------------------------------------- #
function cleanup() { rm "$PDF"; }
# -------------------------------------------------------------------------------------------------- #
function main() {
  check_dependency pandoc
  check_dependency prince
  input
  output
  cleanup
}
# -------------------------------------------------------------------------------------------------- #
ENGINE="$1"
MD="$2"
PDF="$3.pdf"
# -------------------------------------------------------------------------------------------------- #
main
exit $?
#! /usr/bin/env bash

# THIS SCRIPT DOES NOT WORK WITH POLAR 2.0
# THINKING ABOUT FIXING IT FOR LINUX/MAC.
# SUGGESTED BY HELP REQUEST: WAIT[P]ID USING SIGNAL.
# -------------------------------------------------------------------------------------------------- #
function print_help(){
cat << EOF

Usage: ${0##*/} [pdf|http|name]

$ polar filename.pdf
$ polar http://.../URI-ends-with.pdf
$ polar "http://.../URI-does-not-end-with-pdf" <name>
--------^------------------------------------^

URI = scheme:[//authority]path[?query][#fragment]

requirements:
  polar : https://github.com/burtonator/polar-bookshelf
  fd    : https://github.com/sharkdp/fd
  curl  : https://curl.haxx.se/

Example as at 2020-05-22:
$ polar http://ucsd-progsys.github.io/liquidhaskell-tutorial/book.pdf

EOF
}
# -------------------------------------------------------------------------------------------------- #
if [ $# -eq 0 ]; then print_help; exit; fi
# -------------------------------------------------------------------------------------------------- #
function check_dependency() {
  if ! (type "$1" > /dev/null 2>& 1); then
    echo "polar: missing dependency: can't find $1" 1>& 2
    exit 1
  fi
}
# -------------------------------------------------------------------------------------------------- #
function open_polar() {
  PROCESS_NUM="$(pgrep Polar | wc -l | awk '{print $1}')"

  if [ "$PROCESS_NUM" -eq 0 ] ; then
    open -a /Applications/Polar\ Bookshelf.app/
    sleep 25s
  fi
}
# -------------------------------------------------------------------------------------------------- #
function check_curl() {
  if echo "$ARG" | grep -E '^http://|^https://'; then
    if [[ "$NARG" -eq 1 ]] ; then
      curl -O "$ARG"
      ARG="$(fd --show-errors --changed-within 1min --type f --extension pdf --max-depth 1 .)"
    else
      curl "$1" --output "$2.pdf"
      ARG="$2.pdf"
    fi
    CURL=true
  fi
}
# -------------------------------------------------------------------------------------------------- #
function open_pdf() {
  open -a /Applications/Polar\ Bookshelf.app/ "$ARG"
  sleep 5s
}
# -------------------------------------------------------------------------------------------------- #
function main() {
  check_dependency curl
  check_dependency fd
  open_polar
  check_curl "$ARG"
  open_pdf
}
# -------------------------------------------------------------------------------------------------- #
NARG="$#"
ARG="$1"
CURL=false
# -------------------------------------------------------------------------------------------------- #
main
CODE=$?
# -------------------------------------------------------------------------------------------------- #
if $CURL; then rm "$ARG"; fi
# -------------------------------------------------------------------------------------------------- #
exit $CODE

Comments (2)

  1. Kirk Duncan

    Needs to be extended to coexist with other OS native cli open applications.

    CLI_OS="na"
    
    if $(echo "${OSTYPE}" | grep -q msys); then
      CLI_OS="windows"
    elif $(echo "${OSTYPE}" | grep -q darwin); then
      CLI_OS="mac"
    else
      CLI_OS="linux"
    fi
    

    Many ways to do the same task, humans are awesome!

    # OS specific support (must be 'true' or 'false').
    cygwin=false
    msys=false
    darwin=false
    linux=false
    case "`uname`" in
      CYGWIN* )
        cygwin=true
        ;;
      Darwin* )
        darwin=true
        ;;
      Linux* )
        linux=true
        ;;
      MINGW* )
        msys=true
        ;;
    esac
    

    • mac

      • open -a …
    • linux

      • xdg-open $file

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.