template language based on pug + coffeescript + stylus
Project description
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file abstra_sugar-0.2.0a1.tar.gz.
File metadata
- Download URL: abstra_sugar-0.2.0a1.tar.gz
- Upload date:
- Size: 15.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ede69a14eabe1f1eada04a50b467deda818a2dc51db26baec479cd87337cbb17
|
|
| MD5 |
63d2b5f220825316324091c91c79022d
|
|
| BLAKE2b-256 |
c4a3eb49189db7e3797be7aa698b152c2cade6e04bb5138cd61ca3cc8593054b
|
File details
Details for the file abstra_sugar-0.2.0a1-py3-none-any.whl.
File metadata
- Download URL: abstra_sugar-0.2.0a1-py3-none-any.whl
- Upload date:
- Size: 17.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d271ca84f905cf34846b6f6b2eebc48507a5883aa58236d541512c5a1cde345d
|
|
| MD5 |
5f5bca2797fbc71a2b6fb4e10b607792
|
|
| BLAKE2b-256 |
f93b400f511a66e5aab08240bbab1183f107864ccc4ee1c20ed59044350a146e
|