aboutsummaryrefslogtreecommitdiff
path: root/search/index.cgi
blob: 6bcf0d2e23f98d23ca8dcf4bc8a5b6a2fbd83b7e (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
#!/bin/sh

remove_nav_and_footer()
{
	cat $1 | \
			awk 'BEGIN { del=0 } /<nav/ { del=1 } del<=0 { print } /nav>/ { del -= 1 }' | \
			awk 'BEGIN { del=0 } /<footer/ { del=1 } del<=0 { print } /footer>/ { del -= 1 }'
}

build_search_list()
{
	find ../ -name "*.html" -not -path "../search/*" |
			while read html_file; do
				for WORD in $WORDS; do
					# This line removes the navigation bar
					RESULTS=$(remove_nav_and_footer $html_file | grep -i "$WORD")

					if [ ! -z "$RESULTS" ]; then
						# For some reason, CGI doesn't print &. So, we replace unicode codes with the character
						TITLE=$(grep "<title>.*</title>" $html_file | sed 's/<[^>]*>//g' | sed 's/[ ]*//' \
								| sed 's/&agrave;/à/g')

						HREF=$(echo $html_file | sed 's/^[\.]*//g')
						echo '<a href="'$HREF'"><h4>'$TITLE'</h4></a>'
						echo "<ul>"
						remove_nav_and_footer $html_file | grep -i "$WORD" \
								| sed -E 's/<[^>]*>//g' \
								| sed -E 's/[ ]*(.*)/<li>\1<\/li>/g' \
								| sed -E 's/('$WORD')/<span style="background: yellow">\1<\/span>/Ig'
						echo "</ul>"
					fi
				done
			done
}

printf "Content-Type: text/html\n\n"

IFS='&'

for arg in $QUERY_STRING; do
    case $arg in
        q=*)
            WORDS=`echo $arg | sed 's/q=//'`
            ;;
    esac
done

RETURN=$(build_search_list)

while read -r line; do
    if [ $line == '<!-- DELIMITER -->' ]; then
        if [ -z $RETURN ]; then
            echo "<p>Nessun risultato</p>"
        else
            echo $RETURN
        fi
    else
        echo "$line"
    fi
done < index.html