Skip to content

Instantly share code, notes, and snippets.

@gabrielg
Created May 8, 2012 02:15
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gabrielg/2632036 to your computer and use it in GitHub Desktop.
Save gabrielg/2632036 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Formats a man page for reading in a browser, and opens it. Example:
#
# hman xsltproc
#
stylesheet=$(cat <<eocss
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" encoding="UTF-8" indent="yes"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="style" />
<xsl:template match="@style|@valign|@width|@align|@border|@cellspacing|@cellpadding|@frame|@rules" />
<xsl:template match="head">
<head>
<xsl:apply-templates select="@* | *"/>
<style type="text/css">
body {
font-family: Helvetica, arial, sans-serif;
font-size: 14px;
line-height: 1.6;
padding-top: 10px;
padding-bottom: 10px;
background-color: white;
padding: 30px;
}
a { color: #4183C4; }
h1, h2, h3, h4, h5, h6 {
margin: 20px 0 10px;
padding: 0;
font-weight: bold;
-webkit-font-smoothing: antialiased;
cursor: text;
position: relative;
}
h1 {
font-size: 28px;
color: black;
}
h2 {
font-size: 24px;
border-bottom: 1px solid #cccccc;
color: black;
}
h3 { font-size: 18px; }
h4 { font-size: 16px; }
h5 { font-size: 14px; }
h6 {
color: #777777;
font-size: 14px;
}
p, table, pre {
margin: 15px 0;
}
</style>
</head>
</xsl:template>
</xsl:stylesheet>
eocss)
if ! manpath=`man -w $@`
then
exit 1
else
if [ ${manpath##*.} = "gz" ]; then catter="gzcat"; else catter="cat"; fi
outpath="$TMPDIR$$.$RANDOM.html"
$catter $manpath | groff -mandoc -Thtml -P -r | tidy -q -asxhtml --input-encoding ascii --output-encoding utf8 2>/dev/null | xsltproc --html <(echo "$stylesheet") - > $outpath
open $outpath
fi
@gabrielg
Copy link
Author

gabrielg commented May 8, 2012

Example output: screenshot

@mikelikesbikes
Copy link

mike:bin $ hman which
/dev/fd/63:1: parser error : Document is empty

^
/dev/fd/63:1: parser error : Start tag expected, '<' not found

^
cannot parse /dev/fd/63
line 21 column 1 - Warning: <p> proprietary attribute "valign"
line 24 column 1 - Warning: <p> proprietary attribute "valign"
line 30 column 1 - Warning: <p> proprietary attribute "valign"
line 36 column 1 - Warning: <p> proprietary attribute "valign"
line 47 column 1 - Warning: <p> proprietary attribute "valign"
line 54 column 1 - Warning: <p> proprietary attribute "valign"
line 65 column 1 - Warning: <p> proprietary attribute "valign"
line 70 column 1 - Warning: <p> proprietary attribute "valign"
line 75 column 1 - Warning: <p> proprietary attribute "valign"

Thoughts?

@gabrielg
Copy link
Author

gabrielg commented May 8, 2012

I updated it to quote the stylesheet, which I forgot. Try again?

@mikelikesbikes
Copy link

Hrm... same issue. Will poke at it in the morning.

@nickmartini
Copy link

Works for me, using Safari.

@phildobbin
Copy link

generates a lot of noise such as:

`-:3596: element a: validity error : ID heading25 already defined

SIGNALS

`

but works fine in Firefox 12, OS X Snow Leopard.

Nice :-) Thanks...

@sethherr
Copy link

sethherr commented May 8, 2012

This may be something silly, but..

/Users/seth/hman.sh: line 82: syntax error near unexpected token `('
/Users/seth/hman.sh: line 82: `  $catter $manpath | groff -mandoc -Thtml -P -r | tidy -q -asxhtml --input-encoding ascii --output-encoding utf8 | xsltproc --html <(echo "$stylesheet") - > $outpath'

Thoughts on that?

@gabrielg
Copy link
Author

gabrielg commented May 8, 2012

@sethherr - I don't know for sure, but you might have an old version of bash. This is mine:

gabriel@moya ~ » bash --version
GNU bash, version 3.2.48(1)-release (x86_64-apple-darwin11)
Copyright (C) 2007 Free Software Foundation, Inc.

@mikelikesbikes
Copy link

This may be the same issue I was seeing last night. I tried this out on my much more up-to-date work machine, and it worked fine. I did still get a decent amount of console spew afterwards, but I did get the HTML displayed this time.

Here's the console spew I saw:

mbusch:bin $ hman which
line 21 column 1 - Warning: <p> proprietary attribute "valign"
line 24 column 1 - Warning: <p> proprietary attribute "valign"
line 30 column 1 - Warning: <p> proprietary attribute "valign"
line 36 column 1 - Warning: <p> proprietary attribute "valign"
line 47 column 1 - Warning: <p> proprietary attribute "valign"
line 54 column 1 - Warning: <p> proprietary attribute "valign"
line 65 column 1 - Warning: <p> proprietary attribute "valign"
line 70 column 1 - Warning: <p> proprietary attribute "valign"
line 75 column 1 - Warning: <p> proprietary attribute "valign"

@gabrielg
Copy link
Author

gabrielg commented May 8, 2012

That's tidy being noisy. Updated to redirect its stderr to /dev/null.

@nickmartini
Copy link

weird. github lets me edit other people's comments?

@avakarev
Copy link

Didn't work on OSX 10.6.6

$:> hman nginx
/dev/fd/63:1: parser error : Document is empty

^
/dev/fd/63:1: parser error : Start tag expected, '<' not found

^
cannot parse /dev/fd/63

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment