Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
21-06-2024
15-06-2025 - v1.0.1

* Fixed bug in attribute parsing for tags with multiple attributes
* Improved regex parsing to handle quoted attribute values correctly
* Added comprehensive test for multi-attribute tags (e.g., meta name='viewport' initial-scale='1')
* Renamed .skml files to .html.trim and updated all test references
* Added sub-template examples with user objects in examples directory

21-06-2024 - v1.0.0

* make trim-template available on PyPI

Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "trim-template"
version = "1.0.0"
version = "1.0.1"
description = "A templating engine inspired by Ruby's Slim template engine"
authors = ["David Kelly"]
license = "MIT License"
Expand All @@ -10,7 +10,7 @@ keywords = ["template", "trim", "engine", "html"]

[project]
name = "trim-template"
version = "0.1.0"
version = "1.0.1"
description = "A templating engine inspired by Ruby's Slim template engine"
authors = [
{name = "David Kelly", email = "info@futuresbright.com"}
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name='trim-template',
version='1.0',
version='1.0.1',
packages=['trim_template'],
entry_points={
'console_scripts': [
Expand Down
12 changes: 12 additions & 0 deletions tests/test_tag_attributes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from trim_template.trim import TrimTemplate

template = """
meta name='viewport' initial-scale='1'
meta name='description' content='A description of the page'
"""

def test_tag_attributes():
tmpl = TrimTemplate(template, pretty=False)
output = tmpl.render()

assert output == '<meta name="viewport" initial-scale="1"/><meta name="description" content="A description of the page"/>'
16 changes: 12 additions & 4 deletions trim_template/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ def __init__(self, indentation, line = None):
return

self.line = line.strip()
self.parts = self.line.split(" ")
# Use regex to split on key='value', key="value", or key=value, or tag
self.parts = re.findall(r"[^ \t]+='[^']*'|[^ \t]+=\"[^\"]*\"|[^ \t]+", self.line)

if self.line == '':
return
Expand Down Expand Up @@ -109,8 +110,15 @@ def parse_attributes(self):

for a in (self.parts[1:]):
if '=' in a:
key, val = a.split('=')
self.attributes[key] = val.strip('"').strip("'")
equal_pos = a.find('=')
key = a[:equal_pos].strip('"\'')
val = a[equal_pos + 1:]

# Only strip quotes if both ends are quoted
if (val.startswith('"') and val.endswith('"')) or (val.startswith("'") and val.endswith("'")):
val = val[1:-1]

self.attributes[key] = val
else:
self.text += a + " "

Expand All @@ -133,4 +141,4 @@ def has_attributes(self):
return self.attributes != []

def attributes(self):
return ' '.join(self.attributes)
return ' '.join(f'{k}="{v}"' for k, v in self.attributes.items())