Skip to content

Commit

Permalink
Merge pull request #15 from evtn/dev
Browse files Browse the repository at this point in the history
1.1.8: XML Comments
  • Loading branch information
evtn authored Apr 26, 2023
2 parents d3279c4 + 3704f20 commit bc51162
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 6 deletions.
36 changes: 35 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# soda - a fast SVG generation tool

[![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/evtn/soda/build.yml?branch=lord)](https://github.com/evtn/soda/actions/workflows/build.yml)
[![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/evtn/soda/test.yml?branch=lord&label=tests)](https://github.com/evtn/soda/actions/workflows/test.yml)
[![PyPI](https://img.shields.io/pypi/v/soda-svg)](https://pypi.org/project/soda-svg/)
![PyPI - Downloads](https://pepy.tech/badge/soda-svg)
![PyPI - Python Version](https://img.shields.io/pypi/pyversions/soda-svg)
[![Coveralls](https://img.shields.io/coverallsCoverage/github/evtn/soda?label=test%20coverage)](https://coveralls.io/github/evtn/soda?branch=lord)
![License](https://img.shields.io/github/license/evtn/soda)
![Badge Count](https://img.shields.io/badge/badges-8-important)


Here's some basic usage:

```python
Expand Down Expand Up @@ -156,7 +166,7 @@ new_root = Tag.from_str(rendered_root)
assert rendered_root == new_root.render(pretty=True)
```

### Text
## Text

Basic text handling is pretty straightforward:

Expand All @@ -178,6 +188,30 @@ Tag.text(Literal("Hello, World"))

If you need to add unescaped text (such as prerendered XML), you should pass `escape=False` to a `Literal` constructor:

## XML Declarations and comments

To insert an XML declaration (i.e. `<?xml version="1.0" encoding="UTF-8"?>`), use `XMLDeclaration`:

```python
from soda import XMLDeclaration


print(XMLDeclaration(version="2.0", encoding="UTF-8").render()) # '<?xml version="2.0" encoding="UTF-8"?>'
```

Default values for version and encoding are "1.0" and "UTF-8" respectively


XML comments are used similarly:

```python
from soda import XMLComment


print(XMLComment("comment text!!").render()) # '<!-- comment text!! -->'
```


```python
from soda import Tag, Literal

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[tool.poetry]
name = "soda-svg"
packages = [{include = "soda"}]
version = "1.1.7"
version = "1.1.8"
description = "Fast SVG generation tool"
authors = ["Dmitry Gritsenko <soda@evtn.ru>"]
license = "MIT"
Expand Down
6 changes: 5 additions & 1 deletion soda/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
from .tags import Tag as Tag, Literal as Literal, Fragment as Fragment
from .custom_tags import Root as Root, XMLDeclaration as XMLDeclaration
from .custom_tags import (
Root as Root,
XMLDeclaration as XMLDeclaration,
XMLComment as XMLComment,
)
from .paths import Path as Path
from .point import Point as Point, PointPath as PointPath
from .config_mod import config as config
17 changes: 15 additions & 2 deletions soda/custom_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

from base64 import b64encode
from os import PathLike
from typing import BinaryIO
from .tags import Node, Tag
from typing import BinaryIO, Iterable
from .tags import Literal, Node, Tag
from pathlib import Path


Expand All @@ -29,6 +29,19 @@ def __init__(self, version: str = "1.0", encoding: str = "UTF-8"):
super().__init__("xml", version=version, encoding=encoding)


class XMLComment(Tag):
def __init__(self, text: str):
self.text = text

def build(self, tab_size: int = 0, tab_level: int = 0) -> Iterable[str]:
yield " " * (tab_size * tab_level)
yield "<!--"
yield " "
yield from Literal(self.text).build(0, 0)
yield " "
yield "-->"


class Image(Tag):
"""
Expand Down
5 changes: 4 additions & 1 deletion test/test_custom.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from typing import BinaryIO
from soda.custom_tags import Image, Root, XMLDeclaration
from soda.custom_tags import Image, Root, XMLComment, XMLDeclaration
from pathlib import Path
from os import remove

Expand All @@ -24,6 +24,9 @@ def test_xml_decl(self):
== '<?xml version="2.0" encoding="UTF-8"?>'
)

def test_xml_comment(self):
assert XMLComment("what?").render() == "<!-- what? -->"

def test_image(self):
url = "https://example.com/example.jpg"

Expand Down

0 comments on commit bc51162

Please sign in to comment.