]>
git.ipfire.org Git - thirdparty/cups.git/blob - standards/rfctohtml.c
2 * "$Id: rfctohtml.c 5143 2006-02-21 19:13:01Z mike $"
4 * RFC file to HTML conversion program.
6 * Copyright 2006 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 * put_entity() - Put a single character, using entities as needed.
28 * put_line() - Put a whole string for a line.
32 * Include necessary headers.
35 #include <cups/string.h>
37 #include <cups/file.h>
44 void put_entity(cups_file_t
*fp
, int ch
);
45 void put_line(cups_file_t
*fp
, const char *line
);
49 * 'main()' - Convert a man page to HTML.
52 int /* O - Exit status */
53 main(int argc
, /* I - Number of command-line args */
54 char *argv
[]) /* I - Command-line arguments */
56 cups_file_t
*infile
, /* Input file */
57 *outfile
; /* Output file */
58 char line
[1024], /* Line from file */
59 *lineptr
, /* Pointer into line */
60 name
[1024], /* Heading anchor name */
61 *nameptr
; /* Pointer into anchor name */
63 inheading
, /* Inside a heading? */
64 inpre
, /* Inside preformatted text? */
65 intoc
, /* Inside table-of-contents? */
66 toclevel
, /* Current table-of-contents level */
67 linenum
; /* Current line on page */
76 fputs("Usage: rfctohtml [rfcNNNN.txt [rfcNNNN.html]]\n", stderr
);
81 * Open files as needed...
86 if ((infile
= cupsFileOpen(argv
[1], "r")) == NULL
)
93 infile
= cupsFileOpenFd(0, "r");
97 if ((outfile
= cupsFileOpen(argv
[2], "w")) == NULL
)
100 cupsFileClose(infile
);
105 outfile
= cupsFileOpenFd(1, "w");
108 * Read from input and write the output...
111 cupsFilePuts(outfile
,
112 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" "
113 "\"http://www.w3.org/TR/REC-html40/loose.dtd\">\n"
115 "<!-- SECTION: Specifications -->\n"
117 "\t<style type='text/css'><!--\n"
118 "\th1, h2, h3 { font-family: sans-serif; }\n"
119 "\tp, pre { font-family: monospace; }\n"
120 "\th2.title, h3.title, h3.title { border-bottom: solid "
125 * Skip the initial header stuff (working group ID, RFC #, authors, and
132 while (cupsFileGets(infile
, line
, sizeof(line
)))
140 while (cupsFileGets(infile
, line
, sizeof(line
)))
146 else if (!strncasecmp(line
, "Request for Comments:", 21))
147 rfc
= atoi(line
+ 21);
151 * Read the document title...
154 while (cupsFileGets(infile
, line
, sizeof(line
)))
162 for (lineptr
= line
; isspace(*lineptr
& 255); lineptr
++);
164 cupsFilePrintf(outfile
, "<title>RFC %d: %s", rfc
, lineptr
);
166 while (cupsFileGets(infile
, line
, sizeof(line
)))
174 for (lineptr
= line
; isspace(*lineptr
& 255); lineptr
++);
175 cupsFilePrintf(outfile
, " %s", lineptr
);
179 cupsFilePuts(outfile
, "</title>\n"
184 * Read the rest of the file...
192 while (cupsFileGets(infile
, line
, sizeof(line
)))
203 cupsFilePuts(outfile
, "</pre>\n");
210 cupsFilePuts(outfile
, "</h1>\n");
212 cupsFilePrintf(outfile
, "</a></h%d>\n", inheading
);
217 else if ((line
[0] == ' ' ||
218 (!isupper(line
[0] & 255) && !isdigit(line
[0] & 255) &&
219 !strstr(line
, "[Page "))) && !inheading
)
224 cupsFilePuts(outfile
, "</h1>\n");
226 cupsFilePrintf(outfile
, "</a></h%d>\n", inheading
);
231 for (lineptr
= line
; *lineptr
== ' '; lineptr
++);
235 char *temp
; /* Temporary pointer into line */
236 int level
; /* Heading level */
239 if (isdigit(*lineptr
& 255))
241 strlcpy(name
, lineptr
, sizeof(name
));
243 for (nameptr
= name
, level
= -1; *nameptr
;)
244 if (isdigit(*nameptr
& 255))
246 while (isdigit(*nameptr
& 255))
251 else if (*nameptr
== ' ')
259 while (toclevel
> level
)
261 cupsFilePuts(outfile
, "\n</ul>");
265 while (toclevel
< level
)
267 cupsFilePuts(outfile
, "\n<ul style=\"list-style-type: none;\">\n");
271 cupsFilePrintf(outfile
, "\n<%s><a href=\"#s%s\">", toclevel
? "li" : "p",
275 temp
= lineptr
+ strlen(lineptr
) - 1;
277 while (temp
> lineptr
)
278 if (*temp
== ' ' || !isdigit(*temp
& 255))
285 while (temp
> lineptr
)
286 if (*temp
!= ' ' && *temp
!= '.')
294 if (isdigit(*lineptr
& 255))
295 put_line(outfile
, lineptr
);
297 put_line(outfile
, lineptr
- 1);
300 cupsFilePuts(outfile
, "</a>");
304 cupsFilePuts(outfile
, "\n<pre>");
305 put_line(outfile
, line
);
310 cupsFilePutChar(outfile
, '\n');
311 put_line(outfile
, line
);
314 else if (strstr(line
, "[Page "))
317 * Skip page footer and header...
320 cupsFileGets(infile
, line
, sizeof(line
));
321 cupsFileGets(infile
, line
, sizeof(line
));
322 cupsFileGets(infile
, line
, sizeof(line
));
323 cupsFileGets(infile
, line
, sizeof(line
));
326 else if (isdigit(line
[0] & 255) && !inheading
)
328 int level
; /* Heading level */
335 cupsFilePuts(outfile
, "\n</ul>");
339 cupsFilePutChar(outfile
, '\n');
345 cupsFilePuts(outfile
, "</pre>\n");
349 strlcpy(name
, line
, sizeof(name
));
350 for (nameptr
= name
, level
= 1; *nameptr
;)
351 if (isdigit(*nameptr
& 255))
353 while (isdigit(*nameptr
& 255))
358 else if (*nameptr
== ' ')
366 cupsFilePrintf(outfile
, "\n<h%d class='title'><a name='s%s'>", level
,
368 put_line(outfile
, line
);
379 cupsFilePuts(outfile
, "\n</ul>");
383 cupsFilePutChar(outfile
, '\n');
389 cupsFilePuts(outfile
, "\n<h2 class='title'>");
393 put_line(outfile
, line
);
395 intoc
= !strcasecmp(line
, "Table of Contents");
401 cupsFilePuts(outfile
, "</pre>\n");
406 cupsFilePuts(outfile
, "</h2>\n");
408 cupsFilePrintf(outfile
, "</a></h%d>\n", inheading
);
411 cupsFilePuts(outfile
, "</body>\n"
418 cupsFileClose(infile
);
419 cupsFileClose(outfile
);
422 * Return with no errors...
430 * 'put_entity()' - Put a single character, using entities as needed.
434 put_entity(cups_file_t
*fp
, /* I - File */
435 int ch
) /* I - Character */
438 cupsFilePuts(fp
, "&");
440 cupsFilePuts(fp
, "<");
442 cupsFilePutChar(fp
, ch
);
447 * 'put_line()' - Put a whole string for a line.
451 put_line(cups_file_t
*fp
, /* I - File */
452 const char *s
) /* I - String */
454 int whitespace
, /* Saw whitespace */
456 len
; /* Length of keyword */
457 static const char * const keywords
[] =/* Special keywords to boldface */
474 if (whitespace
&& isupper(*s
& 255))
478 for (i
= 0; i
< (int)(sizeof(keywords
) / sizeof(sizeof(keywords
[0]))); i
++)
480 len
= strlen(keywords
[i
]);
481 if (!strncmp(s
, keywords
[i
], len
) && (isspace(s
[len
] & 255) || !*s
))
483 cupsFilePrintf(fp
, "<b>%s</b>", keywords
[i
]);
489 if (i
>= (int)(sizeof(keywords
) / sizeof(sizeof(keywords
[0]))))
490 put_entity(fp
, *s
++);
497 put_entity(fp
, *s
++);
504 * End of "$Id: rfctohtml.c 5143 2006-02-21 19:13:01Z mike $".