forked from standardebooks/tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-draft
executable file
·178 lines (155 loc) · 5.42 KB
/
create-draft
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/bin/bash
usage(){
fmt <<EOF
DESCRIPTION
Create a skeleton of a new Standard Ebook.
USAGE
create-draft [-e,--email=EMAIL] [-s,--se [-g,--github]] [-f,--force] -a,--author=AUTHOR -t,--title=TITLE [-r,--translator=TRANSLATOR] [-i,--illustrator=ILLUSTRATOR]
Create a new ebook skeleton in the current directory.
If --email is specified, initialize the Git repository using that email address as the main committer.
With --se, initialize a new repository on the Standard Ebook server. Must have SSH access to the Standard Ebook server. With --github, initialize a new Github repository for this ebook. Can only be called with --se.
With --force, don't ask to overwrite an existing draft. directory.
EOF
exit
}
die(){ printf "Error: ${1}\n" 1>&2; exit 1; }
if [ $# -eq 1 ]; then if [ "$1" = "--help" -o "$1" = "-h" ]; then usage; fi fi
#End boilerplate
customEmail=""
author=""
title=""
translator=""
illustrator=""
initializeGithub=false
initializeSe=false
force=false
scriptDir="$(cd -P "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
makeUrlSafePath="${scriptDir}/make-url-safe"
for i in "$@"
do
case $i in
-e=*|--email=*)
customEmail=$(echo "${i}" | sed 's/[-a-zA-Z0-9]*=//')
shift
;;
-g|--github)
initializeGithub=true
shift
;;
-f|--force)
force=true
shift
;;
-s|--se)
initializeSe=true
shift
;;
-a=*|--author=*)
author=$(echo "${i}" | sed 's/[-a-zA-Z0-9]*=//')
shift
;;
-t=*|--title=*)
title=$(echo "${i}" | sed 's/[-a-zA-Z0-9]*=//')
shift
;;
-r=*|--translator=*)
translator=$(echo "${i}" | sed 's/[-a-zA-Z0-9]*=//')
shift
;;
-i=*|--illustrator=*)
illustrator=$(echo "${i}" | sed 's/[-a-zA-Z0-9]*=//')
shift
;;
esac
done
if [ "${author}" = "" ]; then
die "Author is required."
fi
if [ "${title}" = "" ]; then
die "Title is required."
fi
scriptDir="$(cd -P "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
urlAuthor=$("${makeUrlSafePath}" "${author}")
urlTitle=$("${makeUrlSafePath}" "${title}")
urlTranslator=$("${makeUrlSafePath}" "${translator}")
urlIllustrator=$("${makeUrlSafePath}" "${illustrator}")
identifier="${urlAuthor}/${urlTitle}"
titleString="${title}, by ${author}"
if [ "${urlTranslator}" != "" ]; then
identifier="${identifier}/${urlTranslator}"
titleString="${titleString}. Translated by ${translator}"
fi
if [ "${urlIllustrator}" != "" ]; then
identifier="${identifier}/${urlIllustrator}"
titleString="${titleString}. Illustrated by ${illustrator}"
fi
repoName=$(echo "${identifier}" | sed "s|/|_|g")
if [ -d "${repoName}" ]; then
if [ "${force}" != true ]; then
while true; do
read -p "./${repoName}/ already exists. Overwrite? " yn
case $yn in
[Yy]* )
force=true
break
;;
* )
exit
;;
esac
done
fi
if [ "${force}" = true ]; then
rm -rf "${repoName}"
fi
fi
mkdir -p "${repoName}/images"
mkdir -p "${repoName}/src/epub/css"
mkdir -p "${repoName}/src/epub/images"
mkdir -p "${repoName}/src/epub/text"
cp -r "${scriptDir}/templates/META-INF/" "${repoName}/src/"
cp "${scriptDir}/templates/mimetype" "${repoName}/src/"
cp "${scriptDir}/templates/onix.xml" "${repoName}/src/epub/"
cp "${scriptDir}/templates/toc.xhtml" "${repoName}/src/epub/"
cp "${scriptDir}/templates/core.css" "${repoName}/src/epub/css/"
cp "${scriptDir}/templates/logo.svg" "${repoName}/src/epub/images/"
cp "${scriptDir}/templates/imprint.xhtml" "${repoName}/src/epub/text/"
cp "${scriptDir}/templates/uncopyright.xhtml" "${repoName}/src/epub/text/"
cp "${scriptDir}/templates/titlepage.svg" "${repoName}/images/"
cp "${scriptDir}/templates/local.css" "${repoName}/src/epub/css/"
cp "${scriptDir}/templates/colophon.xhtml" "${repoName}/src/epub/text/"
cp "${scriptDir}/templates/titlepage.xhtml" "${repoName}/src/epub/text/"
cp "${scriptDir}/templates/content.opf" "${repoName}/src/epub/"
if [ ${#title} -lt 15 ]; then
cp "${scriptDir}/templates/cover-short.svg" "${repoName}/images/cover.svg"
elif [ ${#title} -ge 30 ]; then
cp "${scriptDir}/templates/cover-long.svg" "${repoName}/images/cover.svg"
else
cp "${scriptDir}/templates/cover.svg" "${repoName}/images/cover.svg"
fi
sed --in-place "s|TITLESTRING|${titleString}|g" "${repoName}/src/epub/text/titlepage.xhtml"
sed --in-place "s|TITLESTRING|${titleString}|g" "${repoName}/images/titlepage.svg"
sed --in-place "s|TITLESTRING|${titleString}|g" "${repoName}/images/cover.svg"
sed --in-place "s|SEIDENTIFIER|${identifier}|g" "${repoName}/src/epub/content.opf"
sed --in-place "s|AUTHOR|${author}|g" "${repoName}/src/epub/content.opf"
sed --in-place "s|TITLE|${title}|g" "${repoName}/src/epub/content.opf"
sed --in-place "s|VCSIDENTIFIER|${repoName}|" "${repoName}/src/epub/content.opf"
sed --in-place "s|SEIDENTIFIER|${identifier}|g" "${repoName}/src/epub/text/colophon.xhtml"
sed --in-place "s|TITLE|${title}|g" "${repoName}/src/epub/text/colophon.xhtml"
sed --in-place "s|AUTHOR|${author}|g" "${repoName}/src/epub/text/colophon.xhtml"
git init --quiet "${repoName}"
cd "${repoName}"
if [ "${customEmail}" != "" ]; then
git config user.email "${customEmail}"
fi
if [ "${initializeSe}" = true ]; then
githubOption=""
if [ "${initializeGithub}" = true ]; then
githubOption="--github"
fi
git remote add origin "standardebooks.org:/standardebooks.org/ebooks/${repoName}.git"
#Set git to automatically push to SE
git config branch.master.remote origin
git config branch.master.merge refs/heads/master
ssh standardebooks.org "/standardebooks.org/scripts/init-se-repo --repo-name=${repoName} --title-string=\"${titleString}\" ${githubOption}"
fi