Tiffy is a static website generator that uses Lizard markup files to create a website.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

176 lines
3.2 KiB

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ftw.h>
#include <sys/stat.h>
#include <dirent.h>
#include "files.h"
#include "layout.h"
#include "parser.h"
#define CONFIG_FILE ".tiffyconf"
char *content_dir;
char *build_dir;
Layout layout;
void parseconfig( void ) {
FILE *fp;
fp = fopen( CONFIG_FILE, "r" );
if ( fp == NULL ) {
perror( "fopen" );
exit( EXIT_FAILURE );
}
char *line = NULL;
size_t len = 0;
ssize_t nread;
while ( ( nread = getline( &line, &len, fp ) ) != -1 ) {
switch ( line[ 0 ] ) {
case 'C': // Content path
content_dir = malloc( nread );
memcpy( content_dir, line + 8, nread - 8 );
content_dir[ nread - 7 ] = '\0';
content_dir[ strcspn( content_dir, "\n" ) ] = 0;
break;
case 'B': // Build output
build_dir = malloc( nread );
memcpy( build_dir, line + 6, nread - 6 );
build_dir[ nread - 5 ] = '\0';
build_dir[ strcspn( build_dir, "\n" ) ] = 0;
break;
}
}
free( line );
fclose( fp );
}
int has_ext( const char * fp, const char * ext ) {
char * fext = strrchr( fp, '.' );
if ( fext == NULL ) {
return 0;
}
if ( strncmp( fext, ext, strlen( ext ) ) == 0 ) {
return 1;
}
return 0;
}
char * change_ext( char * f, char * e ) {
char * fext = strrchr( f, '.' );
char nxf[ 200 ] = {};
strncpy( nxf, f, fext - f );
strcat( nxf, e );
return strdup( nxf ) ;
}
void create_dir_if_needed( const char * f ) {
char * ptr_dir = strrchr( f, '/' );
char o_dir[ 100 ] = {};
strncpy( o_dir, f, ptr_dir - f );
if ( exists( o_dir ) == 0 ) {
if ( mkdir( o_dir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH ) != 0 ) {
perror( "build_action mkdir" );
exit( EXIT_FAILURE );
}
}
}
void build_action( const char *f ) {
char * nc_dir = strchr( f, '/' );
if ( nc_dir == NULL ) {
return;
}
char out_dir[ 200 ] = { };
strcat( out_dir, build_dir );
strcat( out_dir, nc_dir );
if ( !isdir( f ) ) {
create_dir_if_needed( out_dir );
if ( has_ext( out_dir, ".liz" ) == 1 ) {
printf( "Building file -> " );
printf( f );
printf( "\n" );
char * of = change_ext( out_dir, ".html" );
char * fc = NULL;
char output[ 60000 ];
readfile( f, &fc );
sprintf( output, "%s%s%s", layout.top, parse( fc ), layout.bottom );
writefile( of, output );
free( of );
free( fc );
} else {
printf( "Copying file -> " );
printf( f );
printf( "\n" );
char * cmd = ( char * )malloc( 400 );
if( cmd == NULL ) {
perror( "malloc while copying file" );
}
sprintf( cmd, "cp \"%s\" \"%s\"", f, out_dir );
if ( system( cmd ) == -1 ) {
perror( "Copying file" );
exit( -1 );
}
free( cmd );
}
}
}
int main( int argc, char ** argv ) {
printf( "Checking config file.\n" );
if ( exists( CONFIG_FILE ) == 0 ) {
printf( "no config" );
exit( EXIT_FAILURE );
}
parseconfig();
char layout_file[ 300 ];
sprintf( layout_file, "%s/tiffy_layout.html", content_dir );
if ( exists( layout_file ) == 0 ) {
perror( "no layout" );
exit( EXIT_FAILURE );
}
printf( "Casetigating last build!\n" );
cleanbuild( build_dir );
printf( "Building.\n" );
getLayout( layout_file, &layout );
dobuild( content_dir, build_action );
}