const char * UsageLines [] = { "Usage: doctohtml (article number) [(article link URLs) ]", "Takes text from standard input and prints simple HTML using", "H2, P, UL, LI, and BR, to standard output.", "", "Input text is treated as a series of articles. Articles are", "separated from each other by three consecutive blank lines.", "Only the specified article will be printed in full. The", "other articles will only print links. A URL should be", "specified for each article except the one being printed.", "Use 1 for the first article, or the only article.", "Input processing stops at end, or at a sequence of four", "consecutive blank lines, whichever comes first.", "Specifying an article number of 0 will print only links.", "The first input line in each article is considered to be", "the article title, and is used in links from the other", "articles.", "", "Input text should not contain any soft line breaks. Any input", "line break within a paragraph will be written as BR. A paragraph", "is separated from the previous paragraph by a blank line.", "A heading (H2) is preceded by two blank lines.", "", "June 2, 2011. Newest is at gopher -p users/julianbr sdf.org", }; const int NumUsageLines = sizeof (UsageLines)/sizeof (UsageLines [0] ); const struct { const char * entity; const unsigned char character; } Entities [] = { {"<", '<'}, {">", '>'}, {"&", '&'}, {" ", 160}, {"¡", 161}, {"¢", 162}, {"£", 163}, {"¤", 164}, {"¥", 165}, {"¦", 166}, {"§", 167}, {"¨", 168}, {"©", 169}, {"ª", 170}, {"«", 171}, {"¬", 172}, {"­", 173}, {"®", 174}, {"¯", 175}, {"°", 176}, {"±", 177}, {"²", 178}, {"³", 179}, {"´", 180}, {"µ", 181}, {"¶", 182}, {"·", 183}, {"¸", 184}, {"&supl;", 185}, {"º", 186}, {"»", 187}, {"¼", 188}, {"½", 189}, {"¾", 190}, {"¿", 191}, {"À", 192}, {"Á", 193}, {"Â", 194}, {"Ã", 195}, {"Ä", 196}, {"Å", 197}, {"Æ", 198}, {"Ç", 199}, {"È", 200}, {"É", 201}, {"Ê", 202}, {"Ë", 203}, {"Ì", 204}, {"Í", 205}, {"Î", 206}, {"Ï", 207}, {"Ð", 208}, {"Ñ", 209}, {"Ò", 210}, {"Ó", 211}, {"Ô", 212}, {"Õ", 213}, {"Ö", 214}, {"×", 215}, {"Ø", 216}, {"Ù", 217}, {"Ú", 218}, {"Û", 219}, {"Ü", 220}, {"Ý", 221}, {"Þ", 222}, {"ß", 223}, {"à", 224}, {"á", 225}, {"â", 226}, {"ã", 227}, {"ä", 228}, {"å", 229}, {"æ", 230}, {"ç", 231}, {"è", 232}, {"é", 233}, {"ê", 234}, {"ë", 235}, {"ì", 236}, {"í", 237}, {"î", 238}, {"ï", 239}, {"ð", 240}, {"ñ", 241}, {"ò", 242}, {"ó", 243}, {"ô", 244}, {"õ", 245}, {"ö", 246}, {"÷", 247}, {"ø", 248}, {"ù", 249}, {"ú", 250}, {"û", 251}, {"ü", 252}, {"ý", 253}, {"þ", 254}, {"ÿ", 255}, }; const int NumEntities = sizeof (Entities)/sizeof (Entities [0] ); /* const int NumEntities = 0 */ /* To NOT replace characters with entities, uncomment above line */ /* and comment the line above that. */ #include <stdio.h> void WriteEntity (unsigned char c) { int i; i = 0; while (i < NumEntities && Entities [i].character != c) i++; if (i < NumEntities) fputs (Entities [i].entity, stdout); else putchar (c); } void Write (int ArticleNum, char * * Urls, int NumUrls) { int UrlNum, NumArticles; char c; printf ("<HTML>\n"); printf ("\t<HEAD>\n"); printf ("\t\t<STYLE><!--\n"); printf ("\t\t\tBODY {\n"); printf ("\t\t\t\tbackground-color: rgb(90%%,90%%,95%%);\n"); printf ("\t\t\t\tborder: lightblue solid 15px;\n"); printf ("\t\t\t\tmargin: 0;\n"); printf ("\t\t\t\tpadding-top: 1%%;\n"); printf ("\t\t\t\tpadding-left: 1%%;\n"); printf ("\t\t\t\tpadding-right: 5%%;\n"); printf ("\t\t\t\tpadding-bottom: 10%%;\n"); printf ("\t\t\t\t}\n"); printf ("\t\t\t--></STYLE>\n"); printf ("\t\t</HEAD>\n"); printf ("\t<BODY>\n"); printf ("\t\t<UL>\n"); NumArticles = 0; UrlNum = 0; c = getchar (); while (c != EOF && c != '\n') { NumArticles++; if (NumArticles == ArticleNum) { /* Title not part of printed article */ while (c != EOF && c != '\n') c = getchar (); } else { if (UrlNum < NumUrls) { printf ("\t\t\t<LI><A HREF"); printf ("=\"%s\">", Urls [UrlNum] ); while (c != EOF && c != '\n') { WriteEntity (c); c = getchar (); } printf ("</A></LI>\n"); } else { fprintf (stderr, "***doctohtml: Need URL"); fprintf (stderr, " for article \""); while (c != EOF && c != '\n') { fputc (c, stderr); c = getchar (); } fprintf (stderr, "\".\n"); } UrlNum++; } if (NumArticles == ArticleNum) printf ("\t\t\t</UL>\n"); /* Blank line optional before first paragraph */ if (c == '\n') c = getchar (); /* Paragraphs */ while (c != EOF && c != '\n') { if (NumArticles == ArticleNum) printf ("\t\t<P>"); while (c != EOF && c != '\n') { while (c != EOF && c != '\n') { if (NumArticles == ArticleNum) WriteEntity (c); c = getchar (); } if (c != EOF) c = getchar (); if (c != EOF && c != '\n') { if (NumArticles == ArticleNum) printf ("<BR>"); } } if (NumArticles == ArticleNum) printf ("</P>\n"); if (c != EOF) c = getchar (); } if (c != EOF) c = getchar (); while (c != EOF && c != '\n') { if (NumArticles == ArticleNum) printf ("\t\t<H2>"); while (c != EOF && c != '\n') { if (NumArticles == ArticleNum) WriteEntity (c); c = getchar (); } if (NumArticles == ArticleNum) printf ("</H2>\n"); if (c != EOF) c = getchar (); /* Blank line optional before first paragraph */ if (c == '\n') c = getchar (); /* Paragraphs */ while (c != EOF && c != '\n') { if (NumArticles == ArticleNum) printf ("\t\t<P>"); while (c != EOF && c != '\n') { while (c != EOF && c != '\n') { if (NumArticles == ArticleNum) WriteEntity (c); c = getchar (); } if (c != EOF) c = getchar (); if (c != EOF && c != '\n') { if (NumArticles == ArticleNum) printf ("<BR>"); } } if (NumArticles == ArticleNum) printf ("</P>\n"); if (c != EOF) c = getchar (); } if (c != EOF) c = getchar (); } if (NumArticles == ArticleNum) printf ("\t\t<UL>\n"); if (c != EOF) c = getchar (); } printf ("\t\t\t</UL>\n"); printf ("\t\t</BODY>\n"); printf ("\t</HTML>\n"); if (NumArticles < ArticleNum) { fprintf (stderr, "***doctohtml: Never found article"); fprintf (stderr, " #%d.\n", ArticleNum); fprintf (stderr, "***doctohtml: Only found"); fprintf (stderr, " %d articles.\n", NumArticles); } while (UrlNum < NumUrls) { fprintf (stderr, "***doctohtml: Never found article"); fprintf (stderr, " for URL \"%s\".\n", Urls [UrlNum] ); UrlNum++; } } int main (int argc, char * argv [] ) { int i, ArticleNum; char c; if (argc < 2) { for (i = 0; i < NumUsageLines; i++) printf ("%s\n", UsageLines [i] ); } else if (argc > 1) { if (sscanf (argv [1], "%d%c", & ArticleNum, & c) != 1) { fprintf (stderr, "***doctohtml: Expecting"); fprintf (stderr, " article number, found"); fprintf (stderr, " \"%s\".\n", argv [1] ); } else Write (ArticleNum, argv + 2, argc - 2); } return 0; }