Exemple #1
0
def generate_templates(src_path, dst_path, locale):
    jinjaenv.init(src_path, locale)
    env = jinjaenv.get()
    for url in env.renderable_urls():
        # Render
        rendered = env.render_path(url)
        if rendered is None:
            print "Warning: Could not find template for url", url
            continue

        # Save
        template_name = env.template_name(url)
        if os.path.splitext(template_name)[1] == '':
            template_name = os.path.join(template_name, "index.html")
        dst_file_path = os.path.join(dst_path, template_name)
        dst_dirname = os.path.dirname(dst_file_path)
        if not os.path.exists(dst_dirname):
            os.makedirs(dst_dirname)
        with open(dst_file_path, "w") as f:
            f.write(rendered)

    # Copy media files
    for folder in media.folders():
        src_folder = os.path.join(src_path, folder)
        dst_folder = os.path.join(dst_path, folder)
        if os.path.exists(dst_folder):
            # TODO synchronize files intelligently
            shutil.rmtree(dst_folder)
        shutil.copytree(src_folder, dst_folder)
Exemple #2
0
    def send_head(self):
        path = self.translate_path(self.path)
        if os.path.isdir(path):
            if not self.path.endswith('/'):
                # redirect browser - doing basically what apache does
                self.send_response(301)
                self.send_header("Location", self.path + "/")
                self.end_headers()
                return None
            for index in "index.html", "index.htm":
                index = os.path.join(path, index)
                if os.path.exists(index):
                    path = index
                    break
            else:
                return self.list_directory(path)
        ctype = self.guess_type(jinjaenv.get().get_path(path))

        rendered = jinjaenv.get().render_path(path)
        if rendered is None:
            self.send_error(404, "File not found: " + path)
            return None

        # Content length and last-modified attributes
        #fs = os.fstat(f.fileno())
        #content_length = fs[6]
        #last_modified = self.date_time_string(fs.st_mtime)

        f = StringIO()
        f.write(rendered)
        content_length = f.tell()
        f.seek(0)

        self.send_response(200)
        self.send_header("Content-type", ctype)
        self.send_header("Content-Length", str(content_length))
        #self.send_header("Last-Modified", last_modified)
        self.end_headers()
        return f
Exemple #3
0
def blog_post_content(alias):
    return jinjaenv.get().get_variable(alias, "post")
Exemple #4
0
def add_alias(alias, template_name, variables={}):
    jinjaenv.get().add_alias(alias, template_name, variables)
Exemple #5
0
def latest_pages(count):
    dates = [(d, t) for t, d in jinjaenv.get().templates_with_variable("date")]
    dates.sort(reverse=True)
    return [d[1] for d in dates[:count]]
Exemple #6
0
def get_title(template_name):
    return jinjaenv.get().get_variable(template_name, "title")
Exemple #7
0
def set_title(template_name, title):
    jinjaenv.get().set_variable(template_name, "title", title)
    return ""
Exemple #8
0
def get_date(template_name):
    return jinjaenv.get().get_variable(template_name, "date")
Exemple #9
0
def set_date(template_name, date):
    jinjaenv.get().set_variable(template_name, "date", date)
    return ""