Added csc:term()

This commit is contained in:
Stefanus Du Toit 2002-04-15 17:37:12 +00:00
parent 9abc317387
commit b5c49e1863
6 changed files with 87 additions and 7 deletions

View File

@ -1,5 +1,5 @@
MAIN = cow
CFLAGS = -I/usr/include/libxml2
CFLAGS = -I/usr/include/libxml2 -DDEBUG -pedantic -ansi -Wall
LDFLAGS = $(CFLAGS) -lxml2 -lxslt
SOURCES = $(wildcard *.c)
OBJECTS = $(SOURCES:.c=.o)

13
cow/debug.h Executable file
View File

@ -0,0 +1,13 @@
#ifndef CSC_COW_DEBUG_H
#define CSC_COW_DEBUG_H
#include <stdio.h>
#ifdef DEBUG
#define DEBUG_PRINT(x) do { fprintf(stderr, "%s:%d (%s): %s\n", __FILE__, \
__LINE__, __FUNCTION__, x); } while (0)
#else
#define DEBUG_PRINT(x) do { } while (0)
#endif
#endif

55
cow/functions.c Executable file
View File

@ -0,0 +1,55 @@
#include "debug.h"
#include <libxslt/extensions.h>
#include <libxml/xpath.h>
#include <libxml/xpathInternals.h>
#define COW_URI ((const xmlChar *)"http://www.csclub.uwaterloo.ca/cow")
/* return the term ({Winter,Summer,Fall} YYYY) for a date in
* YYYY-MM-DD format.
*/
void extension_cow_term(xmlXPathParserContextPtr ctxt,
int nargs)
{
xmlChar *str, *ret;
int str_l, month;
DEBUG_PRINT("term");
if (nargs != 1) {
xmlXPathSetArityError(ctxt);
}
str = xmlXPathPopString(ctxt);
str_l = xmlUTF8Strlen(str);
DEBUG_PRINT(str);
month = (str[5] - '0')*10 + (str[6] - '0');
ret = xmlUTF8Strndup((const xmlChar*)(month < 9 ? (month < 5 ? "Winter " : "Summer ") : "Fall "), 7);
ret = xmlStrcat(ret, xmlUTF8Strsub(str, 0, 4));
xmlXPathReturnString(ctxt, ret);
xmlFree(str);
}
void *extension_init_func(xsltTransformContextPtr ctxt,
const xmlChar *URI)
{
DEBUG_PRINT("Extension init");
return 0;
}
void extension_shutdown_func(xsltTransformContextPtr ctxt,
const xmlChar *URI,
void *data)
{
DEBUG_PRINT("Extension shut down");
}
void init_extensions()
{
DEBUG_PRINT("Registering extension module.");
xsltRegisterExtModule(COW_URI,
extension_init_func, extension_shutdown_func);
xsltRegisterExtModuleFunction((const xmlChar*)"term", COW_URI,
extension_cow_term);
}

View File

@ -10,10 +10,14 @@
#include <libxslt/xsltInternals.h>
#include <libxslt/transform.h>
#include <libxslt/xsltutils.h>
#include <libxslt/extensions.h>
#include "debug.h"
extern int xmlLoadExtDtdDefaultValue;
// output usage message. progname is the name of the executable.
void init_extensions();
/* output usage message. progname is the name of the executable. */
static void usage(const char* progname)
{
printf("Usage: %s [options] stylesheet input\n", progname);
@ -63,6 +67,9 @@ int main(int argc, char** argv)
params[nbparams] = NULL;
xmlSubstituteEntitiesDefault(1);
xmlLoadExtDtdDefaultValue = 1;
init_extensions();
cur = xsltParseStylesheetFile((const xmlChar *)argv[argNum]);
argNum++;
doc = xmlParseFile(argv[argNum]);

View File

@ -1,7 +1,9 @@
<?xml version='1.0'?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:csc="http://www.csclub.uwaterloo.ca/cow"
extension-element-prefixes="csc">
<xsl:output method="html" />
@ -17,6 +19,9 @@
</style>
</head>
<body>
<p>
<xsl:value-of select="csc:term($date)"/>
</p>
<xsl:apply-templates select="biglogo"/>
<xsl:apply-templates select="header">
<xsl:with-param name="title" select="@title"/>
@ -207,12 +212,12 @@
</xsl:template>
<xsl:template match="events-this-term">
<p>The events for <xsl:value-of select="$term"/> are listed here.</p>
<p>The events for <xsl:value-of select="csc:term($date)"/> are listed here.</p>
<xsl:for-each select="document('events.xml')/eventdefs/eventitem">
<xsl:sort select="translate(@date, '-', '')"
order="ascending"
data-type="number"/>
<xsl:if test="@term = $term">
<xsl:if test="@term = csc:term($date)">
<h3><xsl:value-of select="@title"/></h3>
<p>
<b><xsl:value-of select="@room"/>,

View File

@ -18,5 +18,5 @@ clean-recurse: clean
clean:
rm -f *.html
%.html: %.xml $(ROOT)/csc.dtd $(ROOT)/cscweb.xsl $(ROOT)/events.xml $(ROOT)/news.xml $(ROOT)/menu.xml $(ROOT)/default.mk directory.xml
%.html: %.xml $(ROOT)/csc.dtd $(ROOT)/cscweb.xsl $(ROOT)/events.xml $(ROOT)/news.xml $(ROOT)/menu.xml $(ROOT)/default.mk directory.xml $(ROOT)/cow/cow
$(ROOT)/cow/cow $(XSLTARGS) $(ROOT)/cscweb.xsl $< > $@