Enable writing ODT files with correct metadata

This commit is contained in:
David Baer
2016-06-30 14:01:55 -04:00
parent e7216708b6
commit 3b185cb32e
17 changed files with 413 additions and 48 deletions

View File

@@ -31,6 +31,9 @@
#include <stdlib.h>
#include <string.h>
#include <libxml/tree.h>
#include <err.h>
#include "meta.h"
#include "odt.h"
#include "options.h"
#include "sermon.h"
#include "xml.h"
@@ -39,10 +42,31 @@
extern int yyparse(Sermon *);
extern FILE* yyin;
char*
constructMetaXml(const char* sermonTitle, const char* timestamp,
const char* sermonAuthor) {
size_t sz = sizeof(META_XML_TEMPLATE) - 13 + 2 * strlen(sermonTitle)
+ 3 * strlen(timestamp) + 2 * strlen(sermonAuthor);
char* result = malloc(sz);
if (!result) {
perror("malloc");
exit(1);
}
snprintf(result, sz, META_XML_TEMPLATE, sermonTitle, timestamp,
sermonAuthor, timestamp, sermonAuthor, sermonTitle, timestamp);
return result;
}
int main(int argc, char* argv[]) {
Sermon sermon;
xmlDocPtr document, transformed;
int i = 0, block = 0, normal = 0;
#ifdef HAVE_PLEDGE
if (-1 == pledge("stdio rpath wpath tmppath proc exec", NULL)) {
err(1, "pledge");
}
#endif /* !def(HAVE_PLEDGE) */
InitOptions(argc, (const char**)argv);
InitSermon(&sermon);
@@ -53,17 +77,61 @@ int main(int argc, char* argv[]) {
}
yyparse(&sermon);
#if 0
#ifdef HAVE_PLEDGE
if (-1 == pledge("stdio", NULL)) {
err(1, "pledge");
}
#endif /* !def(HAVE_PLEDGE) */
#endif
document = sermonToXmlDoc(&sermon);
transformed = applyStyleSheet(document, options.styleSheetName);
printXML(transformed);
if (strcmp(options.styleSheetName, "none") != 0) {
transformed = applyStyleSheet(document, options.styleSheetName);
} else {
transformed = document;
}
if (strcmp(options.outputFileName, "-") == 0) {
printXML(transformed);
} else {
if ((strcmp(options.styleSheetName, "odt") == 0) ||
(strcmp(options.styleSheetName, "ms_odt") == 0)) {
ODTFileEntry entry[2];
int sz;
xmlChar* doc;
char templateFileName[FILENAME_MAX];
char timestamp[20];
time_t tm;
/* get timestamp */
time(&tm);
strftime(timestamp, 20, "%Y-%m-%dT%H:%M:%S", localtime(&tm));
xmlDocDumpMemoryEnc(transformed, &doc, &sz, "utf-8");
entry[0].path = "content.xml";
entry[0].content = doc;
entry[1].path = "meta.xml";
entry[1].content = constructMetaXml(sermon.sermonTitle, timestamp, sermon.sermonAuthor ? sermon.sermonAuthor : options.authorName);
snprintf(templateFileName, FILENAME_MAX, "%s.odt", options.styleSheetName);
constructODT(options.outputFileName, OptionsDataFile(templateFileName), 2, entry);
free(doc);
free(entry[1].content);
} else {
xmlSaveFileEnc(options.outputFileName, transformed, "utf-8");
}
}
/* clean up, clean up, everybody, everywhere! */
VERBOSE_PRINTF("Cleaning up\n");
xmlFreeDoc(document);
xmlFreeDoc(transformed);
if (strcmp(options.styleSheetName, "none") != 0) {
xmlFreeDoc(transformed);
}
FreeSermon(&sermon);
if (strcmp(options.inputFileName, "-") != 0) {
fclose(yyin);
}
FreeOptions();
VERBOSE_PRINTF("Cleanup done\n");
}