/* Copyright (C) 2003, 2004 Dave Bayer. Subject to the terms and conditions of the MIT License. */
#include "root.h"
#include "file.h"
#include "filter.h"
#include "rules.h"
#include "dict.h"
#include "merge.h"
#include "html.h"
#include "annote.h"
/*, preamble */
static const char preamble[] =
"\n"
"\n"
"
\n"
"\t%s\n"
"\t\n"
"\n"
"\n\n"
;
/*, postamble */
static const char postamble[] =
"
\n"
"\n"
"\n"
"\n"
;
/*. stylefile */
static const char *stylefile[] =
{
"body",
"{",
"\tbackground: white;",
"\tcolor: black;",
"}",
"",
"body, table, strong, code, samp, pre",
"{",
"\tfont-family: lucida grande, geneva, helvetica, arial, sans-serif;",
"\tfont-size: 10.5pt;",
"\tfont-style: normal;",
"\tfont-weight: normal;",
"}",
"",
"pre",
"{",
"\tpadding-top: 0.6em;",
"\tpadding-left: 1.2em;",
"\tpadding-bottom: 0.8em;",
"}",
"",
"p.footer",
"{",
"\tfont-size: small;",
"\tfont-style: italic;",
"}",
"",
"pre.code",
"{",
"\tbackground: #dff;",
"\tborder: 1px solid gray;",
"}",
"",
"samp, pre.samp",
"{",
"\tcolor: #03f;",
"}",
"",
"strong, strong ul",
"{",
"\tbackground: #fcc;",
"}",
"",
"code a, samp a, pre a",
"{",
"\tcolor: #909;",
"\ttext-decoration: none;",
"}",
0
};
/*, makePath */
static char *makePath( const char *fileName )
{
char *s, *name;
int offset;
s = strrchr( fileName, '/' );
offset = s == 0 ? 0 : s - fileName + 1;
name = malloc( ( offset + 1 ) * sizeof( char ));
strncpy( name, fileName, offset );
name[offset] = '\0';
return name;
}
/*, makeName */
static char *makeName( const char *fileName, char *ext, BOOL trunc )
{
char *s, *name;
int offset;
if ( trunc )
{
s = strrchr( fileName, '/' );
if ( s != 0 ) fileName = s + 1;
}
s = strrchr( fileName, '.' );
offset = s == 0 ? strlen( fileName ) : s - fileName;
name = malloc( ( offset + strlen( ext ) + 1 ) * sizeof( char ));
/* strcpy was a bug */
strncpy( name, fileName, offset );
strcpy( name + offset, ext );
return name;
}
/*. makeRelativeName */
void makeRelativeName( const char *fromName, const char *toName, char *buf, int len )
{
int offset, up = 0;
BOOL agree = YES;
char *s;
while (( s = strchr( fromName, '/' )) != 0 )
{
offset = s - fromName + 1;
if ( agree && strncmp( fromName, toName, len ) == 0 )
toName += offset;
else
++up;
fromName += offset;
}
s = buf + len - 1;
while ( up-- > 0 && len >= 3 )
{
strcpy( buf, "../" );
buf += 3;
len -= 3;
}
strncpy( buf, toName, len );
*s = '\0';
}
/*, makeDate */
static void makeDate( char *buf, int len )
{
time_t now;
now = time( 0 );
strftime( buf, len, "%B %e, %Y", localtime( &now ));
buf[ len-1 ] = '\0';
}
/*. htmlFromDoc */
void htmlFromDoc( int nFiles, const char *fileName[] )
{
int i;
FILE *fin, *fout;
line **pDoc;
char **path, **truncName, **htmlName;
const char **ps;
lineFilter filter, filter2, filter3;
filterState state, state2, state3;
char style[LEN], date[LEN];
/* initialize finite state machines */
initFilterTable( sourceTable, sourceRules, docNStates, YES );
initFilterTable( docTable, sourceRules, docNStates, YES );
initFilterTable( docTable, docRules, docNStates, NO );
initFilterTable( htmlTable, htmlRules, htmlNStates, YES );
initFilterTable( parTable, parRules, parNStates, YES );
initFilterTable( dictTable, dictRules, dictNStates, YES );
/* read doc files */
filter.filt = stateFilter;
filter.next = 0;
filter.data = &state;
state.table = docTable;
state.filt = 0;
pDoc = malloc( nFiles * sizeof( *pDoc ));
path = malloc( nFiles * sizeof( *path ));
truncName = malloc( nFiles * sizeof( *truncName ));
htmlName = malloc( nFiles * sizeof( *htmlName ));
for ( i=0; i