一个(真正)最小的静态网站生成器,带有python和jinja2

本文介绍了作者如何为简单静态网站创建一个最小的生成器,以避免重复的工作。通过使用Python和Jinja2,可以实现服务器端的模版化,而无需Flask服务器。文章提到了作者曾尝试过Jekyll、Pelican和Lektor等静态网站生成器,但最终选择编写自己的轻量级生成器,该生成器仅包含一个`compiler.py`文件和一个`src/`目录,其中包含静态资源和Jinja模板。
摘要由CSDN通过智能技术生成

From time to time I throw together a static HTML site for something and love how easy it is to just upload the files to s3 and have a fast site with pretty much no cost or work involved.  For single-page sites in particular, this is pretty near painless, but as you start to get to a few different HTML pages, work starts repeating.

我不时地将静态HTML站点放在一起进行处理,并且喜欢将文件上传到s3并拥有一个快速站点而几乎没有任何成本或工作是多么容易。 特别是对于单页网站,这几乎是轻松的,但是随着您开始访问几个不同HTML页面,工作开始重复。

For larger projects (like GitNOC, or PedalWrencher), I love using Flask, and through that have gotten pretty proficient and pleased with Jinja2 for server-side templating.

对于较大的项目(例如GitNOCPedalWrencher ),我喜欢使用Flask,并且通过该项目,他们非常熟练并且对Jinja2感到满意,可以在服务器端进行模板制作。

So I found myself in this middleground, where I didn’t really need a Flask server for a simple static site, but HTML was getting a bit annoying to muck around with directly.  The natural fit for that is one of the many static site generators out there.  I’ve used at one point or another:

因此,我发现自己处于这个中间地带,在那里我对于一个简单的静态站点实际上并不需要Flask服务器,但是HTML变得令人讨厌直接使用。 很自然的方法是那里有很多静态站点生成器之一。 我曾经在某一点或另一点使用过:

  1. Jekyll
  2. Pelican
  3. Lektor
  1. 杰基尔
  2. 莱克托

All of which are great in their own rights, but in my case, I really only wanted to get the modularity afforded by Jinja2, and really didn’t need a blog or CMS or anything like that.  So I just wrote a minimal generator.  Heres how it works:

所有这些都拥有自己的权利,但是就我而言,我真的只想获得Jinja2提供的模块化,并且实际上不需要博客或CMS之类的东西。 所以我只写了一个最小的生成器。 这是它的工作方式:

To start with, my directory is just a single python file: compiler.py and a single directory: src/.  In src, I put in a subdirectory, static, with all of my images, CSS, and javascript (if I have any), and then I put my jinja templates in src.

首先,我的目录只有一个python文件:compiler.py和一个目录:src /。 在src中,我将所有图像,CSS和javascript(如果有的话)放在一个静态子目录中,然后将jinja模板放入src中。

Compiler looks like:

编译器看起来像:

from jinja2 import Environment
import os
import shutil
from jinja2 import BaseLoader, TemplateNotFound
from os.path import join, exists, getmtime


class Loader(BaseLoader):
    def __init__(self, path):
        self.path = path

    def get_source(self, environment, template):
        path = join(self.path, template)
        if not exists(path):
            raise TemplateNotFound(template)
        mtime = getmtime(path)
        with open(path, 'r') as f:
            source = f.read()
        return source, path, lambda: mtime == getmtime(path)


class SourceBundle(object):
    def __init__(self, static_dir='static', src_dir='src', templates=None, dist_dir='dist'):
        if templates is None:
            self.templates = ['index.html']
        else:
            self.templates = templates

        self.static_dir = static_dir
        self.src_dir = src_dir
        self.dist_dir = dist_dir

    def _root(self):
        return os.path.dirname(os.path.abspath(__file__))

    def clean(self):
        if os.path.exists(self.dist_dir):
            shutil.rmtree(self.dist_dir)

    def build(self, **kwargs):
        if not os.path.exists(os.path.join(self._root(), self.dist_dir)):
            os.mkdir(os.path.join(self._root(), self.dist_dir))

        if self.static_dir is not None:
            if os.path.exists(os.path.join(self._root(), self.dist_dir, self.static_dir)):
                shutil.rmtree(os.path.join(self._root(), self.dist_dir, self.static_dir))
            shutil.copytree(
                os.path.join(self._root(), self.src_dir, self.static_dir),
                os.path.join(self._root(), self.dist_dir, self.static_dir)
            )

        env = Environment(loader=Loader(os.path.join(self._root(), self.src_dir)))
        for template in self.templates:
            with open(os.path.join(self._root(), self.dist_dir, template), 'w') as f2:
                slug = env.get_template(template)
                slug = slug.render(**kwargs)
                f2.write(slug)

        return True

if __name__ == '__main__':
    sb = SourceBundle(templates=[
        'index.html'
    ])
    sb.build()

翻译自: https://www.pybloggers.com/2016/03/a-really-minimal-static-website-generator-with-python-and-jinja2/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值