Skip to content

Commit

Permalink
started working on my own html generator library
Browse files Browse the repository at this point in the history
  • Loading branch information
István Bozsó committed Feb 20, 2020
1 parent 0a0dd62 commit cd64160
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 11 deletions.
19 changes: 9 additions & 10 deletions utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
from utils.utils import *
from utils.path import *
from utils.enforce import *
from utils.cli import *
from utils.cmd import *
from utils.ninja import *
from utils.project import *
from utils.simpledoc import *
from utils.html import *
from utils.jslibs import *
from .utils import *
from .path import *
from .enforce import *
from .cli import *
from .cmd import *
from .ninja import *
from .project import *
from .simpledoc import *
from .html import *
3 changes: 3 additions & 0 deletions utils/html/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .html import *
from .tag import *
from .jslibs import *
File renamed without changes.
2 changes: 1 addition & 1 deletion utils/jslibs.py → utils/html/jslibs.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .html import Library
from .utils import namespace, export
from utils.utils import namespace, export

__all__ = [
"libs", "plotly",
Expand Down
72 changes: 72 additions & 0 deletions utils/html/tag.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
from utils import export, str_t

def parse_options(kwargs):
if "klass" in kwargs:
kwargs["class"] = kwargs.pop("klass")

return " ".join(
"%s=%s" % (key, val)
if val is not True
else "%s" % key
for key, val in kwargs.items()
)

class BaseTag(object):
__slots__ = (
"options",
)

def __init__(self, *args, **kwargs):
self.options = kwargs


class Tag(BaseTag):
__slots__ = (
"children",
)

def __init__(self, *args, **kwargs):
BaseTag.__init__(self, *args, **kwargs)
self.children = args


def render(self):
name = self.__class__.__name__

return "<%s %s>%s</%s>" % (
name, parse_options(self.options),
self.render_children(), name
)

def render_children(self):
return "".join(
child.render()
if not isinstance(child, str_t)
else str(child)
for child in self.children
)


class SelfClosingTag(BaseTag):
def render(self):
return "<%s %s>" % (
self.__class__.__name__,
self.options
)


@export
class html(Tag):
pass

@export
class p(Tag):
pass

@export
class div(Tag):
pass

@export
class img(SelfClosingTag):
pass
10 changes: 10 additions & 0 deletions utils/html/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from utils.html.tag import *


def main():
t = div(p("a"), p("b"), controls=True, src="a/b")

print(t.render())

if __name__ == "__main__":
main()

0 comments on commit cd64160

Please sign in to comment.