-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblosxom.zsh
executable file
·88 lines (73 loc) · 1.88 KB
/
blosxom.zsh
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
#!/usr/bin/env zsh
# vim:ft=zsh:
# array とかはそのまま関数に渡せない?
# あと array のなかに array もいれられない?
# っぽいので文字列として渡していちいちパース
# (q) とか (qq) とか (z) とかつかいまくる
# (qq) は空文字列でも安全
title="bloszom.zsh"
typeset -a pathinfo
: ${(A)pathinfo::=${(s:/:)PATH_INFO}}
flavour=${(M)pathinfo[$#pathinfo-1]%.*}
if [[ $flavour == "" ]]; then
flavour=".html"
fi
function template () {
template=$(<$1)
typeset -A variables
variables=()
: ${(AA)variables::=${(z)*[2,-1]}}
for k in ${(k)variables}; do
# echo "$k: $variables[$k]"
pat=#{$k}
sub=${(Q)variables[$k]}
template=${template//$pat/$sub}
done
echo $template
}
template "head$flavour" title ${(qq)title} home ${(qq)SCRIPT_NAME}
typeset -a entries
entries=()
zmodload zsh/stat
zmodload zsh/datetime
# (^Om) で更新順にソート
for f in data/**/*.txt(^Om); do
typeset -A entry
stat -H st +mtime $f
strftime -s date "%Y-%m-%d %H:%M:%S" $st[mtime]
content=$(<$f)
: ${(A)content::=${(f)content}}
title=$content[0]
body=${(F)content[2,-1]}
entry=(
title ${(qq)title}
time $st[mtime]
date ${(q)date}
body ${(qq)body}
path ${(q)f}
name ${(q)${f##data}%%.*}
home ${(qq)SCRIPT_NAME}
)
entries+=${(kv)entry}
done
if [[ $#pathinfo > 0 && "$PATH_INFO" != "" ]]; then
pathinfo[$#pathinfo-1] = ${pathinfo[$#pathinfo-1]%.*}
if [[ pathinfo[$#pathinfo-1] == "index" ]]; then
pathinfo[$#pathinfo-1] =
fi
for ((i = 1; i < $#entries; i += 1)); do
typeset -A entry
: ${(AA)entry::=${(z)entries[$i]}}
if [[ ${(M)entry[name]#$PATH_INFO} == "" ]]; then
entries[$i]="path 0"
fi
done
fi
for ((i = 1; i < $#entries; i += 1)); do
typeset -A entry
: ${(AA)entry::=${(z)entries[$i]}}
if [[ $entry[path] != "0" ]]; then
template "story$flavour" ${(kv)entry}
fi
done
template "foot$flavour" version ${(qq)ZSH_VERSION}