-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmd2gd
executable file
·64 lines (42 loc) · 1.5 KB
/
md2gd
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
#!/usr/bin/env bash
[ -f "$HOME/.md2gdrc" ] && source "$HOME/.md2gdrc"
import () {
# If reference file exists, include it as a pandoc argument
[ -f "$MD2GD_DIR/reference.docx" ] && REFERENCE_DOCX="--reference-docx=$MD2GD_DIR/reference.docx"
temp_file=$(mktemp --suffix=".docx" "$1.XXX")
pandoc "$1" --from markdown --to docx "$REFERENCE_DOCX" -o "$temp_file"
[ $? -ne 0 ] && { printf "md2gd: Conversion failed\n"; rm "$temp_file"; exit 1; }
doc_id=($(gdrive import "$temp_file" | awk '/Imported/{print $2}'))
[ $? -ne 0 ] && { printf "md2gd: Import failed\n"; rm "$temp_file"; exit 1; }
printf "Imported to https://docs.google.com/document/d/%s/edit\n" "$doc_id"
rm "$temp_file"
exit 0
}
export_ () {
file_name="$(gdrive export "$1" --force --mime application/vnd.openxmlformats-officedocument.wordprocessingml.document | cut -d "'" -f2)"
[ $? -ne 0 ] && { printf "md2gd: Export failed\n"; exit 1; }
pandoc "$file_name" --from docx --to markdown_github -o "$file_name.md"
[ $? -ne 0 ] && { printf "md2gd: Conversion failed\n"; rm "$file_name"; exit 1; }
printf "Exported to %s.md\n" "$file_name"
rm "$file_name"
exit 0
}
usage () {
cat <<EOF
usage:
md2gd import <file>
md2gd export <docid>
EOF
}
assert_dep () {
type "$1" > /dev/null 2>&1
[ $? -eq 0 ] && return
[ $? -ne 0 ] && { printf "dependency %s not found\n" "$1"; exit 1; }
}
assert_dep 'pandoc'
assert_dep 'gdrive'
case $1 in
import) shift; import "$@";;
export) shift; export_ "$@";;
*) usage; exit 1;;
esac