John Mulligan avatar John Mulligan committed 5206fa5

add create and edit commands for managing the site

Comments (0)

Files changed (4)

muyhomepage2/app/cli.py

 import vanity.commander
 
 import muyhomepage2.app.build
+import muyhomepage2.app.modifying
 
 
 def main(argv):
     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):
 
 
 
-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 = [
     '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': [

muyhomepage2/app/modifying.py

+"""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()
+

muyhomepage2/util.py

         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())

skins/default/tagglobal.html

     {% 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>
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.