tazweb view lib/helper.sh @ rev 179

Use helper as as SHell library
author Christophe Lincoln <pankso@slitaz.org>
date Mon Mar 13 01:51:59 2017 +0100 (2017-03-13)
parents
children bb7b18c98c63
line source
1 #!/bin/sh
2 #
3 # TazWeb Helper - Handle bookmarks and cookies
4 #
5 # Coding: No libtaz.sh and so it is usable on any Linux distro
6 #
7 # Copyright (C) 2017 SliTaz GNU/Linux - BSD License
8 # See AUTHORS and LICENSE for detailed information
9 #
11 config="$HOME/.config/tazweb"
12 bm_txt="$config/bookmarks.txt"
13 bm_html="$config/bookmarks.html"
14 cookies_txt="$config/cookies.txt"
15 cookies_html="$config/cookies.html"
17 export TEXTDOMAIN='tazweb-lib'
19 # Parse cmdline options and store values in a variable
20 for opt in "$@"; do
21 opt_name="${opt%%=*}"
22 opt_name="${opt_name#--}"
23 case "$opt" in
24 --*=*) export $opt_name="${opt#*=}" ;;
25 --*) export $opt_name="on" ;;
26 esac
27 done
29 # HTML 5 header with built-in minimal CSS. Usage: html_header "title"
30 html_header() {
31 local title="$1"
32 cat << EOT
33 <!DOCTYPE html>
34 <html lang="en">
35 <head>
36 <meta charset="utf-8" />
37 <title>$title</title>
38 <style type="text/css">
39 body { margin: 2% 10%; font-size: 92%; } ul { padding: 0; }
40 h1 { color: #ccc; border-bottom: 2px solid #ccc; }
41 ul a { text-decoration: none; } ul a:hover { text-decoration: underline; }
42 li { list-style-type: none; line-height: 1.4em; padding: 0; }
43 footer { font-size: 80%; border-top: 2px solid #ccc; padding: 5px 0; color: #888; }
44 </style>
45 </head>
46 <body>
47 <section id="content">
48 <h1>$title</h1>
49 EOT
50 }
52 # HTML 5 footer: html_footer content
53 html_footer() {
54 cat << EOT
55 </section>
56 <footer>
57 ${@}
58 </footer>
59 </body>
60 </html>
61 EOT
62 }
64 # Generate bookmarks.html
65 html_bookmarks() {
66 html_header "$(gettext 'Bookmarks')" > ${bm_html}
67 echo '<ul>' >> ${bm_html}
68 IFS="|"
69 cat ${bm_txt} | while read title url null
70 do
71 cat >> ${bm_html} << EOT
72 <li><a href="${url}">${title}</a></li>
73 EOT
74 done; unset IFS
75 echo '</ul>' >> ${bm_html}
76 html_footer "$(cat $bm_txt | wc -l) $(gettext "Bookmarks") - $(date)" \
77 >> ${bm_html}
78 # Security fix from old cgi-bin bookmarks.cgi
79 chown ${USER}.${USER} ${bm_txt}; chmod 0600 ${bm_txt}
80 }
82 edit_bookmarks() {
83 yad --text-info \
84 --center --width=640 --height=480 --filename=${bm_txt}
85 }
87 # Generate cookies.html (for direct view of cookies in TazWeb)
88 html_cookies() {
89 html_header "$(gettext 'Cookies')" > ${cookies_html}
90 echo '<pre>' >> ${cookies_html}
91 IFS="|"
92 cat ${cookies_txt} | while read line
93 do
94 cat >> ${cookies_html} << EOT
95 ${line#\#HttpOnly_}
96 EOT
97 done; unset IFS
98 echo '</pre>' >> ${cookies_html}
99 html_footer $(cat $cookies_txt | wc -l) $(gettext "Cookies") - $(date) \
100 >> ${cookies_html}
101 }
103 clean_cookies() {
104 rm ${cookies_txt}; touch ${cookies_txt}
105 }
107 #
108 # Execute any shell_function
109 #
110 case "$1" in
112 *_*)
113 cmd=${1}; shift; ${cmd} ${@} ;;
115 *) grep "[a-z]_*()" ${0} | awk '{print $1}' ;;
117 esac; exit 0