diff --git a/README.md b/README.md index 32ee999..3ec7da9 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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: @@ -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. ``), use `XMLDeclaration`: + +```python +from soda import XMLDeclaration + + +print(XMLDeclaration(version="2.0", encoding="UTF-8").render()) # '' +``` + +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()) # '' +``` + + ```python from soda import Tag, Literal diff --git a/pyproject.toml b/pyproject.toml index 334bbaf..5b48b26 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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 "] license = "MIT" diff --git a/soda/__init__.py b/soda/__init__.py index b3aff6b..33719d6 100644 --- a/soda/__init__.py +++ b/soda/__init__.py @@ -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 diff --git a/soda/custom_tags.py b/soda/custom_tags.py index 8929533..af17433 100644 --- a/soda/custom_tags.py +++ b/soda/custom_tags.py @@ -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 @@ -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 "" + + class Image(Tag): """ diff --git a/test/test_custom.py b/test/test_custom.py index d0f1d2c..9148b0a 100644 --- a/test/test_custom.py +++ b/test/test_custom.py @@ -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 @@ -24,6 +24,9 @@ def test_xml_decl(self): == '' ) + def test_xml_comment(self): + assert XMLComment("what?").render() == "" + def test_image(self): url = "https://example.com/example.jpg"