aboutsummaryrefslogtreecommitdiff
path: root/build
blob: 1d4484ea959a8275b25159079a99b8b3458b002a (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
#!/bin/sh -e

ROOT=$PWD

txt2html() {
    printf '<pre>\n'

    # Convert [0-9] to links
    sed -E 's|(.*[a-z].*)\[([0-9].*)\]|\1<a href=#\2>[\2]</a>|g' "$1" |

    # Add span id to sections
    sed -E 's|^\[([0-9].*)\]|<span id=\1><a href=#\1>[\1]</a></span>|' |

    # Make links clickable
    sed -E "s|(http[s]?://[^[:space:]\)]*)|<a href=\1>\1</a>|g"

    printf '</pre>\n'
}

gettitle() {
    unset title
    case "$(sed -n 2p "$1")" in =*=|-*-) title="$(sed -n 1p "$1")"; esac
    file=${1##*/} file=${file%.*}
    printf '%s\n' "${title:-$file}${2:+ | Carbs Linux}"
}

genpages() (
    cd src || return 1
    find . ! -name .git | while read -r file; do

    # If this is a directory, create the directory on the destination and
    # return.
    [ -d "$file" ] && {
        mkdir -p "../docs/${file#./}"
        continue
    }

        # We are treating markdown/plaintext files differently.
        case "$file" in
            *.md|*.txt)
                tohtml "$file" > "../docs/${file%.*}.html"
                cp     "$file"   "../docs/${file%.*}.txt"
            ;;

            # If this is not a markdown/txt file copy as-is.
            *)  cp "$file" "../docs/$file"
        esac
    done
)


tohtml() {
    srcfile=${1#.} srcfile=${srcfile%.*}.txt
    case "${1##*.}" in
        md)  markdown -f footnote -f fencedcode "$1" ;;
        txt) txt2html "$1" ;;
    esac |

    sed '/{{ CONTENT }}/r /dev/stdin' "$ROOT/templates/template.html" |

    sed "s#{{ TITLE }}#$(gettitle "$1" html)#" |

    sed '/{{ CONTENT }}/d' |

    sed "s|{{ SRC }}|$srcfile|"
}

blog_index() {
    cat templates/blog-index > src/blog/index.md
    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")" \
               "${postdate}.html"
    done >> src/blog/index.md
}

site_index() {
    cp index/index.md src/index.md
    :> src/news.md
    i=0
    find index -name '*.news' | sort -r | while read -r news; do
        [ "$(( i += 1 ))" -lt 6 ] && {
            printf '\n'
            cat "$news"
        } >> src/index.md
        [ "$i" -eq 1 ] || printf '\n' >> src/news.md
        cat "$news" >> src/news.md
    done
}

docs_index() (
    cp templates/docs-index src/docs.md
    find src/docs ! -name .git ! -name README | sort | while read -r file; do
        case "$file" in src/docs) continue; esac
        [ -d "$file" ] && printf '### %s\n' "${file#src/docs/}" && continue
        filedest=${file#src} filedest=${filedest%.*}.html
        printf '[%s](%s)\n' "$(gettitle "$file")" "$filedest"
    done >> src/docs.md
)

genrss() {
    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 |

    sed '/{{ CONTENT }}/r /dev/stdin' "$ROOT/templates/rss.xml" |

    sed '/{{ CONTENT }}/d' |

    sed "s|{{ SRC }}|${2:-rss}.xml|" |

    sed "s|{{ DATE }}|$(date -u "+%a %b %d %Y %H:00")|"
}

main() {
    # Recreate docs directory
    rm -rf docs; mkdir -p docs

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

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

    # Generate pages
    genpages
}

main "$@"