hmml_to_html.c: Validate template up front
This commit is contained in:
parent
53aea540ab
commit
ad8fb22211
|
@ -1260,8 +1260,7 @@ PrintUsage(char *BinaryLocation, char *DefaultCSSDir, char *DefaultImagesDir, ch
|
|||
int
|
||||
main(int ArgC, char **Args)
|
||||
{
|
||||
// TODO(matt): Read all defaults from the config and probably validate the
|
||||
// template up front
|
||||
// TODO(matt): Read all defaults from the config
|
||||
char *DefaultCSSDir = ".";
|
||||
char *CSSDir = DefaultCSSDir;
|
||||
|
||||
|
@ -1274,8 +1273,10 @@ main(int ArgC, char **Args)
|
|||
char *DefaultDefaultMedium = "programming";
|
||||
char *DefaultMedium = DefaultDefaultMedium;
|
||||
|
||||
bool HasTemplate = FALSE;
|
||||
char *DefaultTemplateLocation = "template.html";
|
||||
char *TemplateLocation = DefaultTemplateLocation;
|
||||
HasTemplate = TRUE;
|
||||
|
||||
char *DefaultOutLocation = "out.html";
|
||||
char *OutLocation = DefaultOutLocation;
|
||||
|
@ -1381,7 +1382,181 @@ main(int ArgC, char **Args)
|
|||
QuoteDir[StringLength(QuoteDir) - 1] = '\0';
|
||||
}
|
||||
|
||||
// TODO(matt): Validate template
|
||||
// TODO(matt): Put the Errors in the MemoryArena
|
||||
buffer Errors;
|
||||
Errors.ID = "Errors";
|
||||
Errors.Size = Kilobytes(1);
|
||||
if(!(Errors.Location = malloc(Errors.Size)))
|
||||
{
|
||||
perror(Args[0]); return 1;
|
||||
}
|
||||
Errors.Ptr = Errors.Location;
|
||||
bool HaveErrors = FALSE;
|
||||
|
||||
buffer Template;
|
||||
Template.ID = "Template";
|
||||
if(HasTemplate)
|
||||
{
|
||||
if(CINERA_MODE && !StringsDiffer(CINERA_MODE, "INTEGRATE"))
|
||||
{
|
||||
FILE *TemplateFile;
|
||||
if(!(TemplateFile = fopen(TemplateLocation, "r")))
|
||||
{
|
||||
perror(Args[0]); return 1;
|
||||
}
|
||||
|
||||
fseek(TemplateFile, 0, SEEK_END);
|
||||
Template.Size = ftell(TemplateFile);
|
||||
fseek(TemplateFile, 0, SEEK_SET);
|
||||
if(!(Template.Location = malloc(Template.Size)))
|
||||
{
|
||||
perror(Args[0]); return 1;
|
||||
}
|
||||
Template.Ptr = Template.Location;
|
||||
fread(Template.Location, Template.Size, 1, TemplateFile);
|
||||
fclose(TemplateFile);
|
||||
|
||||
char *IncludesTag = "__CINERA_INCLUDES__";
|
||||
char *TitleTag = "__CINERA_TITLE__";
|
||||
char *MenusTag = "__CINERA_MENUS__";
|
||||
char *PlayerTag = "__CINERA_PLAYER__";
|
||||
char *ScriptTag = "__CINERA_SCRIPT__";
|
||||
|
||||
bool FoundIncludes = FALSE;
|
||||
bool FoundMenus = FALSE;
|
||||
bool FoundPlayer = FALSE;
|
||||
bool FoundScript = FALSE;
|
||||
|
||||
while(Template.Ptr - Template.Location < Template.Size)
|
||||
{
|
||||
if(*Template.Ptr == '!' && (Template.Ptr > Template.Location && !StringsDifferT("<!--", &Template.Ptr[-1], 0)))
|
||||
{
|
||||
while(Template.Ptr - Template.Location < Template.Size)
|
||||
{
|
||||
if(!(StringsDifferT(IncludesTag, Template.Ptr, 0)))
|
||||
{
|
||||
if(!ForceIntegration && FoundIncludes == TRUE)
|
||||
{
|
||||
CopyStringToBuffer(&Errors, "Template contains more than one <!-- __CINERA_INCLUDES__ --> tag\n");
|
||||
HaveErrors = TRUE;
|
||||
}
|
||||
FoundIncludes = TRUE;
|
||||
while(Template.Ptr - Template.Location < Template.Size)
|
||||
{
|
||||
if(!StringsDifferT("-->", Template.Ptr, 0))
|
||||
{
|
||||
Template.Ptr += StringLength("-->");
|
||||
break;
|
||||
}
|
||||
++Template.Ptr;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
else if(!(StringsDifferT(TitleTag, Template.Ptr, 0)))
|
||||
{
|
||||
while(Template.Ptr - Template.Location < Template.Size)
|
||||
{
|
||||
if(!StringsDifferT("-->", Template.Ptr, 0))
|
||||
{
|
||||
Template.Ptr += StringLength("-->");
|
||||
break;
|
||||
}
|
||||
++Template.Ptr;
|
||||
}
|
||||
break;
|
||||
}
|
||||
else if(!(StringsDifferT(MenusTag, Template.Ptr, 0)))
|
||||
{
|
||||
if(!ForceIntegration && FoundMenus == TRUE)
|
||||
{
|
||||
CopyStringToBuffer(&Errors, "Template contains more than one <!-- __CINERA_MENUS__ --> tag\n");
|
||||
HaveErrors = TRUE;
|
||||
}
|
||||
FoundMenus = TRUE;
|
||||
while(Template.Ptr - Template.Location < Template.Size)
|
||||
{
|
||||
if(!StringsDifferT("-->", Template.Ptr, 0))
|
||||
{
|
||||
Template.Ptr += StringLength("-->");
|
||||
break;
|
||||
}
|
||||
++Template.Ptr;
|
||||
}
|
||||
break;
|
||||
}
|
||||
else if(!(StringsDifferT(PlayerTag, Template.Ptr, 0)))
|
||||
{
|
||||
if(!ForceIntegration && FoundPlayer == TRUE)
|
||||
{
|
||||
CopyStringToBuffer(&Errors, "Template contains more than one <!-- __CINERA_PLAYER__ --> tag\n");
|
||||
HaveErrors = TRUE;
|
||||
}
|
||||
FoundPlayer = TRUE;
|
||||
while(Template.Ptr - Template.Location < Template.Size)
|
||||
{
|
||||
if(!StringsDifferT("-->", Template.Ptr, 0))
|
||||
{
|
||||
Template.Ptr += StringLength("-->");
|
||||
break;
|
||||
}
|
||||
++Template.Ptr;
|
||||
}
|
||||
break;
|
||||
}
|
||||
else if(!(StringsDifferT(ScriptTag, Template.Ptr, 0)))
|
||||
{
|
||||
if(!ForceIntegration && FoundPlayer == FALSE)
|
||||
{
|
||||
CopyStringToBuffer(&Errors, "<!-- __CINERA_SCRIPT__ --> must come after <!-- __CINERA_PLAYER__ -->\n");
|
||||
HaveErrors = TRUE;
|
||||
}
|
||||
if(!ForceIntegration && FoundScript == TRUE)
|
||||
{
|
||||
CopyStringToBuffer(&Errors, "Template contains more than one <!-- __CINERA_SCRIPT__ --> tag\n");
|
||||
HaveErrors = TRUE;
|
||||
}
|
||||
FoundScript = TRUE;
|
||||
while(Template.Ptr - Template.Location < Template.Size)
|
||||
{
|
||||
if(!StringsDifferT("-->", Template.Ptr, 0))
|
||||
{
|
||||
Template.Ptr += StringLength("-->");
|
||||
break;
|
||||
}
|
||||
++Template.Ptr;
|
||||
}
|
||||
break;
|
||||
}
|
||||
else if(!StringsDifferT("-->", Template.Ptr, 0))
|
||||
{
|
||||
break;
|
||||
}
|
||||
Template.Ptr++;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Template.Ptr++;
|
||||
}
|
||||
}
|
||||
|
||||
if(!HaveErrors && FoundIncludes && FoundMenus && FoundPlayer && FoundScript)
|
||||
{
|
||||
Template.Ptr = Template.Location;
|
||||
free(Errors.Location);
|
||||
}
|
||||
else
|
||||
{
|
||||
if(!FoundIncludes){ CopyStringToBuffer(&Errors, "Template must include one <!-- __CINERA_INCLUDES__ --> tag\n"); };
|
||||
if(!FoundMenus){ CopyStringToBuffer(&Errors, "Template must include one <!-- __CINERA_MENUS__ --> tag\n"); };
|
||||
if(!FoundPlayer){ CopyStringToBuffer(&Errors, "Template must include one <!-- __CINERA_PLAYER__ --> tag\n"); };
|
||||
if(!FoundScript){ CopyStringToBuffer(&Errors, "Template must include one <!-- __CINERA_SCRIPT__ --> tag\n"); };
|
||||
fprintf(stderr, "%s", Errors.Location);
|
||||
free(Template.Location); free(Errors.Location); return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// NOTE(matt): Init MemoryArena
|
||||
buffer MemoryArena;
|
||||
|
@ -2628,25 +2803,6 @@ main(int ArgC, char **Args)
|
|||
|
||||
if(CINERA_MODE && !StringsDiffer(CINERA_MODE, "INTEGRATE"))
|
||||
{
|
||||
FILE *TemplateFile;
|
||||
if(!(TemplateFile = fopen(TemplateLocation, "r")))
|
||||
{
|
||||
perror(Args[0]); hmml_free(&HMML); free(MemoryArena.Location); return 1;
|
||||
}
|
||||
|
||||
fseek(TemplateFile, 0, SEEK_END);
|
||||
buffer Template;
|
||||
Template.ID = "Template";
|
||||
Template.Size = ftell(TemplateFile);
|
||||
fseek(TemplateFile, 0, SEEK_SET);
|
||||
if(!(Template.Location = malloc(Template.Size)))
|
||||
{
|
||||
perror(Args[0]); hmml_free(&HMML); free(MemoryArena.Location); return 1;
|
||||
}
|
||||
Template.Ptr = Template.Location;
|
||||
fread(Template.Location, Template.Size, 1, TemplateFile);
|
||||
fclose(TemplateFile);
|
||||
|
||||
buffer Output;
|
||||
Output.Size = Template.Size + Master.Size;
|
||||
Output.ID = "Output";
|
||||
|
@ -2662,11 +2818,6 @@ main(int ArgC, char **Args)
|
|||
char *PlayerTag = "__CINERA_PLAYER__";
|
||||
char *ScriptTag = "__CINERA_SCRIPT__";
|
||||
|
||||
bool FoundIncludes = FALSE;
|
||||
bool FoundMenus = FALSE;
|
||||
bool FoundPlayer = FALSE;
|
||||
bool FoundScript = FALSE;
|
||||
|
||||
while(Template.Ptr - Template.Location < Template.Size)
|
||||
{
|
||||
if(*Template.Ptr == '!' && (Template.Ptr > Template.Location && !StringsDifferT("<!--", &Template.Ptr[-1], 0)))
|
||||
|
@ -2676,12 +2827,6 @@ main(int ArgC, char **Args)
|
|||
{
|
||||
if(!(StringsDifferT(IncludesTag, Template.Ptr, 0)))
|
||||
{
|
||||
if(FoundIncludes == TRUE)
|
||||
{
|
||||
fprintf(stderr, "Template contains more than one <!-- __CINERA_INCLUDES__ --> tag\n");
|
||||
free(Template.Location); free(Output.Location); hmml_free(&HMML); free(MemoryArena.Location); return 1;
|
||||
}
|
||||
FoundIncludes = TRUE;
|
||||
Output.Ptr = CommentStart;
|
||||
CopyBuffer(&Output, &Includes);
|
||||
while(Template.Ptr - Template.Location < Template.Size)
|
||||
|
@ -2713,12 +2858,6 @@ main(int ArgC, char **Args)
|
|||
}
|
||||
else if(!(StringsDifferT(MenusTag, Template.Ptr, 0)))
|
||||
{
|
||||
if(FoundMenus == TRUE)
|
||||
{
|
||||
fprintf(stderr, "Template contains more than one <!-- __CINERA_MENUS__ --> tag\n");
|
||||
free(Template.Location); free(Output.Location); hmml_free(&HMML); free(MemoryArena.Location); return 1;
|
||||
}
|
||||
FoundMenus = TRUE;
|
||||
Output.Ptr = CommentStart;
|
||||
CopyBuffer(&Output, &Menus);
|
||||
while(Template.Ptr - Template.Location < Template.Size)
|
||||
|
@ -2734,12 +2873,6 @@ main(int ArgC, char **Args)
|
|||
}
|
||||
else if(!(StringsDifferT(PlayerTag, Template.Ptr, 0)))
|
||||
{
|
||||
if(FoundPlayer == TRUE)
|
||||
{
|
||||
fprintf(stderr, "Template contains more than one <!-- __CINERA_PLAYER__ --> tag\n");
|
||||
free(Template.Location); free(Output.Location); hmml_free(&HMML); free(MemoryArena.Location); return 1;
|
||||
}
|
||||
FoundPlayer = TRUE;
|
||||
Output.Ptr = CommentStart;
|
||||
CopyBuffer(&Output, &Player);
|
||||
while(Template.Ptr - Template.Location < Template.Size)
|
||||
|
@ -2755,17 +2888,6 @@ main(int ArgC, char **Args)
|
|||
}
|
||||
else if(!(StringsDifferT(ScriptTag, Template.Ptr, 0)))
|
||||
{
|
||||
if(!ForceIntegration && FoundPlayer == FALSE)
|
||||
{
|
||||
fprintf(stderr, "<!-- __CINERA_SCRIPT__ --> must come after <!-- __CINERA_PLAYER__ -->\n");
|
||||
free(Template.Location); free(Output.Location); hmml_free(&HMML); free(MemoryArena.Location); return 1;
|
||||
}
|
||||
if(FoundScript == TRUE)
|
||||
{
|
||||
fprintf(stderr, "Template contains more than one <!-- __CINERA_SCRIPT__ --> tag\n");
|
||||
free(Template.Location); free(Output.Location); hmml_free(&HMML); free(MemoryArena.Location); return 1;
|
||||
}
|
||||
FoundScript = TRUE;
|
||||
Output.Ptr = CommentStart;
|
||||
CopyBuffer(&Output, &Script);
|
||||
while(Template.Ptr - Template.Location < Template.Size)
|
||||
|
@ -2792,25 +2914,13 @@ main(int ArgC, char **Args)
|
|||
}
|
||||
}
|
||||
|
||||
if(ForceIntegration || (FoundIncludes && FoundMenus && FoundPlayer && FoundScript))
|
||||
FILE *OutFile;
|
||||
if(!(OutFile = fopen(OutIntegratedLocation, "w")))
|
||||
{
|
||||
FILE *OutFile;
|
||||
if(!(OutFile = fopen(OutIntegratedLocation, "w")))
|
||||
{
|
||||
perror(OutIntegratedLocation); free(Template.Location); free(Output.Location); hmml_free(&HMML); free(MemoryArena.Location); return 1;
|
||||
}
|
||||
fwrite(Output.Location, Output.Ptr - Output.Location, 1, OutFile);
|
||||
fclose(OutFile);
|
||||
}
|
||||
else
|
||||
{
|
||||
fprintf(stderr, "Template is missing necessary tags:\n");
|
||||
if(!FoundIncludes) { fprintf(stderr, " <!-- __CINERA_INCLUDES__ -->\n"); }
|
||||
if(!FoundMenus) { fprintf(stderr, " <!-- __CINERA_MENUS__ -->\n"); }
|
||||
if(!FoundPlayer) { fprintf(stderr, " <!-- __CINERA_PLAYER__ -->\n"); }
|
||||
if(!FoundScript) { fprintf(stderr, " <!-- __CINERA_SCRIPT__ -->\n"); }
|
||||
free(Template.Location); free(Output.Location); hmml_free(&HMML); free(MemoryArena.Location); return 1;
|
||||
perror(OutIntegratedLocation); free(Template.Location); free(Output.Location); hmml_free(&HMML); free(MemoryArena.Location); return 1;
|
||||
}
|
||||
fwrite(Output.Location, Output.Ptr - Output.Location, 1, OutFile);
|
||||
fclose(OutFile);
|
||||
|
||||
free(Template.Location);
|
||||
free(Output.Location);
|
||||
|
|
Loading…
Reference in New Issue