hmml_to_html.c: Begin refactor

This commit is contained in:
Matt Mascarenhas 2017-08-23 21:30:08 +01:00
parent 67f6ff4db2
commit d8b21171a1
1 changed files with 83 additions and 11 deletions

View File

@ -1,6 +1,6 @@
#if 0
ctime -begin ${0%.*}.ctm
gcc -g -no-pie -fsanitize=address -Wall -std=c99 -pipe $0 -o ${0%.*} hmml.a -lcurl
gcc -g -fsanitize=address -Wall -std=c99 -pipe $0 -o ${0%.*} hmml.a -lcurl
ctime -end ${0%.*}.ctm
exit
#endif
@ -21,6 +21,8 @@ typedef unsigned int bool;
#include <curl/curl.h>
#include <time.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#define Kilobytes(Bytes) Bytes << 10
#define Megabytes(Bytes) Bytes << 20
@ -123,7 +125,7 @@ category_medium CategoryMedium[] =
{ "trivia", "&#127922;", "Trivia"},
};
#define EDITION EDITION_SINGLE
int EDITION = EDITION_SINGLE;
#define ArrayCount(A) sizeof(A)/sizeof(*(A))
@ -1421,11 +1423,13 @@ ParseConfig(buffer *Buffer, char *Username)
#endif
void
PrintUsage(char *BinaryLocation, char *DefaultCSSDir, char *DefaultImagesDir, char *DefaultJSDir, char *DefaultDefaultMedium, char *DefaultOutLocation, char *DefaultTemplateLocation)
PrintUsage(char *BinaryLocation, char *DefaultBaseDirectory, char *DefaultCSSDir, char *DefaultImagesDir, char *DefaultJSDir, char *DefaultDefaultMedium, char *DefaultOutLocation, char *DefaultProjectDirectory, char *DefaultTemplateLocation)
{
fprintf(stderr, "Usage: %s [option(s)] filename(s)\n"
"\n"
"Options:\n"
" -b <base output directory>\n"
" Override default base output directory (\"%s\")\n"
" -c <CSS directory path>\n"
" Override default CSS directory (\"%s\")\n"
" -f\n"
@ -1438,6 +1442,8 @@ PrintUsage(char *BinaryLocation, char *DefaultCSSDir, char *DefaultImagesDir, ch
" Override default default medium (\"%s\")\n"
" -o <output location>\n"
" Override default output location (\"%s\")\n"
" -p <project directory>\n"
" Override default project directory (\"%s\")\n"
" -t <template location>\n"
" Override default template location (\"%s\")\n"
" and automatically enable integration\n"
@ -1461,13 +1467,16 @@ PrintUsage(char *BinaryLocation, char *DefaultCSSDir, char *DefaultImagesDir, ch
"\n"
"HMML Specification:\n"
" https://git.handmade.network/Annotation-Pushers/Annotation-System/wikis/hmmlspec\n",
BinaryLocation, DefaultCSSDir, DefaultImagesDir, DefaultJSDir, DefaultDefaultMedium, DefaultOutLocation, DefaultTemplateLocation);
BinaryLocation, DefaultBaseDirectory, DefaultCSSDir, DefaultImagesDir, DefaultJSDir, DefaultDefaultMedium, DefaultOutLocation, DefaultProjectDirectory, DefaultTemplateLocation);
}
int
main(int ArgC, char **Args)
{
// TODO(matt): Read all defaults from the config
char *DefaultBaseOutputDir = ".";
char *BaseOutputDir = DefaultBaseOutputDir;
char *DefaultCSSDir = ".";
char *CSSDir = DefaultCSSDir;
@ -1491,6 +1500,9 @@ main(int ArgC, char **Args)
char *DefaultOutIntegratedLocation = "out_integrated.html";
char *OutIntegratedLocation = DefaultOutIntegratedLocation;
char *DefaultProjectDir = ".";
char *ProjectDir = DefaultProjectDir;
char *CINERA_MODE = getenv("CINERA_MODE");
bool DefaultForceIntegration = FALSE;
@ -1508,15 +1520,18 @@ main(int ArgC, char **Args)
if(ArgC < 2)
{
PrintUsage(Args[0], DefaultCSSDir, DefaultImagesDir, DefaultJSDir, DefaultDefaultMedium, DefaultOutLocation, DefaultTemplateLocation);
PrintUsage(Args[0], DefaultBaseOutputDir, DefaultCSSDir, DefaultImagesDir, DefaultJSDir, DefaultDefaultMedium, DefaultOutLocation, DefaultProjectDir, DefaultTemplateLocation);
return 1;
}
char CommandLineArg;
while((CommandLineArg = getopt(ArgC, Args, "c:fi:j:m:o:q:t:h")) != -1)
while((CommandLineArg = getopt(ArgC, Args, "b:c:fi:j:m:o:p:q:t:h")) != -1)
{
switch(CommandLineArg)
{
case 'b':
BaseOutputDir = optarg;
break;
case 'c':
CSSDir = optarg;
break;
@ -1536,6 +1551,9 @@ main(int ArgC, char **Args)
OutLocation = optarg;
OutIntegratedLocation = optarg;
break;
case 'p':
ProjectDir = optarg;
break;
case 't':
TemplateLocation = optarg;
CINERA_MODE="INTEGRATE";
@ -1544,15 +1562,62 @@ main(int ArgC, char **Args)
// Override config path, once we even have a default!
case 'h':
default:
PrintUsage(Args[0], DefaultCSSDir, DefaultImagesDir, DefaultJSDir, DefaultDefaultMedium, DefaultOutLocation, DefaultTemplateLocation);
PrintUsage(Args[0], DefaultBaseOutputDir, DefaultCSSDir, DefaultImagesDir, DefaultJSDir, DefaultDefaultMedium, DefaultOutLocation, DefaultProjectDir, DefaultTemplateLocation);
return 1;
}
}
if(optind == ArgC)
if(StringsDiffer(BaseOutputDir, ".") || StringsDiffer(ProjectDir, "."))
{
if(StringsDiffer(BaseOutputDir, ".") && StringsDiffer(ProjectDir, "."))
{
EDITION = EDITION_PROJECT;
}
else
{
fprintf(stderr, "%s: Both the Base Output Directory and the Project Directory must be set in order for us to enter Project Mode\n", Args[0]);
return 1;
}
}
DIR *ProjectDirHandle;
if(!(ProjectDirHandle = opendir(ProjectDir)))
{
perror(Args[0]);
}
struct dirent *ProjectFiles;
char Filenames[255][255]; // TODO(matt): Figure out how to allow an arbitrary number of filenames here
int FileIndex = 0;
while((ProjectFiles = readdir(ProjectDirHandle)))
{
// TODO(matt):
//
// Test the ProjectFiles->d_name against ".hmml" and, if the filename ends in that string, then stuff that filename into an array
char *Ptr = ProjectFiles->d_name;
Ptr += (StringLength(ProjectFiles->d_name) - StringLength(".hmml"));
if(!(StringsDiffer(Ptr, ".hmml")))
{
CopyString(Filenames[FileIndex], ProjectFiles->d_name);
++FileIndex;
}
}
closedir(ProjectDirHandle);
for(int i = 0; i < ArrayCount(Filenames); ++i)
{
if(StringsDiffer(Filenames[i], ""))
{
printf("%s\n", Filenames[i]);
}
}
return 1;
if((EDITION == EDITION_SINGLE && optind == ArgC) ||
// TODO(matt): Test against the number of .hmml files in the project directory
(EDITION == EDITION_PROJECT && 1))
{
fprintf(stderr, "%s: requires at least one input .hmml file\n", Args[0]);
PrintUsage(Args[0], DefaultCSSDir, DefaultImagesDir, DefaultJSDir, DefaultDefaultMedium, DefaultOutLocation, DefaultTemplateLocation);
PrintUsage(Args[0], DefaultBaseOutputDir, DefaultCSSDir, DefaultImagesDir, DefaultJSDir, DefaultDefaultMedium, DefaultOutLocation, DefaultProjectDir, DefaultTemplateLocation);
return 1;
}
@ -1740,12 +1805,12 @@ main(int ArgC, char **Args)
{
break;
}
Template.Ptr++;
++Template.Ptr;
}
}
else
{
Template.Ptr++;
++Template.Ptr;
}
}
@ -1829,8 +1894,11 @@ main(int ArgC, char **Args)
buffer Script;
buffer FilterState;
//HMMLToBuffers(&CollationBuffers, Filename);
for(int FileIndex = optind; FileIndex < ArgC; ++FileIndex)
{
// NOTE(matt): Start of HMMLToBuffers()
FILE *InFile;
if(!(InFile = fopen(Args[FileIndex], "r")))
{
@ -3066,6 +3134,7 @@ goto Cleanup;
"}\n"
" </script>");
// NOTE(matt): Don't include this in HMMLToBuffers()
#if DEBUG
printf("Buffer Collation\n\n");
#endif
@ -3228,6 +3297,8 @@ goto Cleanup;
fclose(OutFile);
}
// NOTE(matt): Start to include this stuff in HMMLToBuffers()
// NOTE(matt): Tree structure of "global" buffer dependencies
// FilterState
// Script
@ -3263,6 +3334,7 @@ Cleanup:
fprintf(stderr, "%s:%d: %s\n", Args[FileIndex], HMML.error.line, HMML.error.message);
}
hmml_free(&HMML);
// NOTE(matt): End of HMMLToBuffers()
}
free(MemoryArena.Location);
}