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.

14 年之前
14 年之前
14 年之前
14 年之前
14 年之前
14 年之前
14 年之前
14 年之前
14 年之前
14 年之前
14 年之前
14 年之前
14 年之前
14 年之前
14 年之前
14 年之前
14 年之前
14 年之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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, root_path):
  56. """
  57. Build all js files, index.html and template.html
  58. """
  59. from build.version import VersionControl
  60. self.vc = VersionControl(root_path)
  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()