hmml_to_html.c: Fix quoting

This commit is contained in:
Matt Mascarenhas 2017-08-11 23:41:52 +01:00
parent ec556a8e3b
commit bcaa6d63db
1 changed files with 115 additions and 43 deletions

View File

@ -859,7 +859,7 @@ SearchQuotes(buffer QuoteStaging, int CacheSize, quote_info *Info, int ID)
{ {
*OutPtr++ = *QuoteStaging.Ptr++; *OutPtr++ = *QuoteStaging.Ptr++;
} }
*--OutPtr = '\0'; *OutPtr = '\0';
FreeBuffer(&QuoteStaging); FreeBuffer(&QuoteStaging);
return 0; return 0;
@ -876,6 +876,54 @@ SearchQuotes(buffer QuoteStaging, int CacheSize, quote_info *Info, int ID)
return 1; return 1;
} }
int
MakeDir(char *Path)
{
int i = StringLength(Path);
int Ancestors = 0;
while(mkdir(Path, 00755) == -1)
{
while(Path[i] != '/' && i > 0)
{
--i;
}
++Ancestors;
Path[i] = '\0';
if(i == 0) { return 1; }
}
while(Ancestors > 0)
{
while(Path[i] != '\0')
{
++i;
}
Path[i] = '/';
--Ancestors;
if((mkdir(Path, 00755)) == -1)
{
return 1;
}
}
return 0;
}
void
CurlQuotes(buffer *QuoteStaging, char *QuotesURL)
{
CURL *curl = curl_easy_init();
if(curl) {
CURLcode res;
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &QuoteStaging->Ptr);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlIntoBuffer);
curl_easy_setopt(curl, CURLOPT_URL, QuotesURL);
if((res = curl_easy_perform(curl)))
{
fprintf(stderr, "%s", curl_easy_strerror(res));
}
curl_easy_cleanup(curl);
}
}
int int
BuildQuote(quote_info *Info, char *Speaker, int ID, char *CacheDir) BuildQuote(quote_info *Info, char *Speaker, int ID, char *CacheDir)
{ {
@ -886,25 +934,29 @@ BuildQuote(quote_info *Info, char *Speaker, int ID, char *CacheDir)
char QuoteCachePath[255]; char QuoteCachePath[255];
CopyString(QuoteCachePath, "%s/%s", QuoteCacheDir, Speaker); CopyString(QuoteCachePath, "%s/%s", QuoteCacheDir, Speaker);
FILE *QuoteCache; FILE *QuoteCache;
char QuotesURL[256];
CopyString(QuotesURL, "https://dev.abaines.me.uk/quotes/%s.raw", Speaker);
bool CacheAvailable = FALSE;
while(!(QuoteCache = fopen(QuoteCachePath, "a+"))) if(!(QuoteCache = fopen(QuoteCachePath, "a+")))
{ {
char StagingDirectory[255]; if(MakeDir(QuoteCacheDir) == 0)
CopyString(StagingDirectory, QuoteCacheDir);
while(mkdir(StagingDirectory, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) == -1)
{ {
int i = StringLength(StagingDirectory); CacheAvailable = TRUE;
while(StagingDirectory[i] != '/') };
{ if(!(QuoteCache = fopen(QuoteCachePath, "a+")))
--i; {
} perror(QuoteCachePath);
StagingDirectory[i] = '\0'; }
else
{
CacheAvailable = TRUE;
} }
} }
else
fseek(QuoteCache, 0, SEEK_END); {
int FileSize = ftell(QuoteCache); CacheAvailable = TRUE;
fseek(QuoteCache, 0, SEEK_SET); }
buffer QuoteStaging; buffer QuoteStaging;
QuoteStaging.ID = "QuoteStaging"; QuoteStaging.ID = "QuoteStaging";
@ -915,34 +967,38 @@ BuildQuote(quote_info *Info, char *Speaker, int ID, char *CacheDir)
} }
QuoteStaging.Ptr = QuoteStaging.Location; QuoteStaging.Ptr = QuoteStaging.Location;
fread(QuoteStaging.Location, FileSize, 1, QuoteCache); if(CacheAvailable)
fclose(QuoteCache);
if(SearchQuotes(QuoteStaging, FileSize, Info, ID) == 1)
{ {
char QuotesURL[256]; fseek(QuoteCache, 0, SEEK_END);
CopyString(QuotesURL, "https://dev.abaines.me.uk/quotes/%s.raw", Speaker); int FileSize = ftell(QuoteCache);
fseek(QuoteCache, 0, SEEK_SET);
CURL *curl = curl_easy_init(); fread(QuoteStaging.Location, FileSize, 1, QuoteCache);
if(curl) {
CURLcode res;
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &QuoteStaging.Ptr);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlIntoBuffer);
curl_easy_setopt(curl, CURLOPT_URL, QuotesURL);
if((res = curl_easy_perform(curl)))
{
fprintf(stderr, "%s", curl_easy_strerror(res));
}
curl_easy_cleanup(curl);
}
if(!(QuoteCache = fopen(QuoteCachePath, "w")))
{
perror(QuoteCachePath);
}
fwrite(QuoteStaging.Location, QuoteStaging.Ptr - QuoteStaging.Location, 1, QuoteCache);
fclose(QuoteCache); fclose(QuoteCache);
if(SearchQuotes(QuoteStaging, FileSize, Info, ID) == 1)
{
CurlQuotes(&QuoteStaging, QuotesURL);
if(!(QuoteCache = fopen(QuoteCachePath, "w")))
{
perror(QuoteCachePath);
}
fwrite(QuoteStaging.Location, QuoteStaging.Ptr - QuoteStaging.Location, 1, QuoteCache);
fclose(QuoteCache);
int CacheSize = QuoteStaging.Ptr - QuoteStaging.Location;
QuoteStaging.Ptr = QuoteStaging.Location;
if(SearchQuotes(QuoteStaging, CacheSize, Info, ID) == 1)
{
FreeBuffer(&QuoteStaging);
return 1;
}
}
}
else
{
CurlQuotes(&QuoteStaging, QuotesURL);
int CacheSize = QuoteStaging.Ptr - QuoteStaging.Location; int CacheSize = QuoteStaging.Ptr - QuoteStaging.Location;
QuoteStaging.Ptr = QuoteStaging.Location; QuoteStaging.Ptr = QuoteStaging.Location;
if(SearchQuotes(QuoteStaging, CacheSize, Info, ID) == 1) if(SearchQuotes(QuoteStaging, CacheSize, Info, ID) == 1)
@ -951,6 +1007,7 @@ BuildQuote(quote_info *Info, char *Speaker, int ID, char *CacheDir)
return 1; return 1;
} }
} }
return 0; return 0;
} }
@ -2143,21 +2200,36 @@ main(int ArgC, char **Args)
HasQuote = TRUE; HasQuote = TRUE;
char *Speaker = Anno->quote.author ? Anno->quote.author : HMML.metadata.stream_username ? HMML.metadata.stream_username : HMML.metadata.member;
if(BuildQuote(&QuoteInfo, if(BuildQuote(&QuoteInfo,
Anno->quote.author ? Anno->quote.author : HMML.metadata.stream_username ? HMML.metadata.stream_username : HMML.metadata.member, Speaker,
Anno->quote.id, Anno->quote.id,
CacheDir) == 1) CacheDir) == 1)
{ {
fprintf(stderr, "%s:%d: Quote #%s %d not found. Unlucky!\n", fprintf(stderr, "%s:%d: Quote #%s %d not found. Unlucky!\n",
Args[FileIndex], Args[FileIndex],
Anno->line, Anno->line,
Anno->quote.author ? Anno->quote.author : HMML.metadata.stream_username ? HMML.metadata.stream_username : HMML.metadata.member, Speaker,
Anno->quote.id); Anno->quote.id);
hmml_free(&HMML); hmml_free(&HMML);
free(MemoryArena.Location); free(MemoryArena.Location);
free(Template.Location); free(Template.Location);
return 1; return 1;
} }
if(BuildQuote(&QuoteInfo,
Speaker,
Anno->quote.id,
CacheDir) == 2)
{
fprintf(stderr, "%s:%d: Failed to create quote cache at %s/quotes/%s for Quote #%s %d.\n"
"Proceeding anyway, without a cache!\n",
Args[FileIndex],
Anno->line,
CacheDir,
Speaker,
Speaker,
Anno->quote.id);
}
CopyStringToBuffer(&QuoteMenu, CopyStringToBuffer(&QuoteMenu,
" <a target=\"_blank\" class=\"ref\" href=\"https://dev.abaines.me.uk/quotes/%s/%d\">\n" " <a target=\"_blank\" class=\"ref\" href=\"https://dev.abaines.me.uk/quotes/%s/%d\">\n"
@ -2165,7 +2237,7 @@ main(int ArgC, char **Args)
" <span class=\"ref_content\">\n" " <span class=\"ref_content\">\n"
" <div class=\"source\">Quote %d</div>\n" " <div class=\"source\">Quote %d</div>\n"
" <div class=\"ref_title\">", " <div class=\"ref_title\">",
Anno->quote.author ? Anno->quote.author : HMML.metadata.stream_username ? HMML.metadata.stream_username : HMML.metadata.member, Speaker,
Anno->quote.id, Anno->quote.id,
QuoteIdentifier, QuoteIdentifier,
Anno->quote.id); Anno->quote.id);
@ -2180,7 +2252,7 @@ main(int ArgC, char **Args)
" </div>\n" " </div>\n"
" </span>\n" " </span>\n"
" </a>\n", " </a>\n",
Anno->quote.author ? Anno->quote.author : HMML.metadata.stream_username ? HMML.metadata.stream_username : HMML.metadata.member, Speaker,
QuoteInfo.Date, QuoteInfo.Date,
TimecodeToSeconds(Anno->time), TimecodeToSeconds(Anno->time),
QuoteIdentifier, QuoteIdentifier,