comparison piecrust/importing/jekyll.py @ 62:52e4d9a1f917

Simple importer for PieCrust 1 websites.
author Ludovic Chabant <ludovic@chabant.com>
date Wed, 27 Aug 2014 17:14:44 -0700
parents 6e60e0fef2be
children 2823ea40cfac
comparison
equal deleted inserted replaced
61:64f37c4cce68 62:52e4d9a1f917
3 import re 3 import re
4 import shutil 4 import shutil
5 import yaml 5 import yaml
6 import logging 6 import logging
7 from piecrust.configuration import parse_config_header 7 from piecrust.configuration import parse_config_header
8 from piecrust.importing.base import Importer 8 from piecrust.importing.base import FileWalkingImporter
9 from piecrust.uriutil import multi_replace 9 from piecrust.uriutil import multi_replace
10 10
11 11
12 logger = logging.getLogger(__name__) 12 logger = logging.getLogger(__name__)
13 13
14 14
15 class JekyllImporter(Importer): 15 class JekyllImporter(FileWalkingImporter):
16 def __init__(self): 16 def __init__(self):
17 super(JekyllImporter, self).__init__() 17 super(JekyllImporter, self).__init__()
18 self.name = 'jekyll' 18 self.name = 'jekyll'
19 self.description = "Imports content from a Jekyll or Octopress blog." 19 self.description = "Imports content from a Jekyll or Octopress blog."
20 20
21 def setupParser(self, parser, app): 21 def setupParser(self, parser, app):
22 super(JekyllImporter, self).setupParser(parser, app)
22 parser.add_argument('root_dir', 23 parser.add_argument('root_dir',
23 help="The root directory of the Jekyll or Octopress website.") 24 help="The root directory of the Jekyll or Octopress website.")
24 25
25 def importWebsite(self, app, args): 26 def importWebsite(self, app, args):
26 logger.debug("Importing Jekyll site from: %s" % args.root_dir) 27 logger.debug("Importing Jekyll site from: %s" % args.root_dir)
27 for dirpath, dirnames, filenames in os.walk(args.root_dir): 28 self._startWalk(args.root_dir, args.exclude, app)
28 dirnames[:] = list(filter(lambda i: not i[0] == '.', dirnames))
29 for fn in filenames:
30 if fn[0] == '.':
31 continue
32
33 full_fn = os.path.join(dirpath, fn)
34 rel_fn = os.path.relpath(full_fn, args.root_dir)
35 if rel_fn.startswith('.' + os.sep):
36 rel_fn = fn
37
38 logger.debug("- %s" % rel_fn)
39 if rel_fn == '_config.yml':
40 self.convertConfig(app, full_fn)
41 elif rel_fn.startswith('_layouts'):
42 self.convertLayout(app, full_fn, rel_fn[len('_layouts/'):])
43 elif rel_fn.startswith('_includes'):
44 self.convertInclude(app, full_fn, rel_fn[len('_includes/'):])
45 elif rel_fn.startswith('_posts'):
46 self.convertPost(app, full_fn, rel_fn[len('_posts/'):])
47 else:
48 with open(full_fn, 'rb') as fp:
49 firstline = fp.read(3)
50 if firstline == '---':
51 self.convertPage(app, full_fn, rel_fn)
52 else:
53 self.convertStatic(app, full_fn, rel_fn)
54
55 logger.info("The Jekyll website was successfully imported.") 29 logger.info("The Jekyll website was successfully imported.")
30
31 def _importFile(self, full_fn, rel_fn, app):
32 logger.debug("- %s" % rel_fn)
33 if rel_fn == '_config.yml':
34 self.convertConfig(app, full_fn)
35 elif rel_fn.startswith('_layouts'):
36 self.convertLayout(app, full_fn, rel_fn[len('_layouts/'):])
37 elif rel_fn.startswith('_includes'):
38 self.convertInclude(app, full_fn, rel_fn[len('_includes/'):])
39 elif rel_fn.startswith('_posts'):
40 self.convertPost(app, full_fn, rel_fn[len('_posts/'):])
41 else:
42 with open(full_fn, 'rb') as fp:
43 firstline = fp.read(3)
44 if firstline == '---':
45 self.convertPage(app, full_fn, rel_fn)
46 else:
47 self.convertStatic(app, full_fn, rel_fn)
56 48
57 def convertConfig(self, app, src_path): 49 def convertConfig(self, app, src_path):
58 logger.debug(" Converting configuration file.") 50 logger.debug(" Converting configuration file.")
59 with open(src_path, 'r', encoding='utf8') as fp: 51 with open(src_path, 'r', encoding='utf8') as fp:
60 config = yaml.load(fp) 52 config = yaml.load(fp)