Subject: Re: man2html troubles
To: NetBSD - Users <netbsd-users@netbsd.org>
From: Brian Rose <lists@brianrose.net>
List: netbsd-users
Date: 09/05/2003 22:31:26
Brian Rose wrote:
> 
> That worked, but the output is rather ugly. Is there some way to specify 
> a template? A quick look at the man page for groff does not mention 
> anything.
> 

I wrote a simple sed script to search and replace tokens in the man file with HTML tags. It is very primitive, but the output is what I was looking for. I thought I'd throw it out there for everyone to chew on and improve.

It is invoked like this...

 $cat /usr/share/man/man1/ls.1 | ./man2html.sed > output.html

And here's the script

#!/usr/bin/sed -f
#
# SED script to convert a man page to HTML
#
#

# remove all comments
/^\.\\.*$/d


# Replace fields

# .Nm x - Name 
s/^\.Nm \([^ ]*\)/\<B\>\1\<\/B\>/

# .Dd - Date
s/^\.Dd\(.*\)/\<B\>\1\<\/B\>\<BR\>/

# .Dt - Title 
s/^\.Dt\(.*\)/\<B\>\1\<\/B\>/

# .Os - Blank Line 
s/^\.Os\(.*\)/\<BR\>/

# .Pp - New Paragraph 
s/^\.Pp\(.*\)/\<P\>/

# .Sh - Heading
s/^\.Sh\(.*\)/\<H3\>\1\<\/H3\>/

# .Nd - Description 
s/^\.Nd\(.*\)/ - \1/

# .Op Fl - options 
s/^\.Op Fl\(.*\)/\[\1\]/

# .Op Ar - options 
s/^\.Op Ar\(.*\)/\[\1\]/

# .Fl - options 
s/^\.Fl\(.*\)/\<B\>\1\<\/B\>/

# .Ar - options 
s/^\.Ar\(.*\)/\<B\>\1\<\/B\>/

# .Bl - Begin list Indent 
s/^\.Bl\(.*\)indent/\<OL\>/

# .It - List Item 
s/^\.It Fl \([^ ]*\)/\<LI\>\<B\>\1\<\/B\>/

# .It - List Item 
s/^\.It\(.*\)/\<LI\>\1/

# .El - End listIndent 
s/^\.El\(.*\)/\<\/OL\>/

# .Pq Fl -  
s/^\.Pq Fl\(.*\)/\<B\>\1\<\/B\>/

# .Dq - Quoted text 
s/^\.Dq\(.*\)/\"\1\"/

# .Ql -  
s/^\.Ql \\\&\([^ ]*\)/\<U\>\1\<\/U\>/

# .Xr - A Link  
s/^\.Xr \([^ ]*\) [0-9]/\<U\>\1\<\/U\>/

# .St -p - POSIX text 
s/^\.St \-p/POSIX /

# .At  -  At&T Version text
s/^\.At v/AT\&T Version/

# Brackets  
s/\\\*\[Lt\]\([^ ]\)*\\\[Gt\]/\<\1\>/