File: html.sh

package info (click to toggle)
openvas-scanner 23.38.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 22,468 kB
  • sloc: ansic: 41,621; xml: 6,251; pascal: 3,723; yacc: 1,250; sh: 1,068; makefile: 333; sql: 282; javascript: 12
file content (154 lines) | stat: -rwxr-xr-x 3,481 bytes parent folder | download
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
#!/bin/bash
# SPDX-FileCopyrightText: 2023 Greenbone AG
#
# SPDX-License-Identifier: GPL-2.0-or-later

print_progress() {
    # echo -ne "\033[2K"
    terminal_width=$(tput cols)
    bar_width=$((terminal_width - 7))
    percent=$((100 * cfiles / nfiles))
    progress=$((percent * bar_width / 100))
    empty=$((bar_width - progress))
    echo -ne "["
    for ((i=0;i<$progress;i++))
    do
        echo -ne "▇"
    done
    for ((i=0;i<$empty;i++))
    do
        echo -ne " "
    done
    echo -ne "]$percent%"
    if [ $cfiles == $nfiles ]
    then
        echo -ne "\nDone\n"
    else
        echo -ne "\r"
    fi
}

make_entry() {
    toc="$toc<li>"

    line=$(head -n 1 "$entry")
    title=${line:2}

    link=$entry
    link=${link//manual/html}
    link=${link//.md/.html}
    link=${link#"$root_dir_prefix"}
    
    toc="$toc<a href=%ROOT%$link>$title</a>"

    toc="$toc</li>"
}

recursive_toc() {
    if [ $first == 0 ]; then
        first=1
        toc="$toc<ul class=\"collapsible\">" 
        entry="$search_dir"/index.md
        make_entry
        toc="$toc</ul>"
    fi
    for entry in "$search_dir"/*
    do
        toc="$toc<ul class=\"collapsible\">"     
        # In case of folder iterate through it
        if [[ -d $entry ]]; then
            search_dir="$entry"
            entry="$entry"/index.md
            make_entry
            entry=${entry//\/index.md/""}
            recursive_toc
        # Else make an entry for the file
        elif [[ -f $entry ]]; then
            cfiles=$((cfiles + 1))
            print_progress
            filename="$(basename -- $entry)"
            if [ $filename != index.md ]; then
                make_entry
            fi
        fi
        toc="$toc</ul>"
    done
}

create_html_dict() {
    dict=$entry
    dict=${dict//manual/html}
    mkdir $dict
}

make_html() {
    content=$(pandoc -f markdown -t html $entry)
    content=${content//.md/.html}

    head_name=$(head -n 1 $entry)
    head_name=${head_name//\# /}

    html=$template
    html=${html//\%TITLE\%/${head_name}}
    html=${html//\%CSS\%/${root_dir}${css_path}}
    html=${html//\%JS\%/${root_dir}${js_path}}
    toc_relative=${toc//\%ROOT\%/${root_dir}}
    html=${html//\%TOC\%/${toc_relative}}
    html=${html//\%CONTENT\%/${content}}
    
    file=$entry
    file=${file//manual/html}
    file=${file//.md/.html}

    echo "$html" > "$file"
}

recursive_html() {
    for entry in "$search_dir"/*
    do 
        # In case of folder iterate through it
        if [[ -d $entry ]]; then
            create_html_dict
            search_dir="$entry"
            root_dir="$root_dir../"
            recursive_html
            root_dir=${root_dir%"../"}
        # Else make an entry for the file
        elif [[ -f $entry ]]; then
            cfiles=$((cfiles + 1))
            print_progress
            filename="$(basename -- $entry)"
            make_html
        fi
    done
}

rm -rf html
mkdir html
mkdir html/css
mkdir html/js

first=0
base_dir=$( cd "$(dirname "${BASH_SOURCE[0]}")" ; pwd -P )/
template=$(<templates/template.html)
root_dir=""
root_dir_prefix="$base_dir"html/

cp templates/style.css html/css/
css_path=css/style.css
cp templates/script.js html/js/
js_path=js/script.js

toc=""

search_dir="$base_dir"manual
nfiles=$(find "$search_dir"/ -type f | wc -l)
cfiles=0
echo "Creating Table of Content for html pages..."
recursive_toc

search_dir="$base_dir"manual
echo "Creating html pages..."
cfiles=0
recursive_html
exit 0