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 #if 0
ctime -begin ${0%.*}.ctm 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 ctime -end ${0%.*}.ctm
exit exit
#endif #endif
@ -21,6 +21,8 @@ typedef unsigned int bool;
#include <curl/curl.h> #include <curl/curl.h>
#include <time.h> #include <time.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#define Kilobytes(Bytes) Bytes << 10 #define Kilobytes(Bytes) Bytes << 10
#define Megabytes(Bytes) Bytes << 20 #define Megabytes(Bytes) Bytes << 20
@ -123,7 +125,7 @@ category_medium CategoryMedium[] =
{ "trivia", "&#127922;", "Trivia"}, { "trivia", "&#127922;", "Trivia"},
}; };
#define EDITION EDITION_SINGLE int EDITION = EDITION_SINGLE;
#define ArrayCount(A) sizeof(A)/sizeof(*(A)) #define ArrayCount(A) sizeof(A)/sizeof(*(A))
@ -1421,11 +1423,13 @@ ParseConfig(buffer *Buffer, char *Username)
#endif #endif
void 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" fprintf(stderr, "Usage: %s [option(s)] filename(s)\n"
"\n" "\n"
"Options:\n" "Options:\n"
" -b <base output directory>\n"
" Override default base output directory (\"%s\")\n"
" -c <CSS directory path>\n" " -c <CSS directory path>\n"
" Override default CSS directory (\"%s\")\n" " Override default CSS directory (\"%s\")\n"
" -f\n" " -f\n"
@ -1438,6 +1442,8 @@ PrintUsage(char *BinaryLocation, char *DefaultCSSDir, char *DefaultImagesDir, ch
" Override default default medium (\"%s\")\n" " Override default default medium (\"%s\")\n"
" -o <output location>\n" " -o <output location>\n"
" Override default output location (\"%s\")\n" " Override default output location (\"%s\")\n"
" -p <project directory>\n"
" Override default project directory (\"%s\")\n"
" -t <template location>\n" " -t <template location>\n"
" Override default template location (\"%s\")\n" " Override default template location (\"%s\")\n"
" and automatically enable integration\n" " and automatically enable integration\n"
@ -1461,13 +1467,16 @@ PrintUsage(char *BinaryLocation, char *DefaultCSSDir, char *DefaultImagesDir, ch
"\n" "\n"
"HMML Specification:\n" "HMML Specification:\n"
" https://git.handmade.network/Annotation-Pushers/Annotation-System/wikis/hmmlspec\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 int
main(int ArgC, char **Args) main(int ArgC, char **Args)
{ {
// TODO(matt): Read all defaults from the config // TODO(matt): Read all defaults from the config
char *DefaultBaseOutputDir = ".";
char *BaseOutputDir = DefaultBaseOutputDir;
char *DefaultCSSDir = "."; char *DefaultCSSDir = ".";
char *CSSDir = DefaultCSSDir; char *CSSDir = DefaultCSSDir;
@ -1491,6 +1500,9 @@ main(int ArgC, char **Args)
char *DefaultOutIntegratedLocation = "out_integrated.html"; char *DefaultOutIntegratedLocation = "out_integrated.html";
char *OutIntegratedLocation = DefaultOutIntegratedLocation; char *OutIntegratedLocation = DefaultOutIntegratedLocation;
char *DefaultProjectDir = ".";
char *ProjectDir = DefaultProjectDir;
char *CINERA_MODE = getenv("CINERA_MODE"); char *CINERA_MODE = getenv("CINERA_MODE");
bool DefaultForceIntegration = FALSE; bool DefaultForceIntegration = FALSE;
@ -1508,15 +1520,18 @@ main(int ArgC, char **Args)
if(ArgC < 2) 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; return 1;
} }
char CommandLineArg; 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) switch(CommandLineArg)
{ {
case 'b':
BaseOutputDir = optarg;
break;
case 'c': case 'c':
CSSDir = optarg; CSSDir = optarg;
break; break;
@ -1536,6 +1551,9 @@ main(int ArgC, char **Args)
OutLocation = optarg; OutLocation = optarg;
OutIntegratedLocation = optarg; OutIntegratedLocation = optarg;
break; break;
case 'p':
ProjectDir = optarg;
break;
case 't': case 't':
TemplateLocation = optarg; TemplateLocation = optarg;
CINERA_MODE="INTEGRATE"; CINERA_MODE="INTEGRATE";
@ -1544,15 +1562,62 @@ main(int ArgC, char **Args)
// Override config path, once we even have a default! // Override config path, once we even have a default!
case 'h': case 'h':
default: default:
PrintUsage(Args[0], DefaultCSSDir, DefaultImagesDir, DefaultJSDir, DefaultDefaultMedium, DefaultOutLocation, DefaultTemplateLocation); PrintUsage(Args[0], DefaultBaseOutputDir, DefaultCSSDir, DefaultImagesDir, DefaultJSDir, DefaultDefaultMedium, DefaultOutLocation, DefaultProjectDir, DefaultTemplateLocation);
return 1; 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]); 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; return 1;
} }
@ -1740,12 +1805,12 @@ main(int ArgC, char **Args)
{ {
break; break;
} }
Template.Ptr++; ++Template.Ptr;
} }
} }
else else
{ {
Template.Ptr++; ++Template.Ptr;
} }
} }
@ -1829,8 +1894,11 @@ main(int ArgC, char **Args)
buffer Script; buffer Script;
buffer FilterState; buffer FilterState;
//HMMLToBuffers(&CollationBuffers, Filename);
for(int FileIndex = optind; FileIndex < ArgC; ++FileIndex) for(int FileIndex = optind; FileIndex < ArgC; ++FileIndex)
{ {
// NOTE(matt): Start of HMMLToBuffers()
FILE *InFile; FILE *InFile;
if(!(InFile = fopen(Args[FileIndex], "r"))) if(!(InFile = fopen(Args[FileIndex], "r")))
{ {
@ -3066,6 +3134,7 @@ goto Cleanup;
"}\n" "}\n"
" </script>"); " </script>");
// NOTE(matt): Don't include this in HMMLToBuffers()
#if DEBUG #if DEBUG
printf("Buffer Collation\n\n"); printf("Buffer Collation\n\n");
#endif #endif
@ -3228,6 +3297,8 @@ goto Cleanup;
fclose(OutFile); fclose(OutFile);
} }
// NOTE(matt): Start to include this stuff in HMMLToBuffers()
// NOTE(matt): Tree structure of "global" buffer dependencies // NOTE(matt): Tree structure of "global" buffer dependencies
// FilterState // FilterState
// Script // Script
@ -3263,6 +3334,7 @@ Cleanup:
fprintf(stderr, "%s:%d: %s\n", Args[FileIndex], HMML.error.line, HMML.error.message); fprintf(stderr, "%s:%d: %s\n", Args[FileIndex], HMML.error.line, HMML.error.message);
} }
hmml_free(&HMML); hmml_free(&HMML);
// NOTE(matt): End of HMMLToBuffers()
} }
free(MemoryArena.Location); free(MemoryArena.Location);
} }