add create and edit commands for managing the site
4 files changed, 112 insertions(+), 8 deletions(-)

M muyhomepage2/app/cli.py
A => muyhomepage2/app/modifying.py
M muyhomepage2/util.py
M skins/default/tagglobal.html
M muyhomepage2/app/cli.py +48 -6
@@ 5,6 5,7 @@ import sys, os
 import vanity.commander
 
 import muyhomepage2.app.build
+import muyhomepage2.app.modifying
 
 
 def main(argv):

          
@@ 16,14 17,14 @@ def abort(msg, code=1):
     sys.stderr.write('error: %s\n' % msg)
     sys.exit(code)
 
-def _newconfig(opts):
+def _newconfig(opts, base='.'):
     """get a new configuration object"""
     if opts.get('config'):
         cfiles = [opts.get('config')]
     else:
         cfiles = [
-            os.path.join('.', '.homepagerc'),
-            os.path.join('.', '.muyhomepagerc'),
+            os.path.join(base, '.homepagerc'),
+            os.path.join(base, '.muyhomepagerc'),
             ]
     for cfile in cfiles:
         if os.path.isfile(cfile):

          
@@ 79,8 80,31 @@ def dumptitles(sourcedir='.', **opts):
 
 
 
-def edit(title, **opts):
-    pass
+def editpage(sourcedir='.', title=None, **opts):
+    config = _newconfig(opts, sourcedir)
+    if not title:
+        abort('you must specify a page title')
+    filename = muyhomepage2.app.modifying.oldfile(sourcedir, title, opts.get('year'))
+    if filename is None:
+        abort('cannot find any title like: %s' % title)
+    muyhomepage2.app.modifying.edit(filename, config)
+
+
+
+def createpage(sourcedir='.', title=None, tag=None, **opts):
+    config = _newconfig(opts, sourcedir)
+    if not title:
+        abort('you must specify a page title')
+    filename = muyhomepage2.app.modifying.newfile(
+            sourcedir, title, opts.get('year'))
+    muyhomepage2.app.modifying.initpage(
+            filename, config, title, list(tag or []))
+    print 'Created page file: %s' % filename
+    if opts.get('edit'):
+        muyhomepage2.app.modifying.edit(filename, config)
+
+
+
 
 
 OPTS = [

          
@@ 93,13 117,31 @@ CMDS = {
     'flushcache': {
         'target':flushcache, 
         'opts':[], 
-        'help': ''
+        'help': '[SOURCEDIR]'
         },
     'build': {
         'target': build,
         'opts': [],
         'help': '[SOURCEDIR]',
         },
+    'create': {
+        'target': createpage,
+        'opts': [
+            ('title', 't', '', 'new page title'),
+            ('tag', 'T', [], 'a tag for the page'),
+            ('year', 'y', None, 'place page in a directory named for the year'),
+            ('edit', 'e', None, 'edit page after creating it'),
+            ],
+        'help': '[SOURCEDIR] -t TITLE [-T TAG]'
+        },
+    'edit': {
+        'target': editpage,
+        'opts': [
+            ('title', 't', '', 'new page title'),
+            ('year', 'y', None, 'place page in a directory named for the year'),
+            ],
+        'help': '[SOURCEDIR] -t TITLE'
+        },
     'dumptags': {
         'target': dumptags,
         'opts': [

          
A => muyhomepage2/app/modifying.py +58 -0
@@ 0,0 1,58 @@ 
+"""utility functions for editing in the source pages
+"""
+
+import os
+import time
+import tempfile, shutil
+from muyhomepage2 import util
+
+
+def newfile(root, title, year=False):
+    name = '%s.page' % util.encodetitle(title)
+    if year:
+        name = os.path.join(time.strftime('%Y'), name)
+    return os.path.join(root, name)
+
+
+def oldfile(root, title, year=False):
+    found = None
+    name = '%s.page' % util.encodetitle(title)
+    for dpath, dirs, files in os.walk(root):
+        for fn in files:
+            if fn == name:
+                found = os.path.join(dpath, fn)
+    return found
+
+
+def initpage(filename, config, title, tags):
+    if not os.path.isdir(os.path.dirname(filename)):
+        os.makedirs(os.path.dirname(filename))
+    fp = open(filename, 'w')
+    try:
+        now = util.When.now()
+        fp.write('Title: %s\n' % title)
+        fp.write('Created: %s\n' % now.storefmt())
+        if tags:
+            fp.write('Tags: %s\n' % ', '.join(tags))
+        fp.write('\n')
+        fp.write('(This page has not been edited.)\n')
+        pass
+    finally:
+        fp.close()
+
+
+def edit(filename, config):
+    if 'EDITOR' not in os.environ:
+        os.environ['EDITOR'] = 'vi'
+    basename = os.path.basename(filename)
+    fd, tname = tempfile.mkstemp(suffix='mheditor-%s' % basename)
+    fp = os.fdopen(fd, 'w+')
+    shutil.copyfileobj(open(filename), fp)
+    fp.flush()
+    sts = os.system("$EDITOR '%s'" % tname)
+    if sts == 0:
+        fp.seek(0)
+        shutil.copyfileobj(fp, open(filename, 'w'))
+    os.unlink(tname)
+    fp.close()
+

          
M muyhomepage2/util.py +1 -1
@@ 74,7 74,7 @@ class When(object):
         return time.strftime(TIME_FMT_MUY, self.local())
 
     def storefmt(self):
-        return time.strftime(TIME_FMT_MUY, self.utc())
+        return time.strftime(TIME_FMT_MUY.replace('%Z', 'UTC'), self.utc())
 
     def dayfmt(self):
         return time.strftime(TIME_FMT_ATOMID, self.utc())

          
M skins/default/tagglobal.html +5 -1
@@ 18,8 18,12 @@ 
     {% for link, tag, pages in ext.tagdetails %}
     <li><a name="{{link}}"></a><span>{{tag}}</span>
         <ul class="altlist">
+        {# the css must be zero indexed #}
+        {% set oeclass = cycler('even', 'odd') %}
         {% for purl, ptitle in pages %}
-            <li class="xxx"><a href="{{purl}}">{{ptitle}}</a></li>
+            <li class="tagitem {{ oeclass.next() }}">
+                <a href="{{purl}}">{{ptitle}}</a>
+            </li>
         {% endfor %}
         </ul>
     </li>