Skip to content
Browse files

Refactor wok.py to be a class.

  • Loading branch information...
1 parent a85e11e commit d8e0ee05b50e2c41dd4ae288ac3d13685a1b8af3 @mythmon committed May 6, 2011
Showing with 72 additions and 58 deletions.
  1. +1 −1 scripts/wok
  2. 0 setup.py
  3. +71 −57 wok/wok.py
View
2 scripts/wok
@@ -1,3 +1,3 @@
#!/usr/bin/python2
from wok import wok
-wok.main()
+wok.Wok()
View
0 setup.py 100644 → 100755
File mode changed.
View
128 wok/wok.py
@@ -7,62 +7,76 @@
from page import Page
import renderers
-options_defaults = {
- 'content_dir' : 'content',
- 'template_dir': 'templates',
- 'output_dir' : 'output',
- 'media_dir' : 'media',
- 'site_title' : 'Some random Wok site',
-}
-
-def main():
- options = options_defaults.copy()
- if os.path.isfile('config'):
- with open('config') as f:
- yaml_config = yaml.load(f)
-
- if yaml_config:
- options.update(yaml_config)
-
- if os.path.isdir(options['output_dir']):
- shutil.rmtree(options['output_dir'])
- os.mkdir(options['output_dir'])
-
- for name in os.listdir(options['media_dir']):
- path = os.path.join(options['media_dir'], name)
- if os.path.isdir(path):
- shutil.copytree(path, os.path.join(options['output_dir'],name), symlinks=True)
- else:
- shutil.copy(path, options['output_dir'])
-
- site_pages = []
-
- for root, dirs, files in os.walk(options['content_dir']):
- # Grab all the parsable files
- for f in files:
- ext = f.split('.')[-1]
- renderer = renderers.Plain
-
- for r in renderers.all:
- if ext in r.extensions:
- renderer = r
-
- site_pages.append(Page(os.path.join(root,f), options, renderer))
-
- site_tree = {}
- site_pages.sort(key=lambda p: len(p.meta['category']))
- for p in site_pages:
- parent = site_tree
- for cat in p.meta['category']:
- assert(cat in parent)
- parent = parent[cat].subpages
- parent[p.meta['title']] = p
-
- for p in site_pages:
- p.render()
-
- if p.meta['published']:
- p.write()
+class Wok(object):
+ default_options = {
+ 'content_dir' : 'content',
+ 'template_dir': 'templates',
+ 'output_dir' : 'output',
+ 'media_dir' : 'media',
+ 'site_title' : 'Some random Wok site',
+ }
+
+ def __init__(self):
+ self.all_pages = []
+
+ self.read_options()
+ self.prepare_output()
+ self.load_pages()
+ self.make_tree()
+ self.render_site()
+
+ def read_options(self):
+ self.options = Wok.default_options.copy()
+
+ if os.path.isfile('config'):
+ with open('config') as f:
+ yaml_config = yaml.load(f)
+
+ if yaml_config:
+ self.options.update(yaml_config)
+
+ def prepare_output(self):
+ if os.path.isdir(self.options['output_dir']):
+ shutil.rmtree(self.options['output_dir'])
+ os.mkdir(self.options['output_dir'])
+
+ for name in os.listdir(self.options['media_dir']):
+ path = os.path.join(self.options['media_dir'], name)
+ if os.path.isdir(path):
+ shutil.copytree(path, os.path.join(self.options['output_dir'],name), symlinks=True)
+ else:
+ shutil.copy(path, self.options['output_dir'])
+
+ def load_pages(self):
+ for root, dirs, files in os.walk(self.options['content_dir']):
+ # Grab all the parsable files
+ for f in files:
+ ext = f.split('.')[-1]
+ renderer = renderers.Plain
+
+ for r in renderers.all:
+ if ext in r.extensions:
+ renderer = r
+
+ self.all_pages.append(Page(os.path.join(root,f), self.options, renderer))
+
+ def make_tree(self):
+ site_tree = {}
+ # We want to parse these in a approximately breadth first order
+ self.all_pages.sort(key=lambda p: len(p.category))
+
+ for p in self.all_pages:
+ parent = site_tree
+ for cat in p.category:
+ assert(cat in parent)
+ parent = parent[cat].subpages
+ parent[p.title] = p
+
+ def render_site(self):
+ for p in self.all_pages:
+ if p.published:
+ p.render()
+ p.write()
if __name__ == '__main__':
- main()
+ Wok()

0 comments on commit d8e0ee0

Please sign in to comment.
Something went wrong with that request. Please try again.