Added ability to record metadata at top of page data file.
[cmless] / bin / misc.py
index 821ef34..846ebfe 100644 (file)
@@ -2,10 +2,10 @@
 # See LICENSE file for copyright and license details.
 
 # Python imports
 # See LICENSE file for copyright and license details.
 
 # Python imports
-import sys, subprocess
+import os, sys, subprocess, configparser
 
 # CMless imports
 
 # CMless imports
-import debug, config
+import debug, config, template
 
 # Accepts a string containing a filesystem path to a text file.
 # Returns a string containing the contents of that file.
 
 # Accepts a string containing a filesystem path to a text file.
 # Returns a string containing the contents of that file.
@@ -30,3 +30,31 @@ def process_markup(text):
         sys.exit("Unable to open markup processor: " + config.markup_processor)
     text = p.communicate(text.encode('UTF-8'))[0]
     return(text.decode('UTF-8'))
         sys.exit("Unable to open markup processor: " + config.markup_processor)
     text = p.communicate(text.encode('UTF-8'))[0]
     return(text.decode('UTF-8'))
+
+def strip_page_metadata(content):
+    keyword = template.add_delimiter("END_PAGE_METADATA")
+    index = content.find(keyword)
+    if index != -1:
+        return content[index+len(keyword):]
+    else:
+        return content
+
+def lookup_page_metadata(key):
+    page_file = load_file(config.site_data_prefix + os.environ['REQUEST_URI'] +
+            config.markup_file_extension)
+
+    keyword_begin = template.add_delimiter("BEGIN_PAGE_METADATA")
+    keyword_end = template.add_delimiter("END_PAGE_METADATA")
+    index = page_file.find(keyword_end)
+    if index != -1:
+        page_file = page_file[len(keyword_begin):index]
+    else:
+        page_file = ""
+
+    page_metadata = configparser.ConfigParser()
+    page_metadata.read_string(page_file)
+
+    if 'DEFAULT' in page_metadata and key in page_metadata['DEFAULT']:
+        return page_metadata['DEFAULT'][key]
+    else:
+        return ""