tazweb view lib/helper.sh @ rev 192

helper.sh: add bookmarks handler
author Christophe Lincoln <pankso@slitaz.org>
date Thu Mar 16 00:20:44 2017 +0100 (2017-03-16)
parents 33e0f5920655
children a41749316ea9
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'
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>
39 body { margin: 2% 10%; font-size: 92%; }
40 h1 { color: #CCC; border-bottom: 2px solid #CCC; }
41 ul { padding: 0; }
42 ul a { text-decoration: none; } ul a:hover { text-decoration: underline; }
43 li { list-style-type: none; line-height: 1.4em; padding: 0; }
44 footer { font-size: 80%; border-top: 2px solid #CCC; padding: 5px 0; color: #888; }
45 </style>
46 </head>
47 <body>
48 <section id="content">
49 <h1>$title</h1>
50 EOT
51 }
53 # HTML 5 footer: html_footer content
54 html_footer() {
55 cat <<EOT
56 </section>
57 <footer>
58 $@
59 </footer>
60 </body>
61 </html>
62 EOT
63 }
65 # Generate bookmarks.html
66 html_bookmarks() {
67 {
68 html_header "$(gettext 'Bookmarks')"
69 echo '<ul>'
71 IFS="|"
72 while read title url null; do
73 echo "<li><a href=\"$url\">$title</a></li>"
74 done < ${bm_txt}
75 unset IFS
77 echo '</ul>'
78 num=$(wc -l < $bm_txt)
79 html_footer "$(printf "$(ngettext "%d bookmark" "%d bookmarks" "$num")" "$num") - $(date)"
80 } > ${bm_html}
82 # Security fix from old cgi-bin bookmarks.cgi
83 chown $USER:$USER ${bm_txt}; chmod 0600 ${bm_txt}
84 }
86 # List all bookmarks
87 bookmarks_list() {
88 cat ${bm_txt} | while read title url; do
89 echo -e "$title\n$url"
90 done | yad --list \
91 --title="$(gettext 'TazWeb Bookmarks')" \
92 --text-align=center \
93 --text="$(gettext 'Click on a value to edit - Right click to remove a bookmark')\n" \
94 --mouse --width=640 --height=480 \
95 --skip-taskbar \
96 --window-icon=/usr/share/icons/hicolor/32x32/apps/tazweb.png \
97 --editable --print-all \
98 --tooltip-column=2 \
99 --search-column=1 \
100 --column="$(gettext 'Title')" \
101 --column="$(gettext 'URL')"
102 }
104 # Rebuilt bookmarks.txt since some entry may have been edited and remove
105 # selected (TRUE) entries.
106 bookmarks_handler() {
107 IFS="|"
108 bookmarks_list | while read title url null; do
109 echo "$title|$url" >> ${bm_txt}.tmp
110 done; unset IFS
111 if [ -f "${bm_txt}.tmp" ]; then
112 mv -f ${bm_txt}.tmp ${bm_txt}
113 fi
114 }
116 # Generate cookies.html (for direct view of cookies in TazWeb)
117 html_cookies() {
118 {
119 html_header "$(gettext 'Cookies')"
120 echo '<pre>'
122 IFS="|"
123 while read line; do
124 echo "${line#\#HttpOnly_}"
125 done < ${cookies_txt}
126 unset IFS
128 echo '</pre>'
129 num=$(wc -l < $cookies_txt)
130 html_footer "$(printf "$(ngettext "%d cookie" "%d cookies" "$num")" "$num") - $(date)"
131 } > ${cookies_html}
132 }
134 clean_cookies() {
135 rm ${cookies_txt}; touch ${cookies_txt}
136 }
138 #
139 # Execute any shell_function
140 #
141 case "$1" in
143 *_*)
144 cmd=${1}; shift; ${cmd} ${@} ;;
146 *) grep "[a-z]_*()" $0 | awk '{print $1}' ;;
148 esac
149 exit 0