Converting troff to HTML
troff is a very sophisticated system so doing this right would be a lot of work. But, writing something to get rid of 90% of the grunt work of conversion is pretty easy. Paul Dunne did one for the ms macro set which you can find here but mm is different.
As awk was (and maybe still is) my favorite choice for one-shot "fix the data" programs, it got called to duty to do the task. Here is what I decided was worth it.
#!/usr/bin/awk -f
# troff -mm to HTML converter
# Phil Hughes--April 21, 1997
# Updates:
#
#
# This doesn't do everything and it is not intended to do everyting.
# It does what is easy. In particular, headers and the title is
# not mucked with. Also, center requests only center one line (I think).
#
# The goal here was to do the stuff that is a total pain to do by hand.
# This includes section numbering, font changes (which is still rather
# dumb) and lists. The output of this program should be considered
# a good starting point for making good HTML.
#
# Here is what is currently recognized:
# .H - deals with heading levels
# .P - maps to <p> (as does a blank line)
# .BL - maps to <ul>
# .AL - maps to <ol>
# .LE - maps to the end of the list of the type most recently started
# .LI - <li>
# .ds - tossed
# .ce - centers next line
# \fB, \f(HB, \fI, - changes to bold, bold or italic
# \fP - goes back to previous font
# \(em - --
# .PF, .PH - tossed
# \s - tossed
# That's all folks.
#
BEGIN {
BLANKS = " "
# beginning HTML crap
print "<html>"
print "<head>"
print "<title> ============</title>"
print "</head>"
print "<body>"
}
{ # always convert these things
# yes, there is a lot to add here
gsub(/\\\(em/, "--") # \em to --
gsub(/.\\".*$/, "") # trash comments
gsub(/.PF.*$/, "") # trash all sorts of headers & footers
gsub(/.PH.*$/, "") # trash all sorts of headers & footers
gsub(/\\s[0-9+-][1-9]?/, "") # trash point size changes
}
/^\.H / { # heading
head_level = $2
head[head_level+1] = 0
head[head_level+2] = 0
head[head_level+3] = 0
head[head_level+4] = 0
head[head_level+5] = 0
head[head_level]++
$1 = ""
$2 = ""
gsub(/"/,"")
printf "<h" head_level ">"
for (x=1; x <= head_level; x++) {
printf "%d.", head[x]
}
printf " "
print $0 "</h" head_level ">"
next
}
/^ *$/ { # paragraph
"<p>"
next
}
/^\.P *$/ { # paragraph
print "<p>"
next
}
/^\.BL/ { # bulleted list
print "<ul>"
list[++ll] = "</ul>"
indent += 2
next
}
/^\.AL/ { # alpha list
print "<ol>"
list[++ll] = "</ol>"
indent += 2
next
}
/^\.LI/ { # list item
print substr(BLANKS, 1, indent) "<li>"
next
}
/^\.LE/ { # list end
print list[ll--]
indent -= 2
next
}
/\.ds/ { # trash them
next
}
/^\.ce/ { # center next line(s)--only does one line for now
print "<p align=\"center\">"
next
}
{ # print whatever we have left
# hard stuff like font changes where we need to remember
split($0,tmp,"\\")
for (x in tmp) {
if (sub(/^fB/, "<b>", tmp[x]) == 1) {
new_sub = "</b>"
}
if (sub(/^f\(HB/, "<b>", tmp[x]) == 1) {
new_sub = "</b>"
}
if (sub(/^fI/, "<i>", tmp[x]) == 1) {
new_sub = "</i>"
}
if (sub(/^fP/, new_sub, tmp[x]) == 1) {
new_sub = "#####"
}
}
for (x in tmp) {
printf "%s", substr(BLANKS, 1, indent)
printf "%s", tmp[x]
}
print ""
}
END {
# ending HTML crap
print "</body>"
print "</html>"
}
Most of this is pretty ordinary and brute force. Note that the indenting I add in the output is cosmetic to make it easier to see what is going on. The only "hard part" was dealing with the headings.
In troff with mm, headings are of the form section.subsection.subsubsection ... followed by text. For example, 3.5.1 This is a test would be a standard looking heading. I put this together by using an HTML heading tag of a corresponding level manually counting the number of sections at the current level. For those unfamiliar with awk, let's look at this piece of the code:
head[head_level]++
$1 = ""
$2 = ""
gsub(/"/,"")
printf ""
for (x=1; x <= head_level; x++) {
printf "%d.", head[x]
}
printf " "
print $0 " "
The head array keeps track of the current section number at each level. After incrementing the section, the next two lines look a bit strange. In awk, $0 is the full input line and the pieces (parsed using the current field separator) get assigned to $1, $2 and so on. If you assign to htem, $0 gets updated so all this does is toss the first two fields from the input line—the troff .H tag and the level number.
The for look builds the section string and the final print statement prints the original input line minus the first two fields and appends the appropriate <\h to it.
Like with my shell script, this one a one-time fix that focused on the task at hand. Depending on what you had done in your troff code, there may be other tags worth converting.
Phil Hughes
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Designing Electronics with Linux | May 22, 2013 |
| Dynamic DNS—an Object Lesson in Problem Solving | May 21, 2013 |
| Using Salt Stack and Vagrant for Drupal Development | May 20, 2013 |
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
- New Products
- Linux Systems Administrator
- Senior Perl Developer
- Technical Support Rep
- UX Designer
- Web & UI Developer (JavaScript & j Query)
- Designing Electronics with Linux
- Dynamic DNS—an Object Lesson in Problem Solving
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Using Salt Stack and Vagrant for Drupal Development
- Reply to comment | Linux Journal
4 hours 26 min ago - Nice article, thanks for the
15 hours 7 min ago - I once had a better way I
20 hours 53 min ago - Not only you I too assumed
21 hours 10 min ago - another very interesting
23 hours 3 min ago - Reply to comment | Linux Journal
1 day 57 min ago - Reply to comment | Linux Journal
1 day 7 hours ago - Reply to comment | Linux Journal
1 day 8 hours ago - Favorite (and easily brute-forced) pw's
1 day 9 hours ago - Have you tried Boxen? It's a
1 day 15 hours ago
Enter to Win an Adafruit Pi Cobbler Breakout Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Pi Cobbler Breakout Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- 5-21-13, Prototyping Pi Plate Kit: Philip Kirby
- Next winner announced on 5-27-13!
Featured Jobs
| Linux Systems Administrator | Houston and Austin, Texas | Host Gator |
| Senior Perl Developer | Austin, Texas | Host Gator |
| Technical Support Rep | Houston and Austin, Texas | Host Gator |
| UX Designer | Austin, Texas | Host Gator |
| Web & UI Developer (JavaScript & j Query) | Austin, Texas | Host Gator |
Free Webinar: Hadoop
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Some of key questions to be discussed are:
- What is the “typical” Hadoop cluster and what should be installed on the different machine types?
- Why should you consider the typical workload patterns when making your hardware decisions?
- Are all microservers created equal for Hadoop deployments?
- How do I plan for expansion if I require more compute, memory, storage or networking?



Comments
yecch, doesn't work with tbl
I'm trying to get a printable version of the procps ("Linux ps") manpage.
groff -t -mandoc -Thtml sets the tables in images, and they're not even close to usable.
groff -t -mandoc -Tps sets the tables in Postscript but the paragraphs in table cells don't wrap, they go way off the page.
Best I can do so far is leave off the -t. The markup is easier to read than what groff does with it.
Ok, nice try. Are you aware
Ok, nice try.
Are you aware of groff which is able to generate HTML using -Thtml as output device
typical use is
$ groff -mwww -Thtml inputfile > out.html
Heinz