Coder Social home page Coder Social logo

pyges's Introduction

pyges

Pyges is a highly customizable Python-based static site generator and runtime designed for flexibility.

Usage

Site Creation

The pyges module revolves around the Site class. To create one first create a Config and specify your src folder, this is the folder where all of your site will be generated from.

from pyges import Site, Config

config = Config(src="src")

Next, create a Site instance supplying it the already created Config

site = Site(config)

Adding a Page

Apart from the styles.css style sheet, CNAME file and Assets folder that have to be created inside the src folder. HTML pages can be created from Markdown files using the site.add decorator.

from pyges.nodes import Html

@site.add("index.md")
def index(content: str) -> Html:
    return Html(content)

In the above example Pyges will read the index.md markdown file transform it to an HTML string and pass it as an an argument - content in our case to the decorated function. Then, inside the function body an Html tag with the index.md HTML content is created.

After building the site using

if __name__ == '__main__':
    site.build()

a file named index.html will be saved in the out folder (out by default) at the same relative path as index.md was to src.

For example, in case src/index.md contained:

# Test

This is index.md

After building the site out/index.html will contain:

<html>
  <h1>Test</h1>
  <p>The <i>index</i> function will return the following HTML</p>
</html>

Adding a Page Pages

In case there's a folder that consists of pages that should have the same template, instead of adding a creator function for each one the folder name can be supplied to the site.add decorator. For example

from pyges.nodes import Html, Article


@site.add("blogs")
def blog(content: str) -> Html:
    return Html(Article(content))

Flexible HTML and Markdown

Pyges has support for (soon) all HTML tags, meaning a creator function (a name for a function decorated with the site.add add) can utilize all of those to create a specific page as for it's needs. Not only that, but using simple yaml syntax in the start of any Markdown file in conjunction with inheriting from the Scheme dataclass for it's validation, creates a creates a robust method to access any page metadata.

In the end, a customizable and rich templating system can be achieved, let's improve the previous example, be adding to each blog the following yaml configuration

---
name: <name>
date: <date>
---
// blog //

This is specified as mentioned using a dataclass the inherits from Scheme

import datetime

@dataclass
class Blog(Scheme):
    name: str
    date: datetime.date

Now our improved blog function can look like this:

@site.add("blogs/", scheme=Blog)
def blog(content: str, name: str, date: datetime.date) -> Html:
    return Html(
        Article(
            [
                Div([H(size=1, value=name), P(str(date))]),
                content,
            ]
        )
    )

Notice that all Blog scheme properties are passed to the creator function.

Another great feature, is the ability to get all pages created by a specific creator, this is done by the site.pages method. Let's say we want to create a page to show all blogs created by blog, we can do that in the following manner

@site.add("blogs.md")
def blog(content: str) -> Html:
    return Html(
        [
            P(content)
            Article(
                [
                    Div(b.name)
                    for b in sorted(
                        site.pages(creator=blog),
                        key=lambda x: x.properties["date"],
                    )
                ],
            )
        ]
    )

Debugging

Lastly, when developing a website, instead of building it every time, use the site.debug method, this will open a flask runtime, that updates changes automatically as you work!

pyges's People

Contributors

arielalon24 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.