slitaz-base-files view rootfs/usr/bin/man @ rev 290

New man command
author Lucas Levrel <llevrel@yahoo.fr>
date Wed Dec 23 22:02:35 2015 +0100 (2015-12-23)
parents 91fcb259b3bf
children 5bc2687bfecf
line source
1 #!/bin/sh
2 #
3 # Tiny man fake using online manuals.
4 # Copyright (C) 2009-2015 SliTaz GNU/Linux.
5 #
6 . /lib/libtaz.sh
8 # Internationalization.
9 TEXTDOMAIN='slitaz-base'
10 . /etc/locale.conf
11 export TEXTDOMAIN LANG
13 display_html() {
14 if [ -x /usr/bin/retawq ]; then
15 retawq --dump=file://"$1" | less -M
16 else
17 # Rationale: busybox "less" is unable to use control chars, so we're
18 # stuck with regular text (no bold, colors...), and use other ways to
19 # emphasis: quotes, underscores, brackets, tabs.
20 # Explanation for following sequence:
21 # 1) keep only what's in the HTML body
22 # 2) make sure <pre> and </pre> are on a line of their own (see 3)
23 # 3a) newlines must be obeyed between <pre> tags, so explicitly add <br>
24 # because newlines are going to be stripped; also remove <em> (added
25 # emphasis characters would be misleading)
26 # 3b) the </pre> line is matched also, but should not get a <br> added,
27 # so remove it (let it on the <pre> line, starts a new paragraph)
28 # 4) strip newlines, remove comments (should be done earlier)
29 # 5) emphasize headers and titles with a Tab, add newline after
30 # 6) add newline for <br> <p> </p> <section> <article[ id=...]>
31 # 7) suppress <a [href=...]> </a> and some other tags
32 # 8) add newline and hyphen for list items, newline for list end
33 # 9) emphasize <tt> and <code> blocks by ‘’ ; <em> by _
34 # 10) render x-details between brackets []
35 # 11) remove start-of-line and end-of-line spaces
36 # 12) render HTML-encoded chars
37 # 13) wrap at word boundaries
38 cat "$1" | sed '1,/<body>/d; /<\/body>/d' \
39 | sed -r 's!(.)<(/?)pre>!\1\n<\2pre>!g; s!<(/?)pre>(.)!<\1pre>\n\2!g;' \
40 | sed -r '/<pre>/,/<\/pre>/{s!$!<br>!g; s!</?em>!!g}; \
41 /<\/pre>/s!<br>$!!' | tr '\n' ' ' \
42 | sed -r 's%<!-- .* -->%%g; \
43 s!<(header|h[1-4])>! !g; s!<footer>!\n !; \
44 s!</(header|h[1-4]|footer)>!\n!g; \
45 s!<(br|/?p|section|article[^>]*)>!\n!g; \
46 s!<(a [^>]*|/a|/section|/article|/html|/?pre|/?sup)>!!g; \
47 s!<li>!\n — !g; s!<ul>!!g; s!</(li|ul)>!\n!g; \
48 s!<(tt|code)>!‘!g; s!</(tt|code)>!’!g; s!</?em>!_!g; \
49 s!<x-details>![!g; s!</x-details>!]!g; \
50 s!&lt;!<!g; s!&gt;!>!g; s!&copy;!©!g; s!&quot;!"!g; s!&amp;!\&!g; \
51 s!&reg;!™!g; ' | sed -r 's!(^ *| *$)!!' \
52 | fold -s | less -M
53 fi
54 }
56 case "$1" in
57 ''|-*)
58 emsg "$(_ '<b>Usage:</b> man [section] command')"
59 return ;;
60 esac
62 local SECTION SECTIONS MSG TOPIC MAN_SECTION MAN_LANG
64 SECTION='all'
65 MAN_SECTION='[1-8]'
66 MAN_LANG=$(locale | sed -nr 's/LC_MESSAGES="?([^"_.]*).*"?/\1/p')
67 MSG=''
69 if [ -n "$2" ]; then
70 SECTION="$1"
71 MAN_SECTION="$1"
72 MSG=" $(_n 'in section %s' "$SECTION")"
73 shift
74 fi
76 TOPIC="$1"
78 # localized SliTaz doc?
79 DOC=$(find /usr/share/doc/"$TOPIC" /usr/share/doc/slitaz* \
80 -name "$TOPIC".$MAN_LANG.html 2>/dev/null | head -1)
81 # generic SliTaz doc?
82 [ -n "$DOC" ] || DOC=$(find /usr/share/doc/"$TOPIC" /usr/share/doc/slitaz* \
83 -name "$TOPIC".html 2>/dev/null | head -1)
84 # other doc?
85 [ -n "$DOC" ] || DOC=$(find /usr/share/doc \
86 -name "$TOPIC".html 2>/dev/null | head -1)
88 if [ -n "$DOC" ]; then
89 display_html "$DOC"
90 return
91 elif [ -f "/usr/share/doc/slitaz/$TOPIC.txt" ]; then
92 # SliTaz tools/libraries documentation (man-alike format)
93 less -M "/usr/share/doc/slitaz/$TOPIC.txt"
94 return
95 fi
97 MANPAGE=$(find /usr/share/man/$MAN_LANG*/man$MAN_SECTION \
98 /usr/local/share/man/$MAN_LANG*/man$MAN_SECTION \
99 /usr/local/man/$MAN_LANG*/man$MAN_SECTION \
100 /usr/share/man/man$MAN_SECTION \
101 /usr/local/share/man/man$MAN_SECTION \
102 /usr/local/man/man$MAN_SECTION \
103 -name "$TOPIC".$MAN_SECTION\* 2>/dev/null)
104 if [ -n "$MANPAGE" ]; then
105 case "$MANPAGE" in
106 *html) display_html "$MANPAGE"
107 return;;
108 esac
109 # "less"-ing a manpage is a BAD IDEA: man format is unreadable as is.
110 # Use nroff if available; it outputs control chars, which busybox less
111 # cannot handle: use "more" instead (or GNU less)
112 if [ -x /usr/bin/nroff ]; then
113 if [ x$(readlink $(which less)) == x/bin/busybox ]; then
114 VIEW_CMD="more"
115 else
116 VIEW_CMD="less -rM"
117 fi
118 case "$MANPAGE" in
119 *gz) (zcat "$MANPAGE" || unlzma -c "$MANPAGE" 2>/dev/null) | \
120 nroff -man | $VIEW_CMD;;
121 *) nroff -man "$MANPAGE" | $VIEW_CMD;;
122 esac
123 return
124 else
125 _ 'Found local manpage %s but no tool to display it.' $MANPAGE
126 _ 'Consider installing groff by running: %s' \
127 "su -c 'tazpkg get-install groff'"
128 # do not quit, go on searching online
129 fi
130 fi
132 if [ "$SECTION" == 'all' ]; then
133 #search alphabetically
134 LETTER=$(printf "${TOPIC::1}" | tr 'A-Z[:punct:][:digit:]' a-z00)
135 if [ $LETTER == '0' ]; then LETTER=other; fi
136 SECTIONS=$(wget -q -O - http://linux.die.net/man/$LETTER.html | \
137 sed -n -r "s%.*href=\"(.)/$TOPIC\".*%\1%p")
138 [ -n "$SECTIONS" ] || { _ 'No manual entry for %s' "$TOPIC"; exit 0;}
139 _n '%s found in the following sections:\n%s\nPlease choose one: ' "$TOPIC" \
140 "$SECTIONS"
141 read SECTION
142 fi
144 MANURL="http://linux.die.net/man/$SECTION/$TOPIC"
146 if ! wget -q --spider "$MANURL" 2>/dev/null ; then
147 _ 'No manual entry for %s' "$TOPIC$MSG"
148 exit 0
149 fi
151 if [ -x /usr/bin/retawq ]; then
152 retawq "$MANURL"
153 else
154 URL="http://www.w3.org/services/html2txt?url=$MANURL&noinlinerefs=on&nonums=on"
155 wget -q -O - "$URL" | tail -n+3 | \
156 sed 's!\[[0-9]*\]!!g;/\[INS: :INS\]/d;/^ Site Search$/,$d' | less -M
157 fi
159 exit 0