diff options
author | Hsieh Chin Fan <typebrook@gmail.com> | 2022-02-02 13:34:47 +0800 |
---|---|---|
committer | Hsieh Chin Fan <pham@topo.tw> | 2024-11-30 21:09:29 +0800 |
commit | 9934dd538b0ce116e3b1600272cb46369b082246 (patch) | |
tree | 2f28c6c362201151eaf8218e566479ed7eb72070 /www/scripts |
init commit
Diffstat (limited to 'www/scripts')
-rwxr-xr-x | www/scripts/add-graph.py | 35 | ||||
-rwxr-xr-x | www/scripts/build.sh | 188 |
2 files changed, 223 insertions, 0 deletions
diff --git a/www/scripts/add-graph.py b/www/scripts/add-graph.py new file mode 100755 index 0000000..455b45a --- /dev/null +++ b/www/scripts/add-graph.py | |||
@@ -0,0 +1,35 @@ | |||
1 | #! /bin/python3 | ||
2 | |||
3 | import sys, subprocess | ||
4 | from datetime import date | ||
5 | from bs4 import BeautifulSoup | ||
6 | |||
7 | def handle(html): | ||
8 | soup = BeautifulSoup(html, 'html.parser') | ||
9 | for graph in soup.select('pre.language-graph'): | ||
10 | print() | ||
11 | print(date.today().strftime("%H:%M:%S")) | ||
12 | print('before:') | ||
13 | print(graph.string) | ||
14 | |||
15 | process = subprocess.Popen( | ||
16 | ["/usr/bin/vendor_perl/graph-easy", "--boxart"], | ||
17 | stdin=subprocess.PIPE, | ||
18 | stdout=subprocess.PIPE, | ||
19 | stderr=subprocess.PIPE | ||
20 | ) | ||
21 | output, error = process.communicate(input=graph.get_text().encode()) | ||
22 | graph.string = output.decode() | ||
23 | |||
24 | print('After:') | ||
25 | print(graph.string) | ||
26 | print('Error:') | ||
27 | print(error.decode()) | ||
28 | return str(soup) | ||
29 | |||
30 | for line in sys.stdin: | ||
31 | file = line.rstrip("\n") | ||
32 | with open(file, 'r') as html: | ||
33 | new_content = handle(html) | ||
34 | with open(file, 'w') as html: | ||
35 | html.write(new_content) | ||
diff --git a/www/scripts/build.sh b/www/scripts/build.sh new file mode 100755 index 0000000..9b95c99 --- /dev/null +++ b/www/scripts/build.sh | |||
@@ -0,0 +1,188 @@ | |||
1 | #! /bin/bash | ||
2 | |||
3 | set -e | ||
4 | |||
5 | # Executable command for markdown | ||
6 | markdown_bin="markdown -f fencedcode,autolink,alphalist,autolink,footnote" | ||
7 | |||
8 | # Directory for input/output | ||
9 | input_dir=${input_dir:?ENV \"input_dir\" is not set} | ||
10 | output_dir=${output_dir:?ENV \"output_dir\" is not set} | ||
11 | assets_dir=${assets_dir:?ENV \"assets_dir\" is not set} | ||
12 | template_dir=${template_dir:?ENV \"template_dir\" is not set} | ||
13 | |||
14 | # functions {{{ | ||
15 | |||
16 | # add indent for each line except <pre> | ||
17 | indent() { | ||
18 | indent="$(printf "%${1}s")" | ||
19 | sed "s/^/${indent}/; /<pre>/!b; :pre; N; /<\/pre>/!b pre" | ||
20 | } | ||
21 | |||
22 | # use heredoc to generate html from .md file and templates | ||
23 | html() { | ||
24 | <<-END_OF_HTML sed '1d;$d' | ||
25 | |||
26 | <!DOCTYPE html> | ||
27 | <html lang="en"> | ||
28 | ${head} | ||
29 | <body> | ||
30 | ${header} | ||
31 | <hr><br> | ||
32 | <main> | ||
33 | $(${markdown_bin} | indent 4) | ||
34 | </main> | ||
35 | <br><hr> | ||
36 | ${footer} | ||
37 | </body> | ||
38 | </html> | ||
39 | |||
40 | END_OF_HTML | ||
41 | } | ||
42 | |||
43 | # list of latest posts in markdown format | ||
44 | latest_posts() { | ||
45 | (IFS=$'\n'; echo "${index_list[*]}") | sort -r | head -20 | while read date path title; do | ||
46 | echo "- <time datetime="$date">$date</time> [$title](/$path)" | ||
47 | done | ||
48 | } | ||
49 | |||
50 | # print frontmatter from markdown file with format: "<key> <value>" | ||
51 | get_frontmatter() { | ||
52 | sed -n '1 {/<!--/ !q; n}; /-->/q; s/"//g; s/://p' | ||
53 | } | ||
54 | |||
55 | # process frontmatter | ||
56 | add_index() { | ||
57 | unset title public index date draft | ||
58 | |||
59 | # define local variables for frontmatter | ||
60 | while read key value; do | ||
61 | local -r $key="$value" | ||
62 | done <<<"$(get_frontmatter)" | ||
63 | |||
64 | # don't process draft after function call | ||
65 | test "$draft" != "" && return 1 | ||
66 | |||
67 | # skip making index in some cases | ||
68 | test "$public" = false && return 0 | ||
69 | test "$index" = false && return 0 | ||
70 | test "$type" = demo && return 0 | ||
71 | test "$title" = "" && return 0 | ||
72 | iso8601=$(date --iso --date "${date:-NULL}" 2>/dev/null) | ||
73 | test "$iso8601" = "" && return 0 | ||
74 | |||
75 | # put frontmatter info into variable "index" if title and date are valid | ||
76 | index_list+=("$iso8601 $path $title") | ||
77 | } | ||
78 | |||
79 | # remove SGML comments but keep the top one as frontmatter | ||
80 | ignore_comment() { | ||
81 | sed '1 !{ /^<!--$/,/^-->$/ d }' | ||
82 | } | ||
83 | |||
84 | # Generate the feed file | ||
85 | make_rss() { | ||
86 | echo -n "Making RSS " | ||
87 | |||
88 | rssfile=$blog_feed.$RANDOM | ||
89 | while [[ -f $rssfile ]]; do rssfile=$blog_feed.$RANDOM; done | ||
90 | |||
91 | { | ||
92 | pubdate=$(LC_ALL=C date +"$date_format_full") | ||
93 | cat <<-EOF | ||
94 | <?xml version="1.0" encoding="UTF-8" ?>' | ||
95 | <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">' | ||
96 | <channel><title>Dummy Website</title><link>https://topo.tw/index.xml</link>" | ||
97 | <description>$global_description</description><language>en</language>" | ||
98 | <lastBuildDate>$pubdate</lastBuildDate>" | ||
99 | <pubDate>$pubdate</pubDate>" | ||
100 | <atom:link href="$global_url/$blog_feed" rel="self" type="application/rss+xml" />" | ||
101 | EOF | ||
102 | |||
103 | n=0 | ||
104 | while IFS='' read -r i; do | ||
105 | is_boilerplate_file "$i" && continue | ||
106 | ((n >= number_of_feed_articles)) && break # max 10 items | ||
107 | echo -n "." 1>&3 | ||
108 | echo '<item><title>' | ||
109 | get_post_title "$i" | ||
110 | echo '</title><description><![CDATA[' | ||
111 | get_html_file_content 'text' 'entry' $cut_do <"$i" | ||
112 | echo "]]></description><link>$global_url/${i#./}</link>" | ||
113 | echo "<guid>$global_url/$i</guid>" | ||
114 | echo "<dc:creator>$(get_post_author "$i")</dc:creator>" | ||
115 | echo "<pubDate>$(LC_ALL=C date -r "$i" +"$date_format_full")</pubDate></item>" | ||
116 | |||
117 | n=$(( n + 1 )) | ||
118 | done < <(ls -t ./*.html) | ||
119 | |||
120 | echo '</channel></rss>' | ||
121 | } 3>&1 >"$rssfile" | ||
122 | echo "" | ||
123 | |||
124 | mv "$rssfile" "$blog_feed" | ||
125 | chmod 644 "$blog_feed" | ||
126 | } | ||
127 | |||
128 | # }}} | ||
129 | # prepare directory for outputs {{{ | ||
130 | |||
131 | mkdir -p $output_dir/ | ||
132 | rm -rf $output_dir/** | ||
133 | ln -s $assets_dir/* $output_dir/ | ||
134 | |||
135 | # }}} | ||
136 | # content of templates {{{ | ||
137 | |||
138 | head="$(cat $template_dir/head.html)" | ||
139 | header="$(cat $template_dir/header.html | indent 2)" | ||
140 | footer="$(cat $template_dir/footer.html | indent 2)" | ||
141 | index_list=() | ||
142 | index_template="$(cat $template_dir/index.md)" | ||
143 | |||
144 | # }}} | ||
145 | # for each markdown file {{{ | ||
146 | |||
147 | files="$(find "$input_dir" -type f -name '*md')" | ||
148 | total=$(wc -l <<<"$files") | ||
149 | declare -i counter | ||
150 | for file in $files; do | ||
151 | # set variables | ||
152 | path=$(<<<"$file" sed "s#^${input_dir}/##; s/\.md$//").html; mkdir -p $(dirname $output_dir/$path) | ||
153 | content="$(cat ${file} | ignore_comment)" | ||
154 | |||
155 | # use frontmatter to decide making html file or not | ||
156 | <<<"$content" add_index || continue | ||
157 | |||
158 | # log | ||
159 | echo -e "\033[1K\r$((counter+=1))/$total \t\t processing $path" | ||
160 | |||
161 | # make html file for draft | ||
162 | h1="$(<<<"$content" get_frontmatter | sed -n 's/^title *//p')" | ||
163 | echo "$content" \ | ||
164 | | tee $output_dir/${file#${input_dir}/} \ | ||
165 | | { [ -n "$h1" ] && echo "# $h1"; cat; } \ | ||
166 | | html >$output_dir/$path | ||
167 | done | ||
168 | echo | ||
169 | |||
170 | # }}} | ||
171 | # make index.html {{{ | ||
172 | |||
173 | { | ||
174 | echo "${index_template}" | ||
175 | echo -e '<br><br>\n\n' | ||
176 | latest_posts | ||
177 | } \ | ||
178 | | tee $output_dir/index.md \ | ||
179 | | html >$output_dir/index.html | ||
180 | |||
181 | echo -e index.html "\t" generated | ||
182 | |||
183 | # }}} | ||
184 | # make index.xml {{{ | ||
185 | |||
186 | |||
187 | |||
188 | # }}} | ||