slitaz-base-files rev 290

New man command
author Lucas Levrel <llevrel@yahoo.fr>
date Wed Dec 23 22:02:35 2015 +0100 (2015-12-23)
parents cca198d5952c
children 967cede25fb4
files rootfs/usr/bin/man
line diff
     1.1 --- a/rootfs/usr/bin/man	Thu Dec 03 04:59:59 2015 +0200
     1.2 +++ b/rootfs/usr/bin/man	Wed Dec 23 22:02:35 2015 +0100
     1.3 @@ -10,24 +10,60 @@
     1.4  . /etc/locale.conf
     1.5  export TEXTDOMAIN LANG
     1.6  
     1.7 -check_retawq() {
     1.8 -	if [ ! -x /usr/bin/retawq ]; then
     1.9 -		echo; _ "Missing Retawq web browser..."
    1.10 -		_ 'Please run: %s' "su -c 'tazpkg get-install retawq'"
    1.11 -		exit 0
    1.12 +display_html() {
    1.13 +	if [ -x /usr/bin/retawq ]; then
    1.14 +		retawq --dump=file://"$1" | less -M
    1.15 +	else
    1.16 +		# Rationale: busybox "less" is unable to use control chars, so we're
    1.17 +		# stuck with regular text (no bold, colors...), and use other ways to
    1.18 +		# emphasis: quotes, underscores, brackets, tabs.
    1.19 +		# Explanation for following sequence:
    1.20 +		# 1) keep only what's in the HTML body
    1.21 +		# 2) make sure <pre> and </pre> are on a line of their own (see 3)
    1.22 +		# 3a) newlines must be obeyed between <pre> tags, so explicitly add <br>
    1.23 +		#     because newlines are going to be stripped; also remove <em> (added
    1.24 +		#     emphasis characters would be misleading)
    1.25 +		# 3b) the </pre> line is matched also, but should not get a <br> added,
    1.26 +		#     so remove it (let it on the <pre> line, starts a new paragraph)
    1.27 +		# 4) strip newlines, remove comments (should be done earlier)
    1.28 +		# 5) emphasize headers and titles with a Tab, add newline after
    1.29 +		# 6) add newline for <br> <p> </p> <section> <article[ id=...]>
    1.30 +		# 7) suppress <a [href=...]> </a> and some other tags
    1.31 +		# 8) add newline and hyphen for list items, newline for list end
    1.32 +		# 9) emphasize <tt> and <code> blocks by ‘’ ; <em> by _
    1.33 +		# 10) render x-details between brackets []
    1.34 +		# 11) remove start-of-line and end-of-line spaces
    1.35 +		# 12) render HTML-encoded chars
    1.36 +		# 13) wrap at word boundaries
    1.37 +		cat "$1" | sed '1,/<body>/d; /<\/body>/d' \
    1.38 +		| sed -r 's!(.)<(/?)pre>!\1\n<\2pre>!g; s!<(/?)pre>(.)!<\1pre>\n\2!g;' \
    1.39 +		| sed -r '/<pre>/,/<\/pre>/{s!$!<br>!g; s!</?em>!!g}; \
    1.40 +			/<\/pre>/s!<br>$!!' | tr '\n' ' ' \
    1.41 +		| sed -r 's%<!-- .* -->%%g; \
    1.42 +			s!<(header|h[1-4])>!	!g; s!<footer>!\n	!; \
    1.43 +			s!</(header|h[1-4]|footer)>!\n!g; \
    1.44 +			s!<(br|/?p|section|article[^>]*)>!\n!g; \
    1.45 +			s!<(a [^>]*|/a|/section|/article|/html|/?pre|/?sup)>!!g; \
    1.46 +			s!<li>!\n — !g; s!<ul>!!g; s!</(li|ul)>!\n!g; \
    1.47 +			s!<(tt|code)>!‘!g; s!</(tt|code)>!’!g; s!</?em>!_!g; \
    1.48 +			s!<x-details>![!g; s!</x-details>!]!g; \
    1.49 +			s!&lt;!<!g; s!&gt;!>!g; s!&copy;!©!g; s!&quot;!"!g; s!&amp;!\&!g; \
    1.50 +			s!&reg;!™!g; ' | sed -r 's!(^ *| *$)!!' \
    1.51 +		| fold -s | less -M
    1.52  	fi
    1.53  }
    1.54  
    1.55 -local i SECTION SECTIONS MSG TOPIC MAN_SECTION
    1.56 -
    1.57  case "$1" in
    1.58  	''|-*)
    1.59  		emsg "$(_ '<b>Usage:</b> man [section] command')"
    1.60  		return ;;
    1.61  esac
    1.62  
    1.63 +local SECTION SECTIONS MSG TOPIC MAN_SECTION MAN_LANG
    1.64 +
    1.65  SECTION='all'
    1.66 -MAN_SECTION='*'
    1.67 +MAN_SECTION='[1-8]'
    1.68 +MAN_LANG=$(locale | sed -nr 's/LC_MESSAGES="?([^"_.]*).*"?/\1/p')
    1.69  MSG=''
    1.70  
    1.71  if [ -n "$2" ]; then
    1.72 @@ -39,54 +75,85 @@
    1.73  
    1.74  TOPIC="$1"
    1.75  
    1.76 -if [ check_retawq -a -f /usr/share/doc/$TOPIC/$TOPIC.html ]; then
    1.77 -	retawq --dump=file:///usr/share/doc/$TOPIC/$TOPIC.html | less -M
    1.78 +# localized SliTaz doc?
    1.79 +DOC=$(find /usr/share/doc/"$TOPIC" /usr/share/doc/slitaz* \
    1.80 +	-name "$TOPIC".$MAN_LANG.html 2>/dev/null | head -1)
    1.81 +# generic SliTaz doc?
    1.82 +[ -n "$DOC" ] || DOC=$(find /usr/share/doc/"$TOPIC" /usr/share/doc/slitaz* \
    1.83 +	-name "$TOPIC".html 2>/dev/null | head -1)
    1.84 +# other doc?
    1.85 +[ -n "$DOC" ] || DOC=$(find /usr/share/doc \
    1.86 +	-name "$TOPIC".html 2>/dev/null | head -1)
    1.87 +
    1.88 +if [ -n "$DOC" ]; then
    1.89 +	display_html "$DOC"
    1.90  	return
    1.91 -elif [ check_retawq -a -f /usr/share/doc/slitaz/$TOPIC.html ]; then
    1.92 -	retawq --dump=file:///usr/share/doc/slitaz/$TOPIC.html | less -M
    1.93 -	return
    1.94 -elif [ check_retawq -a -f /usr/share/doc/slitaz-tools/$TOPIC.html ]; then
    1.95 -	retawq --dump=file:///usr/share/doc/slitaz-tools/$TOPIC.html | less -M
    1.96 -	return
    1.97 -elif [ -f /usr/share/doc/slitaz/$TOPIC.txt ]; then
    1.98 -	# SliTaz tools/libraries documentation (man a like format)
    1.99 -	less -M /usr/share/doc/slitaz/$TOPIC.txt
   1.100 +elif [ -f "/usr/share/doc/slitaz/$TOPIC.txt" ]; then
   1.101 +	# SliTaz tools/libraries documentation (man-alike format)
   1.102 +	less -M "/usr/share/doc/slitaz/$TOPIC.txt"
   1.103  	return
   1.104  fi
   1.105  
   1.106 -for i in /usr/share/man/$LC_ALL/man$MAN_SECTION /usr/share/man/man$MAN_SECTION; do
   1.107 -	if [ -f $i/$TOPIC.* ]; then
   1.108 -		i=$(ls $i/$TOPIC.* 2> /dev/null)
   1.109 -		if [ "x$i" != "x" ]; then
   1.110 -			case "$i" in
   1.111 -				*gz) (zcat $i || unlzma -c $i 2>/dev/null) | less -M;;
   1.112 -				*) less -M $i;;
   1.113 -			esac
   1.114 +MANPAGE=$(find /usr/share/man/$MAN_LANG*/man$MAN_SECTION \
   1.115 +	/usr/local/share/man/$MAN_LANG*/man$MAN_SECTION \
   1.116 +	/usr/local/man/$MAN_LANG*/man$MAN_SECTION \
   1.117 +	/usr/share/man/man$MAN_SECTION \
   1.118 +	/usr/local/share/man/man$MAN_SECTION \
   1.119 +	/usr/local/man/man$MAN_SECTION \
   1.120 +	-name "$TOPIC".$MAN_SECTION\* 2>/dev/null)
   1.121 +if [ -n "$MANPAGE" ]; then
   1.122 +	case "$MANPAGE" in
   1.123 +		*html) display_html "$MANPAGE"
   1.124 +			return;;
   1.125 +	esac
   1.126 +	# "less"-ing a manpage is a BAD IDEA: man format is unreadable as is.
   1.127 +	# Use nroff if available; it outputs control chars, which busybox less
   1.128 +	# cannot handle: use "more" instead (or GNU less)
   1.129 +	if [ -x /usr/bin/nroff ]; then
   1.130 +		if [ x$(readlink $(which less)) == x/bin/busybox ]; then
   1.131 +			VIEW_CMD="more"
   1.132 +		else
   1.133 +			VIEW_CMD="less -rM"
   1.134  		fi
   1.135 +		case "$MANPAGE" in
   1.136 +			*gz) (zcat "$MANPAGE" || unlzma -c "$MANPAGE" 2>/dev/null) | \
   1.137 +				nroff -man | $VIEW_CMD;;
   1.138 +			*) nroff -man "$MANPAGE" | $VIEW_CMD;;
   1.139 +		esac
   1.140  		return
   1.141 +	else
   1.142 +		_ 'Found local manpage %s but no tool to display it.' $MANPAGE
   1.143 +		_ 'Consider installing groff by running: %s' \
   1.144 +			"su -c 'tazpkg get-install groff'"
   1.145 +		# do not quit, go on searching online
   1.146  	fi
   1.147 -	if [ check_retawq -a -f $i/$TOPIC.html ]; then
   1.148 -		retawq --dump=file://$i/$TOPIC.html | less -M
   1.149 -		return
   1.150 -	fi
   1.151 -done
   1.152 -
   1.153 -if [ "$SECTION" == 'all' ]
   1.154 -	then SECTIONS='1 2 3 4 5 6 7 8'
   1.155 -	else SECTIONS="$SECTION"
   1.156  fi
   1.157  
   1.158 -for SECTION in $SECTIONS; do
   1.159 -  URL=$( wget -q -O - http://man7.org/linux/man-pages/dir_all_alphabetic.html | \
   1.160 -        sed -n -r "s%.*href=\"\./(man./.*html)\">$TOPIC\($SECTION\).*%\1%p" )
   1.161 -  if [ -n "$URL" ]
   1.162 -  then
   1.163 -    wget -q -O - "http://man7.org/linux/man-pages/$URL" | \
   1.164 -    awk "BEGIN {s=0} /headline/{s=1} /COLOPHON/{s=0} {if(s) print}" | \
   1.165 -    sed -e 's%<span class="top-link">top</span>%%' -e 's/<[^>]*>//g' -e 's/&lt;/</g' \
   1.166 -        -e 's/&gt;/>/g' -e 's/&amp;/\&/g' -e 's/&nbsp;//g' | less -M
   1.167 -    exit 0
   1.168 -  fi
   1.169 -done
   1.170 -_ 'No manual entry for %s' "$TOPIC$MSG"
   1.171 +if [ "$SECTION" == 'all' ]; then
   1.172 +	#search alphabetically
   1.173 +	LETTER=$(printf "${TOPIC::1}" | tr 'A-Z[:punct:][:digit:]' a-z00)
   1.174 +	if [ $LETTER == '0' ]; then LETTER=other; fi
   1.175 +	SECTIONS=$(wget -q -O - http://linux.die.net/man/$LETTER.html | \
   1.176 +		sed -n -r "s%.*href=\"(.)/$TOPIC\".*%\1%p")
   1.177 +	[ -n "$SECTIONS" ] || { _ 'No manual entry for %s' "$TOPIC"; exit 0;}
   1.178 +	_n '%s found in the following sections:\n%s\nPlease choose one: ' "$TOPIC" \
   1.179 +		"$SECTIONS"
   1.180 +	read SECTION
   1.181 +fi
   1.182 +
   1.183 +MANURL="http://linux.die.net/man/$SECTION/$TOPIC"
   1.184 +
   1.185 +if ! wget -q --spider "$MANURL" 2>/dev/null ; then
   1.186 +	_ 'No manual entry for %s' "$TOPIC$MSG"
   1.187 +	exit 0
   1.188 +fi
   1.189 +
   1.190 +if [ -x /usr/bin/retawq ]; then
   1.191 +	retawq "$MANURL"
   1.192 +else
   1.193 +	URL="http://www.w3.org/services/html2txt?url=$MANURL&noinlinerefs=on&nonums=on"
   1.194 +	wget -q -O - "$URL" | tail -n+3 | \
   1.195 +		sed 's!\[[0-9]*\]!!g;/\[INS: :INS\]/d;/^   Site Search$/,$d' | less -M
   1.196 +fi
   1.197 +
   1.198  exit 0