From 04f2fcd5294101a4a9b482c2a2a8e34e2d300bdf Mon Sep 17 00:00:00 2001 From: Matt Mascarenhas Date: Wed, 29 Mar 2017 04:38:12 +0100 Subject: [PATCH] hmml_to_html.c: Quote and Ref Menus [#16 #20] PPiC 063 --- hmml_to_html/hmml_to_html.c | 205 ++++++++++++++++++++++++++++++------ 1 file changed, 170 insertions(+), 35 deletions(-) diff --git a/hmml_to_html/hmml_to_html.c b/hmml_to_html/hmml_to_html.c index f062d0b..f359eaa 100644 --- a/hmml_to_html/hmml_to_html.c +++ b/hmml_to_html/hmml_to_html.c @@ -106,6 +106,7 @@ CopyBuffer(buffer *Src, buffer *Dest) } } +__attribute__ ((format (printf, 2, 3))) void CopyStringToBuffer(buffer *Dest, char *Format, ...) { @@ -214,15 +215,13 @@ BuildCategories(buffer *AnnotationClass, buffer *Category, int *MarkerIndex, boo } if(*HasCategory == FALSE) { - CopyStringToBuffer(Category, "
", - SanitisePunctuation(Marker)); + CopyStringToBuffer(Category, ""); *HasCategory = TRUE; } - else - { - CopyStringToBuffer(Category, "
", - SanitisePunctuation(Marker)); - } + + CopyStringToBuffer(Category, "
", + SanitisePunctuation(Marker)); + CopyStringToBuffer(AnnotationClass, " cat_%s", SanitisePunctuation(Marker)); ++*MarkerIndex; @@ -284,6 +283,12 @@ main(int ArgC, char **Args) ClaimBuffer(MemoryArena, &ClaimedMemory, &Title, 1024 * 16); ClaimBuffer(MemoryArena, &ClaimedMemory, &Player, 1024 * 256); + bool HasQuoteMenu = FALSE; + bool HasReferenceMenu = FALSE; + + int QuoteIdentifier = 945; + int RefIdentifier = 1; + CopyStringToBuffer(&Title, "
\n" " %s\n", HMML.metadata.title); @@ -295,69 +300,123 @@ main(int ArgC, char **Args) for(int AnnotationIndex = 0; AnnotationIndex < HMML.annotation_count; ++AnnotationIndex) { + HMML_Annotation *Anno = HMML.annotations + AnnotationIndex; bool HasCategory = FALSE; - ClaimBuffer(MemoryArena, &ClaimedMemory, &AnnotationHeader, 256); + bool HasQuote = FALSE; + bool HasReference = FALSE; + + ClaimBuffer(MemoryArena, &ClaimedMemory, &AnnotationHeader, 512); ClaimBuffer(MemoryArena, &ClaimedMemory, &Category, 256); - ClaimBuffer(MemoryArena, &ClaimedMemory, &AnnotationClass, 128); + ClaimBuffer(MemoryArena, &ClaimedMemory, &AnnotationClass, 256); ClaimBuffer(MemoryArena, &ClaimedMemory, &Text, 1024 * 4); CopyStringToBuffer(&AnnotationHeader, "
time)); CopyStringToBuffer(&AnnotationClass, " class=\"marker"); - if(HMML.annotations[AnnotationIndex].author) + if(Anno->author) { CopyStringToBuffer(&AnnotationClass, " authored"); CopyStringToBuffer(&Text, "%s ", -StringToColourHash(HMML.annotations[AnnotationIndex].author), -HMML.annotations[AnnotationIndex].author); +StringToColourHash(Anno->author), +Anno->author); } - InPtr = HMML.annotations[AnnotationIndex].text; + InPtr = Anno->text; int MarkerIndex = 0, RefIndex = 0; - while(*InPtr) + while(*InPtr || RefIndex < Anno->reference_count) { - if(MarkerIndex < HMML.annotations[AnnotationIndex].marker_count && - InPtr - HMML.annotations[AnnotationIndex].text == HMML.annotations[AnnotationIndex].markers[MarkerIndex].offset) + if(MarkerIndex < Anno->marker_count && + InPtr - Anno->text == Anno->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) + char *Readable = Anno->markers[MarkerIndex].parameter + ? Anno->markers[MarkerIndex].parameter + : Anno->markers[MarkerIndex].marker; + if(Anno->markers[MarkerIndex].type == HMML_MEMBER) { CopyStringToBuffer(&Text, "%.*s", -HMML.annotations[AnnotationIndex].markers[MarkerIndex].marker, -StringToColourHash(HMML.annotations[AnnotationIndex].markers[MarkerIndex].marker), +Anno->markers[MarkerIndex].marker, +StringToColourHash(Anno->markers[MarkerIndex].marker), StringLength(Readable), InPtr); InPtr += StringLength(Readable); ++MarkerIndex; } - else if(HMML.annotations[AnnotationIndex].markers[MarkerIndex].type == HMML_PROJECT) + else if(Anno->markers[MarkerIndex].type == HMML_PROJECT) { CopyStringToBuffer(&Text, "%s", -HMML.annotations[AnnotationIndex].markers[MarkerIndex].marker, -StringToColourHash(HMML.annotations[AnnotationIndex].markers[MarkerIndex].marker), +Anno->markers[MarkerIndex].marker, +StringToColourHash(Anno->markers[MarkerIndex].marker), Readable); InPtr += StringLength(Readable); ++MarkerIndex; } - else if(HMML.annotations[AnnotationIndex].markers[MarkerIndex].type == HMML_CATEGORY) + else if(Anno->markers[MarkerIndex].type == HMML_CATEGORY) { - BuildCategories(&AnnotationClass, &Category, &MarkerIndex, &HasCategory, HMML.annotations[AnnotationIndex].markers[MarkerIndex].marker); + BuildCategories(&AnnotationClass, &Category, &MarkerIndex, &HasCategory, Anno->markers[MarkerIndex].marker); } } - if(RefIndex < HMML.annotations[AnnotationIndex].reference_count && - InPtr - HMML.annotations[AnnotationIndex].text == HMML.annotations[AnnotationIndex].references[RefIndex].offset) + if(RefIndex < Anno->reference_count && + InPtr - Anno->text == Anno->references[RefIndex].offset) { + HMML_Reference *CurrentRef = Anno->references + RefIndex; + if(!HasReferenceMenu) + { + ClaimBuffer(MemoryArena, &ClaimedMemory, &ReferenceMenu, 1024 * 16); + + CopyStringToBuffer(&ReferenceMenu, +"
\n" +" References ▼\n" +"
\n" +"
\n"); + + HasReferenceMenu = TRUE; + } + + if(!HasReference) + { + ClaimBuffer(MemoryArena, &ClaimedMemory, &AnnotationData, 128); + CopyStringToBuffer(&AnnotationData, " data-ref=\"%s", CurrentRef->editor); + + HasReference = TRUE; + } + else + { + CopyStringToBuffer(&AnnotationData, ",%s", CurrentRef->editor); + } + + CopyStringToBuffer(&ReferenceMenu, +" \n" +" \n" +"
%s
\n" +"
%s
\n" +"
\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 +"
\n" +" [%d]%s\n" +"
\n" +"
\n", +CurrentRef->editor, +"http://example.com/", +"Source", +"Title", +TimecodeToSeconds(Anno->time), +RefIdentifier, +Anno->time); + + CopyStringToBuffer(&Text, "%d", RefIdentifier); + ++RefIndex; + ++RefIdentifier; } if(*InPtr) @@ -366,13 +425,70 @@ Readable); } } - while(MarkerIndex < HMML.annotations[AnnotationIndex].marker_count) + if(Anno->is_quote) { - BuildCategories(&AnnotationClass, &Category, &MarkerIndex, &HasCategory, HMML.annotations[AnnotationIndex].markers[MarkerIndex].marker); + if(!HasQuoteMenu) + { + ClaimBuffer(MemoryArena, &ClaimedMemory, &QuoteMenu, 1024 * 16); + CopyStringToBuffer(&QuoteMenu, +"
\n" +" Quotes ▼\n" +"
\n" +"
\n"); + + HasQuoteMenu = TRUE; + } + + if(!HasReference) + { + ClaimBuffer(MemoryArena, &ClaimedMemory, &AnnotationData, 32); + CopyStringToBuffer(&AnnotationData, " data-ref=\"&#%d;", QuoteIdentifier); + } + else + { + CopyStringToBuffer(&AnnotationData, ",&#%d;", QuoteIdentifier); + } + + HasQuote = TRUE; + + CopyStringToBuffer(&QuoteMenu, +" \n" +" \n" +"
#%d • %s
\n" +"
%s
\n" +"
\n" +"
\n" +" [&#%d;]%s\n" +"
\n" +"
\n", +QuoteIdentifier, +Anno->quote.id, +"Quote date", +"Quote text", +TimecodeToSeconds(Anno->time), +QuoteIdentifier, +Anno->time); + if(!Anno->text[0]) + { + CopyStringToBuffer(&Text, "“Quote text”"); + } + CopyStringToBuffer(&Text, "&#%d;", QuoteIdentifier); + ++QuoteIdentifier; + } + + while(MarkerIndex < Anno->marker_count) + { + BuildCategories(&AnnotationClass, &Category, &MarkerIndex, &HasCategory, Anno->markers[MarkerIndex].marker); } CopyStringToBuffer(&AnnotationClass, "\""); CopyBuffer(&AnnotationClass, &AnnotationHeader); + + if(HasQuote || HasReference) + { + CopyStringToBuffer(&AnnotationData, "\""); + CopyBuffer(&AnnotationData, &AnnotationHeader); + } CopyStringToBuffer(&AnnotationHeader, ">\n"); ClaimBuffer(MemoryArena, &ClaimedMemory, &Annotation, 1024 * 4); @@ -380,7 +496,7 @@ Readable); CopyBuffer(&AnnotationHeader, &Annotation); CopyStringToBuffer(&Annotation, "
%s", -HMML.annotations[AnnotationIndex].time); +Anno->time); if(HasCategory) { @@ -395,7 +511,7 @@ HMML.annotations[AnnotationIndex].time); CopyStringToBuffer(&Annotation, "
\n" "
\n" "
%s", -HMML.annotations[AnnotationIndex].time); +Anno->time); CopyBuffer(&Text, &Annotation); @@ -403,7 +519,7 @@ HMML.annotations[AnnotationIndex].time); "
\n" "
\n" "
%s", -HMML.annotations[AnnotationIndex].time); +Anno->time); CopyBuffer(&Text, &Annotation); @@ -420,6 +536,22 @@ HMML.annotations[AnnotationIndex].time); ClaimedMemory -= Annotation.Size; } + if(HasQuoteMenu) + { + CopyStringToBuffer(&QuoteMenu, +"
\n" +"
\n"); + CopyBuffer(&QuoteMenu, &Title); + } + + if(HasReferenceMenu) + { + CopyStringToBuffer(&ReferenceMenu, +"
\n" +"
\n"); + CopyBuffer(&ReferenceMenu, &Title); + } + CopyStringToBuffer(&Title, " Annotator: %s\n" "
\n", HMML.metadata.annotator); @@ -535,6 +667,9 @@ HMML.annotations[AnnotationIndex].time); fwrite(Master.Location, Master.Ptr - Master.Location, 1, OutFile); fclose(OutFile); + ClaimedMemory -= AnnotationData.Size; + ClaimedMemory -= QuoteMenu.Size; + ClaimedMemory -= ReferenceMenu.Size; ClaimedMemory -= Title.Size; ClaimedMemory -= Master.Size; }