aboutsummaryrefslogtreecommitdiff
path: root/build
blob: 04fad8130d8c7fa9cf4d9eb052ac0e1ecce99529 (plain)
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/bin/sh -e

header() {
    cat <<EOF
<!DOCTYPE HTML>
<html lan="en">
<head>
<title>$1 | Carbs Linux</title>
<link rel="stylesheet" href="/assets/style.css">
<meta charset="utf-8">
<meta name="Description" content="Carbs Linux - a simple busybox linux distribution">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
</head>
<p class=header><strong>Carbs Linux - a simple busybox linux distribution</strong></p>
<div class="header"><nav>
<a href='/'>index</a>
<a href='https://github.com/CarbsLinux'>github</a>
<a href='//dl.carbslinux.org'>downloads</a>
<a href='/blog'>blog</a>
<a href='/wiki'>wiki</a>
<a href='/wiki/install.html'>installation</a>
</nav></div><div class="border"></div>
EOF
}

footer() {
    [ -z "$1" ] || printf '<a href="%s">View Page Source</a>' "$1"
    cat <<EOF
<div class=border></div>
<p class=footer>Linux® is a registered trademark of Linus Torvalds</p>
<p class=footer>Copyright © 2019-2020 Cem Keylan</p>
</body>
</html>
EOF
}

gettitle() {
    case "$(sed 1q "$1")" in
         Title:*) sed 's/^[Tt]itle: //;1q' "$1"  ;;
         *)
             title=$(
             grep -n '=*=' -- "$1" |

                 while IFS=: read -r num _; do
                     sed "$(( num - 1 ))q;d" "$1"
                     return 0
                 done
             )

             file=${1##*/} file=${file%.md}
             printf '%s\n' "${title:-$file}"
    esac
}

genpages() {
    # Create directories in docs
    find src -type d | while read -r dir ; do mkdir -p "docs${dir#src}" ; done
    # Generate html and txt files
    for page in $(cd src || return 1 ; find . \( -name '*.md' -o -name '*.txt' \) ) ; do
        tohtml "src/$page" > "docs/${page%.*}.html"
        sed '/^Title:/d' "src/$page" > "docs/${page%.*}.txt"
    done
    # Copy rest of the files
    (cd src || return 1 ; find . -type f ! -name '*.md' -exec cp -u \{\} ../docs/\{\} \; )

}


tohtml() {
    header "$(gettitle "$1")"
    case "${1##*.}" in
        md)  sed '/^[Tt]itle:/d' "$1" | markdown   ;;
        txt) printf '<pre>'; cat "$1"; printf '</pre>' ;;
    esac
    srcfile=${1#src/.} srcfile=${srcfile%.*}.txt
    footer "$srcfile"
}

wiki_index() {
    cat templates/wiki-index > src/wiki/index.md
    for page in $(find src/wiki -type f | grep -v 'src/wiki/index.md' | sort) ; do
        printf '* [%s](%s)\n' "$(gettitle "$page")" "$(printf "$page" | sed 's#src/wiki/##;s/.md/.html/')" >> src/wiki/index.md
    done
}

blog_index() {
    cat <<EOF > src/blog/index.md
Blog Index
================================================================================

This is the Carbs Linux Blog Index. You can find every post here. [RSS Feed]

[RSS Feed]: /rss.xml

EOF
    find src/blog -type f ! -name index.md | sort -r | while read -r post ; do
        postdate="${post##*/}"; postdate="${postdate%%.*}"
        printf '* %s - [%s](%s)\n' "$(date --date="$postdate" +%b\ %d\ %Y)" "$(gettitle "$post")" "$(printf "$post" | sed 's#src/blog/##;s/.md/.html/')" >> src/blog/index.md
    done
}

site_index() {
    cp index/index.md src/index.md
    :> src/news.md
    # find index -name '*.news' | sort -r | sed 3q | while read -r news ; do
    # shellcheck disable=2046
    set -- $(find index -name '*.news' | sort -r)
    for news in "$1" "$2" "$3" "$4" "$5" ; do
        newsdate="${news##*/}" ; newsdate="${newsdate%.news}"
        printf '\n### %s\n\n' "$(date --date="$newsdate" +%b\ %d\ %Y)" >> src/index.md
        cat "$news" >> src/index.md
    done
    for news; do
        newsdate="${news##*/}" ; newsdate="${newsdate%.news}"
        printf '\n%s\n-----------\n\n' "$(date --date="$newsdate" +%b\ %d\ %Y)" >> src/news.md
        cat "$news" >> src/news.md
    done
}

genrss() {
    cat <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
    xmlns:atom="http://www.w3.org/2005/Atom"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
    xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
>
<channel>
<title>Carbs Linux</title>
<description>a simple busybox linux distribution</description>		
<link>https://carbslinux.org</link>
<atom:link href="https://carbslinux.org/${2:-rss}.xml" rel="self" type="application/rss+xml" />
<lastBuildDate>$(date -u +%b\ %a\ %Y\ %H):00</lastBuildDate>
EOF
    find "$1" -type f ! -name index.md | sort -r | while read -r post ; do
        postdate="${post##*/}" ; postdate="${postdate%.*}"
        cat <<EOF
<item>
<title>$(gettitle "$post")</title>
<pubDate>$(date --date="$postdate" +%a,\ %d\ %b\ %Y)</pubDate>
<dc:creator>Cem Keylan</dc:creator>
<link>https://carbslinux.org/${3:-blog/$(printf "${post##*/}" | sed 's/.md/.html/')}</link>
<description>$(grep -v '^Title:' "$post" | markdown -f cdata)</description>
</item>
EOF
    done
cat <<EOF
</channel>
</rss>
EOF
}

main() {
    # Create docs directory if it doesn't exist
    mkdir -p docs

    # Remove all html, txt, and rss files
    find docs \( -name '*.html' -o -name '*.txt' -o -name '*.xml' \) -delete

    # Generate the indexes for blog and the wiki
    site_index; wiki_index; blog_index

    # Generate rss feeds
    genrss index news index.html > docs/news.xml ; genrss src/blog > docs/rss.xml

    # Generate pages
    genpages
}

main "$@"