Mercurial > piecrust2
comparison piecrust/importing/base.py @ 60:6e60e0fef2be
Add `import` command, Jekyll importer.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Tue, 26 Aug 2014 23:20:48 -0700 |
parents | |
children | 52e4d9a1f917 |
comparison
equal
deleted
inserted
replaced
59:e3e3de44377c | 60:6e60e0fef2be |
---|---|
1 import os.path | |
2 import codecs | |
3 import logging | |
4 import yaml | |
5 from piecrust.pathutil import SiteNotFoundError | |
6 | |
7 | |
8 logger = logging.getLogger(__name__) | |
9 | |
10 | |
11 class Importer(object): | |
12 def __init__(self): | |
13 self.name = None | |
14 self.description = None | |
15 | |
16 def setupParser(self, parser, app): | |
17 raise NotImplementedError() | |
18 | |
19 def importWebsite(self, app, args): | |
20 raise NotImplementedError() | |
21 | |
22 def checkedImportWebsite(self, ctx): | |
23 if ctx.app.root_dir is None: | |
24 raise SiteNotFoundError() | |
25 self.importWebsite(ctx.app, ctx.args) | |
26 return 0 | |
27 | |
28 | |
29 def create_page(app, endpoint_dir, slug, metadata, content): | |
30 path = os.path.join(app.root_dir, endpoint_dir, slug) | |
31 logging.debug("Creating page: %s" % os.path.relpath(path, app.root_dir)) | |
32 header = yaml.dump(metadata) | |
33 os.makedirs(os.path.dirname(path), 0o755, True) | |
34 with codecs.open(path, 'w', 'utf8') as fp: | |
35 fp.write("---\n") | |
36 fp.write(header) | |
37 fp.write("---\n") | |
38 fp.write(content) | |
39 |