dot

packages and services management
Log | Files | Refs | README

commit 5e8616f12b77d02e0517ca948eb30211b6e9779e
parent 9cb1d8ec1e13c56b5d980ea1b67b5d199367e8a6
Author: Josuah Demangeon <mail@josuah.net>
Date:   Fri,  5 May 2017 14:07:57 +0200

formatting raw wiki for terminal

Diffstat:
Mbin/doc-wiki | 66+++++++++++++++++++++++++++++++++++++++++++++++++++++-------------
1 file changed, 53 insertions(+), 13 deletions(-)

diff --git a/bin/doc-wiki b/bin/doc-wiki @@ -1,23 +1,63 @@ -# search for a wikipedia article and display raw wiki in a pager +# search for a wikipedia article and display it in a pager API='http://en.wikipedia.org/w/api.php?action=query&format=json' WIKI='http://en.wikipedia.org/wiki' +E="$(printf '\033')" + # search for a title -wget -qO- "$API&list=search&srsearch=$*" | awk -v RS='"title": *"' 'NR > 1 { - gsub(/".*/, ""); - print; -}' | iomenu -p wiki | +wget -qO- "$API&list=search&srsearch=$*" | +awk -v RS='"title": *"' 'NR > 1 { gsub(/".*/, ""); print; }' | + +# prompt the results +iomenu -p wiki | -# download the page's wiki +# download the raw wiki text xargs -I % wget -qO- "$WIKI/%?action=raw" | -# format and send it into a pager -awk '{ - gsub(/\]\]/, "\033[m"); - gsub(/\[\[[^|]*\|/, "\033[3m"); - gsub(/\[\[/, "\033[3m"); +# format and display the page +sed -r ' + # bold + s/'"'''"'([^'"'"']*)'"'''"'/{\1}/g # '''text''' + + # wiki links + s/\[\[[^]|]*\|/[/g # [[*| + s/\{\{[^}|]*\|/[/g # {{*| + s/\[\[/[/g; s/\]\]/]/g # [[ ]] + s/\{\{/[/g; s/\}\}/]/g # {{ }} + + # bullet lists + s/^(\*\**) */\n\1 / # * Text + + # strip html entities + s/<[^>]*\/ *>//g # <tag /> + s/<!--[^>]*-->//g # <!-- comment --> + s/<[^>]*>[^<]*<\/[^>]*>//g # <tag> * </tag> + s/&[^;]*;/ /g # &escaped; + +' | fold -w 72 -s | sed -r ' + + # paragraphs + /^[^=;]/ s/^/ / + + # heading + s/^== *([^=].*) *==$/\n{\1}\n/g # == Heading == + s/^==(=*) *(.*[^=]) *(=*)==$/\n{\1 \2 \3}\n/g + + # subheading + s/^; *(.*)/\n {\1}\n/g # === Subheading === + + # underlined + s/\[/'$E[4m'/g # [ * ] + s/\]/'$E[m'/g + + # bold + s/\{/'$E[1m'/g # { * } + s/\}/'$E[m'/g + +' | sed ' - print; + # double empty lines + N; s/^\n$//; P; D -}' | fold -w 80 -s | sed 's/ *$//' | $PAGER +' | less -R