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 #endif
// TODO(matt): MakeReference()
int int
TimecodeToSeconds(char *Timecode) TimecodeToSeconds(char *Timecode)
{ {
@ -121,16 +119,14 @@ CopyBuffer(buffer *Dest, buffer *Src)
} }
} }
__attribute__ ((format (printf, 2, 3)))
void void
CopyString(char Dest[], char *Src) CopyString(char Dest[], char *Format, ...)
{ {
int i = 0; va_list Args;
while(*Src) va_start(Args, Format);
{ vsprintf(Dest, Format, Args);
Dest[i] = *Src++; va_end(Args);
++i;
}
Dest[i] = '\0';
} }
__attribute__ ((format (printf, 2, 3))) __attribute__ ((format (printf, 2, 3)))
@ -164,27 +160,22 @@ typedef struct
hsl_colour hsl_colour
CharToColour(char Char) CharToColour(char Char)
{ {
// Hue between 0 - 256
// Saturation between 0 - 100
// Lightness = 74
// hsl(120, 100%, 50%)
hsl_colour Colour; hsl_colour Colour;
if(Char >= 'a' && Char <= 'z') if(Char >= 'a' && Char <= 'z')
{ {
Colour.Hue = (((float)Char - 'a') / ('z' - 'a') * 256); Colour.Hue = (((float)Char - 'a') / ('z' - 'a') * 360);
Colour.Saturation = (((float)Char - 'a') / ('z' - 'a') * 100); Colour.Saturation = (((float)Char - 'a') / ('z' - 'a') * 26 + 74);
} }
else if(Char >= 'A' && Char <= 'Z') else if(Char >= 'A' && Char <= 'Z')
{ {
Colour.Hue = (((float)Char - 'A') / ('Z' - 'A') * 256); Colour.Hue = (((float)Char - 'A') / ('Z' - 'A') * 360);
Colour.Saturation = (((float)Char - 'A') / ('Z' - 'A') * 100); Colour.Saturation = (((float)Char - 'A') / ('Z' - 'A') * 26 + 74);
} }
else if(Char >= '0' && Char <= '9') else if(Char >= '0' && Char <= '9')
{ {
Colour.Hue = (((float)Char - '0') / ('9' - '0') * 256); Colour.Hue = (((float)Char - '0') / ('9' - '0') * 360);
Colour.Saturation = (((float)Char - '0') / ('9' - '0') * 100); Colour.Saturation = (((float)Char - '0') / ('9' - '0') * 26 + 74);
} }
else else
{ {
@ -207,9 +198,8 @@ StringToColourHash(buffer *Buffer, char *String)
Colour.Saturation += CharToColour(String[i]).Saturation; Colour.Saturation += CharToColour(String[i]).Saturation;
} }
// hsl(120, 100%, 50%)
Colour.Hue = Colour.Hue % 360; Colour.Hue = Colour.Hue % 360;
Colour.Saturation = Colour.Saturation % 100; Colour.Saturation = Colour.Saturation % 26 + 74;
Buffer->Ptr = Buffer->Location; Buffer->Ptr = Buffer->Location;
CopyStringToBuffer(Buffer, "hsl(%d, %d%%, %d%%)", Colour.Hue, Colour.Saturation, Colour.Lightness); CopyStringToBuffer(Buffer, "hsl(%d, %d%%, %d%%)", Colour.Hue, Colour.Saturation, Colour.Lightness);
return(Buffer->Location); return(Buffer->Location);
@ -256,6 +246,46 @@ char *CategoryMedium[] =
"run", "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 void
BuildCategories(buffer *AnnotationClass, buffer *Category, int *MarkerIndex, bool *HasCategory, char *Marker) BuildCategories(buffer *AnnotationClass, buffer *Category, int *MarkerIndex, bool *HasCategory, char *Marker)
{ {
@ -497,14 +527,8 @@ Readable);
" <div class=\"mouse_catcher\"></div>\n" " <div class=\"mouse_catcher\"></div>\n"
" <div class=\"refs\">\n"); " <div class=\"refs\">\n");
CopyString(ReferencesArray[RefIdentifier - 1].ID, CurrentRef->editor); BuildReference(ReferencesArray, RefIdentifier, UniqueRefs, *CurrentRef, *Anno);
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;
++ReferencesArray[RefIdentifier - 1].IdentifierCount; ++ReferencesArray[RefIdentifier - 1].IdentifierCount;
++UniqueRefs; ++UniqueRefs;
HasReferenceMenu = TRUE; HasReferenceMenu = TRUE;
@ -513,21 +537,34 @@ Readable);
{ {
for(int i = 0; i < UniqueRefs; ++i) for(int i = 0; i < UniqueRefs; ++i)
{ {
if(!StringsDiffer(CurrentRef->editor, ReferencesArray[i].ID)) if(CurrentRef->isbn)
{ {
CopyString(ReferencesArray[i].Identifier[ReferencesArray[i].IdentifierCount].Timecode, Anno->time); if(!StringsDiffer(CurrentRef->isbn, ReferencesArray[i].ID))
ReferencesArray[i].Identifier[ReferencesArray[i].IdentifierCount].Identifier = RefIdentifier; {
++ReferencesArray[i].IdentifierCount; CopyString(ReferencesArray[i].Identifier[ReferencesArray[i].IdentifierCount].Timecode, Anno->time);
goto AppendedIdentifier; ReferencesArray[i].Identifier[ReferencesArray[i].IdentifierCount].Identifier = RefIdentifier;
++ReferencesArray[i].IdentifierCount;
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); BuildReference(ReferencesArray, RefIdentifier, UniqueRefs, *CurrentRef, *Anno);
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;
++ReferencesArray[UniqueRefs].IdentifierCount; ++ReferencesArray[UniqueRefs].IdentifierCount;
++UniqueRefs; ++UniqueRefs;
} }
@ -535,13 +572,41 @@ AppendedIdentifier:
if(!HasReference) if(!HasReference)
{ {
ClaimBuffer(MemoryArena, &ClaimedMemory, &AnnotationData, 128); 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; HasReference = TRUE;
} }
else 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); CopyStringToBuffer(&Text, "<sup>%d</sup>", RefIdentifier);
@ -684,18 +749,33 @@ Anno->time);
{ {
CopyStringToBuffer(&ReferenceMenu, CopyStringToBuffer(&ReferenceMenu,
" <a data-id=\"%s\" href=\"%s\" target=\"_blank\" class=\"ref\">\n" " <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=\"source\">%s</div>\n"
" <div class=\"ref_title\">%s</div>\n" " <div class=\"ref_title\">%s</div>\n",
" </span>\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 // 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 // class="ref_index" and ensure to put these <=3 spans on the same line without
// a space between them // a space between them
" <div class=\"ref_indices\">\n ", CopyStringToBuffer(&ReferenceMenu,
ReferencesArray[i].ID, " <div class=\"ref_indices\">\n ");
"http://example.com/",
"Source",
"Title");
for(int j = 0; j < ReferencesArray[i].IdentifierCount; ++j) for(int j = 0; j < ReferencesArray[i].IdentifierCount; ++j)
{ {
CopyStringToBuffer(&ReferenceMenu, CopyStringToBuffer(&ReferenceMenu,