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.
 
 
 
 
 
 

91 lines
2.0 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.bundle import Bundle
  16. from nav import Nav
  17. self.bundle = Bundle()
  18. self.nav = Nav()
  19. def boot(self):
  20. """
  21. returns bootstrap js
  22. """
  23. import json
  24. corejs = open('lib/js/core.min.js', 'r')
  25. v = int(self.vc.repo.get_value('last_version_number') or 0) + 1
  26. boot = ('window._version_number="%s"' % str(v)) + \
  27. '\n' + corejs.read()
  28. corejs.close()
  29. return boot
  30. def render_templates(self):
  31. """
  32. Generate static files from templates
  33. """
  34. # render templates
  35. import os
  36. from jinja2 import Environment, FileSystemLoader
  37. from build.markdown2_extn import Markdown2Extension
  38. env = Environment(loader=FileSystemLoader('templates'), extensions=[Markdown2Extension])
  39. # dynamic boot info
  40. env.globals['boot'] = self.boot()
  41. env.globals['nav'] = self.nav.html()
  42. page_info = self.nav.page_info()
  43. for wt in os.walk('templates'):
  44. for fname in wt[2]:
  45. if fname.split('.')[-1]=='html' and not fname.startswith('template'):
  46. fpath = os.path.relpath(os.path.join(wt[0], fname), 'templates')
  47. temp = env.get_template(fpath)
  48. env.globals.update(self.nav.page_info_template)
  49. env.globals.update(page_info.get(fpath, {}))
  50. # out file in parent folder of template
  51. f = open(fpath, 'w')
  52. f.write(temp.render())
  53. f.close()
  54. print "Rendered %s | %.2fkb" % (fpath, os.path.getsize(fpath) / 1024.0)
  55. def build(self):
  56. """
  57. Build all js files, index.html and template.html
  58. """
  59. from build.version import VersionControl
  60. self.vc = VersionControl()
  61. self.vc.add_all()
  62. # index, template if framework is dirty
  63. if self.vc.repo.uncommitted():
  64. self.bundle.bundle(self.vc)
  65. self.render_templates()
  66. # again add all bundles
  67. self.vc.add_all()
  68. self.vc.repo.commit()
  69. self.vc.close()