home *** CD-ROM | disk | FTP | other *** search
- /*
- * doc2imp.c -- program to convert Gnuplot .DOC format to impression
- * textstory (Document Description) format.
- *
- * usage: doc2imp file.doc textstory
- * or: doc2imp < file.doc > textstory
- *
- * Bugs fixed & Output improved : 24/6/93 by Paul Field
- * Namely :
- * Added 'VERBOSE' so that doc2imp with file indirection works OK.
- * No more spurious spaces at the beginning of paragraphs.
- * { is now encoded properly so that bits of text don't disappear.
- * ` characters are converted to ' which Impression can use for 'smart quotes'.
- * Tables now line up properly (but unfortunately don't use TABs).
- * Examples and syntax are now in a 'verbatim' style.
- * N.B. The definition of 'basestyle' output appears to be ignored by
- * Impression (V2.17).
- *
- * Bugs fixed & improvment: 08 Jul 1993 Erik van de Pol
- * Namely:
- * Paul got the command line parsing wrong :- misplaced else (hi Paul!).
- * Improved chapter numbering.
- */
-
- #include <stdio.h>
- #include <ctype.h>
-
- #define MAX_LINE_LEN 256
- #define BOOL int
- #define TRUE 1
- #define FALSE 0
- #define VERBOSE TRUE
-
- void ddf_newstyle(FILE * outfile, const char *name, const char *fontname,
- unsigned size, int spaceabove);
- void process_line(char *line, FILE * f);
- void convert(FILE * a, FILE * f);
- void section(char *line, FILE * f);
- void verbatimline(char *line, FILE * f);
- void outputline(char *line, FILE * f);
- void outputchar(char c, FILE * f);
- void troffline(char *line, FILE * f);
-
-
- static int chapter[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
-
-
- int main(int argc, char *argv[])
- {
- FILE *infile;
- FILE *outfile;
-
- infile = stdin;
- outfile = stdout;
- if (argc > 3 || argc < 2) {
- fprintf(stderr, "Usage: %s infile outfile\n", argv[0]);
- return 1;
- }
- if (argc >= 2) {
- if ((infile = fopen(argv[1], "r")) == NULL) {
- fprintf(stderr, "%s: Can't open %s for reading\n", argv[0], argv[1]);
- return 1;
- }
- if (argc == 3) {
- if ((outfile = fopen(argv[2], "w")) == NULL) {
- fprintf(stderr, "%s: Can't open %s for writing\n", argv[0], argv[2]);
- return 1;
- }
- }
- #if VERBOSE
- else {
- fprintf(stderr, "%s: Can't use stdout for output - recompile without VERBOSE\n", argv[0]);
- return 1;
- }
- #endif
- }
-
- /* init impression */
- fprintf(outfile, "{define style \"BaseStyle\"; font Trinity.Medium; fontsize 12pt; spaceabove 10pt; spacebelow 0pt; leftmargin 1.8pt; rightmargin 0pt; returnmargin 1.8pt; condframethrow 0pt; rulewidth 0pt; ruleoffset 0pt; fontaspect 100%%; fontcolour rgb=(0.000,0.000,0.000); linecolour rgb=(0.000,0.000,0.000); rulecolour rgb=(0.000,0.000,0.000); justify left; underline 0; strikeout off; script off; hyphenate off; leader \"\"; locktolinespace off; tabs 72pt,144pt,216pt,288pt,360pt,432pt,504pt,576pt,648pt}");
-
- ddf_newstyle(outfile, "Item1", "Homerton.Bold", 32, 10);
- ddf_newstyle(outfile, "Item2", "Homerton.Bold", 18, 10);
- ddf_newstyle(outfile, "Item3", "Homerton.Medium", 18, 10);
- ddf_newstyle(outfile, "Item4", "Homerton.Medium", 14, 10);
- ddf_newstyle(outfile, "Item5", "Homerton.Medium", 12, 10);
- ddf_newstyle(outfile, "Table", "Homerton.Medium", 10, 0);
- ddf_newstyle(outfile, "Table header", "Homerton.Bold", 10, 0);
- ddf_newstyle(outfile, "Verbatim", "Corpus.Medium", 10, 0);
- convert(infile, outfile);
- return 0;
- }
-
-
- void ddf_newstyle(FILE * outfile, const char *name, const char *fontname,
- unsigned size, int spaceabove)
- {
- fprintf(outfile, "{define style \"%s\"; font %s; fontsize %dpt; spaceabove %dpt}",
- name, fontname, size, spaceabove);
- }
-
-
- void convert(FILE * a, FILE * b)
- {
- static char line[MAX_LINE_LEN];
-
- while (fgets(line, MAX_LINE_LEN, a)) {
- process_line(line, b);
- }
- }
-
-
- void tidy(char *line)
- {
- int pos;
-
- for (pos = 0; line[pos] != NULL; pos++) {
- if (line[pos] < ' ') {
- line[pos] = ' ';
- }
- }
- for (pos--; line[pos] == ' ' && pos > 0; pos--) {
- line[pos] = NULL;
- }
- }
-
-
- void process_line(char *line, FILE * f)
- {
- static int line_count = 0;
- static BOOL table_mode = 0;
- static BOOL plain_text = 0;
-
- line_count++;
- tidy(line);
-
- /*
- * I think this checks for an 'end of paragraph', I have no idea what 'plain_text' stands for but it
- * seems to signal when some text is output (so you don't output more than 1 newline in a row).
- */
- if (plain_text && (line[0] != ' ' || line[1] == ' ' || line[1] == NULL)) {
- plain_text = FALSE;
- fputc('\n', f);
- }
- /* Deal with the control character */
- switch (line[0]) {
- case '?':
- break; /* ignore */
-
- case '@': /* start/end table */
- {
- if (!table_mode) {
- table_mode = TRUE;
- fprintf(f, "{\"Table\" on}");
- } else {
- table_mode = FALSE;
- fprintf(f, "{\"Table\" off}\n");
- }
- break;
- }
-
- case '#':
- break; /* latex table entry - ignore */
- case '%':
- if (line[1] != NULL) {
- troffline(line + 1, f);
- }
- break; /* troff table entry - convert this to something useful */
- case '\n': /* empty text line */
- case ' ': /* normal text line */
- {
- if (table_mode) {
- break; /* inore this - we already use the troff table entry */
- } else {
- if (line[1] != NULL) {
- if (line[1] != ' ') {
- plain_text = TRUE;
- outputline(line + 1, f);
- } else {
- verbatimline(line + 1, f);
- }
- }
- }
- break;
- }
-
- default:
- {
- if (isdigit(line[0])) { /* start of section */
- if (!table_mode) { /* ignore while in table */
- section(line, f);
- }
- } else {
- fprintf(stderr, "unknown control code '%c' in column 1, line %d\n",
- line[0], line_count);
- break;
- }
- }
- }
- }
-
-
- /* process a line with a digit control char */
- /* starts a new [sub]section */
-
- void section(char *line, FILE * f)
- {
- int sh_i;
- int lvl;
-
- (void) sscanf(line, "%d", &sh_i);
-
- #if VERBOSE
- printf("lv=%d ", sh_i);
- #endif
-
- chapter[sh_i] += 1;
- chapter[sh_i + 1] = 0;
-
- /* NOTE: section level 1 is used only for the document title */
-
- fprintf(f, "{\"Item%d\" on}", sh_i);
- fprintf(f, "%d", chapter[2]);
- #if VERBOSE
- printf("%d", chapter[2]);
- #endif
- for (lvl = 3; chapter[lvl] > 0; lvl++) {
- fprintf(f, ".%d", chapter[lvl]);
- #if VERBOSE
- printf(".%d", chapter[lvl]);
- #endif
- }
- #if VERBOSE
- printf(" %s\n", line + 2);
- #endif
- fprintf(f, " %s", line + 2);
- fprintf(f, "{\"Item%d\" off}\n", sh_i);
- }
-
-
- void verbatimline(char *line, FILE * f)
- {
- fputs("{\"Verbatim\" on}", f);
- outputline(line, f);
- fputc('\n', f);
- fputs("{\"Verbatim\" off}", f);
- }
-
-
- void outputline(char *line, FILE * f)
- {
- for (; *line != NULL; line++) {
- outputchar(*line, f);
- }
- fputc(' ', f);
- }
-
-
- void troffline(char *line, FILE * f)
- {
- for (; *line != NULL; line++) {
- if (*line == '@') {
- fputs("{tab}", f);
- } else {
- outputchar(*line, f);
- }
- }
- fputc('\n', f);
- }
-
-
- /* Translates various characters as they are output :
- * '{' starts a DDF command so must be translated to "{\123}"
- * Changing ` to ' allows Impression to use 'smart quotes' if you configure it
- */
-
- void outputchar(char c, FILE * f)
- {
- switch (c) {
- case '{':fputs("{\\123}", f);
- break;
- case '`':
- fputc('\'', f);
- break;
- default:
- fputc(c, f);
- }
- }
-