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.
对于较大的项目(例如GitNOC或PedalWrencher ),我喜欢使用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变得令人讨厌直接使用。 很自然的方法是那里有很多静态站点生成器之一。 我曾经在某一点或另一点使用过:
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/