import uuid import os import os.path import shutil from datetime import date, timedelta from jinja2 import Template from .headers import URL, Inline class Page: template = """ {{ title }} {% for style in css %} {{ style }} {% endfor %} {% for script in js %} {{ script }} {% endfor %}
{% for section in sections %} {{ section }} {% endfor %}
""" def __init__(self, sections, expire=90): self.uuid = str(uuid.uuid4()) self._sections = sections self._today = date.today() self.expire = self._today + timedelta(days=expire) def title(self): return self._sections[0].title() def summary(self, buffer): for section in self._sections: print(section.summary(), file=buffer) def prepare(self, configuration): for section in self._sections: section.prepare(configuration.get(section.name, {})) def html(self, buffer): template = Template(self.template) title = self.title() sections = [ section.html() for section in self._sections ] js = [] for section in self._sections: for script in section.js(): if isinstance(script, URL): line = "".format(url=script) elif isinstance(script, Inline): line = "".format(code=script) if line not in js: js.append(line) css = [] for section in self._sections: for style in section.css(): if isinstance(style, URL): line = "".format(url=style) elif isinstance(style, Inline): line = "".format(code=style) if line not in css: css.append(line) on_ready = [] for section in self._sections: for script in section.on_ready(): on_ready.append(script) html = template.render(title=title, sections=sections, css=css, js=js, date=self._today, expire=self.expire, on_ready=on_ready, ) print(html, file=buffer) def attachments(self): for section in self._sections: for attachment in section.attachments(): yield attachment #"//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.1.1/css/bootstrap.css" # a.gallery img { # margin-top: 3px; # } # """, # ], # "title": "", # } # self.tmpdir = tmpdir # self.pagedir = os.path.join(tmpdir, self.uuid) # os.mkdir(self.pagedir) # os.chdir(self.pagedir) # self._index = open(os.path.join(self.tmpdir, self.uuid, "index.html"), "w") # def clean(self): # shutil.rmtree(self.tmpdir) # def ready(self, line): # if line not in self.headers["ready"]: # self.headers["ready"].append(line) # """.format(title=title, date=self._today.isoformat())) # index.write("
\n")