Skip to main content

An Python-based static site generator using Jinja templates.

Project description

Jinjabread

CircleCI GitHub release PyPI downloads License

A Python-based static site generator using Jinja templates.

Inspired by staticjinja, jekyll, and hugo.

Install

pip install jinjabread

Usage

Create new site project

python -m jinjabread new mysite

Build site

python -m jinjabread build mysite

Preview site locally

python -m jinjabread serve mysite
# Visit http://127.0.0.1:8000 in your browser.

Features

  • Write pages in Markdown, HTML, or text.
  • Use Jinja2 templating language in Markdown, HTML, or text.
  • Supports YAML metadata in Markdown pages.
  • Keep static media alongside static pages.
  • Index pages (i.e., index.html) can list all other pages in the same directory.
  • Preview your static site locally with a built-in web server.
  • Prettify all generated HTML (because why not?)

File structure

Important files and directories

Name Description Example path
Project directory The project root mysite
Content directory Contains site content where each file becomes a site page mysite/content
Layouts directory Contains page layouts that gets used by the site content mysite/layouts
Static directory Contains static media that gets copied to the output directory mysite/static
Output directory The complete generated site, ready to be hosted mysite/public
Config file Custom site configurations in TOML format mysite/jinjabread.toml

Example: Site project structure

mysite/
├── content
│   ├── about.html
│   ├── index.html
│   └── posts
│       ├── index.html
│       ├── my-journey
│       │   ├── index.html
│       │   └── profile-photo.jpg
│       └── my-story.html
├── jinjabread.toml
├── layouts
│   └── base.html
├── public
│   ├── about.html
│   ├── index.html
│   ├── posts
│   │   ├── index.html
│   │   ├── my-journey
│   │   │   ├── index.html
│   │   │   └── profile-photo.jpg
│   │   └── my-story.html
│   └── static
│       └── style.css
└── static
    └── style.css

Example: File to URL translation

File path URL path
public/index.html /
public/about.html /about
public/posts/index.html /posts/
public/posts/my-story.html /posts/my-story
public/posts/my-journey/index.html /posts/my-journey/
public/posts/my-journey/photo.jpg /posts/my-journey/profile-photo.jpg
public/static/style.css /static/style.css

Site config

Default config

content_dir = "content"
layouts_dir = "layouts"
static_dir = "static"
output_dir = "public"
prettify_html = true

[context]

[[pages]]
  type = "jinjabread.MarkdownPage"
  glob_pattern = "**/*.md"
  layout_name = "base.html"

[[pages]]
  type = "jinjabread.Page"
  glob_pattern = "**/*"

Custom config

Change output directory

# jinjabread.toml
output_dir = "dist"

Add global Jinja context variables

# jinjabread.toml
[context]
  site_name = "My site"
  url_origin = "https://mysite.com"

Change Markdown layout file

# jinjabread.toml
[[pages]]
  type = "jinjabread.MarkdownPage"
  glob_pattern = "**/*.md"
  layout_name = "post.html"

[[pages]]
  type = "jinjabread.Page"
  glob_pattern = "**/*"

Add page-specific Jinja context variables

# jinjabread.toml
[[pages]]
  type = "jinjabread.MarkdownPage"
  glob_pattern = "**/*.md"
  layout_name = "base.html"

[[pages]]
  type = "jinjabread.Page"
  glob_pattern = "**/*.txt"

  [pages.context]
    foo = "bar"

[[pages]]
  type = "jinjabread.Page"
  glob_pattern = "**/*"

Pages type

Page type Keyword arguments
jinjabread.Page - glob_pattern
jinjabread.MarkdownPage - glob_pattern
- layout_name

Context variables

Default variables

All pages have these context variables:

Name Description Example value
url_path The URL path of the current page /
file_path The file path of the current page, relative to the output directory index.html

Variable precedence

Page-specific context variables override global site context variables.

For example:

# jinjabread.toml
[context]
  fee = "fie"
  foe = "fum"

[[pages]]
  type = "jinjabread.Page"
  glob_pattern = "**/*.txt"

  [pages.context]
    foe = "foo"

[[pages]]
  type = "jinjabread.Page"
  glob_pattern = "**/*.html"

  [pages.context]
    foe = "bar"
  • .txt pages will have the following extra context variables:
    fee = "fie"
    foe = "foo"
    
  • .html pages have the following extra context variables:
    fee = "fie"
    foe = "bar"
    

Index pages

All index pages (i.e., index.*) has an extra context variable named pages which is list of dictionaries of context variables from its sibling files.

For example, given the following file structure:

mysite/content/posts/
├── index.html
├── post1.md
└── post2.md

You can list all the pages in the posts directory using:

<!-- mysite/content/posts/index.html -->
{% for page in pages %}
<a href="{{ page.url_path }}">
  {{ page.url_path.split('/') | last | title }}
</a>
{% endfor %}

Resulting in:

<!-- mysite/public/posts/index.html -->
<a href="/posts/post1">
  Post1
</a>
<a href="/posts/post2">
  Post2
</a>

Contributing

Setup

python -m venv venv && \
  . venv/bin/activate && \
  pip install pip pip-tools --upgrade && \
  pip-sync requirements.txt

Test

python -m unittest discover .

Build

python -m build

Release

export TWINE_USERNAME='__token__'
export TWINE_PASSWORD='secret-token'
python -m twine upload dist/*

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

jinjabread-0.6.1.tar.gz (16.0 kB view details)

Uploaded Source

Built Distribution

jinjabread-0.6.1-py3-none-any.whl (10.8 kB view details)

Uploaded Python 3

File details

Details for the file jinjabread-0.6.1.tar.gz.

File metadata

  • Download URL: jinjabread-0.6.1.tar.gz
  • Upload date:
  • Size: 16.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.1

File hashes

Hashes for jinjabread-0.6.1.tar.gz
Algorithm Hash digest
SHA256 3b59cfd6e8fee641d54b20ce05f775b89e02426001ec77d9465a060331ecdb5b
MD5 a47d6af2ed385ee68f2cecde0cb130ee
BLAKE2b-256 81ccab6765c29539b9a6a62aa27f77930d041a6db8172213bfb1eb4870098b89

See more details on using hashes here.

File details

Details for the file jinjabread-0.6.1-py3-none-any.whl.

File metadata

  • Download URL: jinjabread-0.6.1-py3-none-any.whl
  • Upload date:
  • Size: 10.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.1

File hashes

Hashes for jinjabread-0.6.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2cab49228bc0ba17ed49856d66620b707b9d1b6706b693e5074214826425e5ad
MD5 47fbbfd32dba8d64d40162bdacfb2ff2
BLAKE2b-256 c549992bbc8f886239a244edfdac69282c5021f6c010a15ebbb0ce9d603b80ff

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page