]>
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-2009 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 title
[2048], /* Title string */
52 *titleptr
, /* Pointer into title */
53 name
[1024], /* Heading anchor name */
54 *nameptr
; /* Pointer into anchor name */
56 inheading
, /* Inside a heading? */
57 inpre
, /* Inside preformatted text? */
58 intoc
, /* Inside table-of-contents? */
59 toclevel
, /* Current table-of-contents level */
60 linenum
; /* Current line on page */
69 fputs("Usage: rfctohtml [rfcNNNN.txt [rfcNNNN.html]]\n", stderr
);
74 * Open files as needed...
79 if ((infile
= cupsFileOpen(argv
[1], "r")) == NULL
)
86 infile
= cupsFileOpenFd(0, "r");
90 if ((outfile
= cupsFileOpen(argv
[2], "w")) == NULL
)
93 cupsFileClose(infile
);
98 outfile
= cupsFileOpenFd(1, "w");
101 * Read from input and write the output...
104 cupsFilePuts(outfile
,
105 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" "
106 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
108 "<!-- SECTION: Specifications -->\n"
110 "\t<link rel=\"stylesheet\" type=\"text/css\" "
111 "href=\"../cups-printable.css\">\n");
114 * Skip the initial header stuff (working group ID, RFC #, authors, and
121 while (cupsFileGets(infile
, line
, sizeof(line
)))
129 while (cupsFileGets(infile
, line
, sizeof(line
)))
135 else if (!strncasecmp(line
, "Request for Comments:", 21))
136 rfc
= atoi(line
+ 21);
140 * Read the document title...
143 while (cupsFileGets(infile
, line
, sizeof(line
)))
151 for (lineptr
= line
; isspace(*lineptr
& 255); lineptr
++);
153 snprintf(title
, sizeof(title
), "RFC %d: %s", rfc
, lineptr
);
154 titleptr
= title
+ strlen(title
);
156 while (cupsFileGets(infile
, line
, sizeof(line
)))
164 for (lineptr
= line
; isspace(*lineptr
& 255); lineptr
++);
166 snprintf(titleptr
, sizeof(title
) - (titleptr
- title
), " %s", lineptr
);
167 titleptr
+= strlen(titleptr
);
171 cupsFilePrintf(outfile
, "\t<title>%s</title>\n"
174 "<h1 class='title'>%s</h1>\n", title
, title
);
177 * Read the rest of the file...
185 while (cupsFileGets(infile
, line
, sizeof(line
)))
196 cupsFilePuts(outfile
, "</pre>\n");
203 cupsFilePuts(outfile
, "</h2>\n");
205 cupsFilePrintf(outfile
, "</a></h%d>\n", inheading
);
210 else if ((line
[0] == ' ' ||
211 (!isupper(line
[0] & 255) && !isdigit(line
[0] & 255) &&
212 !strstr(line
, "[Page "))) && !inheading
)
217 cupsFilePuts(outfile
, "</h2>\n");
219 cupsFilePrintf(outfile
, "</a></h%d>\n", inheading
);
224 for (lineptr
= line
; *lineptr
== ' '; lineptr
++);
228 char *temp
; /* Temporary pointer into line */
229 int level
; /* Heading level */
232 if (isdigit(*lineptr
& 255))
234 strlcpy(name
, lineptr
, sizeof(name
));
236 for (nameptr
= name
, level
= -1; *nameptr
;)
237 if (isdigit(*nameptr
& 255))
239 while (isdigit(*nameptr
& 255))
244 else if (*nameptr
== ' ')
252 while (toclevel
> level
)
254 cupsFilePuts(outfile
, "\n</ul>");
258 while (toclevel
< level
)
260 cupsFilePuts(outfile
, "\n<ul style=\"list-style-type: none;\">\n");
264 cupsFilePrintf(outfile
, "\n<%s><a href=\"#s%s\">", toclevel
? "li" : "p",
268 temp
= lineptr
+ strlen(lineptr
) - 1;
270 while (temp
> lineptr
)
271 if (*temp
== ' ' || !isdigit(*temp
& 255))
278 while (temp
> lineptr
)
279 if (*temp
!= ' ' && *temp
!= '.')
287 if (isdigit(*lineptr
& 255))
288 put_line(outfile
, lineptr
);
290 put_line(outfile
, lineptr
- 1);
293 cupsFilePuts(outfile
, "</a>");
297 cupsFilePuts(outfile
, "\n<pre>");
298 put_line(outfile
, line
);
303 cupsFilePutChar(outfile
, '\n');
304 put_line(outfile
, line
);
307 else if (strstr(line
, "[Page "))
310 * Skip page footer and header...
313 cupsFileGets(infile
, line
, sizeof(line
));
314 cupsFileGets(infile
, line
, sizeof(line
));
315 cupsFileGets(infile
, line
, sizeof(line
));
316 cupsFileGets(infile
, line
, sizeof(line
));
319 else if (isdigit(line
[0] & 255) && !inheading
)
321 int level
; /* Heading level */
328 cupsFilePuts(outfile
, "\n</ul>");
332 cupsFilePutChar(outfile
, '\n');
338 cupsFilePuts(outfile
, "</pre>\n");
342 strlcpy(name
, line
, sizeof(name
));
343 for (nameptr
= name
, level
= 1; *nameptr
;)
344 if (isdigit(*nameptr
& 255))
346 while (isdigit(*nameptr
& 255))
351 else if (*nameptr
== ' ')
359 cupsFilePrintf(outfile
, "\n<h%d class='title'><a name='s%s'>", level
,
361 put_line(outfile
, line
);
372 cupsFilePuts(outfile
, "\n</ul>");
376 cupsFilePutChar(outfile
, '\n');
382 cupsFilePuts(outfile
, "\n<h2 class='title'>");
386 put_line(outfile
, line
);
388 intoc
= !strcasecmp(line
, "Table of Contents");
394 cupsFilePuts(outfile
, "</pre>\n");
399 cupsFilePuts(outfile
, "</h2>\n");
401 cupsFilePrintf(outfile
, "</a></h%d>\n", inheading
);
404 cupsFilePuts(outfile
, "</body>\n"
411 cupsFileClose(infile
);
412 cupsFileClose(outfile
);
415 * Return with no errors...
423 * 'put_entity()' - Put a single character, using entities as needed.
427 put_entity(cups_file_t
*fp
, /* I - File */
428 int ch
) /* I - Character */
431 cupsFilePuts(fp
, "&");
433 cupsFilePuts(fp
, "<");
435 cupsFilePutChar(fp
, ch
);
440 * 'put_line()' - Put a whole string for a line.
444 put_line(cups_file_t
*fp
, /* I - File */
445 const char *s
) /* I - String */
447 int whitespace
, /* Saw whitespace */
449 len
; /* Length of keyword */
450 static const char * const keywords
[] =/* Special keywords to boldface */
472 if (whitespace
&& isupper(*s
& 255))
476 for (i
= 0; i
< (int)(sizeof(keywords
) / sizeof(sizeof(keywords
[0]))); i
++)
478 len
= strlen(keywords
[i
]);
479 if (!strncmp(s
, keywords
[i
], len
) &&
480 (isspace(s
[len
] & 255) || ispunct(s
[len
] & 255) || !*s
))
482 cupsFilePrintf(fp
, "<b>%s</b>", keywords
[i
]);
488 if (i
>= (int)(sizeof(keywords
) / sizeof(sizeof(keywords
[0]))))
489 put_entity(fp
, *s
++);
496 put_entity(fp
, *s
++);
503 * End of "$Id: rfctohtml.c 6649 2007-07-11 21:46:42Z mike $".