-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert.sh
More file actions
executable file
·120 lines (99 loc) · 2.75 KB
/
Copy pathconvert.sh
File metadata and controls
executable file
·120 lines (99 loc) · 2.75 KB
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
#!/bin/bash
BUILD_DIR="export"
if [ -z "${PLATFORM}" ]; then
echo Missing PLATFORM
exit 1
fi
if [ -z "${POST_PATH}" ]; then
echo Missing POST_PATH
exit 1
fi
if [ -z "${SLUG}" ]; then
echo Missing SLUG
exit 1
fi
echo "Converting from ${POST_PATH}..."
# Wipe and re-create build dir
rm -r ${BUILD_DIR}
mkdir $BUILD_DIR
TARGET="$BUILD_DIR/index.md"
TARGET_TEMP="$BUILD_DIR/temp"
cp "$POST_PATH" $TARGET
# Replace figure with markdown image format
gsed -i 's/{{<\sfigure\ssrc="\(.*\)"\salt="\(.*\)"\scaption="\(.*\)".*}}/!\[\2\](\1 "\3")<figcaption>\3<\/figcaption>/' $TARGET
# Remove captured width="..."
gsed -i 's/"\swidth="[0-9]*//g' $TARGET
# Strip all figcaption if medium
if [[ $PLATFORM == "medium" ]]; then
gsed -i 's/<figcaption>.*<\/figcaption>/ \\/g' $TARGET
fi
# Replace relative path with absolute path
gsed -i "s/(\.\//(https:\/\/joncloudgeek.com\/blog\/${SLUG}\//" $TARGET
# Wipe out Frontmatter
gawk '
BEGIN { del=2 }
del == 0 { print }
/---/ { del -= 1 }
' $TARGET > $TARGET_TEMP
mv $TARGET_TEMP $TARGET
# Create Table of Contents
gawk '
@include "join"
BEGIN {
print "## Table of Contents"
print "<!-- MEDIUM_LIST_PRESERVE -->"
}
{
if (match($0,/##\s(.*)$/, a) != 0) {
split(a[1], b, " ")
printf " 1. [%s](#%s)\n", a[1], joinlower(b,1,length(b),"-")
}
}
END { print "" } ' $TARGET > "$BUILD_DIR/toc"
# Prepend Credits to target
BLOG_PATH=${POST_PATH%index.md}
BLOG_PATH=${BLOG_PATH#content/}
FREE=""
if [[ "$PLATFORM" == "medium" ]]; then
FREE=" (free)"
fi
gsed -i "1s;^;\nThis post was originally posted on [JonCloudGeek](https://joncloudgeek.com/${BLOG_PATH})${FREE}\n\n;" $TARGET
if [[ "$PLATFORM" == "dev" ]]; then
# Insert TOC file contents before first image
gsed -i "/\.jpg/e cat ${BUILD_DIR}/toc" $TARGET
fi
# Promote books at end
gawk '
{ print }
END { print "\n## My GCP Books and Courses\n\nIf you found this blog post helpful, kindly check out my [books and courses on GCP](https://joncloudgeek.com/store/)!" }
' $TARGET > $TARGET_TEMP
mv $TARGET_TEMP $TARGET
#
# MARKDOWN OPTIMIZED FOR MEDIUM
#
gawk '
BEGIN { preserve=0 }
/MEDIUM_LIST_PRESERVE/ { preserve=1 }
# Reset
/^$/ { preserve=0 }
/##/ { preserve=0 }
/INLINE_IMAGES_END/ { inline=0 }
/^\s*[0-9]*\.?\s+/ {
if (preserve == 0) {
gsub(/^\s*[0-9]\.\s+/, "")
gsub(/$/, "\n")
}
}
/##/ { gsub(/##/, "#") }
/^\s+/ {
if (preserve == 0) {
gsub(/^\s+/, "")
}
} 1
' $TARGET > "$BUILD_DIR/medium.md"
# Generate medium.html
pandoc -o "$BUILD_DIR/medium.html" "$BUILD_DIR/medium.md"
# Add image title as figcaption
gsed -i 's/<img\ssrc=".*"\stitle="\(.*\)"\salt=".*"\s\/>/<figure>\0<figcaption>\1<\/figcaption><\/figure>/' "$BUILD_DIR/medium.html"
# Replace <strong> with <b>
gsed -i 's/strong>/b>/g' "$BUILD_DIR/medium.html"