} else if (c == '.' && *lp == '.' && lp[1] == '.') {

fputs("\\ldots", out);

if (lp[2] == ' ') {

fputs(" ", out);

} else {

fputs("\\ ", out);

}

lp += 2;

/* ASCII characters requiring verbatim handling */

} else if (c == '~') {

fputs("\\verb+", out);

fputc(c, out);

fputc('+', out);

} else if (!inmath && c == '^') {

fputs("\\verb+", out);

fputc(c, out);

fputc('+', out);

/* ASCII characters requiring math-mode quoting. */

} else if (!inmath && strchr("|<>", c) != NULL) {

fputc('$', out);

fputc(c, out);

fputc('$', out);

/* Try to convert dumb ASCII quotes to open and close quotes. */

} else if (!inmath && c == '"') {

fputs(quoth ? "''" : "``", out);

quoth = !quoth;

/* Normal character */

} else {

if (nonbreak && isspace(c) && *lp != 0 &&

strchr(PUNCTUATION, *lp) != NULL) {

putc('~', out);

} else {

/* Characters requiring protection against interpretation

as control sequences. */

if (strchr("$&%#", c) != NULL) {

putc('\\', out);

}

if (!inmath && strchr("{}", c) != NULL) {

putc('\\', out);

}

putc(c, out);

}

}

}

/* Note that other characters, specifically those in the

range from 127 through 160, get dropped. */

}

#undef out

}

}

/* ENDQUOTE -- Terminate a block quotation when a non-conforming

line is encountered. */

static void endquote(out)

FILE *out;

{

if (html) {

if (saith) {

fputs("\n</blockquote>\n", out);

saith = 0;

}

} else {

if (saith) {

fputs("\n\\end{quote}\n", out);

saith = 0;

}

}

}

/* NAVAIDS -- Create navigation panel buttons. */

static void navaids(basename, chapno, last, out)

char *basename;

int chapno, last;

FILE *out;

{

char s[256];

/* Previous chapter button. */

if (chapno > 1) {

sprintf(s, "<A HREF=\"%s_chap%d.html\">", basename, chapno - 1);

fputs(s, out);

fputs("<IMG ALIGN=MIDDLE SRC=\"prev.gif\" HEIGHT=32 WIDTH=32 ALT=\"Previous\"></A>\n", out);

} else {

fputs("<IMG ALIGN=MIDDLE SRC=\"prev_gr.gif\" HEIGHT=32 WIDTH=32 ALT=\"\">\n", out);

}

/* Up to table of contents button. */

sprintf(s, "<A HREF=\"%s.html\"> <IMG ALIGN=MIDDLE SRC=\"up.gif\" HEIGHT=32 WIDTH=32 ALT=\"Contents\"></A>\n",

basename);

fputs(s, out);

/* Next chapter button. */

if (!last) {

sprintf(s, "<A HREF=\"%s_chap%d.html\">", basename, chapno + 1);

fputs(s, out);

}

sprintf(s, "<IMG ALIGN=MIDDLE SRC=\"next%s.gif\" HEIGHT=32 WIDTH=32 ALT=\"%s\">",

last ? "_gr" : "", last ? "" : "Next");

fputs(s, out);

if (!last) {

fputs("</A>\n", out);

} else {

fputc('\n', out);

}

}

/* ENDCHAP -- Standard end of chapter processing. */

static void endchap(basename, chapno, last, out)

char *basename;

int chapno, last;

FILE *out;

{

fputs("<HR>\n", out);

navaids(basename, chapno, last, out);

fputs("</BODY>\n", out);

fputs("</HTML>\n", out);

fclose(out);

}

/* Main program */

int main(argc, argv)

int argc; char *argv[];

{

int i, f = 0, peeked = 0, titling = 1,

chapno = 0, ctl, ttl = 0, atl = 0;

char *cp, opt;

FILE *in = stdin, *out = stdout, *chap = NULL;

unsigned char line[256], peek[256], basename[256], sbuf[256];

unsigned char titre[10][256], auteur[10][256], ctitle[10][256];

#define tstream ((chap == NULL) ? out : chap)

#define ostream ((chap == NULL) ? out : (infoot ? foot : chap))

for (i = 1; i < argc; i++) {

cp = argv[i];

if (*cp == '-') {

opt = *(++cp);

if (islower(opt))

opt = toupper(opt);

switch (opt) {

case 'F':

nonbreak = 1;

break;

case 'H':

if (f > 0) {

fprintf(stderr, "Must specify -h before any file name.\n");

return 2;

}

html = 1;

break;

case '?':

case 'U':

fprintf(stderr,"\nETSET -- Typeset ISO 8859 Latin-1 Etext. Call");

fprintf(stderr,

"\n with etset [input [output]]");

fprintf(stderr,"\n");

fprintf(stderr,"\n Options:");

fprintf(stderr,"\n -f Use nonbreaking spaces for French punctuation");

fprintf(stderr,"\n -h Generate HTML");

fprintf(stderr,"\n");

fprintf(stderr,"\n");

return 0;

}

} else {

switch (f) {

case 0:

if ((in = fopen(cp, "r")) == NULL) {

fprintf(stderr, "Cannot open input file %s\n", cp);

return 2;

}

f++;

break;

case 1:

if (html) {

strcpy(basename, cp);

sprintf(line, "%s/%s.html", basename, basename);

if ((out = fopen(line, "w")) == NULL) {

fprintf(stderr, "Cannot open output file %s\n", line);

return 2;

}

} else {

if ((out = fopen(cp, "w")) == NULL) {

fprintf(stderr, "Cannot open output file %s\n", cp);

return 2;

}

}

f++;

break;

default:

fprintf(stderr, "Too many file names specified.\n");

}

}

}

if (html && f < 2) {

fprintf(stderr, "Must specify input and output file names for HTML.\n");

}

/* Output document header. */

if (html) {

outgif(basename, "foot", d_foot, sizeof d_foot);

outgif(basename, "next", d_next, sizeof d_next);

outgif(basename, "prev", d_prev, sizeof d_prev);

outgif(basename, "up", d_up, sizeof d_up);

outgif(basename, "next_gr", d_next_gr, sizeof d_next_gr);

outgif(basename, "prev_gr", d_prev_gr, sizeof d_prev_gr);

fprintf(out, "<!-- ETSET version %s -- %s. -->\n",

VERSION, REVDATE);

fputs("<HTML>\n", out);

fputs("<HEAD>\n", out);

fputs("<TITLE>\n", out);

} else {

fprintf(out, "%%\n%% ETSET version %s -- %s.\n%%\n",

VERSION, REVDATE);

fputs("\\documentclass{report}\n", out);

fputs("\\date{}\n\\title{\n", out);

}

/* Line level file processing. */

for (;;) {

unsigned char *lp = line;

char *eline = NULL;

if (peeked) {

memcpy(line, peek, sizeof line);

peeked = 0;

} else {

if (fgets((char *) line, (sizeof line) - 1, in) == NULL) {

break;

}

trim(line);

}

/* Reset quote parity on paragraph breaks. */

if (line[0] == '\n' && (!html || textstate == 1)) {

quoth = inpara = 0;

if (html) {

fputs("<P>\n", ostream);

}

}

/* Check for section separator. */

if (strcmp(line, SectionSep) == 0) {

textstate++;

if (textstate == 1) {

continue; /* Ditch section separator */

} else if (textstate > 1) {

break;

}

}

if (textstate < 1) {

continue; /* Ignore prologue text */

}

/* Check for title separator. */

if (titling) {

int cheddar = 1, chonk = 0;

unsigned char *pp = lp;

/* Check for title separator */

while (*pp != EOS) {

if (!(isspace(*pp) || (*pp == '='))) {

cheddar = 0;

break;

}

if (*pp++ == '=') {

chonk++;

}

}

if (cheddar && chonk > 2) {

titling = 0;

author = 1;

if (html) {

fputs("</TITLE>\n<ADDRESS>\n", out);

} else {

fputs("}\n\\author{\n", out);

}

continue;

}

}

/* Heuristic justification for lines which begin with

white space. */

if (line[0] == ' ') {

int n = strlen(line) - 2, w;

unsigned char *ap = line + 1;

while (isspace(*ap)) {

ap++;

n--;

}

line[strlen(line) - 1] = EOS;

if (abs((w = ((FormatWidth - n) / 2) - (ap - lp))) < 3) {

/* It's centered: check for chapter separators. */

if (fgets((char *) peek, (sizeof peek) - 1, in) != NULL) {

unsigned char *pp = peek;

int cheddar = 1, chonk = 0;

peeked = 1;

trim(peek);

while (*pp != EOS) {

if (!(isspace(*pp) || (*pp == '-'))) {

cheddar = 0;

break;

}

if (*pp++ == '-') {

chonk++;

}

}

if (cheddar && chonk > 2) {

chapout = 0;

peeked = 0;

if (author) {

author = 0;

if (html) {

int n;

fputs("</ADDRESS>\n</HEAD>\n<BODY>\n", out);

if (ttl > 0) {

fputs("<H1>", out);

for (n = 0; n < ttl; n++) {

protext(titre[n], out, basename);

fputc('\n', out);

}

fputs("</H1>\n", out);

}

if (atl > 0) {

fputs("<H2>", out);

for (n = 0; n < atl; n++) {

protext(auteur[n], out, basename);

fputc('\n', out);

}

fputs("</H2>\n", out);

}

fputs("<UL>\n", out);

} else {

fputs("}\n\\begin{document}\n\\maketitle\n", out);

if (nonbreak) {

fputs("\\frenchspacing\n", out);

}

}

}

while (fgets((char *) peek, (sizeof peek) - 1, in) != NULL) {

trim(peek);

pp = peek;

peek[strlen(peek) - 1] = EOS;

while (*pp && isspace(*pp)) {

pp++;

}

if (*pp == EOS) {

break;

}

if (!chapout) {

if (html) {

if (chap != NULL) {

endchap(basename, chapno, 0, chap);

chap = NULL;

}

firstchap = 1;

chapout = 1;

ctl = 0;

sprintf(line, "%s/%s_chap%d.html",

basename, basename, ++chapno);

chap = fopen(line, "w");

fputs("<HTML>\n<HEAD>\n<TITLE>", ostream);

} else {

if (!firstchap) {

fputs("\\tableofcontents\n", out);

}

fputs("\\chapter{\n", out);

firstchap = 1;

chapout = 1;

}

}

if (html) {

strcpy(ctitle[ctl++], pp);

protext(pp, ostream, basename);

fputc('\n', ostream);

} else {

protext(pp, out, NULL);

fputc('\n', out);

}

}

if (firstchap && !chapout) {

/* Untitled chapter after one or more titled ones */

if (html) {

fputs("<HEAD>\n<TITLE> ", ostream);

} else {

fputs("\\chapter{", out);

}

chapout = 1;

}

if (chapout) {

if (html) {

int i;

fputs("</TITLE>\n</HEAD>\n<BODY>\n", ostream);

navaids(basename, chapno, 0, ostream);

sprintf(sbuf, "<HR>\n<H1><A NAME=Chapter%d>",

chapno);

fputs(sbuf, ostream);

for (i = 0; i < ctl; i++) {

protext(ctitle[i], ostream, basename);

fputc('\n', ostream);

}

fputs("</A></H1><P>\n", ostream);

/* Now make link to chapter from table of

contents. */

sprintf(sbuf, "<LI> <A HREF=\"%s_chap%d.html\">",

basename, chapno);

fputs(sbuf, out);

for (i = 0; i < ctl; i++) {

protext(ctitle[i], out, basename);

fputc('\n', out);

}

fputs("</A></LI>\n", out);

} else {

fputs("}\n", out);

}

}

continue;

}

}

if (!titling && !author) {

if (html) {

endquote(ostream);

fputs("\n<P ALIGN=CENTER>\n", ostream);

eline = "</P>\n";

} else {

endquote(out);

fputs("\n{\\centering ", out);

eline = "\\\\}\n";

}

}

} else {

int n;

/* Check for a block quote. */

for (n = 1; n < QuoteIndent; n++) {

if (line[n] != ' ') {

n = -1;

break;

}

}

if (n > 1 && !isspace(line[QuoteIndent])) {

if (!saith) {

if (html) {

fputs("<BLOCKQUOTE>\n", ostream);

} else {

fputs("\\begin{quote}\n", out);

}

saith = 1;

}

/* Ragged left or ragged right: determine which and

output appropriate command. */

} else if (!titling && !author) {

if (html) {

endquote(ostream);

} else {

endquote(out);

fputs(w < 0 ? "{\\raggedleft" : "{\\raggedright", out);

eline = "\\\\ }\n\n";

}

}

}

lp = ap - 1;

} else {

if (html) {

endquote(ostream);

} else {

endquote(out);

}

}

if (titling || author) {

unsigned char *ap = line;

int nonbs = 0;

while (*ap != EOS) {

if (!isspace(*ap)) {

nonbs = 1;

break;

}

ap++;

}

if (!nonbs) {

continue;

}

}

if (html) {

if (titling) {

strcpy(titre[ttl++], lp);

}

if (author) {

strcpy(auteur[atl++], lp);

continue;

}

protext(lp, tstream, basename);

if (eline) {

fputs(eline, tstream);

}

} else {

protext(lp, out, NULL);

if (titling || author) {

fputs("\\\\\n", out);

}

if (eline) {

fputs(eline, out);

}

}

}

/* Output document trailer. */

if (html) {

fputs("</UL>\n", out);

fputs("</BODY>\n", out);

fputs("</HTML>\n", out);

if (chap != NULL) {

endchap(basename, chapno, 1, chap);

}

if (foot != NULL) {

fputs("</BODY>\n", foot);

fputs("</HTML>\n", foot);

fclose(foot);

}

} else {

fputs("\\end{document}\n", out);

}

fclose(out);

return 0;

}

Le tour du monde en quatre-vingts jours(#3456) · 第 19 / 19 段