]>
git.ipfire.org Git - thirdparty/cups.git/blob - man/mantohtml.c
2 * "$Id: mantohtml.c 4867 2005-12-03 15:45:53Z mike $"
4 * Man page to HTML conversion program.
6 * Copyright 2004-2005 by Easy Software Products.
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
26 * main() - Convert a man page to HTML.
27 * putc_entity() - Put a single character, using entities as needed.
28 * strmove() - Move characters within a string.
32 * Include necessary headers.
35 #include <cups/string.h>
43 static void putc_entity(int ch
, FILE *fp
);
44 static void strmove(char *d
, const char *s
);
48 * 'main()' - Convert a man page to HTML.
51 int /* O - Exit status */
52 main(int argc
, /* I - Number of command-line args */
53 char *argv
[]) /* I - Command-line arguments */
55 FILE *infile
, /* Input file */
56 *outfile
; /* Output file */
57 char line
[1024], /* Line from file */
58 *lineptr
, /* Pointer into line */
59 name
[1024]; /* Man page name */
60 int section
, /* Man page section */
61 pre
, /* Preformatted */
62 font
, /* Current font */
63 blist
, /* In a bullet list? */
64 list
, /* In a list? */
65 linenum
; /* Current line number */
66 const char *post
; /* Text to add after the current line */
67 static const char /* Start/end tags for fonts */
68 * const start_fonts
[] = { "", "<b>", "<i>" },
69 * const end_fonts
[] = { "", "</b>", "</i>" };
77 fputs("Usage: mantohtml [filename.man [filename.html]]\n", stderr
);
82 * Open files as needed...
87 if ((infile
= fopen(argv
[1], "r")) == NULL
)
98 if ((outfile
= fopen(argv
[2], "w")) == NULL
)
109 * Read from input and write the output...
112 fputs("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" "
113 "\"http://www.w3.org/TR/REC-html40/loose.dtd\">\n"
115 "<!-- SECTION: Man Pages -->\n"
117 "\t<style type='text/css'><!--\n"
118 "\th1, h2, h3, p { font-family: sans-serif; text-align: justify; }\n"
119 "\ttt, pre a:link, pre a:visited, tt a:link, tt a:visited { font-weight: bold; color: #7f0000; }\n"
120 "\tpre { font-weight: bold; color: #7f0000; margin-left: 2em; }\n"
121 "\th1.title, h2.title, h3.title { border-bottom: solid 2px #000000; }\n"
122 "\t--></style>\n", outfile
);
132 while (fgets(line
, sizeof(line
), infile
))
139 * Strip leading whitespace...
142 while (line
[1] == ' ' || line
[1] == '\t')
143 strmove(line
+ 1, line
+ 2);
146 * Process man page commands...
149 if (!strncmp(line
, ".TH ", 4) && section
< 0)
152 * Grab man page title...
155 sscanf(line
+ 4, "%s%d", name
, §ion
);
158 "\t<title>%s</title>\n"
161 "<h2 class='title'><a name=\'%s.%d\'>%s(%d)</a></h2>\n"
163 name
, name
, section
, name
, section
, start_fonts
[font
]);
165 else if (section
< 0)
167 else if (!strncmp(line
, ".SH ", 4) || !strncmp(line
, ".Sh ", 4))
175 fputs(end_fonts
[font
], outfile
);
179 fputs("</li>\n</ul>\n", outfile
);
186 fputs("</dt>\n", outfile
);
188 fputs("</dd>\n", outfile
);
190 fputs("</dl>\n", outfile
);
195 fputs("<h3>", outfile
);
197 fputs("<h4>", outfile
);
199 for (lineptr
= line
+ 4; *lineptr
; lineptr
++)
200 if (*lineptr
== '\"')
202 else if (*lineptr
== ' ')
204 putc_entity(' ', outfile
);
208 else if (*lineptr
!= '\n')
211 putc_entity(toupper(*lineptr
), outfile
);
213 putc_entity(tolower(*lineptr
), outfile
);
219 fprintf(outfile
, "</h3>\n%s", start_fonts
[font
]);
221 fprintf(outfile
, "</h4>\n%s", start_fonts
[font
]);
223 else if (!strncmp(line
, ".LP", 3) || !strncmp(line
, ".PP", 3))
229 fputs(end_fonts
[font
], outfile
);
233 fputs("</li>\n</ul>\n", outfile
);
240 fputs("</dt>\n", outfile
);
242 fputs("</dd>\n", outfile
);
244 fputs("</dl>\n", outfile
);
248 fputs("<p>", outfile
);
251 else if (!strncmp(line
, ".TP ", 4))
257 fputs(end_fonts
[font
], outfile
);
261 fputs("</li>\n</ul>\n", outfile
);
266 fputs("<dl>\n", outfile
);
268 fputs("</dt>\n", outfile
);
270 fputs("</dd>\n", outfile
);
272 fputs("<dt>", outfile
);
276 else if (!strncmp(line
, ".br", 3))
284 fputs("</dt>\n<dd>", outfile
);
288 fputs("</dd>\n<dd>", outfile
);
290 fputs("<br>\n", outfile
);
292 else if (!strncmp(line
, ".de ", 4))
295 * Define macro - ignore...
298 while (fgets(line
, sizeof(line
), infile
))
302 if (!strncmp(line
, "..", 2))
306 else if (!strncmp(line
, ".RS", 3))
312 fputs("<div style='margin-left: 3em;'>\n", outfile
);
314 else if (!strncmp(line
, ".RE", 3))
320 fputs("</div>\n", outfile
);
322 else if (!strncmp(line
, ".ds ", 4) || !strncmp(line
, ".rm ", 4) ||
323 !strncmp(line
, ".tr ", 4) || !strncmp(line
, ".hy ", 4) ||
324 !strncmp(line
, ".IX ", 4) || !strncmp(line
, ".PD", 3) ||
325 !strncmp(line
, ".Sp", 3))
328 * Ignore unused commands...
331 else if (!strncmp(line
, ".Vb", 3) || !strncmp(line
, ".nf", 3))
334 * Start preformatted...
338 fputs("<pre>\n", outfile
);
340 else if (!strncmp(line
, ".Ve", 3) || !strncmp(line
, ".fi", 3))
343 * End preformatted...
349 fputs("</pre>\n", outfile
);
352 else if (!strncmp(line
, ".IP \\(bu", 8))
359 fputs("</li>\n", outfile
);
362 fputs("<ul>\n", outfile
);
366 fputs("<li>", outfile
);
368 else if (!strncmp(line
, ".IP ", 4))
371 * Indented paragraph...
376 fputs("</li>\n</ul>\n", outfile
);
380 fputs("<p style='margin-left: 3em;'>", outfile
);
382 for (lineptr
= line
+ 4; isspace(*lineptr
); lineptr
++);
384 if (*lineptr
== '\"')
386 strmove(line
, lineptr
+ 1);
388 if ((lineptr
= strchr(line
, '\"')) != NULL
)
393 strmove(line
, lineptr
);
395 if ((lineptr
= strchr(line
, ' ')) != NULL
)
400 * Process the text as if it was in-line...
403 post
= "\n<br />\n<br />";
406 else if (!strncmp(line
, ".\\}", 3))
409 * Ignore close block...
412 else if (!strncmp(line
, ".ie", 3) || !strncmp(line
, ".if", 3) ||
413 !strncmp(line
, ".el", 3))
416 * If/else - ignore...
419 if (strchr(line
, '{') != NULL
)
422 * Skip whole block...
425 while (fgets(line
, sizeof(line
), infile
))
429 if (strchr(line
, '}') != NULL
)
435 else if (!strncmp(line
, ". ", 4))
442 else if (!strncmp(line
, ".B ", 3))
448 fprintf(outfile
, "%s<b>%s</b>%s", end_fonts
[font
], line
+ 3,
451 else if (!strncmp(line
, ".I ", 3))
454 * Grab italic text...
457 fprintf(outfile
, "%s<i>%s</i>%s", end_fonts
[font
], line
+ 3,
460 else if (strncmp(line
, ".\\\"", 3))
466 if ((lineptr
= strchr(line
, ' ')) != NULL
)
468 else if ((lineptr
= strchr(line
, '\n')) != NULL
)
471 fprintf(stderr
, "mantohtml: Unknown man page command \'%s\' on line %d!\n",
476 * Skip continuation lines...
479 lineptr
= line
+ strlen(line
) - 2;
480 if (lineptr
>= line
&& *lineptr
== '\\')
482 while (fgets(line
, sizeof(line
), infile
))
485 lineptr
= line
+ strlen(line
) - 2;
487 if (lineptr
< line
|| *lineptr
!= '\\')
495 * Process man page text...
500 for (lineptr
= line
; *lineptr
; lineptr
++)
502 if (*lineptr
== '\\')
507 else if (isdigit(lineptr
[0]) && isdigit(lineptr
[1]) &&
510 fprintf(outfile
, "&#%d;", ((lineptr
[0] - '0') * 8 +
511 lineptr
[1] - '0') * 8 +
515 else if (*lineptr
== '&')
517 else if (*lineptr
== 's')
519 while (lineptr
[1] == '-' || isdigit(lineptr
[1]))
522 else if (*lineptr
== '*')
526 else if (*lineptr
!= 'f')
527 putc_entity(*lineptr
, outfile
);
535 fputs(end_fonts
[font
], outfile
);
539 default : /* Regular */
542 case 'B' : /* Bold */
546 case 'I' : /* Italic */
552 fputs(start_fonts
[font
], outfile
);
557 putc_entity(*lineptr
, outfile
);
562 fputs(post
, outfile
);
568 fprintf(outfile
, "%s\n", end_fonts
[font
]);
572 fputs("</li>\n</ul>\n", outfile
);
579 fputs("</dt>\n", outfile
);
581 fputs("</dd>\n", outfile
);
583 fputs("</dl>\n", outfile
);
588 "</html>\n", outfile
);
597 if (outfile
!= stdout
)
601 * Return with no errors...
609 * 'putc_entity()' - Put a single character, using entities as needed.
613 putc_entity(int ch
, /* I - Character */
614 FILE *fp
) /* I - File */
626 * 'strmove()' - Move characters within a string.
630 strmove(char *d
, /* I - Destination */
631 const char *s
) /* I - Source */
641 * End of "$Id: mantohtml.c 4867 2005-12-03 15:45:53Z mike $".