109 lines
2.8 KiB
Fish
Executable File
109 lines
2.8 KiB
Fish
Executable File
#!/usr/bin/env fish
|
|
# Lee2sman 2021
|
|
|
|
# Defaults
|
|
set theme themes/medium.css
|
|
set format html
|
|
set output .
|
|
|
|
function check_prereq
|
|
if not command -sq pandoc
|
|
echo "Bookmobile requires pandoc but it's not installed. Exiting."
|
|
exit 1
|
|
end
|
|
end
|
|
|
|
function usage
|
|
echo "Bookmobile: Simple command line script to download articles for offline reading as html, epub or markdown. Saves a simplified html document to current directory as default."
|
|
echo ""
|
|
echo "Optionally, you can specify a theme, format and output directory"
|
|
echo ""
|
|
echo "Usage: ./bookmobile url [ --theme THEME ] [ --format FORMAT ] [ --output OUTPUT ]"
|
|
echo " [-h | --help ]"
|
|
echo ""
|
|
echo "Options:"
|
|
echo ""
|
|
echo " --help"
|
|
echo " -h Display usage info"
|
|
echo ""
|
|
echo " --theme THEME"
|
|
echo " -t THEME Set theme for reader format. Default: medium"
|
|
echo " Theme options: "
|
|
for theme in themes/*.css
|
|
echo (string replace -r '\.[^\.]+$' '' (basename $theme))
|
|
end
|
|
echo ""
|
|
echo " --format FORMAT"
|
|
echo " -f FORMAT Set file format (html, reader, markdown, epub)"
|
|
echo " default: html"
|
|
echo ""
|
|
echo " --output OUTPUT"
|
|
echo " -o OUTPUT Set output location"
|
|
echo " default: current directory"
|
|
echo ""
|
|
end
|
|
|
|
function check_args
|
|
for current_arg in (seq 1 (count $argv))
|
|
set next_arg $argv[(math $current_arg + 1)]
|
|
|
|
switch $argv[$current_arg]
|
|
case -h --help
|
|
usage
|
|
break
|
|
case -t --theme
|
|
echo "theme: " $next_arg
|
|
set -g theme themes/$next_arg.css
|
|
case -f --format
|
|
echo "format: " $next_arg
|
|
set -g format $next_arg
|
|
case -o --output
|
|
echo "output: " $next_arg
|
|
set -g output $next_arg
|
|
end
|
|
end
|
|
end
|
|
|
|
function set_metadata
|
|
set -g url $argv[1]
|
|
|
|
set -g title (readable $url --properties "title")
|
|
|
|
if test -z "$title" #if title was empty, set title to date time
|
|
set -g title (date "+%Y-%m-%d_%H:%M:%S")
|
|
end
|
|
|
|
echo "Title: " $title
|
|
echo "URL: " $url
|
|
end
|
|
|
|
function process_article
|
|
# pulldown html page
|
|
readable $url --output $output/$title.html
|
|
|
|
switch $format
|
|
case html
|
|
#just a placeholder
|
|
echo "Html saved to " $output
|
|
case reader
|
|
pandoc -s $output/$title.html -c $theme -o $output/$title-article.html
|
|
rm -f $output/$title.html
|
|
echo "Reader file saved to " $output " with theme " $theme
|
|
case markdown
|
|
pandoc -f html -t markdown_github-raw_html -o $output/$title.md $output/$title.html
|
|
rm -f $output/$title.html
|
|
echo "Markdown file saved to " $output
|
|
case epub
|
|
pandoc -s $output/$title.html -f html -t epub -o $output/$title.epub
|
|
rm -f $output/$title.html
|
|
echo "Epub saved to " $output
|
|
case '*'
|
|
echo "Invalid output format"
|
|
end
|
|
end
|
|
|
|
check_prereq
|
|
check_args $argv
|
|
set_metadata $argv
|
|
process_article
|