/
mdweb
executable file
·146 lines (126 loc) · 4.46 KB
/
mdweb
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
#!/bin/bash
source config
# Creates a new page from a template
newpage() {
read -erp "Enter the path for the new page [top level]: " path
path=${path:-""}
read -erp "Enter a title for the new page: " title
while [ -z "$title" ]; do
read -erp "You have to enter a title: " title
done
filename="${title// /-}.md"
echo "Choose one of the following templates for the new page:"
ls templates/markdown
read -erp "Enter the name of the template [default.md]: " template
template=${template:-default.md}
if [ "$path" != "" ]; then
read -erp "Would you like a custom header and footer for all pages in path $local_dir/$path? (y/n) [n]" custheadfoot
fi
mkdir -p "markdown/$path"
cp "templates/markdown/$template" "markdown/$path/$filename"
format "markdown/$path/$filename"
echo
echo "Page created successfully!"
echo "You can now edit it by hand and add content."
if [ "$custheadfoot" = "y" ]; then
mkdir -p "templates/html/$path"
cp templates/html/header.html "templates/html/$path/header.html"
cp templates/html/footer.html "templates/html/$path/footer.html"
echo "You can edit the custom header and footer in templates/html/$path. They will be used on all pages in $local_dir/$path automatically."
fi
echo "When you're done, run './mdweb u' to create all html pages in the html directory or './mdweb d' to directly deploy to the server."
}
log() {
read -erp "Title: " title
read -erp "Text: " text
time=$(date +"%Y %h %d, %H:%M")
seconds=$(date +"%s")
link="<a id='$seconds' href='#$seconds'>#$seconds</a>"
logtxt="- $link $time – **$title**\\n\\n\\t$text\\n"
sed -ie "/^===/a $logtxt" "markdown/log/index.md"
echo "Successfully added '$title' to the log."
}
# Replaces %MACROS% in the given MD file
format() {
day=$(date +"%a")
date=$(date +"%d.%m.%Y")
time=$(date +"%k:%M")
year=$(date +"%Y")
pagename=$(basename $1 | sed "s/-/ /g; s/.html//g")
sed -i "s|%PAGENAME%|$pagename|g" $1
sed -i "s|%TITLE%|$title|g" $1
sed -i "s|%AUTHOR%|$author|g" $1
sed -i "s|%EMAIL%|$email|g" $1
sed -i "s|%DAY%|$day|g" $1
sed -i "s|%DATE%|$date|g" $1
sed -i "s|%TIME%|$time|g" $1
sed -i "s|%YEAR%|$year|g" $1
sed -i "s|%CSS%|$css|g" $1
sed -i "s|%INDEX%|$domain|g" $1
sed -i "1,2d" $1
}
# Converts a MD file to HTML
convert() {
output="${1%.md}.html"
cat $1 | sed -e '1,2d' > "$1.prebuild"
pandoc -f markdown --mathjax -t html -o "$1.build" "$1.prebuild"
path_full=$(dirname $1)
path=${path_full#"$local_dir/"}
headerfile="templates/html/header.html"
footerfile="templates/html/footer.html"
url_path=${output#"$local_dir"}
title=$(head -n 1 $1)
if [ -f "templates/html/$path/header.html" ]; then
headerfile="templates/html/$path/header.html"
fi
if [ -f "templates/html/$path/footer.html" ]; then
footerfile="templates/html/$path/footer.html"
fi
# Check if the file is part of a blog
case $1 in $blogpath)
# Cutting the date (formatted as _dd.mm.yyyy_) out of the blog posts third line and getting it into the date format that RSS accepts, damn hacky I know
datestr=$(head -n 3 $1 | tail -n 1 | head -c -2 | tail -c +2)
d=$(echo $datestr | cut -f1 -d.)
m=$(echo $datestr | cut -f2 -d.)
y=$(echo $datestr | cut -f3 -d.)
rssdate=$(date --date="$y/$m/$d" +"%a, %d %b %Y 12:00:00 %z")
printf "\\n<item>\\n<title>%s</title>\\n<guid>%s%s</guid>\\n<pubDate>%s</pubDate>\\n<description><![CDATA[\\n%s\\n]]></description>\\n</item>\\n\\n" "$(head -n 1 $1)" "$domain" "$url_path" "$rssdate" "$(cat $1.build)" >> "$local_dir/rss.xml"
echo "Added RSS entry for $url_path."
esac
cat $headerfile $1.build $footerfile > $output
format $output
rm $1
rm $1.prebuild
rm $1.build
echo "Created $output."
}
# Creates HTML files from all the MD files
update() {
rm -r "$local_dir/"
mkdir -p "$local_dir"
mkdir -p "$local_dir/css"
mkdir -p "$local_dir/res"
cp -r markdown/* "$local_dir"
cat templates/rss/top.xml > "$local_dir/rss.xml"
for file in $(find "$local_dir" -name "*.md"); do
convert $file
done
cat templates/rss/bottom.xml >> "$local_dir/rss.xml"
cp -r css $local_dir
cp -r res $local_dir
cp favicon.ico $local_dir
echo ""
}
# Deploys the local files to the server
deploy() {
update
rsync -avz --delete $local_dir/ $server_dir
echo "Successfully updated server files."
}
case "$1" in
n*) newpage ;;
l*) log ;;
u*) update ;;
d*) deploy ;;
*) printf "Markdown Web system by Niklas Bühler <hi@niklasbuehler.com>.\\nUsage:\\n mdweb n:\\t new page\\n mdweb l:\\t new log entry\\n mdweb u:\\t update html\\n mdweb d:\\t deploy to server\\n" ;;
esac