Andriy Kornatskyy avatar Andriy Kornatskyy committed 907e419

HTML markup with tag and fragment.

Comments (0)

Files changed (1)

src/wheezy/html/markup.py

+
+""" ``markup`` module.
+    http://www.w3schools.com/tags/
+"""
+
+from wheezy.html.comp import iteritems
+
+
+class Tag(object):
+    """
+    """
+    __slots__ = ['name', 'inner', 'attr']
+
+    def __init__(self, name, inner=None, attr=None, **kwargs):
+        self.name = name
+        self(inner, attr, **kwargs)
+
+    def __call__(self, inner=None, attr=None, **kwargs):
+        self.inner = repr(inner)
+        if attr:
+            if kwargs:
+                attr.update(kwargs)
+            self.attr = attr
+        else:
+            self.attr = kwargs
+        return self
+
+    def __repr__(self):
+        """
+            Normal tag with attributes
+
+            >>> print(Tag('span')("abc", class_='error'))
+            <span class="error">abc</span>
+
+            Self closing tag
+
+            >>> t = Tag('input')
+            >>> print(Tag('input')(id='name', value='abc'))
+            <input id="name" value="abc" />
+        """
+        parts = []
+        append = parts.append
+        append('<' + self.name)
+        if self.attr:
+            for name, value in iteritems(self.attr):
+                append(' ' + name.rstrip('_') +
+                        '="' + value + '"')
+        if self.inner:
+            append('>' + self.inner + '</' + self.name + '>')
+        else:
+            append(' />')
+        return ''.join(parts)
+
+
+class Fragment(object):
+    """
+    """
+
+    __slots__ = ['tags']
+
+    def __init__(self, *tags):
+        self.tags = tags
+
+    def __str__(self):
+        return self.__repr__()
+
+    def __repr__(self):
+        return ''.join(map(repr, self.tags))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.