comparison piecrust/commands/builtin/scaffolding.py @ 187:d5b7c2a4ec9d

prepare: Add user-defined scaffolding templates. Any files found in `scaffold/pages` will be used as possible templates for the `chef prepare` command, in addition to the default ones.
author Ludovic Chabant <ludovic@chabant.com>
date Sun, 04 Jan 2015 15:49:31 -0800
parents 8355eb9dd8fe
children f130365568ff
comparison
equal deleted inserted replaced
186:e61fbae61402 187:d5b7c2a4ec9d
1 import os 1 import os
2 import os.path 2 import os.path
3 import re 3 import re
4 import io 4 import io
5 import time 5 import time
6 import glob
6 import logging 7 import logging
7 import textwrap 8 import textwrap
8 from piecrust import RESOURCES_DIR 9 from piecrust import RESOURCES_DIR
9 from piecrust.chefutil import print_help_item 10 from piecrust.chefutil import print_help_item
10 from piecrust.commands.base import ExtendableChefCommand, ChefCommandExtension 11 from piecrust.commands.base import ExtendableChefCommand, ChefCommandExtension
81 None) 82 None)
82 if ext is None: 83 if ext is None:
83 raise Exception("No such page template: %s" % tpl_name) 84 raise Exception("No such page template: %s" % tpl_name)
84 85
85 tpl_text = ext.getTemplate(ctx.app, tpl_name) 86 tpl_text = ext.getTemplate(ctx.app, tpl_name)
87 if tpl_text is None:
88 raise Exception("Error loading template: %s" % tpl_name)
86 title = (metadata.get('slug') or metadata.get('path') or 89 title = (metadata.get('slug') or metadata.get('path') or
87 'Untitled page') 90 'Untitled page')
88 title = make_title(title) 91 title = make_title(title)
89 tokens = { 92 tokens = {
90 '%title%': title, 93 '%title%': title,
99 with open(path, 'w') as f: 102 with open(path, 'w') as f:
100 f.write(tpl_text) 103 f.write(tpl_text)
101 104
102 105
103 class DefaultPrepareTemplatesCommandExtension(ChefCommandExtension): 106 class DefaultPrepareTemplatesCommandExtension(ChefCommandExtension):
104 """ Provides the default scaffolding tempaltes to the `prepare` 107 """ Provides the default scaffolding templates to the `prepare`
105 command. 108 command.
106 """ 109 """
107 def __init__(self): 110 def __init__(self):
108 super(DefaultPrepareTemplatesCommandExtension, self).__init__() 111 super(DefaultPrepareTemplatesCommandExtension, self).__init__()
109 self.command_name = 'prepare' 112 self.command_name = 'prepare'
120 123
121 def getTemplate(self, app, name): 124 def getTemplate(self, app, name):
122 assert name in ['default', 'rss', 'atom'] 125 assert name in ['default', 'rss', 'atom']
123 src_path = os.path.join(RESOURCES_DIR, 'prepare', '%s.html' % name) 126 src_path = os.path.join(RESOURCES_DIR, 'prepare', '%s.html' % name)
124 with open(src_path, 'r', encoding='utf8') as fp: 127 with open(src_path, 'r', encoding='utf8') as fp:
128 return fp.read()
129
130
131 class UserDefinedPrepareTemplatesCommandExtension(ChefCommandExtension):
132 """ Provides user-defined scaffolding templates to the `prepare`
133 command.
134 """
135 def __init__(self):
136 super(UserDefinedPrepareTemplatesCommandExtension, self).__init__()
137 self.command_name = 'prepare'
138
139 def _getTemplatesDir(self, app):
140 return os.path.join(app.root_dir, 'scaffold/pages')
141
142 def supports(self, app):
143 return os.path.isdir(self._getTemplatesDir(app))
144
145 def getTemplateNames(self, app):
146 names = os.listdir(self._getTemplatesDir(app))
147 return map(lambda n: os.path.splitext(n)[0], names)
148
149 def getTemplateDescription(self, app, name):
150 return "User-defined template."
151
152 def getTemplate(self, app, name):
153 templates_dir = self._getTemplatesDir(app)
154 pattern = os.path.join(templates_dir, '%s.*' % name)
155 matches = glob.glob(pattern)
156 if not matches:
157 raise Exception("No such page scaffolding template: %s" % name)
158 if len(matches) > 1:
159 raise Exception(
160 "More than one scaffolding template has name: %s" % name)
161 with open(matches[0], 'r', encoding='utf8') as fp:
125 return fp.read() 162 return fp.read()
126 163
127 164
128 class DefaultPrepareTemplatesHelpTopic(ChefCommandExtension): 165 class DefaultPrepareTemplatesHelpTopic(ChefCommandExtension):
129 """ Provides help topics for the `prepare` command. 166 """ Provides help topics for the `prepare` command.