Commit 03ccba43 authored by Matt Mascarenhas's avatar Matt Mascarenhas

hmml_to_html.c: Integrate keeping comments [#25]

It straight up replaces <!-- CINERA_* --> comments with our content,
while preserving other comments
parent dd9c844b
......@@ -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,120 +2420,113 @@ 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(!(StringsDifferT(HeaderTag, Template.Ptr, 0)))
{
if(*TemplatePtr == '_')
if(FoundHeader == TRUE)
{
fprintf(stderr, "Template contains more than one <!-- __CINERA_HEADER__ --> tag\n");
free(Template.Location); free(Output.Location); hmml_free(&HMML); free(MemoryArena); return 1;
}
FoundHeader = TRUE;
Output.Ptr = CommentStart;
CopyBuffer(&Output, &Header);
while(Template.Ptr - Template.Location < Template.Size)
{
if(!(StringsDifferT(HeaderTag, TemplatePtr, '\0')))
if(!StringsDifferT("-->", 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;
}
FoundHeader = TRUE;
OutputPtr = CommentStart;
OutputPtr += CopyStringNoFormat(OutputPtr, Header.Location);
while(TemplatePtr - TemplateBuffer < TemplateSize)
{
if(TemplatePtr[0] == '>' && TemplatePtr[-1] == '-' && TemplatePtr[-2] == '-')
{
break;
}
++TemplatePtr;
}
++TemplatePtr;
Template.Ptr += StringLength("-->");
break;
}
else if(!(StringsDifferT(TitleTag, TemplatePtr, '\0')))
++Template.Ptr;
}
break;
}
else if(!(StringsDifferT(TitleTag, Template.Ptr, 0)))
{
if(FoundTitle == TRUE)
{
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))
{
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;
Template.Ptr += StringLength("-->");
printf("%c%c%c\n", Template.Ptr[-1], Template.Ptr[0], Template.Ptr[1]);
break;
}
else if(!(StringsDifferT(PlayerTag, TemplatePtr, '\0')))
++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))
{
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;
Template.Ptr += StringLength("-->");
break;
}
else if(!(StringsDifferT(ScriptTag, TemplatePtr, '\0')))
++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))
{
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;
Template.Ptr += StringLength("-->");
break;
}
++Template.Ptr;
}
*OutputPtr++ = *TemplatePtr++;
break;
}
}
else
{
*OutputPtr++ = *TemplatePtr++;
else if(!StringsDifferT("-->", Template.Ptr, 0))
{
break;
}
*Output.Ptr++ = *Template.Ptr++;
}
}
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");
//
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment