You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

83 regels
1.9 KiB

  1. verbose = False
  2. class Project:
  3. """
  4. Build a project
  5. Make files::
  6. index.html
  7. assets/template.html
  8. assets/js/core.min.js
  9. assets/timestamps.json
  10. """
  11. def __init__(self,):
  12. """
  13. load libraries
  14. """
  15. from build.timestamps import Timestamps
  16. from build.bundle import Bundle
  17. from nav import Nav
  18. self.timestamps = Timestamps()
  19. self.bundle = Bundle()
  20. self.nav = Nav()
  21. def boot(self):
  22. """
  23. returns bootstrap js
  24. """
  25. import json
  26. corejs = open('lib/js/core.min.js', 'r')
  27. boot = 'var asset_timestamps_=' + self.timestamps.get('json', ('js', 'html', 'css')) \
  28. + '\n' + corejs.read()
  29. corejs.close()
  30. return boot
  31. def render_templates(self):
  32. """
  33. Generate static files from templates
  34. """
  35. # render templates
  36. import os
  37. from jinja2 import Environment, FileSystemLoader
  38. from build.markdown2_extn import Markdown2Extension
  39. env = Environment(loader=FileSystemLoader('templates'), extensions=[Markdown2Extension])
  40. # dynamic boot info
  41. env.globals['boot'] = self.boot()
  42. env.globals['nav'] = self.nav.html()
  43. page_info = self.nav.page_info()
  44. for wt in os.walk('templates'):
  45. for fname in wt[2]:
  46. if fname.split('.')[-1]=='html' and not fname.startswith('template'):
  47. fpath = os.path.relpath(os.path.join(wt[0], fname), 'templates')
  48. temp = env.get_template(fpath)
  49. env.globals.update(self.nav.page_info_template)
  50. env.globals.update(page_info.get(fpath, {}))
  51. # out file in parent folder of template
  52. f = open(fpath, 'w')
  53. f.write(temp.render())
  54. f.close()
  55. print "Rendered %s | %.2fkb" % (fpath, os.path.getsize(fpath) / 1024.0)
  56. def build(self):
  57. """
  58. Build all js files, timestamps.js, index.html and template.html
  59. """
  60. # make bundles
  61. self.bundle.bundle(self.timestamps)
  62. # index, template if framework is dirty
  63. if self.timestamps.dirty:
  64. self.render_templates()
  65. self.timestamps.write()