Author: Daniel Kahn Gillmor <dkg@fifthhorseman.net>
Date: Thu, 22 Sep 2011 23:38:54 -0400

addresses https://trac-hacks.org/ticket/9199 -- adds three special keywords that get replaced with
the page name, the page version (if it is anything but the latest) and the timestamp of generation.


diff --git a/odtexport/odtexport.py b/odtexport/odtexport.py
index 1941578..fafdb58 100644
--- a/odtexport/odtexport.py
+++ b/odtexport/odtexport.py
@@ -15,6 +15,7 @@ import os
 import zipfile
 import urllib2
 import urlparse
+import datetime
 
 # pylint: disable-msg=E0611
 from pkg_resources import resource_filename
@@ -49,6 +50,9 @@ class ODTExportPlugin(Component):
     img_dpi = IntOption('odtexport', 'dpi', '96')
     get_remote_images = BoolOption('odtexport', 'get_remote_images', True)
     replace_keyword = Option('odtexport', 'replace_keyword', 'TRAC-ODT-INSERT')
+    wikiversion_keyword = Option('odtexport', 'wikiversion_keyword', 'TRAC-ODT-WIKIVERSION')
+    wikiname_keyword = Option('odtexport', 'wikiname_keyword', 'TRAC-ODT-WIKINAME')
+    timestamp_keyword = Option('odtexport', 'timestamp_keyword', 'TRAC-ODT-TIMESTAMP')
     cut_start_keyword = Option('odtexport', 'cut_start_keyword',
                                'TRAC-ODT-CUT-START')
     cut_stop_keyword = Option('odtexport', 'cut_stop_keyword',
@@ -67,13 +71,16 @@ class ODTExportPlugin(Component):
         template = self.get_template_name(content)
         html = self.wiki_to_html(content, req)
         #return (html, "text/plain")
-        odtfile = ODTFile(self.page_name, template, self.env, # pylint: disable-msg=E1101
+        odtfile = ODTFile(self.page_name, req.args.get('version', 'latest'), template, self.env, # pylint: disable-msg=E1101
                           options={
                               "img_width": self.img_width,
                               "img_height": self.img_height,
                               "img_dpi": self.img_dpi,
                               "get_remote_images": self.get_remote_images,
                               "replace_keyword": self.replace_keyword,
+                              "wikiversion_keyword": self.wikiversion_keyword,
+                              "wikiname_keyword": self.wikiname_keyword,
+                              "timestamp_keyword": self.timestamp_keyword,
                               "cut_start_keyword": self.cut_start_keyword,
                               "cut_stop_keyword": self.cut_stop_keyword,
                           })
@@ -155,8 +162,9 @@ class ODTFile(object):
         template. The first match found is used. 
     """
 
-    def __init__(self, page_name, template, env, options):
+    def __init__(self, page_name, page_version, template, env, options):
         self.page_name = page_name
+        self.page_version = page_version
         self.template = template
         self.env = env
         self.options = options
@@ -344,9 +352,21 @@ class ODTFile(object):
         return full_tag.replace(src, newsrc)
 
     def insert_content(self, content):
-        self.options["replace_keyword"] = str(self.options["replace_keyword"])
-        self.options["cut_start_keyword"] = str(self.options["cut_start_keyword"])
-        self.options["cut_stop_keyword"] = str(self.options["cut_stop_keyword"])
+        for kw in ["replace", "wikiversion", "wikiname", "timestamp", "cut_start", "cut_stop"]:
+            self.options[kw+"_keyword"] = str(self.options[kw+"_keyword"])
+
+        for k, v in {'wikiversion': str(self.page_version),
+                     'wikiname': str(self.page_name),
+                     'timestamp': str(datetime.datetime.now())}.iteritems():
+            for xmltype in self.xml.keys():
+                #self.env.log.debug("Key %s, Value %s, xml %s\n"%(k,v,xmltype))
+                if self.options[k+"_keyword"] and self.xml[xmltype].count(
+                    self.options[k+"_keyword"]) > 0:
+                    self.xml[xmltype] = re.sub(
+                        re.escape(self.options[k+"_keyword"]),
+                        v, self.xml[xmltype])
+
+
         if self.options["replace_keyword"] and self.xml["content"].count(
                 self.options["replace_keyword"]) > 0:
             self.xml["content"] = re.sub(
