Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

Sugar

A template language that compiles to HTML, CSS, and JavaScript. Combines the best ideas from Pug, Stylus, and CoffeeScript into a single indentation-based syntax.

Install

pip install abstra-sugar

Usage

from abstra_sugar import sugar

# static
html = sugar(open("page.sugar").read())

# with data (templating)
html = sugar(open("page.sugar").read(), {"users": [...], "title": "Home"})

Example

html:
    head:
        style:
            .title:
                color: #333
                font-size: 24px
    body:
        h1.title: Hello World
        p: Welcome to Sugar
        ul:
            li: Simple
            li: Fast
            li: Clean
        script:
            greet(name):
                console.log(`Hello, ${name}!`)
            greet("World")

Compiles to:

<html>
    <head>
        <style>
            .title {
                color: #333;
                font-size: 24px;
            }
        </style>
    </head>
    <body>
        <h1 class="title">Hello World</h1>
        <p>Welcome to Sugar</p>
        <ul>
            <li>Simple</li>
            <li>Fast</li>
            <li>Clean</li>
        </ul>
        <script>
            function greet(name) {
                console.log(`Hello, ${name}!`);
            }
            greet("World");
        </script>
    </body>
</html>

Syntax Overview

Feature Sugar Compiles to
Element div: <div></div>
Implicit div .foo: <div class="foo"></div>
ID shorthand #main: <div id="main"></div>
Combined canvas#game.full: <canvas id="game" class="full"></canvas>
Class div.foo.bar: <div class="foo bar"></div>
Attribute a href=/about: <a href="/about"></a>
Text h1: Hello <h1>Hello</h1>
Interpolation li: {user.name} <li>Alice</li> (with data)
Inline element td: a href=#: Click <td><a href="#">Click</a></td>
Void element hr: <hr>
Comment # ignored (removed)
Template for for x of arr: repeats children (with data)
Template if if cond: conditional render (with data)
Implicit child ul: > : Item <ul><li>Item</li></ul>
CSS property color: red color: red;
CSS selector .foo: .foo {
JS function greet(x): function greet(x) {
JS arrow (x): (x) => {
JS inline arrow (x): x * 2 (x) => x * 2
JS for loop for x of arr: for (let x of arr) {
JS if if x > 0: if (x > 0) {
JS class class Foo: class Foo {
JS object indented key/values {key: value, ...}
html! literal html!: + sugar `<div>...</div>`
Component def card = (title): (reusable template)
Component call card("Users"): (expands template)
Slot slot: (replaced by call children)
CSS mixin def reset = (): (reusable properties)
CSS mixin call @reset() (expands properties)

Documentation

See the docs folder for the full language reference.

Architecture

sugar(string) → string

    scan(code)    →  List[Token]      # lexer.py
    parse(tokens) →  List[Node]       # parser.py
    compile(nodes) → string           # compiler.py

License

MIT — see LICENSE.

Metadata

Release files for abstra-sugar 0.2.0a1

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

Source distribution (sdist)

Source distribution for abstra-sugar 0.2.0a1
File Size Uploaded
abstra_sugar-0.2.0a1.tar.gz 15.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for abstra-sugar 0.2.0a1
File Interpreter ABI Platform
abstra_sugar-0.2.0a1-py3-none-any.whl Python 3 none any Details

Total release size: 33.0 kB

Release files / abstra_sugar-0.2.0a1.tar.gz

Download URL abstra_sugar-0.2.0a1.tar.gz
Size 15.9 kB
Tags Source
SHA-256 checksum
How to use checksums
ede69a14eabe1f1eada04a50b467deda818a2dc51db26baec479cd87337cbb17
BLAKE2b-256 checksum
How to use checksums
c4a3eb49189db7e3797be7aa698b152c2cade6e04bb5138cd61ca3cc8593054b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release files / abstra_sugar-0.2.0a1-py3-none-any.whl

Download URL abstra_sugar-0.2.0a1-py3-none-any.whl
Size 17.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d271ca84f905cf34846b6f6b2eebc48507a5883aa58236d541512c5a1cde345d
BLAKE2b-256 checksum
How to use checksums
f93b400f511a66e5aab08240bbab1183f107864ccc4ee1c20ed59044350a146e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release history Release notifications | RSS feed

This release

0.2.0a1 This release

2 release files

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