Skip to main content

PyJan26 is a static site generator written in Python. It allows you to generate static websites from templates and content files, with support for pagination, custom pages, custom filters, and custom collections.

Project description

PyJan26

PyJan26 is a static site generator written in Python. It allows you to generate static websites from templates and content files, with support for pagination, custom pages, custom filters, and custom collections.

Install PyJan26 using pip:

pip install pyjan26

Initialize a new project:

python -m pyjan26.main s <project_name>
cd <project_name>

Generate static site:

python -m pyjan26.main g

To run your generated static site :

python -m http.server --directory public/

Directory Structure

PyJan26 follows a specific directory structure:

project_directory/
│
├── _templates/          # Base and extendable templates   ├── base.html           └── custom_template.html  
│
├── _content/            # Content files (Markdown)   ├── post1.md           ├── post2.md           └── about.md        

│
└── public/               # Generated HTML files
    ├── index.html      
    ├── post1/
       └── index.html   # Generated page for post1
    ├── post2/
       └── index.html   # Generated page for post2
    └── about/
        └── index.html   # Generated page for about

Pagination

To configure pagination, add the following YAML front matter to your content files:

layout: custom_template.html   # Specify the layout template
title: Blog Post 1                  # Set the title of the page
paginated:                     # Configure pagination
  items: blogs                  # Specify the collections or  this can be a sub folder under _contents, such as `blogs`
  size: 10                      # Set the number of items per page
  alias: myblogs                # Set a custom alias for the paginated items

Pagination Variables

When pagination is enabled, PyJan26 provides built-in template variables that you can use to generate pagination links:

{% if myblogs %}
  {% for blog in myblogs %}
    <span><a href="/blog/{{ blog.name }}">{{ blog.name }}</a></span>
  {% endfor %}
{% endif %}

{% if pagination.prev_page %}
  <a href="{{ pagination.prev_page }}">Previous</a>
{% endif %}

{% for page_num in pagination.page_numbers %}
  <a href="{{ page_num.url }}">{{ page_num.page_number }}</a>
{% endfor %}

{% if pagination.page_number and pagination.total_pages %}
  Page {{ pagination.page_number}} of: {{ pagination.total_pages }}
{% endif %}

{% if pagination.next_page %}
  <a href="{{ pagination.next_page }}">Next</a>
{% endif %}

In this example:

  • myblogs represents the paginated blogs.
  • pagination.total_pages represents the total pages.
  • pagination.prev_page provides a link to the previous page.
  • pagination.page_number represents the current page number.
  • pagination.page_numbers generates links to each page.
  • pagination.next_page provides a link to the next page.

Copy Static Files

To specify static files to be copied to the public directory, add them to the STATIC_PATHS variable in your configuration (settings.py):

# Copy Static files
STATIC_PATHS = [
    "images",     # whole directory
    "assets/robots.txt"   # specific file
]

Adjust the paths as needed to include directories or specific files you want to copy.

Custom Collections

Define custom collections in custom_collections.py:

# custom_collections.py

from pyjan26.registry import register_custom_collections

def tag_list(collections):
    """
    Define a list of tags for the 'tags' collection.
    """
    return {'tags': [{'tag': 'javascript'}, {'tag': 'python'}]}

# Register custom collections
register_custom_collections([tag_list])

Custom Filters

Define custom filters in custom_filters.py:

# custom_filters.py

from pyjan26.registry import register_custom_filters

def capitalize_words(value):
    return ' '.join(word.capitalize() for word in value.split())


# Register custom filters
register_custom_filters([capitalize_words])

To use the custom filter in your templates, follow this syntax:

{{ content | capitalize_words }}

Custom Page Rendering

Define custom page rendering in custom_page.py:

# custom_page.py

from pyjan26.registry import register_custom_page
from pyjan26.core import render_page, render_string

def custom_page1(*args, **kwargs):
    """
    Custom page rendering function.
    """
    item, collection_name, collections, settings = args
    out_dir = kwargs.get('out_dir')

    # render jinja variable
    if out_dir:
        out_dir = render_string(out_dir, item)

    page_data = {
        'collection_name': collection_name,
        'collections': collections,
        'items': item,
        'out_dir': out_dir
    }

    render_page(page_data)

    return { 'skip_next': False }


register_custom_page([custom_page1])

To apply custom page rendering to a content markdown file, add custom_page1: True to the YAML front matter:

layout: custom_template.html   # Specify the layout template
title: Post 1  
custom_page1: True                  # Apply custom page rendering

This instructs PyJan26 to use the custom_page1 function for rendering this specific content. Adjust metadata as needed.

Global Variable

To create a global variable, simply define it in settings.py:

# settings.py

#example only
AUTHOR = 'Josnin'

This renders the template value of AUTHOR defined in the settings.py module.

{{ settings.AUTHOR }}

How to run development server?

git clone https://github.com/josnin/pyjan26.git
cd ~/Documents/pyjan26/

Help

Need help? Open an issue in: ISSUES

Contributing

Want to improve and add feature? Fork the repo, add your changes and send a pull request.

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

pyjan26-0.12.tar.gz (11.8 kB view details)

Uploaded Source

Built Distribution

pyjan26-0.12-py3-none-any.whl (12.4 kB view details)

Uploaded Python 3

File details

Details for the file pyjan26-0.12.tar.gz.

File metadata

  • Download URL: pyjan26-0.12.tar.gz
  • Upload date:
  • Size: 11.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.10.3

File hashes

Hashes for pyjan26-0.12.tar.gz
Algorithm Hash digest
SHA256 2be50977a871693360b605b6e7c3cf3176665393e2989a0c43a2c55b4c607d11
MD5 eb795abc0c48f1ede3de0dbb8f1ada72
BLAKE2b-256 a9caaf809a7bb9ea3168eccb014307b78b11d229336281a1ae9f800a8669e18d

See more details on using hashes here.

File details

Details for the file pyjan26-0.12-py3-none-any.whl.

File metadata

  • Download URL: pyjan26-0.12-py3-none-any.whl
  • Upload date:
  • Size: 12.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.10.3

File hashes

Hashes for pyjan26-0.12-py3-none-any.whl
Algorithm Hash digest
SHA256 c80be428e2ff54f6a26ec4cceb107fb6b3039343b7d4844fdce99e471f79c146
MD5 70890483f50dd72fd5a9b10cb68fc022
BLAKE2b-256 e3eb8a73237d24dbe935815d48ed3bd8c0e58a730d3677e6cfb451c005a0a8e1

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