Added csc:term()
authorStefanus Du Toit <sjdutoit@csclub.uwaterloo.ca>
Mon, 15 Apr 2002 17:37:12 +0000 (17:37 +0000)
committerStefanus Du Toit <sjdutoit@csclub.uwaterloo.ca>
Mon, 15 Apr 2002 17:37:12 +0000 (17:37 +0000)
cow/Makefile
cow/debug.h [new file with mode: 0755]
cow/functions.c [new file with mode: 0755]
cow/main.c
cscweb.xsl
default.mk.in

index f43e61d..4465291 100755 (executable)
@@ -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)
diff --git a/cow/debug.h b/cow/debug.h
new file mode 100755 (executable)
index 0000000..39d239b
--- /dev/null
@@ -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
diff --git a/cow/functions.c b/cow/functions.c
new file mode 100755 (executable)
index 0000000..a7902bc
--- /dev/null
@@ -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);
+}
index 6c4a514..4bdf483 100755 (executable)
 #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);
@@ -59,10 +63,13 @@ int main(int argc, char** argv)
     usage(argv[0]);
     return 1;
   }
-  
+
   params[nbparams] = NULL;
   xmlSubstituteEntitiesDefault(1);
   xmlLoadExtDtdDefaultValue = 1;
+
+  init_extensions();
+  
   cur = xsltParseStylesheetFile((const xmlChar *)argv[argNum]);
   argNum++;
   doc = xmlParseFile(argv[argNum]);
index cf8f383..9810323 100755 (executable)
@@ -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"/>
 </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"/>,
index b018be0..b04ef3d 100755 (executable)
@@ -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 $< > $@