hmml_to_html.c: Category styling

Also upgrade hmml.a for lower-cased markers
This commit is contained in:
Matt Mascarenhas 2017-03-25 02:07:55 +00:00
parent 769c79b0af
commit 30df421b9e
2 changed files with 148 additions and 11 deletions

Binary file not shown.

View File

@ -1,6 +1,7 @@
#if 0 #if 0
ctime -begin ${0%.*}.ctm ctime -begin ${0%.*}.ctm
clang -g -Wall -Wno-unused-variable -fsanitize=address -std=c99 $0 -o ${0%.*} hmml.a #gcc -g -Wall -Wno-unused-variable -fsanitize=address -std=c99 $0 -o ${0%.*} hmml.a
gcc -g -Wall -fsanitize=address -std=c99 $0 -o ${0%.*} hmml.a
ctime -end ${0%.*}.ctm ctime -end ${0%.*}.ctm
exit exit
#endif #endif
@ -28,6 +29,8 @@ typedef struct
char *RefTitle; char *RefTitle;
} ref_info; } ref_info;
#define ArrayCount(A) sizeof(A)/sizeof(*(A))
void void
ClaimBuffer(char *MemoryArena, int *ClaimedMemory, buffer *Buffer, int Size) ClaimBuffer(char *MemoryArena, int *ClaimedMemory, buffer *Buffer, int Size)
{ {
@ -158,6 +161,48 @@ StringToColourHash(char *String)
return Result / i; return Result / i;
} }
int
StringLength(char *String)
{
int i = 0;
while(String[i])
{
++i;
}
return i;
}
char *
SanitisePunctuation(char *String)
{
char *Ptr = String;
while(*Ptr)
{
if(*Ptr == ' ')
{
*Ptr = '_';
}
if((*Ptr < '0' || *Ptr > '9') &&
(*Ptr < 'a' || *Ptr > 'z') &&
(*Ptr < 'A' || *Ptr > 'Z'))
{
*Ptr = '-';
}
++Ptr;
}
return String;
}
char *CategoryMedium[] =
{
"blackboard",
"Blackboard",
"research",
"Research",
"run",
"Run",
};
int int
main(int ArgC, char **Args) main(int ArgC, char **Args)
{ {
@ -224,6 +269,7 @@ main(int ArgC, char **Args)
for(int AnnotationIndex = 0; AnnotationIndex < HMML.annotation_count; ++AnnotationIndex) for(int AnnotationIndex = 0; AnnotationIndex < HMML.annotation_count; ++AnnotationIndex)
{ {
bool HasCategory = FALSE;
ClaimBuffer(MemoryArena, &ClaimedMemory, &AnnotationHeader, 256); ClaimBuffer(MemoryArena, &ClaimedMemory, &AnnotationHeader, 256);
ClaimBuffer(MemoryArena, &ClaimedMemory, &AnnotationClass, 128); ClaimBuffer(MemoryArena, &ClaimedMemory, &AnnotationClass, 128);
ClaimBuffer(MemoryArena, &ClaimedMemory, &Text, 1024 * 4); ClaimBuffer(MemoryArena, &ClaimedMemory, &Text, 1024 * 4);
@ -235,7 +281,6 @@ TimecodeToSeconds(HMML.annotations[AnnotationIndex].time));
CopyStringToBuffer(&AnnotationClass, CopyStringToBuffer(&AnnotationClass,
" class=\"marker"); " class=\"marker");
#if 0
if(HMML.annotations[AnnotationIndex].author) if(HMML.annotations[AnnotationIndex].author)
{ {
CopyStringToBuffer(&AnnotationClass, " authored"); CopyStringToBuffer(&AnnotationClass, " authored");
@ -244,18 +289,102 @@ TimecodeToSeconds(HMML.annotations[AnnotationIndex].time));
StringToColourHash(HMML.annotations[AnnotationIndex].author), StringToColourHash(HMML.annotations[AnnotationIndex].author),
HMML.annotations[AnnotationIndex].author); HMML.annotations[AnnotationIndex].author);
} }
#endif
InPtr = HMML.annotations[AnnotationIndex].text;
int MarkerIndex = 0, RefIndex = 0;
while(*InPtr)
{
if(MarkerIndex < HMML.annotations[AnnotationIndex].marker_count &&
InPtr - HMML.annotations[AnnotationIndex].text == HMML.annotations[AnnotationIndex].markers[MarkerIndex].offset)
{
char *Readable = HMML.annotations[AnnotationIndex].markers[MarkerIndex].parameter
? HMML.annotations[AnnotationIndex].markers[MarkerIndex].parameter
: HMML.annotations[AnnotationIndex].markers[MarkerIndex].marker;
if(HMML.annotations[AnnotationIndex].markers[MarkerIndex].type == HMML_MEMBER)
{
CopyStringToBuffer(&Text,
"<a href=\"https://handmade.network/m/%s\" target=\"blank\" style=\"color: #%X; text-decoration: none\">%s</a>",
HMML.annotations[AnnotationIndex].markers[MarkerIndex].marker,
StringToColourHash(HMML.annotations[AnnotationIndex].markers[MarkerIndex].marker),
Readable);
InPtr += StringLength(Readable);
}
else if(HMML.annotations[AnnotationIndex].markers[MarkerIndex].type == HMML_PROJECT)
{
CopyStringToBuffer(&Text,
"<a href=\"https://%s.handmade.network/\" target=\"blank\" style=\"color: #%X; text-decoration: none\">%s</a>",
HMML.annotations[AnnotationIndex].markers[MarkerIndex].marker,
StringToColourHash(HMML.annotations[AnnotationIndex].markers[MarkerIndex].marker),
Readable);
InPtr += StringLength(Readable);
}
else if(HMML.annotations[AnnotationIndex].markers[MarkerIndex].type == HMML_CATEGORY)
{
for(int i = 0; i < ArrayCount(CategoryMedium); ++i)
{
if(!StringsDiffer(CategoryMedium[i], HMML.annotations[AnnotationIndex].markers[MarkerIndex].marker))
{
CopyStringToBuffer(&AnnotationClass, " %s", SanitisePunctuation(HMML.annotations[AnnotationIndex].markers[MarkerIndex].marker));
goto NextCategory;
}
}
if(!HasCategory)
{
ClaimBuffer(MemoryArena, &ClaimedMemory, &Category, 256);
CopyStringToBuffer(&Category, "<span class=\"categories\"><div class=\"category %s\"></div>",
SanitisePunctuation(HMML.annotations[AnnotationIndex].markers[MarkerIndex].marker));
HasCategory = TRUE;
}
else
{
CopyStringToBuffer(&Category, "<div class=\"category %s\"></div>",
SanitisePunctuation(HMML.annotations[AnnotationIndex].markers[MarkerIndex].marker));
}
CopyStringToBuffer(&AnnotationClass, " cat_%s",
SanitisePunctuation(HMML.annotations[AnnotationIndex].markers[MarkerIndex].marker));
}
NextCategory: ++MarkerIndex;
}
if(RefIndex < HMML.annotations[AnnotationIndex].reference_count &&
InPtr - HMML.annotations[AnnotationIndex].text == HMML.annotations[AnnotationIndex].references[RefIndex].offset)
{
++RefIndex;
}
if(*InPtr)
{
*Text.Ptr++ = *InPtr++;
}
}
while(MarkerIndex < HMML.annotations[AnnotationIndex].marker_count)
{
//TODO(matt): Replace this CopyStringToBuffer() with real stuff! for(int i = 0; i < ArrayCount(CategoryMedium); ++i)
CopyStringToBuffer(&Text, HMML.annotations[AnnotationIndex].text); {
if(!StringsDiffer(CategoryMedium[i], HMML.annotations[AnnotationIndex].markers[MarkerIndex].marker))
{
CopyStringToBuffer(&AnnotationClass, " %s", SanitisePunctuation(HMML.annotations[AnnotationIndex].markers[MarkerIndex].marker));
goto NextCategoryInNode;
}
}
if(!HasCategory)
{
ClaimBuffer(MemoryArena, &ClaimedMemory, &Category, 256);
CopyStringToBuffer(&Category, "<span class=\"categories\"><div class=\"category %s\"></div>",
SanitisePunctuation(HMML.annotations[AnnotationIndex].markers[MarkerIndex].marker));
HasCategory = TRUE;
}
else
{
CopyStringToBuffer(&Category, "<div class=\"category %s\"></div>",
SanitisePunctuation(HMML.annotations[AnnotationIndex].markers[MarkerIndex].marker));
}
CopyStringToBuffer(&AnnotationClass, " cat_%s",
SanitisePunctuation(HMML.annotations[AnnotationIndex].markers[MarkerIndex].marker));
NextCategoryInNode: ++MarkerIndex;
}
CopyStringToBuffer(&AnnotationClass, "\""); CopyStringToBuffer(&AnnotationClass, "\"");
CopyBuffer(&AnnotationClass, &AnnotationHeader); CopyBuffer(&AnnotationClass, &AnnotationHeader);
@ -268,6 +397,14 @@ HMML.annotations[AnnotationIndex].author);
" <div class=\"content\"><span class=\"timecode\">%s</span>", " <div class=\"content\"><span class=\"timecode\">%s</span>",
HMML.annotations[AnnotationIndex].time); HMML.annotations[AnnotationIndex].time);
if(HasCategory)
{
CopyStringToBuffer(&Category, "</span>");
CopyBuffer(&Category, &Text);
}
*Text.Ptr = '\0';
CopyBuffer(&Text, &Annotation); CopyBuffer(&Text, &Annotation);
CopyStringToBuffer(&Annotation, "</div>\n" CopyStringToBuffer(&Annotation, "</div>\n"
@ -309,7 +446,7 @@ HMML.annotations[AnnotationIndex].time);
ClaimBuffer(MemoryArena, &ClaimedMemory, &Master, 1024 * 512); ClaimBuffer(MemoryArena, &ClaimedMemory, &Master, 1024 * 512);
CopyStringToBuffer(&Master, CopyStringToBuffer(&Master,
"<!DOCTYPE html>\n" "<html>\n"
" <head>\n" " <head>\n"
" <meta charset=\"UTF-8\">\n" " <meta charset=\"UTF-8\">\n"
"\n" "\n"