Skip to main content

Tiny template parser which will convert a pugjs like syntax to HTML with the help of popular template engines.

spenx is not a direct port of the javascript pugjs library but has been heavily influenced by it so don’t expect full pugjs features to work. That being said, if you need to add a feature, don’t hesitate to submit your pull requests or open an issue!

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 and Mako 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
)

# Only preprocess files with given extensions
env.spenx_process_extensions = ['.pug', '.spenx'] # Those are the default

# And use env.get_template and render as usual

# Using mako
from spenx.ext.mako import preprocessor
from mako.template import Template

tpl = Template("p Hello world", preprocessor=preprocessor)

# And use render as usual

Syntax

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

Valid attributes values are:

  • strings, enclosed in ‘ or ` (type=’text’)

  • booleans (required=True)

  • numbers (value=42.0)

<!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>

Testing

$ pip install -e .[test]
$ python -m nose --with-doctest -v --with-coverage --cover-package=spenx

Release files for spenx 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for spenx 1.1.0
File Size Uploaded
spenx-1.1.0.tar.gz 6.6 kB Details

Release files / spenx-1.1.0.tar.gz

Download URL spenx-1.1.0.tar.gz
Size 6.6 kB
Tags Source
SHA-256 checksum
How to use checksums
d218be0e33b4638f7ab8d47fad375d464ce5bde972f68e2c61c94a2e8815be29
BLAKE2b-256 checksum
How to use checksums
db3e6fc13541ee67af1a251679c3775d5aff9b6cc6fe21f6985a272cb5540139
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

This release

1.1.0 This release

1 release file

1.0.2

1 release file

1.0.1

1 release file

1.0.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page