add support for arguments

This commit is contained in:
lee2sman 2021-12-31 03:53:25 -05:00
parent 776c01cee6
commit 7fe6a10fb9
1 changed files with 100 additions and 9 deletions

View File

@ -1,17 +1,108 @@
#!/usr/bin/env fish
# Lee2sman 2021
set theme themes/medium.css
# Defaults
set theme themes/medium.css
set format html
set output .
# get title
set title (readable $argv --properties "title")
function check_prereq
if not command -sq pandoc
echo "Bookmobile requires pandoc but it's not installed. Exiting."
exit 1
end
end
echo "title: " $title
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
# pulldown html page
readable $argv --output originals/$title.html
function check_args
for current_arg in (seq 1 (count $argv))
set next_arg $argv[(math $current_arg + 1)]
pandoc --self-contained -f html -t html -c $theme -o reader/$title.html originals/$title.html
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
pandoc -f html -t markdown_github-raw_html -o markdown/$title.md originals/$title.html
function set_metadata
set -g url $argv[1]
pandoc -f html -t epub -o epubs/$title.epub originals/$title.html
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