-
-
Save grenade/a07396c21e036ce2d1a419f967200bea to your computer and use it in GitHub Desktop.
this code converts the markdown files in a gist into a google doc where each markdown file from the gist is separated by a docx page break
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# curl -sL https://gist.github.com/grenade/a07396c21e036ce2d1a419f967200bea/raw/gist-to-gdoc.sh | sh -s -- 8e487477663c8e57c7bf31e8371f454a rob-thijssen-curriculum-vitae google-drive-rthijssen | |
for exe in pandoc stack; do | |
if [ -x $(which ${exe}) ]; then | |
echo "detected ${exe}" | |
else | |
if sudo dnf install -y ${exe}; then | |
echo "installed ${exe}" | |
else | |
echo "installation of ${exe} failed" | |
exit 1 | |
fi | |
fi | |
done | |
filter_path=${HOME}/.local/bin/sed-docx-page-break.sh | |
filter_url=https://gist.github.com/grenade/a07396c21e036ce2d1a419f967200bea/raw/sed-docx-page-break.sh | |
if [ -x ${filter_path} ]; then | |
echo "detected ${filter_path}" | |
else | |
if curl -sLo ${filter_path} --url ${filter_url} && chmod +x ${filter_path}; then | |
echo "downloaded ${filter_path} from ${filter_url}" | |
else | |
echo "download of ${filter_path} from ${filter_url} failed" | |
exit 1 | |
fi | |
fi | |
tmp_dir=$(mktemp -d) | |
# combine each markdown file in the gist into a single file with a page-break token (PAGEBREAK) separating the original markdown files | |
if curl -sL https://api.github.com/gists/${1} | jq -r ' | |
( | |
[ | |
.files | |
| to_entries[] | |
| select(.value.type == "text/markdown" and (.value.filename | test("experience|education") | not)) | |
| .value.content | |
] + ( | |
[ | |
.files | |
| to_entries[] | |
| select(.value.type == "text/markdown" and (.value.filename | test("experience"))) | |
| .value.content | |
| gsub("^[\\n\\s]+|[\\n\\s]+$";"") | |
] | |
| reverse | |
) + ( | |
[ | |
.files | |
| to_entries[] | |
| select(.value.type == "text/markdown" and (.value.filename | test("education"))) | |
| .value.content | |
| gsub("^[\\n\\s]+|[\\n\\s]+$";"") | |
] | |
| reverse | |
) | |
) | |
| join("\n\nPAGEBREAK\n\n") | |
' | sed '/.png/d' > ${tmp_dir}/${2}.md; then | |
echo "created combined markdown file: ${tmp_dir}/${2}.md" | |
else | |
echo "creation of combined markdown file failed" | |
rm -rf ${tmp_dir} | |
exit 1 | |
fi | |
# run the pandoc markdown to docx conversion using the pandoc-docx-pagebreak filter | |
if pandoc "${tmp_dir}/${2}.md" --from gfm --filter ${filter_path} --to docx -o "${tmp_dir}/${2}.docx"; then | |
echo "converted to docx file: ${tmp_dir}/${2}.docx" | |
else | |
echo "conversion to docx file failed" | |
rm -rf ${tmp_dir} | |
exit 1 | |
fi | |
# upload the docx file to google docs | |
if rclone copy ${tmp_dir}/${2}.docx ${3}:; then | |
echo "uploaded to google drive as https://docs.google.com/document/d/185EKDZFrRZlJvR2q9vWEtHLI1e9PvaxM" | |
else | |
echo "upload to google drive failed" | |
rm -rf ${tmp_dir} | |
exit 1 | |
fi | |
exit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
jq . | tee /tmp/in.json | jq -c . | sed 's#{"t":"Para","c":\[{"t":"Str","c":"PAGEBREAK"}]}#{"t":"RawBlock","c":["openxml","<w:p><w:r><w:br w:type=\\"page\\"/></w:r></w:p>"]}#g' | jq . | tee /tmp/out.json |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
jgm/pandoc#1934 (comment)