Skip to main content

Python 3 template parser to generate HTML from a pug/jade like syntax

Project description

Tiny template parser which will convert a pugjs like syntax to HTML with the help of popular template engine (Jinja2 only at the time being).

In spenx, statements, like conditions and loops, are not processed and outputted “as it” to be processed by the template engine you wish to use.

At the time being, only Jinja2 has been tested but adding support for anything else should be easy.

Why another parser?

I know there’s a lot of port of jade, pugjs and so on for python. But everyone seems to be unmaintained. I really like the syntax use by pug and want something simplier and easier to maintain in the future with a strict set of features.

This is why the parser is defined using Arpeggio and the cleanpeg syntax. It’s easier to read, understand and maintain.

The spenx code is really tiny, check it yourself!

Installation

$ pip install spenx

Usage

Using spenx is fairly easy:

from spenx import Parser

# Without backend
parser = Parser()

parser.parse("""
p Hello world
""")

# => <p>Hello world</p>

# Using jinja2
from spenx.ext.jinja import Spenx
from jinja import Environment

env = Environment(
  # Common jinja parameters
  extensions=[Spenx], # And the spenx extension
)

# And use env.get_template and render as usual

# Using mako
from spenx.ext.mako import preprocessor
mako.template.Template("p Hello world", preprocessor=preprocessor)

Syntax

If you’re already using pugjs, you should feel familiar with the syntax (see the tests/ folder for more insights).

For the moment, you should use the multiline string to handle the doctype but that may change in the future.

| <!DOCTYPE html>
html(lang='en')
  head
    meta(charset='utf-8')
    title My first spenx webpage!
  body
    h1 spenx
    p.welcome
      | Did I said you'll feel right at home if you're using pugjs?
      | Because I guess that's right!
    .container
      p Without a tag defined, div will be assumed
        strong pretty cool huh?

And this is what’s rendered by spenx:

<!DOCTYPE html><html lang="en"><head><meta charset="utf-8" /><title>My first spenx webpage!</title></head><body><h1>spenx</h1><p class="welcome">Did I said you'll feel right at home if you're using pugjs? Because I guess that's right!</p><div class="container"><p>Without a tag defined, div will be assumed <strong>pretty cool huh?</strong></p></div></body></html>

And after using BeautifulSoup:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8"/>
  <title>
  My first spenx webpage!
  </title>
</head>
<body>
  <h1>
  spenx
  </h1>
  <p class="welcome">
  Did I said you'll feel right at home if you're using pugjs? Because I guess that's right!
  </p>
  <div class="container">
  <p>
    Without a tag defined, div will be assumed
    <strong>
    pretty cool huh?
    </strong>
  </p>
  </div>
</body>
</html>

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

spenx-1.0.0.tar.gz (6.0 kB view details)

Uploaded Source

File details

Details for the file spenx-1.0.0.tar.gz.

File metadata

  • Download URL: spenx-1.0.0.tar.gz
  • Upload date:
  • Size: 6.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.20.1 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3

File hashes

Hashes for spenx-1.0.0.tar.gz
Algorithm Hash digest
SHA256 b172cc8f860c8da6501274400ad5726c5e2d9c908ae1c7d79151bb0801192e2f
MD5 7918d476c01aaa96c56083a089dc5b7a
BLAKE2b-256 fe01322b6b787a99f1559cd6e34d5baab64d4fda255cf2037eee0442da0ea2f9

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