forked from kodecocodes/swift-algorithm-club
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgfm-render.sh
executable file
·141 lines (122 loc) · 3.37 KB
/
gfm-render.sh
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
#!/usr/bin/env bash
set -e
# $1 - readme file name
function render_markdown_to_html {
# escape escaping characters on Darwin only
content=$(
cat "$1" \
| sed 's/\\/\\\\/g' \
| sed 's/"/\\"/g' \
| sed $'s/\t/\\\\t/g' \
| sed -e ':a' -e 'N' -e '$!ba' -e 's/\n/\\\n/g' \
)
# network call to GitHub API
json="{\"text\":\"$content\",\"mode\":\"gfm\",\"context\":\"$USERNAME/swift-algorithm-club\"}"
echo -e "$(curl -s --data "$json" -u $USERNAME:$TOKEN https://api.github.com/markdown)"
}
# download github systax highlight stylesheet
echo "> Downloading github-light.css..."
curl -s -O https://raw.githubusercontent.com/primer/github-syntax-light/master/lib/github-light.css
# slightly modify the main stylesheet
echo "> Modifying github-light.css..."
cat >> github-light.css << EOF
#container {
margin: 0 auto;
width: 75%;
min-width: 768px;
max-width: 896px;
position: relative;
}
body {
font-size: 18px;
}
code {
padding: 0.2em;
margin: 0;
font-size: 85%;
background-color: #f6f8fa;
line-height: 1.45;
border-radius: 3px
}
pre code {
padding: 0px;
background-color: transparent;
}
.highlight {
margin: 0px;
padding: 0px 16px;
font-size: 85%;
line-height: 1.45;
overflow: auto;
background-color: #f6f8fa;
border-radius: 3px;
}
@media (max-width: 768px) {
#container {
position: absolute;
margin: 0;
width: 100%;
height: 100%;
min-width: 100%;
}
}
EOF
# other markdown articles
for title in "What are Algorithms" "Big-O Notation" "Algorithm Design" "Why Algorithms"; do
echo "> Generating $title.html..."
cat > "$title.html" << EOF
<!DOCTYPE html>
<head>
<title>$title</title>
<link rel="stylesheet" type="text/css" href="github-light.css">
</head>
<body>
<div id="container">$(render_markdown_to_html "$title.markdown")</div>
</body>
</html>
EOF
done
# if index.html does not exist, create one;
# otherwise, empty its content.
echo "> Generating index.html..."
cat > index.html << EOF
<!DOCTYPE html>
<head>
<title>Swift Algorithm Club</title>
<link rel="stylesheet" type="text/css" href="github-light.css">
</head>
<body>
<div id="container">$(render_markdown_to_html README.markdown | sed 's/.markdown/.html/g')</div>
</body>
</html>
EOF
# iterate immediate directories
find . -maxdepth 1 -type d | while read folder; do
readme=''
# get the right extension for the README file if there is one
if [[ -f $folder/README.md ]]; then readme="$folder/README.md"; fi
if [[ -f $folder/README.markdown ]]; then readme="$folder/README.markdown"; fi
# skip if there is no README or it it the README of the repository
if [[ (-z $readme) || $readme == "./README.markdown" ]]; then continue; fi
# render README to HTML
name=$(basename "$folder")
echo "> Generating $name/index.html..."
cat > "$folder/index.html" << EOF
<!DOCTYPE html>
<head>
<title>$name</title>
<link rel="stylesheet" type="text/css" href="../github-light.css">
</head>
<body>
<div id="container">$(render_markdown_to_html "$readme")</div>
</body>
</html>
EOF
done
# push to gh-pages
if [[ $CI = true ]]; then
git checkout -b gh-pages
git add .
git commit -m "$Generated by TravisCI on $(date +%D)"
git push -f https://[email protected]/$USERNAME/swift-algorithm-club.git gh-pages
fi