hmml_to_html.c: Command-line args [#25]

This commit is contained in:
Matt Mascarenhas 2017-06-25 19:05:58 +01:00
parent ad9d64b31c
commit b58226fd89
1 changed files with 965 additions and 850 deletions

View File

@ -16,6 +16,7 @@ typedef unsigned int bool;
#include <stdio.h> // NOTE(matt): printf, sprintf, vsprintf, fprintf, perror #include <stdio.h> // NOTE(matt): printf, sprintf, vsprintf, fprintf, perror
#include <stdlib.h> // NOTE(matt): calloc, malloc, free #include <stdlib.h> // NOTE(matt): calloc, malloc, free
#include "hmmlib.h" #include "hmmlib.h"
#include <getopt.h> // NOTE(matt): getopts
//#include "config.h" // TODO(matt): Implement config.h //#include "config.h" // TODO(matt): Implement config.h
#define Kilobytes(Bytes) Bytes << 10 #define Kilobytes(Bytes) Bytes << 10
@ -403,7 +404,7 @@ SanitisePunctuation(char *String)
} }
int int
BuildCredits(buffer *CreditsMenu, buffer *HostInfo, buffer *AnnotatorInfo, bool *HasCreditsMenu, char *Host, char *Annotator) BuildCredits(buffer *CreditsMenu, buffer *HostInfo, buffer *AnnotatorInfo, bool *HasCreditsMenu, char *ImagesDir, char *Host, char *Annotator)
{ {
// TODO(matt): Handle co-hosts and guests // TODO(matt): Handle co-hosts and guests
bool FoundHost = FALSE; bool FoundHost = FALSE;
@ -439,8 +440,9 @@ Credentials[CredentialIndex][1]);
if(*Credentials[CredentialIndex][4] && *Credentials[CredentialIndex][3]) if(*Credentials[CredentialIndex][4] && *Credentials[CredentialIndex][3])
{ {
CopyStringToBuffer(HostInfo, CopyStringToBuffer(HostInfo,
" <a class=\"support\" href=\"%s\" target=\"_blank\"><img src=\"%s\"></a>\n", " <a class=\"support\" href=\"%s\" target=\"_blank\"><img src=\"%s/%s\"></a>\n",
Credentials[CredentialIndex][4], Credentials[CredentialIndex][4],
ImagesDir,
Credentials[CredentialIndex][3]); Credentials[CredentialIndex][3]);
} }
@ -476,8 +478,9 @@ Credentials[CredentialIndex][1]);
if(*Credentials[CredentialIndex][4] && *Credentials[CredentialIndex][3]) if(*Credentials[CredentialIndex][4] && *Credentials[CredentialIndex][3])
{ {
CopyStringToBuffer(AnnotatorInfo, CopyStringToBuffer(AnnotatorInfo,
" <a class=\"support\" href=\"%s\" target=\"_blank\"><img src=\"%s\"></a>\n", " <a class=\"support\" href=\"%s\" target=\"_blank\"><img src=\"%s/%s\"></a>\n",
Credentials[CredentialIndex][4], Credentials[CredentialIndex][4],
ImagesDir,
Credentials[CredentialIndex][3]); Credentials[CredentialIndex][3]);
} }
@ -759,19 +762,14 @@ StringToInt(char *String)
} }
int int
BuildQuote(quote_info *Info, char *Speaker, int ID) BuildQuote(quote_info *Info, char *Speaker, int ID, char *QuoteDir)
{ {
// TODO(matt): Pull these paths from the config
// Also notable that there are different paths for different projects
char *QuoteDir = "/home/matt/git/GitHub/insofaras/25fc16d58a297a486334";
char Path[255] = {0}; char Path[255] = {0};
sprintf(Path, "%s/#%s", QuoteDir, Speaker); sprintf(Path, "%s/#%s", QuoteDir, Speaker);
FILE *File; FILE *File;
if(!(File = fopen(Path, "r"))) if(!(File = fopen(Path, "r")))
{ {
perror("hmml_to_html"); perror(Path);
return 1; return 1;
} }
@ -781,7 +779,7 @@ BuildQuote(quote_info *Info, char *Speaker, int ID)
char *Buffer; char *Buffer;
if(!(Buffer = malloc(Length))) if(!(Buffer = malloc(Length)))
{ {
perror("hmml_to_html"); perror("BuildQuote");
} }
fread(Buffer, Length, 1, File); fread(Buffer, Length, 1, File);
fclose(File); fclose(File);
@ -839,7 +837,7 @@ BuildQuote(quote_info *Info, char *Speaker, int ID)
} }
void void
GenerateTopicColours(buffer *Colour, char *Topic) GenerateTopicColours(buffer *Colour, char *Topic, char *TopicsDir)
{ {
for(int i = 0; i < ArrayCount(CategoryMedium); ++i) for(int i = 0; i < ArrayCount(CategoryMedium); ++i)
{ {
@ -852,6 +850,8 @@ GenerateTopicColours(buffer *Colour, char *Topic)
FILE *TopicsFile; FILE *TopicsFile;
char *TopicsBuffer; char *TopicsBuffer;
// TODO(matt): Consider (optionally) pulling this path from the config // TODO(matt): Consider (optionally) pulling this path from the config
char TopicsPath[255];
CopyString(TopicsPath, "%s/cinera_topcs.css", TopicsDir);
if((TopicsFile = fopen("cinera_topics.css", "a+"))) if((TopicsFile = fopen("cinera_topics.css", "a+")))
{ {
fseek(TopicsFile, 0, SEEK_END); fseek(TopicsFile, 0, SEEK_END);
@ -1166,15 +1166,127 @@ ParseConfig(buffer *Buffer, char *Username)
#endif #endif
void
PrintUsage(char *BinaryLocation, char *DefaultCSSDir, char *DefaultImagesDir, char *DefaultJSDir, char *DefaultOutLocation, char *DefaultQuoteDir, char *DefaultTemplateLocation)
{
fprintf(stderr, "Usage: %s [option(s)] filename(s)\n"
"\n"
"Options:\n"
" -c <CSS directory path>\n"
" Override default CSS directory (\"%s\")\n"
" -i <images directory path>\n"
" Override default images directory (\"%s\")\n"
" -j <JS directory path>\n"
" Override default JS directory (\"%s\")\n"
" -o <output location>\n"
" Override default output location (\"%s\")\n"
" -q <quotes directory path>\n"
" Override default quotes directory (\"%s\")\n"
" -t <template location>\n"
" Override default template location (\"%s\")\n"
" and automatically enable integration\n"
//" -c config location\n"
" -h\n"
" display this help\n"
"\n"
"Environment Variables:\n"
" CINERA_MODE\n"
" =INTEGRATE\n"
" Enable integration\n",
BinaryLocation, DefaultCSSDir, DefaultImagesDir, DefaultJSDir, DefaultQuoteDir, DefaultOutLocation, DefaultTemplateLocation);
}
int int
main(int ArgC, char **Args) main(int ArgC, char **Args)
{ {
// TODO(matt): Read all defaults from the config and probably validate the
// template up front
char *DefaultCSSDir = ".";
char *CSSDir = DefaultCSSDir;
char *DefaultImagesDir = ".";
char *ImagesDir = DefaultImagesDir;
char *DefaultJSDir = ".";
char *JSDir = DefaultJSDir;
char *DefaultTemplateLocation = "template.html";
char *TemplateLocation = DefaultTemplateLocation;
char *DefaultOutLocation = "out.html";
char *OutLocation = DefaultOutLocation;
char *DefaultOutIntegratedLocation = "out_integrated.html";
char *OutIntegratedLocation = DefaultOutIntegratedLocation;
char *DefaultQuoteDir = "/home/matt/git/GitHub/insofaras/25fc16d58a297a486334";
char *QuoteDir = DefaultQuoteDir;
char *CINERA_MODE = getenv("CINERA_MODE");
if(ArgC < 2) if(ArgC < 2)
{ {
fprintf(stderr, "Usage: %s filename(s)\n", Args[0]); PrintUsage(Args[0], DefaultCSSDir, DefaultImagesDir, DefaultJSDir, DefaultQuoteDir, DefaultTemplateLocation, DefaultOutLocation);
return 1; return 1;
} }
char CommandLineArg;
while((CommandLineArg = getopt(ArgC, Args, "c:i:j:o:q:t:h")) != -1)
{
switch(CommandLineArg)
{
case 'c':
CSSDir = optarg;
break;
case 'i':
ImagesDir = optarg;
break;
case 'j':
JSDir = optarg;
break;
case 'o':
OutLocation = optarg;
OutIntegratedLocation = optarg;
break;
case 'q':
QuoteDir = optarg;
case 't':
TemplateLocation = optarg;
CINERA_MODE="INTEGRATE";
break;
//case 'c':
// Override config path, once we even have a default!
case 'h':
default:
PrintUsage(Args[0], DefaultCSSDir, DefaultImagesDir, DefaultJSDir, DefaultQuoteDir, DefaultTemplateLocation, DefaultOutLocation);
return 1;
}
}
if(optind == ArgC)
{
fprintf(stderr, "%s: requires at least one input .hmml file\n", Args[0]);
PrintUsage(Args[0], DefaultCSSDir, DefaultImagesDir, DefaultJSDir, DefaultQuoteDir, DefaultTemplateLocation, DefaultOutLocation);
return 1;
}
if(CSSDir[StringLength(CSSDir) - 1] == '/')
{
CSSDir[StringLength(CSSDir) - 1] = '\0';
}
if(ImagesDir[StringLength(ImagesDir) - 1] == '/')
{
ImagesDir[StringLength(ImagesDir) - 1] = '\0';
}
if(JSDir[StringLength(JSDir) - 1] == '/')
{
JSDir[StringLength(JSDir) - 1] = '\0';
}
if(QuoteDir[StringLength(QuoteDir) - 1] == '/')
{
QuoteDir[StringLength(QuoteDir) - 1] = '\0';
}
// NOTE(matt): Init MemoryArena // NOTE(matt): Init MemoryArena
char *MemoryArena; char *MemoryArena;
int ArenaSize = Megabytes(4); int ArenaSize = Megabytes(4);
@ -1237,12 +1349,12 @@ main(int ArgC, char **Args)
buffer Script; buffer Script;
buffer FilterState; buffer FilterState;
for(int FileIndex = 1; FileIndex < ArgC; ++FileIndex) for(int FileIndex = optind; FileIndex < ArgC; ++FileIndex)
{ {
FILE *InFile; FILE *InFile;
if(!(InFile = fopen(Args[FileIndex], "r"))) if(!(InFile = fopen(Args[FileIndex], "r")))
{ {
perror(Args[0]); perror(Args[FileIndex]);
free(MemoryArena); free(MemoryArena);
return 1; return 1;
} }
@ -1329,7 +1441,7 @@ Args[FileIndex]);
// TODO(matt): Handle multiple annotators // TODO(matt): Handle multiple annotators
if(HMML.metadata.annotator_count > 0) if(HMML.metadata.annotator_count > 0)
{ {
BuildCredits(&CreditsMenu, &HostInfo, &AnnotatorInfo, &HasCreditsMenu, HMML.metadata.member, HMML.metadata.annotators[0]); BuildCredits(&CreditsMenu, &HostInfo, &AnnotatorInfo, &HasCreditsMenu, ImagesDir, HMML.metadata.member, HMML.metadata.annotators[0]);
} }
else else
{ {
@ -1478,7 +1590,7 @@ Readable);
} }
else if(Anno->markers[MarkerIndex].type == HMML_CATEGORY) else if(Anno->markers[MarkerIndex].type == HMML_CATEGORY)
{ {
GenerateTopicColours(&Colour, Anno->markers[MarkerIndex].marker); GenerateTopicColours(&Colour, Anno->markers[MarkerIndex].marker, CSSDir);
// TODO(matt): Maybe stuff this into BuildCategories // TODO(matt): Maybe stuff this into BuildCategories
if(!HasFilterMenu) if(!HasFilterMenu)
{ {
@ -1665,7 +1777,7 @@ AppendedIdentifier:
HasQuote = TRUE; HasQuote = TRUE;
if(BuildQuote(&QuoteInfo, HMML.metadata.stream_username ? HMML.metadata.stream_username : HMML.metadata.member, Anno->quote.id) == 1) if(BuildQuote(&QuoteInfo, HMML.metadata.stream_username ? HMML.metadata.stream_username : HMML.metadata.member, Anno->quote.id, QuoteDir) == 1)
{ {
fprintf(stderr, "%s:%d: Quote #%s %d not found! Consider pulling the latest quotes\n", fprintf(stderr, "%s:%d: Quote #%s %d not found! Consider pulling the latest quotes\n",
Args[FileIndex], Args[FileIndex],
@ -1711,7 +1823,7 @@ Anno->time);
while(MarkerIndex < Anno->marker_count) while(MarkerIndex < Anno->marker_count)
{ {
GenerateTopicColours(&Colour, Anno->markers[MarkerIndex].marker); GenerateTopicColours(&Colour, Anno->markers[MarkerIndex].marker, CSSDir);
// TODO(matt): Maybe stuff this into BuildCategories // TODO(matt): Maybe stuff this into BuildCategories
if(!HasFilterMenu) if(!HasFilterMenu)
{ {
@ -1878,10 +1990,10 @@ ReferencesArray[i].Identifier[j].Timecode);
CopyStringToBuffer(&FilterMenu, CopyStringToBuffer(&FilterMenu,
" <div class=\"menu filter\">\n" " <div class=\"menu filter\">\n"
" <span><img src=\"cinera_icon_filter.png\"></span>\n" " <span><img src=\"%s/cinera_icon_filter.png\"></span>\n"
" <div class=\"filter_container\">\n" " <div class=\"filter_container\">\n"
" <div class=\"filter_mode inclusive\">Filter mode: </div>\n" " <div class=\"filter_mode inclusive\">Filter mode: </div>\n"
" <div class=\"filters\">\n"); " <div class=\"filters\">\n", ImagesDir);
{ {
bool HasTopic = FALSE; bool HasTopic = FALSE;
@ -2197,11 +2309,15 @@ CategoryMedium[j][2]
"<meta charset=\"UTF-8\">\n" "<meta charset=\"UTF-8\">\n"
"\n" "\n"
" <!-- Load the player -->\n" " <!-- Load the player -->\n"
" <script type=\"text/javascript\" src=\"cinera.js\"></script>\n" " <script type=\"text/javascript\" src=\"%s/cinera.js\"></script>\n"
" <link rel=\"stylesheet\" type=\"text/css\" href=\"cinera.css\">\n" " <link rel=\"stylesheet\" type=\"text/css\" href=\"%s/cinera.css\">\n"
" <link rel=\"stylesheet\" type=\"text/css\" href=\"cinera__%s.css\">\n" " <link rel=\"stylesheet\" type=\"text/css\" href=\"%s/cinera__%s.css\">\n"
" <link rel=\"stylesheet\" type=\"text/css\" href=\"cinera_topics.css\">", " <link rel=\"stylesheet\" type=\"text/css\" href=\"%s/cinera_topics.css\">",
HMML.metadata.project); JSDir,
CSSDir,
CSSDir,
HMML.metadata.project,
CSSDir);
CopyStringToBuffer(&Script, CopyStringToBuffer(&Script,
" <script type=\"text/javascript\">\n" " <script type=\"text/javascript\">\n"
@ -2396,11 +2512,10 @@ HMML.metadata.project);
printf("Buffer Collation\n\n"); printf("Buffer Collation\n\n");
#endif #endif
char *CINERA_MODE = getenv("CINERA_MODE");
if(CINERA_MODE && !StringsDiffer(CINERA_MODE, "INTEGRATE")) if(CINERA_MODE && !StringsDiffer(CINERA_MODE, "INTEGRATE"))
{ {
FILE *TemplateFile; FILE *TemplateFile;
if(!(TemplateFile = fopen("template.html", "r"))) if(!(TemplateFile = fopen(TemplateLocation, "r")))
{ {
perror(Args[0]); hmml_free(&HMML); free(MemoryArena); return 1; perror(Args[0]); hmml_free(&HMML); free(MemoryArena); return 1;
} }
@ -2566,9 +2681,9 @@ HMML.metadata.project);
if(FoundIncludes && FoundMenus && FoundPlayer && FoundScript) if(FoundIncludes && FoundMenus && FoundPlayer && FoundScript)
{ {
FILE *OutFile; FILE *OutFile;
if(!(OutFile = fopen("out_integrated.html", "w"))) if(!(OutFile = fopen(OutIntegratedLocation, "w")))
{ {
perror(Args[0]); free(Template.Location); free(Output.Location); hmml_free(&HMML); free(MemoryArena); return 1; perror(OutIntegratedLocation); free(Template.Location); free(Output.Location); hmml_free(&HMML); free(MemoryArena); return 1;
} }
fwrite(Output.Location, Output.Ptr - Output.Location, 1, OutFile); fwrite(Output.Location, Output.Ptr - Output.Location, 1, OutFile);
fclose(OutFile); fclose(OutFile);
@ -2612,9 +2727,9 @@ HMML.metadata.project);
"</html>\n"); "</html>\n");
FILE *OutFile; FILE *OutFile;
if(!(OutFile = fopen("out.html", "w"))) if(!(OutFile = fopen(OutLocation, "w")))
{ {
perror(Args[0]); hmml_free(&HMML); free(MemoryArena); return 1; perror(OutLocation); hmml_free(&HMML); free(MemoryArena); return 1;
} }
fwrite(Master.Location, Master.Ptr - Master.Location, 1, OutFile); fwrite(Master.Location, Master.Ptr - Master.Location, 1, OutFile);
fclose(OutFile); fclose(OutFile);