-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmdtopdf.sh
executable file
·101 lines (82 loc) · 2.39 KB
/
mdtopdf.sh
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
#!/bin/bash
# ------------------------------------------------------------------
# [Author] mdtopdf
# Wrapper for converting Markdown to pdf using Pandoc
# and Weasyprint engine.
#
# This script uses shFlags -- Advanced command-line flag
# library for Unix shell scripts.
# https://github.com/kward/shflags
#
# Dependency:
# https://github.com/kward/shflags/blob/master/shflags
# ------------------------------------------------------------------
VERSION=0.1.0
SUBJECT=mdtopdf
USAGE="Usage: mdtopdf -pfisd args"
# --- Option processing --------------------------------------------
if [ $# == 0 ] ; then
echo $USAGE
exit 1;
fi
BASE=/Users/nkashya1/.nkashya1/mdtopdf/
CURRDIR=$(pwd)
. "$BASE"shflags
DEFINE_string prefix '' 'output file prefix' p
DEFINE_string infile '*md' 'input file' f
DEFINE_string indir '' 'input directory' i
DEFINE_string style 'custom' 'stylesheet path' s
DEFINE_string debug '' 'debug mode' d
# parse command line
FLAGS "$@" || exit 1
eval set -- "${FLAGS_ARGV}"
shift $(($OPTIND - 1))
if [ "$FLAGS_prefix" != '' ]; then
outfile="$FLAGS_prefix"
else
outfile=$(date '+%Y%m%d%S')
fi
if [ "$FLAGS_debug" == 1 ]; then
debug='-d'
else
debug=''
fi
if [ "${FLAGS_indir:0:1}" == '/' ] || [ "${FLAGS_infile:0:1}" == '/' ]
then
if [ "$FLAGS_indir" != '' ]; then
in_files=$FLAGS_indir/$FLAGS_infile
else
in_files=$FLAGS_infile
fi
else
if [ "$FLAGS_indir" != '' ]; then
in_files=$CURRDIR/$FLAGS_indir/$FLAGS_infile
else
in_files=$CURRDIR/$FLAGS_infile
fi
fi
normalize="$BASE"normalize.css
stylesheet="$BASE""$FLAGS_style".css
# --- Locks -------------------------------------------------------
LOCK_FILE=/tmp/${SUBJECT}.lock
if [ -f "$LOCK_FILE" ]; then
echo "Script is already running"
exit
fi
trap "rm -f $LOCK_FILE" EXIT
touch $LOCK_FILE
# -- Body ---------------------------------------------------------
echo -e '\n'
echo -e "\t\xf0\x9f\x91\x8c generating $CURRDIR/$outfile.pdf ... \xf0\x9f\x91\x8c"
echo -e "\t ... from $in_files"
echo -e '\n'
# -----------------------------------------------------------------
cd $BASE
pandoc $in_files \
--standalone \
--css=$stylesheet \
--output $CURRDIR/$outfile.html
pagedjs-cli $CURRDIR/$outfile.html \
$debug -h -o $CURRDIR/$outfile.pdf
rm $CURRDIR/$outfile.html
# -----------------------------------------------------------------