📄 doc2ipf.c
字号:
static int startpage = 1;
char str[MAX_LINE_LEN];
char topic[MAX_LINE_LEN];
int k, l;
static int tabl=0;
static int para=0;
static int inquote = FALSE;
static int inref = FALSE;
static int intable = FALSE ;
static int intablebut = FALSE ;
static FILE *bo= NULL, *bt = NULL ;
line_count++;
if( bo == NULL ) bo = b ;
i = 0;
j = 0;
while (line[i] != '\0')
{
switch(line[i])
{
case '$':
if( intable && line[0] == '%' ) {
++i ;
if( line[i+1]=='$'|| line[i]=='x' || line[i]=='|'){
while ( line[i] != '$' ) line2[j++]=line[i++] ;
--j;
}
else {
while( line[i] != '$' ) i++ ;
if( line[i+1]==',' ) i++ ;
if( line[i+1]==' ' ) i++ ;
line2[j]=line[++i] ;
}
}
else
line2[j] = line[i];
break ;
case ':':
strcpy( &line2[j], "&colon." ) ;
j += strlen( "&colon." ) - 1 ;
break ;
case '\047':
strcpy( &line2[j], "&apos." ) ;
j += strlen( "&apos." ) - 1 ;
break ;
case '&':
strcpy( &line2[j], "&." ) ;
j += strlen( "&." ) - 1 ;
break ;
case '\r':
case '\n':
break;
case '`': /* backquotes mean boldface or link */
if ((!inref) && (!inquote))
{
k=i+1; /* index into current string */
l=0; /* index into topic string */
while ((line[k] != '`') && (line[k] != '\0'))
{
topic[l] = line[k];
k++;
l++;
}
topic[l] = '\0';
k = lookup(topic);
if ((k > 0) && (k != last_line))
{
sprintf( hyplink1, ":link reftype=hd res=%d.", k ) ;
strcpy( line2+j, hyplink1 ) ;
j += strlen( hyplink1 )-1 ;
inref = k;
}
else
{
if (debug && k != last_line)
fprintf(stderr,"Can't make link for \042%s\042 on line %d\n",topic,line_count);
line2[j++] = ':';
line2[j++] = 'h';
line2[j++] = 'p';
line2[j++] = '2';
line2[j] = '.';
inquote = TRUE;
}
}
else
{
if (inquote && inref)
fprintf(stderr, "Warning: Reference Quote conflict line %d\n", line_count);
if (inquote)
{
line2[j++] = ':';
line2[j++] = 'e';
line2[j++] = 'h';
line2[j++] = 'p';
line2[j++] = '2';
line2[j] = '.';
inquote = FALSE;
}
if (inref)
{
/* must be inref */
line2[j++] = ':';
line2[j++] = 'e';
line2[j++] = 'l';
line2[j++] = 'i';
line2[j++] = 'n';
line2[j++] = 'k';
line2[j] = '.';
inref = 0;
}
}
break;
default:
line2[j] = line[i];
}
i++;
j++;
line2[j] = '\0';
}
i = 1;
switch(line[0]) { /* control character */
case '?': { /* interactive help entry */
if( intable ) intablebut = TRUE ;
fprintf(bo,"\n:i1. %s", &(line[1])); /* index entry */
break;
}
case '@': { /* start/end table */
intable = !intable ;
if( intable ) {
tablelines = 0;
tablecols = 0 ;
tableins = &table ;
for(j=0;j<3;j++)tablewidth[j]=0 ;
}
else { /* dump table */
intablebut = FALSE ;
tableins = table.next ;
fprintf(b,":table cols=\'") ;
for( j=0;j<3;j++)
if(tablewidth[j]>0) fprintf(b," %d",tablewidth[j]);
fprintf(b,"\'.\n") ;
tableins=tableins->next ;
while( tableins != NULL ) {
if( tableins->col[0][0] != '_' ) {
fprintf(b,":row.\n" ) ;
for( j=0;j<tablecols;j++)
fprintf(b,":c.%s\n", tableins->col[j] ) ;
}
tableins = tableins->next ;
}
fprintf(b,":etable.\n") ;
if( bt != NULL ) {
rewind( bt ) ;
while( fgets(str, MAX_LINE_LEN, bt) )
fputs( str, b ) ;
fclose( bt ) ;
remove( "doc2ipf.tmp" ) ;
bt = NULL ;
bo = b ;
}
}
break; /* ignore */
}
case '#': { /* latex table entry */
break; /* ignore */
}
case '%': { /* troff table entry */
if( intable ) {
tablerow = line2 ;
tableins->next = (struct TABENTRY *)malloc( sizeof( struct TABENTRY ) ) ;
tableins = tableins->next ;
tableins->next = NULL ;
j=0 ;
while((pt=strtok( tablerow, "%@\n" ))!=NULL) {
if( *pt != '\0' ) { /* ignore null columns */
strcpy( tableins->col[j], pt ) ;
k=strlen( pt ) ;
if( k > tablewidth[j] ) tablewidth[j]=k ;
++j ;
tablerow = NULL ;
if( j > tablecols ) tablecols = j ;
}
}
for( ; j<3; j++ ) tableins->col[j][0]='\0' ;
}
break; /* ignore */
}
case '\n': /* empty text line */
if (tabl)
fprintf(bo,":ecgraphic.\n"); /* rjl */
para = 0;
tabl = 0;
fprintf(bo,":p.");
break;
case ' ': { /* normal text line */
if( intable && ! intablebut ) break ;
if( intablebut ) { /* indexed items in table, copy
to file after table by saving in
a temp file meantime */
if( bt == NULL ) {
fflush(bo) ;
bt = fopen( "doc2ipf.tmp", "w+" ) ;
if( bt==NULL ) fprintf(stderr, "cant open temp\n" ) ;
else bo = bt ;
}
}
if( intablebut && (bt==NULL )) break ;
if ((line2[1] == '\0') || (line2[1] == '\n'))
{
fprintf(bo,":p.");
para = 0;
}
if (line2[1] == ' ')
{
if (!tabl)
{
/* fprintf(bo,":p."); */
fprintf(bo,":cgraphic.\n");
}
fprintf(bo,"%s\n",&line2[1]);
/* fprintf(bo,"\n.br\n"); */
tabl = 1;
para = 0;
}
else
{
if (tabl) {
fprintf(bo,":ecgraphic.\n"); /* rjl */
fprintf(bo,":p."); /* rjl */
}
tabl = 0;
if (!para)
para = 1; /* not in para so start one */
fprintf(bo,"%s \n",&line2[1]);
}
fflush(bo) ;
break;
}
default: {
if (isdigit((int)(line[0]))) { /* start of section */
if (tabl)
fprintf(bo,":ecgraphic.\n"); /* rjl */
if( intable ) {
intablebut = TRUE ;
if( bt == NULL ) {
fflush(bo) ;
bt = fopen( "doc2ipf.tmp", "w+" ) ;
if( bt==NULL ) fprintf(stderr, "cant open temp\n" ) ;
else bo = bt ;
}
}
if (!startpage)
{
refs(last_line,bo);
}
para = 0; /* not in a paragraph */
tabl = 0;
last_line = line_count;
startpage = 0;
if (debug)
fprintf( stderr, "%d: %s\n", line_count, &line2[1] ) ;
k=lookup(&line2[1]) ;
#ifdef OLD
/* if( k<0 ) fprintf(bo,":h%c.", line[0]=='1'?line[0]:line[0]-1);
else*/ fprintf(bo,":h%c res=%d.", line[0]=='1'?line[0]:line[0]-1,line_count);
#else
fprintf(bo,":h%c res=%d name='%s'.", line[0]=='1'?line[0]:line[0]-1,line_count, &(line2[1]));
#endif
fprintf(bo,&(line2[1])); /* title */
fprintf(bo,"\n:p." ) ;
} else
fprintf(stderr, "unknown control code '%c' in column 1, line %d\n",
line[0], line_count);
break;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -