hmml_to_html.c: Introduce BuildReference() [#16]

Also increase saturation of generated colours
This commit is contained in:
Matt Mascarenhas 2017-04-19 02:10:45 +01:00
parent 6925dfb565
commit ba13066d63
1 changed files with 131 additions and 51 deletions

View File

@ -77,8 +77,6 @@ ParseRef(HMML_Reference RefInput)
}
#endif
// TODO(matt): MakeReference()
int
TimecodeToSeconds(char *Timecode)
{
@ -121,16 +119,14 @@ CopyBuffer(buffer *Dest, buffer *Src)
}
}
__attribute__ ((format (printf, 2, 3)))
void
CopyString(char Dest[], char *Src)
CopyString(char Dest[], char *Format, ...)
{
int i = 0;
while(*Src)
{
Dest[i] = *Src++;
++i;
}
Dest[i] = '\0';
va_list Args;
va_start(Args, Format);
vsprintf(Dest, Format, Args);
va_end(Args);
}
__attribute__ ((format (printf, 2, 3)))
@ -164,27 +160,22 @@ typedef struct
hsl_colour
CharToColour(char Char)
{
// Hue between 0 - 256
// Saturation between 0 - 100
// Lightness = 74
// hsl(120, 100%, 50%)
hsl_colour Colour;
if(Char >= 'a' && Char <= 'z')
{
Colour.Hue = (((float)Char - 'a') / ('z' - 'a') * 256);
Colour.Saturation = (((float)Char - 'a') / ('z' - 'a') * 100);
Colour.Hue = (((float)Char - 'a') / ('z' - 'a') * 360);
Colour.Saturation = (((float)Char - 'a') / ('z' - 'a') * 26 + 74);
}
else if(Char >= 'A' && Char <= 'Z')
{
Colour.Hue = (((float)Char - 'A') / ('Z' - 'A') * 256);
Colour.Saturation = (((float)Char - 'A') / ('Z' - 'A') * 100);
Colour.Hue = (((float)Char - 'A') / ('Z' - 'A') * 360);
Colour.Saturation = (((float)Char - 'A') / ('Z' - 'A') * 26 + 74);
}
else if(Char >= '0' && Char <= '9')
{
Colour.Hue = (((float)Char - '0') / ('9' - '0') * 256);
Colour.Saturation = (((float)Char - '0') / ('9' - '0') * 100);
Colour.Hue = (((float)Char - '0') / ('9' - '0') * 360);
Colour.Saturation = (((float)Char - '0') / ('9' - '0') * 26 + 74);
}
else
{
@ -207,9 +198,8 @@ StringToColourHash(buffer *Buffer, char *String)
Colour.Saturation += CharToColour(String[i]).Saturation;
}
// hsl(120, 100%, 50%)
Colour.Hue = Colour.Hue % 360;
Colour.Saturation = Colour.Saturation % 100;
Colour.Saturation = Colour.Saturation % 26 + 74;
Buffer->Ptr = Buffer->Location;
CopyStringToBuffer(Buffer, "hsl(%d, %d%%, %d%%)", Colour.Hue, Colour.Saturation, Colour.Lightness);
return(Buffer->Location);
@ -256,6 +246,46 @@ char *CategoryMedium[] =
"run",
};
void
BuildReference(ref_info *ReferencesArray, int RefIdentifier, int UniqueRefs, HMML_Reference Ref, HMML_Annotation Anno)
{
if(Ref.isbn)
{
CopyString(ReferencesArray[UniqueRefs].ID, Ref.isbn);
// NOTE(matt): I could probably do with Asserting some of this stuff
CopyString(ReferencesArray[UniqueRefs].Source, Ref.author);
CopyString(ReferencesArray[UniqueRefs].RefTitle, Ref.title);
if(Ref.url)
{
CopyString(ReferencesArray[UniqueRefs].URL, Ref.url);
}
else
{
// TODO(matt): Find better ISBN search site?
CopyString(ReferencesArray[UniqueRefs].URL, "http://www.isbnsearch.org/isbn/%s", Ref.isbn);
}
}
else if(Ref.url)
{
CopyString(ReferencesArray[UniqueRefs].ID, Ref.url); // NOTE(matt): Normalise?
if(Ref.page)
{
CopyString(ReferencesArray[UniqueRefs].Source, Ref.site);
CopyString(ReferencesArray[UniqueRefs].RefTitle, Ref.page);
}
else
{
// NOTE(matt): Empty Source
CopyString(ReferencesArray[UniqueRefs].RefTitle, Ref.site);
}
CopyString(ReferencesArray[UniqueRefs].URL, Ref.url);
}
CopyString(ReferencesArray[UniqueRefs].Identifier[ReferencesArray[UniqueRefs].IdentifierCount].Timecode, Anno.time);
ReferencesArray[UniqueRefs].Identifier[ReferencesArray[UniqueRefs].IdentifierCount].Identifier = RefIdentifier;
}
void
BuildCategories(buffer *AnnotationClass, buffer *Category, int *MarkerIndex, bool *HasCategory, char *Marker)
{
@ -497,14 +527,8 @@ Readable);
" <div class=\"mouse_catcher\"></div>\n"
" <div class=\"refs\">\n");
CopyString(ReferencesArray[RefIdentifier - 1].ID, CurrentRef->editor);
CopyString(ReferencesArray[RefIdentifier - 1].RefTitle, "Title");
CopyString(ReferencesArray[RefIdentifier - 1].URL, "http://example.com/");
CopyString(ReferencesArray[RefIdentifier - 1].Source, "Source");
CopyString(ReferencesArray[RefIdentifier - 1].Identifier[ReferencesArray[RefIdentifier - 1].IdentifierCount].Timecode, Anno->time);
ReferencesArray[RefIdentifier - 1].Identifier[ReferencesArray[RefIdentifier - 1].IdentifierCount].Identifier = RefIdentifier;
BuildReference(ReferencesArray, RefIdentifier, UniqueRefs, *CurrentRef, *Anno);
++ReferencesArray[RefIdentifier - 1].IdentifierCount;
++UniqueRefs;
HasReferenceMenu = TRUE;
@ -513,7 +537,9 @@ Readable);
{
for(int i = 0; i < UniqueRefs; ++i)
{
if(!StringsDiffer(CurrentRef->editor, ReferencesArray[i].ID))
if(CurrentRef->isbn)
{
if(!StringsDiffer(CurrentRef->isbn, ReferencesArray[i].ID))
{
CopyString(ReferencesArray[i].Identifier[ReferencesArray[i].IdentifierCount].Timecode, Anno->time);
ReferencesArray[i].Identifier[ReferencesArray[i].IdentifierCount].Identifier = RefIdentifier;
@ -521,13 +547,24 @@ Readable);
goto AppendedIdentifier;
}
}
else if(CurrentRef->url)
{
if(!StringsDiffer(CurrentRef->url, ReferencesArray[i].ID))
{
CopyString(ReferencesArray[i].Identifier[ReferencesArray[i].IdentifierCount].Timecode, Anno->time);
ReferencesArray[i].Identifier[ReferencesArray[i].IdentifierCount].Identifier = RefIdentifier;
++ReferencesArray[i].IdentifierCount;
goto AppendedIdentifier;
}
}
else
{
fprintf(stderr, "%s:%d: Reference must have an ISBN or URL\n", Args[FileIndex], Anno->line);
return 1;
}
}
CopyString(ReferencesArray[UniqueRefs].ID, CurrentRef->editor);
CopyString(ReferencesArray[UniqueRefs].RefTitle, "Title");
CopyString(ReferencesArray[UniqueRefs].URL, "http://example.com/");
CopyString(ReferencesArray[UniqueRefs].Source, "Source");
CopyString(ReferencesArray[UniqueRefs].Identifier[ReferencesArray[UniqueRefs].IdentifierCount].Timecode, Anno->time);
ReferencesArray[UniqueRefs].Identifier[ReferencesArray[UniqueRefs].IdentifierCount].Identifier = RefIdentifier;
BuildReference(ReferencesArray, RefIdentifier, UniqueRefs, *CurrentRef, *Anno);
++ReferencesArray[UniqueRefs].IdentifierCount;
++UniqueRefs;
}
@ -535,13 +572,41 @@ AppendedIdentifier:
if(!HasReference)
{
ClaimBuffer(MemoryArena, &ClaimedMemory, &AnnotationData, 128);
CopyStringToBuffer(&AnnotationData, " data-ref=\"%s", CurrentRef->editor);
if(CurrentRef->isbn)
{
CopyStringToBuffer(&AnnotationData, " data-ref=\"%s", CurrentRef->isbn);
}
else if(CurrentRef->url)
{
CopyStringToBuffer(&AnnotationData, " data-ref=\"%s", CurrentRef->url);
}
else
{
fprintf(stderr, "%s:%d: Reference must have an ISBN or URL\n", Args[FileIndex], Anno->line);
free(MemoryArena);
hmml_free(&HMML);
return 1;
}
HasReference = TRUE;
}
else
{
CopyStringToBuffer(&AnnotationData, ",%s", CurrentRef->editor);
if(CurrentRef->isbn)
{
CopyStringToBuffer(&AnnotationData, ",%s", CurrentRef->isbn);
}
else if(CurrentRef->url)
{
CopyStringToBuffer(&AnnotationData, ",%s", CurrentRef->url);
}
else
{
fprintf(stderr, "%s:%d: Reference must have an ISBN or URL", Args[0], Anno->line);
free(MemoryArena);
hmml_free(&HMML);
return 1;
}
}
CopyStringToBuffer(&Text, "<sup>%d</sup>", RefIdentifier);
@ -684,18 +749,33 @@ Anno->time);
{
CopyStringToBuffer(&ReferenceMenu,
" <a data-id=\"%s\" href=\"%s\" target=\"_blank\" class=\"ref\">\n"
" <span class=\"ref_content\">\n"
" <span class=\"ref_content\">\n",
ReferencesArray[i].ID,
ReferencesArray[i].URL);
if(ReferencesArray[i].Source)
{
CopyStringToBuffer(&ReferenceMenu,
" <div class=\"source\">%s</div>\n"
" <div class=\"ref_title\">%s</div>\n"
" </span>\n"
" <div class=\"ref_title\">%s</div>\n",
ReferencesArray[i].Source,
ReferencesArray[i].RefTitle);
}
else
{
CopyStringToBuffer(&ReferenceMenu,
" <div class=\"ref_title\">%s</div>\n",
ReferencesArray[i].Source);
}
CopyStringToBuffer(&ReferenceMenu,
" </span>\n");
// TODO(matt): Fill the div class="ref_indices" with <= 3 span
// class="ref_index" and ensure to put these <=3 spans on the same line without
// a space between them
" <div class=\"ref_indices\">\n ",
ReferencesArray[i].ID,
"http://example.com/",
"Source",
"Title");
CopyStringToBuffer(&ReferenceMenu,
" <div class=\"ref_indices\">\n ");
for(int j = 0; j < ReferencesArray[i].IdentifierCount; ++j)
{
CopyStringToBuffer(&ReferenceMenu,