Commits

faassen committed 919fa68

Allow generating the blog even though the code isn't checked into
hg yet. Useful for my testing purposes.

  • Participants
  • Parent commits 067d355

Comments (0)

Files changed (1)

 # -*- encoding: utf-8 -*-
 from string import Template
 from datetime import datetime, timedelta
-from time import time
-import os, os.path, shutil, re
+from time import time, mktime
+import sys, os, os.path, shutil, re
 from docutils.core import publish_parts
 from hgapi import hgapi
 import PyRSS2Gen
             'description': u"Unknown description",
             'entries': 'entries',
             },
+        'datetime': {
+            'use_filename_for_creation_time': False,
+            },
         'html': {
             'templates_path': 'templates',
             'html_path': 'html',
     entries.sort(reverse=True)
     return entries
 
-def get_data_entries(entries, entries_path):
+def get_data_entries(entries, entries_path, use_filename):
     """Read and parse entries, return document parts as generated by docutils"""
     data = []
     for entry in entries:
         with open(os.path.join(entries_path, entry)) as source:
             parts = get_data(source.read().decode("utf-8"))
-            parts.update(get_entry_dates(entry, entries_path))
+            if not use_filename:
+                entry_timeinfo = get_entry_dates(entry, entries_path)
+            else:
+                entry_timeinfo = get_filename_entry_dates(entry)
+            parts.update(entry_timeinfo)
             data.append(parts)
     return data
 
         modified = hgdate_to_utc(times[0])
     return {'created': created, 'modified': modified}
 
+def get_filename_entry_dates(entry):
+    """Get creation and last modification based on filename as unix time UTC."""
+    year, month, day, extra = entry.split('-')
+    t = int(mktime(datetime(int(year), int(month), int(day)).timetuple()))
+    return {'created': t, 'modified': t}
+
 def get_timestamp(created, modified, created_tpl, modified_tpl):
     """Return string representing timestamp in post"""
     cr, mod = [datetime.fromtimestamp(ts) for ts in created, modified]
         f.close()
         return result
 
-def generate_blaag():
+def generate_blaag(config_path):
     """Generate html from blaag entries."""
-    config = get_configuration('blaag.cfg')
+    config = get_configuration(config_path)
 
     templates = Templates(config.get('html', 'templates_path'))
 
 
     entries_path = config.get('content', 'entries_path')
     entries = get_entries(entries_path)
-    data = get_data_entries(entries, entries_path)
+    data = get_data_entries(
+        entries, entries_path,
+        config.getboolean('datetime', 'use_filename_for_creation_time'))
     main = []
 
     base_url = config.get('html', 'base_url')
         
 def pretxncommit_hook(*args, **kwargs):
     """Run before commit"""
-    generate_blaag()
+    generate_blaag('blaag.cfg')
 
 if __name__ == "__main__":
-    generate_blaag()
+    try:
+        config_path = sys.argv[1]
+    except IndexError:
+        config_path = 'blaag.cfg'
+
+    generate_blaag(config_path)
+