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