]>
git.ipfire.org Git - thirdparty/cups.git/blob - standards/rfctohtml.c
2 * "$Id: rfctohtml.c 6649 2007-07-11 21:46:42Z mike $"
4 * RFC file to HTML conversion program.
6 * Copyright 2007 by Apple Inc.
7 * Copyright 2006 by Easy Software Products.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
17 * main() - Convert a man page to HTML.
18 * put_entity() - Put a single character, using entities as needed.
19 * put_line() - Put a whole string for a line.
23 * Include necessary headers.
26 #include <cups/string.h>
28 #include <cups/file.h>
35 void put_entity(cups_file_t
*fp
, int ch
);
36 void put_line(cups_file_t
*fp
, const char *line
);
40 * 'main()' - Convert a man page to HTML.
43 int /* O - Exit status */
44 main(int argc
, /* I - Number of command-line args */
45 char *argv
[]) /* I - Command-line arguments */
47 cups_file_t
*infile
, /* Input file */
48 *outfile
; /* Output file */
49 char line
[1024], /* Line from file */
50 *lineptr
, /* Pointer into line */
51 name
[1024], /* Heading anchor name */
52 *nameptr
; /* Pointer into anchor name */
54 inheading
, /* Inside a heading? */
55 inpre
, /* Inside preformatted text? */
56 intoc
, /* Inside table-of-contents? */
57 toclevel
, /* Current table-of-contents level */
58 linenum
; /* Current line on page */
67 fputs("Usage: rfctohtml [rfcNNNN.txt [rfcNNNN.html]]\n", stderr
);
72 * Open files as needed...
77 if ((infile
= cupsFileOpen(argv
[1], "r")) == NULL
)
84 infile
= cupsFileOpenFd(0, "r");
88 if ((outfile
= cupsFileOpen(argv
[2], "w")) == NULL
)
91 cupsFileClose(infile
);
96 outfile
= cupsFileOpenFd(1, "w");
99 * Read from input and write the output...
102 cupsFilePuts(outfile
,
103 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" "
104 "\"http://www.w3.org/TR/REC-html40/loose.dtd\">\n"
106 "<!-- SECTION: Specifications -->\n"
108 "\t<style type='text/css'><!--\n"
109 "\th1, h2, h3 { font-family: sans-serif; }\n"
110 "\tp, pre { font-family: monospace; }\n"
111 "\th2.title, h3.title, h3.title { border-bottom: solid "
116 * Skip the initial header stuff (working group ID, RFC #, authors, and
123 while (cupsFileGets(infile
, line
, sizeof(line
)))
131 while (cupsFileGets(infile
, line
, sizeof(line
)))
137 else if (!strncasecmp(line
, "Request for Comments:", 21))
138 rfc
= atoi(line
+ 21);
142 * Read the document title...
145 while (cupsFileGets(infile
, line
, sizeof(line
)))
153 for (lineptr
= line
; isspace(*lineptr
& 255); lineptr
++);
155 cupsFilePrintf(outfile
, "<title>RFC %d: %s", rfc
, lineptr
);
157 while (cupsFileGets(infile
, line
, sizeof(line
)))
165 for (lineptr
= line
; isspace(*lineptr
& 255); lineptr
++);
166 cupsFilePrintf(outfile
, " %s", lineptr
);
170 cupsFilePuts(outfile
, "</title>\n"
175 * Read the rest of the file...
183 while (cupsFileGets(infile
, line
, sizeof(line
)))
194 cupsFilePuts(outfile
, "</pre>\n");
201 cupsFilePuts(outfile
, "</h1>\n");
203 cupsFilePrintf(outfile
, "</a></h%d>\n", inheading
);
208 else if ((line
[0] == ' ' ||
209 (!isupper(line
[0] & 255) && !isdigit(line
[0] & 255) &&
210 !strstr(line
, "[Page "))) && !inheading
)
215 cupsFilePuts(outfile
, "</h1>\n");
217 cupsFilePrintf(outfile
, "</a></h%d>\n", inheading
);
222 for (lineptr
= line
; *lineptr
== ' '; lineptr
++);
226 char *temp
; /* Temporary pointer into line */
227 int level
; /* Heading level */
230 if (isdigit(*lineptr
& 255))
232 strlcpy(name
, lineptr
, sizeof(name
));
234 for (nameptr
= name
, level
= -1; *nameptr
;)
235 if (isdigit(*nameptr
& 255))
237 while (isdigit(*nameptr
& 255))
242 else if (*nameptr
== ' ')
250 while (toclevel
> level
)
252 cupsFilePuts(outfile
, "\n</ul>");
256 while (toclevel
< level
)
258 cupsFilePuts(outfile
, "\n<ul style=\"list-style-type: none;\">\n");
262 cupsFilePrintf(outfile
, "\n<%s><a href=\"#s%s\">", toclevel
? "li" : "p",
266 temp
= lineptr
+ strlen(lineptr
) - 1;
268 while (temp
> lineptr
)
269 if (*temp
== ' ' || !isdigit(*temp
& 255))
276 while (temp
> lineptr
)
277 if (*temp
!= ' ' && *temp
!= '.')
285 if (isdigit(*lineptr
& 255))
286 put_line(outfile
, lineptr
);
288 put_line(outfile
, lineptr
- 1);
291 cupsFilePuts(outfile
, "</a>");
295 cupsFilePuts(outfile
, "\n<pre>");
296 put_line(outfile
, line
);
301 cupsFilePutChar(outfile
, '\n');
302 put_line(outfile
, line
);
305 else if (strstr(line
, "[Page "))
308 * Skip page footer and header...
311 cupsFileGets(infile
, line
, sizeof(line
));
312 cupsFileGets(infile
, line
, sizeof(line
));
313 cupsFileGets(infile
, line
, sizeof(line
));
314 cupsFileGets(infile
, line
, sizeof(line
));
317 else if (isdigit(line
[0] & 255) && !inheading
)
319 int level
; /* Heading level */
326 cupsFilePuts(outfile
, "\n</ul>");
330 cupsFilePutChar(outfile
, '\n');
336 cupsFilePuts(outfile
, "</pre>\n");
340 strlcpy(name
, line
, sizeof(name
));
341 for (nameptr
= name
, level
= 1; *nameptr
;)
342 if (isdigit(*nameptr
& 255))
344 while (isdigit(*nameptr
& 255))
349 else if (*nameptr
== ' ')
357 cupsFilePrintf(outfile
, "\n<h%d class='title'><a name='s%s'>", level
,
359 put_line(outfile
, line
);
370 cupsFilePuts(outfile
, "\n</ul>");
374 cupsFilePutChar(outfile
, '\n');
380 cupsFilePuts(outfile
, "\n<h2 class='title'>");
384 put_line(outfile
, line
);
386 intoc
= !strcasecmp(line
, "Table of Contents");
392 cupsFilePuts(outfile
, "</pre>\n");
397 cupsFilePuts(outfile
, "</h2>\n");
399 cupsFilePrintf(outfile
, "</a></h%d>\n", inheading
);
402 cupsFilePuts(outfile
, "</body>\n"
409 cupsFileClose(infile
);
410 cupsFileClose(outfile
);
413 * Return with no errors...
421 * 'put_entity()' - Put a single character, using entities as needed.
425 put_entity(cups_file_t
*fp
, /* I - File */
426 int ch
) /* I - Character */
429 cupsFilePuts(fp
, "&");
431 cupsFilePuts(fp
, "<");
433 cupsFilePutChar(fp
, ch
);
438 * 'put_line()' - Put a whole string for a line.
442 put_line(cups_file_t
*fp
, /* I - File */
443 const char *s
) /* I - String */
445 int whitespace
, /* Saw whitespace */
447 len
; /* Length of keyword */
448 static const char * const keywords
[] =/* Special keywords to boldface */
465 if (whitespace
&& isupper(*s
& 255))
469 for (i
= 0; i
< (int)(sizeof(keywords
) / sizeof(sizeof(keywords
[0]))); i
++)
471 len
= strlen(keywords
[i
]);
472 if (!strncmp(s
, keywords
[i
], len
) && (isspace(s
[len
] & 255) || !*s
))
474 cupsFilePrintf(fp
, "<b>%s</b>", keywords
[i
]);
480 if (i
>= (int)(sizeof(keywords
) / sizeof(sizeof(keywords
[0]))))
481 put_entity(fp
, *s
++);
488 put_entity(fp
, *s
++);
495 * End of "$Id: rfctohtml.c 6649 2007-07-11 21:46:42Z mike $".