hmml_to_html.c: Integrate keeping comments [#25]

It straight up replaces <!-- CINERA_* --> comments with our content,
while preserving other comments
This commit is contained in:
Matt Mascarenhas 2017-06-16 08:55:59 +01:00
parent dd9c844b6f
commit 03ccba43bb
1 changed files with 119 additions and 126 deletions

View File

@ -304,7 +304,7 @@ StringsDiffer(char *A, char *B) // NOTE(matt): Two null-terminated strings
bool bool
StringsDifferT(char *A, // NOTE(matt): Null-terminated string StringsDifferT(char *A, // NOTE(matt): Null-terminated string
char *B, // NOTE(matt): Not null-terminated string (e.g. one mid-buffer) char *B, // NOTE(matt): Not null-terminated string (e.g. one mid-buffer)
char Terminator // NOTE(matt): Caller definable terminator. Pass '\0' to only match on the extent of A char Terminator // NOTE(matt): Caller definable terminator. Pass 0 to only match on the extent of A
) )
{ {
int ALength = StringLength(A); int ALength = StringLength(A);
@ -2169,11 +2169,11 @@ CategoryMedium[j][2]
" </div>\n" " </div>\n"
" </div>\n" " </div>\n"
" </div>\n"); " </div>");
CopyStringToBuffer(&Player, CopyStringToBuffer(&Player,
" </div>\n" " </div>\n"
" </div>\n"); " </div>");
// TODO(matt): Maybe do something about indentation levels // TODO(matt): Maybe do something about indentation levels
CopyStringToBuffer(&Header, CopyStringToBuffer(&Header,
@ -2186,12 +2186,11 @@ CategoryMedium[j][2]
" <script type=\"text/javascript\" src=\"player.js\"></script>\n" " <script type=\"text/javascript\" src=\"player.js\"></script>\n"
" <link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\">\n" " <link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\">\n"
" <link rel=\"stylesheet\" type=\"text/css\" href=\"%s.css\">\n" " <link rel=\"stylesheet\" type=\"text/css\" href=\"%s.css\">\n"
" <link rel=\"stylesheet\" type=\"text/css\" href=\"topics.css\">\n", " <link rel=\"stylesheet\" type=\"text/css\" href=\"topics.css\">",
HMML.metadata.project); HMML.metadata.project);
CopyStringToBuffer(&Script, CopyStringToBuffer(&Script,
" <script>\n" " <script type=\"text/javascript\">\n"
"\n"
"var menuState = [];\n"); "var menuState = [];\n");
CopyStringToBuffer(&Script, CopyStringToBuffer(&Script,
@ -2378,8 +2377,7 @@ HMML.metadata.project);
" setDotLightness(topicDots[i]);\n" " setDotLightness(topicDots[i]);\n"
"}\n" "}\n"
" </script>"); " </script>");
//
//NOTE(matt): Collate the buffers!
#if DEBUG #if DEBUG
printf("Buffer Collation\n\n"); printf("Buffer Collation\n\n");
#endif #endif
@ -2394,25 +2392,24 @@ HMML.metadata.project);
} }
fseek(TemplateFile, 0, SEEK_END); fseek(TemplateFile, 0, SEEK_END);
int TemplateSize = ftell(TemplateFile); buffer Template;
Template.ID = "Template";
Template.Size = ftell(TemplateFile);
fseek(TemplateFile, 0, SEEK_SET); fseek(TemplateFile, 0, SEEK_SET);
char *TemplateBuffer; if(!(Template.Location = malloc(Template.Size)))
if(!(TemplateBuffer = malloc(TemplateSize)))
{ {
perror(Args[0]); hmml_free(&HMML); free(MemoryArena); return 1; perror(Args[0]); hmml_free(&HMML); free(MemoryArena); return 1;
} }
fread(TemplateBuffer, TemplateSize, 1, TemplateFile); fread(Template.Location, Template.Size, 1, TemplateFile);
fclose(TemplateFile); fclose(TemplateFile);
char *OutputBuffer; buffer Output;
if(!(OutputBuffer = malloc(TemplateSize + Master.Size))) Output.ID = "Output";
if(!(Output.Location = malloc(Template.Size + Master.Size)))
{ {
perror(Args[0]); free(TemplateBuffer); hmml_free(&HMML); free(MemoryArena); return 1; perror(Args[0]); free(Template.Location); hmml_free(&HMML); free(MemoryArena); return 1;
} }
char *TemplatePtr = TemplateBuffer;
char *OutputPtr = OutputBuffer;
char *HeaderTag = "__CINERA_HEADER__"; char *HeaderTag = "__CINERA_HEADER__";
char *TitleTag = "__CINERA_TITLE__"; char *TitleTag = "__CINERA_TITLE__";
char *PlayerTag = "__CINERA_PLAYER__"; char *PlayerTag = "__CINERA_PLAYER__";
@ -2423,120 +2420,113 @@ HMML.metadata.project);
bool FoundPlayer = FALSE; bool FoundPlayer = FALSE;
bool FoundScript = FALSE; bool FoundScript = FALSE;
while(TemplatePtr - TemplateBuffer < TemplateSize) while(Template.Ptr - Template.Location < Template.Size)
{ {
if(*TemplatePtr == '!') if(*Template.Ptr == '!' && (Template.Ptr > Template.Location && !StringsDifferT("<!--", &Template.Ptr[-1], 0)))
{ {
if((TemplatePtr > TemplateBuffer && TemplatePtr[-1] == '<') && char *CommentStart = &Output.Ptr[-1];
(TemplatePtr+1 - TemplateBuffer < TemplateSize && TemplatePtr[1] == '-') && while(Template.Ptr - Template.Location < Template.Size)
(TemplatePtr+2 - TemplateBuffer < TemplateSize && TemplatePtr[2] == '-'))
{ {
char *CommentStart = &OutputPtr[-1]; if(!(StringsDifferT(HeaderTag, Template.Ptr, 0)))
while(TemplatePtr - TemplateBuffer < TemplateSize)
{ {
if(*TemplatePtr == '_') if(FoundHeader == TRUE)
{ {
if(!(StringsDifferT(HeaderTag, TemplatePtr, '\0'))) fprintf(stderr, "Template contains more than one <!-- __CINERA_HEADER__ --> tag\n");
{ free(Template.Location); free(Output.Location); hmml_free(&HMML); free(MemoryArena); return 1;
if(FoundHeader == TRUE)
{
fprintf(stderr, "Template contains more than one <!-- __CINERA_HEADER__ --> tag\n");
free(TemplateBuffer); free(OutputBuffer); hmml_free(&HMML); free(MemoryArena); return 1;
}
FoundHeader = TRUE;
OutputPtr = CommentStart;
OutputPtr += CopyStringNoFormat(OutputPtr, Header.Location);
while(TemplatePtr - TemplateBuffer < TemplateSize)
{
if(TemplatePtr[0] == '>' && TemplatePtr[-1] == '-' && TemplatePtr[-2] == '-')
{
break;
}
++TemplatePtr;
}
++TemplatePtr;
break;
}
else if(!(StringsDifferT(TitleTag, TemplatePtr, '\0')))
{
if(FoundTitle == TRUE)
{
fprintf(stderr, "Template contains more than one <!-- __CINERA_TITLE__ --> tag\n");
free(TemplateBuffer); free(OutputBuffer); hmml_free(&HMML); free(MemoryArena); return 1;
}
/* TODO(matt): Allow setting a Title format, e.g.
<-- __CINERA_TITLE__ "%s - HandmadeDev" -->
Where %s is replaced with the HMML.metadata.title
Perhaps it would be saner to do this in the config file
*/
FoundTitle = TRUE;
OutputPtr = CommentStart;
OutputPtr += CopyStringNoFormat(OutputPtr, Title.Location);
while(TemplatePtr - TemplateBuffer < TemplateSize)
{
if(TemplatePtr[0] == '>' && TemplatePtr[-1] == '-' && TemplatePtr[-2] == '-')
{
break;
}
++TemplatePtr;
}
++TemplatePtr;
break;
}
else if(!(StringsDifferT(PlayerTag, TemplatePtr, '\0')))
{
if(FoundPlayer == TRUE)
{
fprintf(stderr, "Template contains more than one <!-- __CINERA_PLAYER__ --> tag\n");
free(TemplateBuffer); free(OutputBuffer); hmml_free(&HMML); free(MemoryArena); return 1;
}
FoundPlayer = TRUE;
OutputPtr = CommentStart;
OutputPtr += CopyStringNoFormat(OutputPtr, Player.Location);
while(TemplatePtr - TemplateBuffer < TemplateSize)
{
if(TemplatePtr[0] == '>' && TemplatePtr[-1] == '-' && TemplatePtr[-2] == '-')
{
break;
}
++TemplatePtr;
}
++TemplatePtr;
break;
}
else if(!(StringsDifferT(ScriptTag, TemplatePtr, '\0')))
{
if(FoundScript == TRUE)
{
fprintf(stderr, "Template contains more than one <!-- __CINERA_SCRIPT__ --> tag\n");
free(TemplateBuffer); free(OutputBuffer); hmml_free(&HMML); free(MemoryArena); return 1;
}
FoundScript = TRUE;
OutputPtr = CommentStart;
OutputPtr += CopyStringNoFormat(OutputPtr, Script.Location);
while(TemplatePtr - TemplateBuffer < TemplateSize)
{
if(TemplatePtr[0] == '>' && TemplatePtr[-1] == '-' && TemplatePtr[-2] == '-')
{
break;
}
++TemplatePtr;
}
++TemplatePtr;
break;
}
} }
*OutputPtr++ = *TemplatePtr++; FoundHeader = TRUE;
Output.Ptr = CommentStart;
CopyBuffer(&Output, &Header);
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)))
else {
{ if(FoundTitle == TRUE)
*OutputPtr++ = *TemplatePtr++; {
fprintf(stderr, "Template contains more than one <!-- __CINERA_TITLE__ --> tag\n");
free(Template.Location); free(Output.Location); hmml_free(&HMML); free(MemoryArena); return 1;
}
/* TODO(matt): Allow setting a Title format, e.g.
<-- __CINERA_TITLE__ "%s - HandmadeDev" -->
Where %s is replaced with the HMML.metadata.title
Perhaps it would be saner to do this in the config file
*/
FoundTitle = TRUE;
Output.Ptr = CommentStart;
CopyBuffer(&Output, &Title);
while(Template.Ptr - Template.Location < Template.Size)
{
if(!StringsDifferT("-->", Template.Ptr, 0))
{
Template.Ptr += StringLength("-->");
printf("%c%c%c\n", Template.Ptr[-1], Template.Ptr[0], Template.Ptr[1]);
break;
}
++Template.Ptr;
}
break;
}
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); return 1;
}
FoundPlayer = TRUE;
Output.Ptr = CommentStart;
CopyBuffer(&Output, &Player);
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(FoundScript == TRUE)
{
fprintf(stderr, "Template contains more than one <!-- __CINERA_SCRIPT__ --> tag\n");
free(Template.Location); free(Output.Location); hmml_free(&HMML); free(MemoryArena); return 1;
}
FoundScript = TRUE;
Output.Ptr = CommentStart;
CopyBuffer(&Output, &Script);
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;
}
*Output.Ptr++ = *Template.Ptr++;
} }
} }
else else
{ {
*OutputPtr++ = *TemplatePtr++; *Output.Ptr++ = *Template.Ptr++;
} }
} }
@ -2545,9 +2535,9 @@ HMML.metadata.project);
FILE *OutFile; FILE *OutFile;
if(!(OutFile = fopen("out_integrated.html", "w"))) if(!(OutFile = fopen("out_integrated.html", "w")))
{ {
perror(Args[0]); free(TemplateBuffer); free(OutputBuffer); hmml_free(&HMML); free(MemoryArena); return 1; perror(Args[0]); free(Template.Location); free(Output.Location); hmml_free(&HMML); free(MemoryArena); return 1;
} }
fwrite(OutputBuffer, OutputPtr - OutputBuffer, 1, OutFile); fwrite(Output.Location, Output.Ptr - Output.Location, 1, OutFile);
fclose(OutFile); fclose(OutFile);
} }
else else
@ -2557,11 +2547,11 @@ HMML.metadata.project);
if(!FoundTitle) { fprintf(stderr, " <!-- __CINERA_TITLE__ -->\n"); } if(!FoundTitle) { fprintf(stderr, " <!-- __CINERA_TITLE__ -->\n"); }
if(!FoundPlayer) { fprintf(stderr, " <!-- __CINERA_PLAYER__ -->\n"); } if(!FoundPlayer) { fprintf(stderr, " <!-- __CINERA_PLAYER__ -->\n"); }
if(!FoundScript) { fprintf(stderr, " <!-- __CINERA_SCRIPT__ -->\n"); } if(!FoundScript) { fprintf(stderr, " <!-- __CINERA_SCRIPT__ -->\n"); }
free(TemplateBuffer); free(OutputBuffer); hmml_free(&HMML); free(MemoryArena); return 1; free(Template.Location); free(Output.Location); hmml_free(&HMML); free(MemoryArena); return 1;
} }
free(TemplateBuffer); free(Template.Location);
free(OutputBuffer); free(Output.Location);
} }
else else
{ {
@ -2573,10 +2563,13 @@ HMML.metadata.project);
//NOTE(matt): Here is where we do all our CopyBuffer() calls //NOTE(matt): Here is where we do all our CopyBuffer() calls
CopyBuffer(&Master, &Header); CopyBuffer(&Master, &Header);
CopyStringToBuffer(&Master, CopyStringToBuffer(&Master,
"\n"
" </head>\n" " </head>\n"
" <body>\n"); " <body>\n");
CopyBuffer(&Master, &Title); CopyBuffer(&Master, &Title);
CopyStringToBuffer(&Master, "\n");
CopyBuffer(&Master, &Player); CopyBuffer(&Master, &Player);
CopyStringToBuffer(&Master, "\n");
CopyBuffer(&Master, &Script); CopyBuffer(&Master, &Script);
CopyStringToBuffer(&Master, "\n"); CopyStringToBuffer(&Master, "\n");
// //