forked from sjones4/confluence-to-github
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate
executable file
·285 lines (235 loc) · 7.35 KB
/
generate
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#!/bin/bash
usage() {
cat <<USAGE
Usage: $0 [options]
$0 -?|-h|--help|help
Convert a Confluence XML space export to GitHub flavoured Markdown pages.
Options
--confluence-url URL Sets the URL for Confluence links. This should be the
-u URL root URL, as the transformation scripts will add /wiki
as required.
--jira-url URL Sets the URL for JIRA links. Defaults to the Confluence
-j URL URL. The transformation scripts will add /browse as
required for ticket links.
--output PATH Sets the output path.
-o PATH
--intput PATH Sets the intput path.
-i PATH
--format, -F If prettier is installed, runs prettier on the output.
--skip-extract, -E Skip the page extraction and image mapping step.
--skip-images, -I Skip the image copy step.
--skip-markdown, -M Skip the page conversion step.
--clean, -C Clean the output directory before execution. This will
only clean the output required for each step.
--format
--force, -f Force processing.
--debug, -d Enable debug output. May be repeated to increase debug
level.
Debug Levels
0 (default) No debug logging.
1 (-d) Tracing will be enabled to show the parameters to
xsltproc.
2 (-d -d) The output of the image copy mapping script will be
printed before execution. Unless --force is provided,
confirmation will be required.
3 (-d -d -d) xsltproc verbose output will be enabled and the output
will be saved in 'OUTPUT_PATH/log'. This logging is very
verbose.
USAGE
}
declare -a xslt_params
declare -i debug
declare extract_pages copy_images convert_pages input_path output_path \
force script_path confluence_url jira_url clean format
clean=false
convert_pages=true
copy_images=true
extract_pages=true
force=false
format=false
input_path=.
output_path=out
script_path="$(
cd "$(dirname "$0")" || exit 1
pwd -P
)" || {
echo >&2 "Error setting script path."
exit 1
}
while (($#)); do
case "$1" in
-\? | -h | --help | help)
usage
exit 0
;;
--clean | -C) clean=true ;;
--debug | -d) ((debug++)) ;;
--force | -f) force=true ;;
--skip-extract | -E) extract_pages=false ;;
--skip-images | -I) copy_images=false ;;
--skip-markdown | -M) convert_pages=false ;;
--format | -F) format=true ;;
--confluence-url | -u)
confluence_url="${2:?}"
xslt_params+=(--stringparam confluence-url "${2:?}")
shift
;;
--jira-url | -j)
jira_url="${2:?}"
xslt_params+=(--stringparam jira-url "${2:?}")
shift
;;
--output | -o)
output_path="${2:?}"
shift
;;
--input | -i)
if ! [[ -d "${2:?}" ]]; then
echo >&2 "Input path ${2:?} does not exist."
exit 1
fi
input_path="${2:?}"
shift
;;
*)
echo >&2 "Unknown parameter or options '$1'."
exit 1
;;
esac
shift
done
if ((debug > 0)); then
xslt_params+=(--stringparam debug "true")
fi
if ! command -v xsltproc >/dev/null 2>/dev/null; then
echo >&2 "This requires xsltproc to be present."
echo >&2 "Check your system instructions for installing libxslt and/or xsltproc."
exit 1
fi
if ! [[ -s "${input_path}/entities.xml" ]]; then
echo >&2 "Has a confluence space export been unzipped into ${input_path}?"
echo >&2 "Cannot find 'entities.xml' in that directory."
usage >&2
exit 1
fi
if [[ -d "${output_path}/page-xml" ]]; then
if ! "${extract_pages}" && ! "${force}" && ! "${clean}"; then
echo >&2 "Extracted pages already exist. You may want to remove"
echo >&2 "${output_path} before generating."
echo >&2 "Halting. Use --force or --clean to continue anyway."
usage >&2
exit 1
fi
fi
xslt_params+=(
--stringparam input-path "${input_path}"
--stringparam output-path "${output_path}"
--stringparam script-path "${script_path}"
)
cat <<STEPS
Input : ${input_path}/entities.xml
Output : ${output_path}
Confluence URL : ${confluence_url:-Not supplied}
JIRA URL : ${jira_url:-Not supplied}
Steps
Extract: ${extract_pages}
Image Copy: ${copy_images}
Convert Pages: ${convert_pages}
${force:+Overwrite enabled.}
STEPS
run() {
declare -a cmd
declare -i status
declare log capture
while (($#)); do
case "$1" in
log=*) log="${1##log=}" ;;
capture=*) capture="${1##capture=}" ;;
*) cmd+=("$1") ;;
esac
shift
done
set -- "${cmd[@]}"
((debug > 0)) && echo "$*"
if [[ -n "${log}" ]] && [[ -n "${capture}" ]]; then
"$@" >"${capture}" 2>"${log}"
elif [[ -n "${capture}" ]]; then
"$@" >"${capture}"
elif [[ -n "${log}" ]]; then
"$@" 2>"${log}"
else
"$@"
fi
status=$?
if ((status > 0)); then
echo >&2 "Error ${status} running: $*"
exit ${status}
fi
}
mkdir -p "${output_path}"/{page-xml,logs,wiki/images}
if "${extract_pages}"; then
"${clean}" && rm -rf "${output_path}/page-xml"
mkdir -p "${output_path}"/{page-xml,logs}
printf "Extracting page XML and image mapping\n"
if ((debug > 2)); then
run xsltproc -v "${xslt_params[@]}" entities.xsl "${input_path}"/entities.xml log="${output_path}/logs/entities.log"
else
run xsltproc "${xslt_params[@]}" entities.xsl "${input_path}"/entities.xml
fi
printf "Pages extracted.\n\n"
fi
if "${copy_images}"; then
"${clean}" && rm -rf "${output_path}/wiki/images"
mkdir -p "${output_path}"/{wiki/images,logs}
if ! [[ -s "${output_path}"/image-mappings.xml ]]; then
echo >&2 "${output_path}/image-mappings.xml does not exist."
echo >&2 "The page extraction step needs to be run at least once."
exit 1
fi
if ! [[ -d "${input_path}/attachments" ]]; then
echo >&2 "Warning: Cannot find ${input_path}/attachments. Attachment copy may fail."
fi
printf "Copying images from attachments\n"
if ((debug > 1)); then
xsltproc "${xslt_params[@]}" image-mappings.xsl "${output_path}/image-mappings.xml"
"${force}" || read -t 5 -r -p "Press RETURN within 5 seconds to continue"
fi
if ((debug > 2)); then
run xsltproc -v "${xslt_params[@]}" \
image-mappings.xsl \
"${output_path}/image-mappings.xml" \
log="${output_path}/logs/image-mapping.log" |
bash
else
run xsltproc "${xslt_params[@]}" \
image-mappings.xsl \
"${output_path}/image-mappings.xml" |
bash
fi
printf "Images copied.\n\n"
fi
if "${convert_pages}"; then
"${clean}" && rm -f "${output_path}"/wiki/*.md
printf "Converting pages to markdown\n"
for page in "${output_path}"/page-xml/*.xml; do
xml="${page##"${output_path}"/page-xml/}"
markdown="${output_path}/wiki/${xml%%.xml}.md"
log="${output_path}/logs/${xml%%.xml}.log"
if ((debug > 2)); then
run xsltproc -v --path "${PWD}" \
"${xslt_params[@]}" \
page.xsl "${page}" capture="${markdown}" log="${log}"
else
run xsltproc --path "${PWD}" \
"${xslt_params[@]}" \
page.xsl "${page}" capture="${markdown}"
fi
done
printf "Pages converted to markdown.\n"
if "${format}" && command -v prettier >/dev/null 2>/dev/null; then
printf "Formatting markdown pages\n"
prettier -w "${output_path}/wiki" >/dev/null 2>/dev/null &&
prettier -w "${output_path}/wiki" >/dev/null 2>/dev/null
printf "Formatted markdown.\n"
fi
fi