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
StringsDifferT(char *A, // NOTE(matt): Null-terminated string
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);
@ -2169,11 +2169,11 @@ CategoryMedium[j][2]
" </div>\n"
" </div>\n"
" </div>\n");
" </div>");
CopyStringToBuffer(&Player,
" </div>\n"
" </div>\n");
" </div>");
// TODO(matt): Maybe do something about indentation levels
CopyStringToBuffer(&Header,
@ -2186,12 +2186,11 @@ CategoryMedium[j][2]
" <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=\"%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);
CopyStringToBuffer(&Script,
" <script>\n"
"\n"
" <script type=\"text/javascript\">\n"
"var menuState = [];\n");
CopyStringToBuffer(&Script,
@ -2378,8 +2377,7 @@ HMML.metadata.project);
" setDotLightness(topicDots[i]);\n"
"}\n"
" </script>");
//
//NOTE(matt): Collate the buffers!
#if DEBUG
printf("Buffer Collation\n\n");
#endif
@ -2394,25 +2392,24 @@ HMML.metadata.project);
}
fseek(TemplateFile, 0, SEEK_END);
int TemplateSize = ftell(TemplateFile);
buffer Template;
Template.ID = "Template";
Template.Size = ftell(TemplateFile);
fseek(TemplateFile, 0, SEEK_SET);
char *TemplateBuffer;
if(!(TemplateBuffer = malloc(TemplateSize)))
if(!(Template.Location = malloc(Template.Size)))
{
perror(Args[0]); hmml_free(&HMML); free(MemoryArena); return 1;
}
fread(TemplateBuffer, TemplateSize, 1, TemplateFile);
fread(Template.Location, Template.Size, 1, TemplateFile);
fclose(TemplateFile);
char *OutputBuffer;
if(!(OutputBuffer = malloc(TemplateSize + Master.Size)))
buffer Output;
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 *TitleTag = "__CINERA_TITLE__";
char *PlayerTag = "__CINERA_PLAYER__";
@ -2423,46 +2420,40 @@ HMML.metadata.project);
bool FoundPlayer = 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] == '<') &&
(TemplatePtr+1 - TemplateBuffer < TemplateSize && TemplatePtr[1] == '-') &&
(TemplatePtr+2 - TemplateBuffer < TemplateSize && TemplatePtr[2] == '-'))
char *CommentStart = &Output.Ptr[-1];
while(Template.Ptr - Template.Location < Template.Size)
{
char *CommentStart = &OutputPtr[-1];
while(TemplatePtr - TemplateBuffer < TemplateSize)
{
if(*TemplatePtr == '_')
{
if(!(StringsDifferT(HeaderTag, TemplatePtr, '\0')))
if(!(StringsDifferT(HeaderTag, Template.Ptr, 0)))
{
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;
free(Template.Location); free(Output.Location); hmml_free(&HMML); free(MemoryArena); return 1;
}
FoundHeader = TRUE;
OutputPtr = CommentStart;
OutputPtr += CopyStringNoFormat(OutputPtr, Header.Location);
while(TemplatePtr - TemplateBuffer < TemplateSize)
Output.Ptr = CommentStart;
CopyBuffer(&Output, &Header);
while(Template.Ptr - Template.Location < Template.Size)
{
if(TemplatePtr[0] == '>' && TemplatePtr[-1] == '-' && TemplatePtr[-2] == '-')
if(!StringsDifferT("-->", Template.Ptr, 0))
{
Template.Ptr += StringLength("-->");
break;
}
++TemplatePtr;
++Template.Ptr;
}
++TemplatePtr;
break;
}
else if(!(StringsDifferT(TitleTag, TemplatePtr, '\0')))
else if(!(StringsDifferT(TitleTag, Template.Ptr, 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;
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" -->
@ -2470,73 +2461,72 @@ HMML.metadata.project);
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)
Output.Ptr = CommentStart;
CopyBuffer(&Output, &Title);
while(Template.Ptr - Template.Location < Template.Size)
{
if(TemplatePtr[0] == '>' && TemplatePtr[-1] == '-' && TemplatePtr[-2] == '-')
if(!StringsDifferT("-->", Template.Ptr, 0))
{
Template.Ptr += StringLength("-->");
printf("%c%c%c\n", Template.Ptr[-1], Template.Ptr[0], Template.Ptr[1]);
break;
}
++TemplatePtr;
++Template.Ptr;
}
++TemplatePtr;
break;
}
else if(!(StringsDifferT(PlayerTag, TemplatePtr, '\0')))
else if(!(StringsDifferT(PlayerTag, Template.Ptr, 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;
free(Template.Location); free(Output.Location); hmml_free(&HMML); free(MemoryArena); return 1;
}
FoundPlayer = TRUE;
OutputPtr = CommentStart;
OutputPtr += CopyStringNoFormat(OutputPtr, Player.Location);
while(TemplatePtr - TemplateBuffer < TemplateSize)
Output.Ptr = CommentStart;
CopyBuffer(&Output, &Player);
while(Template.Ptr - Template.Location < Template.Size)
{
if(TemplatePtr[0] == '>' && TemplatePtr[-1] == '-' && TemplatePtr[-2] == '-')
if(!StringsDifferT("-->", Template.Ptr, 0))
{
Template.Ptr += StringLength("-->");
break;
}
++TemplatePtr;
++Template.Ptr;
}
++TemplatePtr;
break;
}
else if(!(StringsDifferT(ScriptTag, TemplatePtr, '\0')))
else if(!(StringsDifferT(ScriptTag, Template.Ptr, 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;
free(Template.Location); free(Output.Location); hmml_free(&HMML); free(MemoryArena); return 1;
}
FoundScript = TRUE;
OutputPtr = CommentStart;
OutputPtr += CopyStringNoFormat(OutputPtr, Script.Location);
while(TemplatePtr - TemplateBuffer < TemplateSize)
Output.Ptr = CommentStart;
CopyBuffer(&Output, &Script);
while(Template.Ptr - Template.Location < Template.Size)
{
if(TemplatePtr[0] == '>' && TemplatePtr[-1] == '-' && TemplatePtr[-2] == '-')
if(!StringsDifferT("-->", Template.Ptr, 0))
{
Template.Ptr += StringLength("-->");
break;
}
++Template.Ptr;
}
break;
}
else if(!StringsDifferT("-->", Template.Ptr, 0))
{
break;
}
++TemplatePtr;
}
++TemplatePtr;
break;
}
}
*OutputPtr++ = *TemplatePtr++;
*Output.Ptr++ = *Template.Ptr++;
}
}
else
{
*OutputPtr++ = *TemplatePtr++;
}
}
else
{
*OutputPtr++ = *TemplatePtr++;
*Output.Ptr++ = *Template.Ptr++;
}
}
@ -2545,9 +2535,9 @@ HMML.metadata.project);
FILE *OutFile;
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);
}
else
@ -2557,11 +2547,11 @@ HMML.metadata.project);
if(!FoundTitle) { fprintf(stderr, " <!-- __CINERA_TITLE__ -->\n"); }
if(!FoundPlayer) { fprintf(stderr, " <!-- __CINERA_PLAYER__ -->\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(OutputBuffer);
free(Template.Location);
free(Output.Location);
}
else
{
@ -2573,10 +2563,13 @@ HMML.metadata.project);
//NOTE(matt): Here is where we do all our CopyBuffer() calls
CopyBuffer(&Master, &Header);
CopyStringToBuffer(&Master,
"\n"
" </head>\n"
" <body>\n");
CopyBuffer(&Master, &Title);
CopyStringToBuffer(&Master, "\n");
CopyBuffer(&Master, &Player);
CopyStringToBuffer(&Master, "\n");
CopyBuffer(&Master, &Script);
CopyStringToBuffer(&Master, "\n");
//