Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save SimonSchubert/79623461945415d1d8e2 to your computer and use it in GitHub Desktop.
Save SimonSchubert/79623461945415d1d8e2 to your computer and use it in GitHub Desktop.
#!/bin/bash
# $1 = command
# $2 = man page section
# $3 = special file extension
OUT_DIR="/home/simon/manpages"
special=${3-$2}
cd "/usr/share/man/man"$2
dname=`dirname $1.$2.gz`
mkdir -p $OUT_DIR/$dname
# Fetch man page as html
manpage="$(zcat $1.$special.gz | groff -mandoc -Thtml)"
# Replace double quotes with single quotes
manpage="${manpage//\"/\'}"
# Remove style
manpage="$(echo $manpage | sed -r 's/<style([^<]|<[^\/]|<\/[^s]|<\/s[^t])*<\/style>//g')"
# Remove header
manpage="$(echo $manpage | sed -r 's/<head([^<]|<[^\/]|<\/[^s]|<\/s[^t])*<\/head>//g')"
# Remove comments
manpage="$(echo $manpage | sed -r 's/<!--.*?-->//g')"
# Remove structure list
manpage="$(echo $manpage | sed -r 's/<h1 align.*?<br> <hr>//g')"
# Change header colors
# manpage="${manpage//\<h2/\<h2 style=\'color:#FFD9D9\'}"
manpage="${manpage//\<h2/\<font color=\'#FFD9D9\'><h2}"
manpage="${manpage//\<\/h2>/\<\/h2><\/font>}"
# remove blue font
manpage="${manpage//\<font color=\'#0000FF\'/<font}"
# remove black font
manpage="${manpage//\<font color=\'#000000\'/<font}"
# Replace dash with minus
manpage="${manpage//&mdash;/-}"
manpage="${manpage//&minus;/-}"
# Strip out description
description="$(echo $manpage | awk -v FS="( - |</p>)" '{print $2}')"
text="INSERT INTO commands (category, name, description, manpage) VALUES (\"$2\",\"$1\",\"$description\",\"$manpage\")"
echo -n $text | xclip -selection c
echo ""
echo $description
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment