Skip to content

Commit

Permalink
Element functions are moved to separate file. Add utils module.
Browse files Browse the repository at this point in the history
  • Loading branch information
frodo821 committed Feb 15, 2019
1 parent d0ad978 commit 354c616
Show file tree
Hide file tree
Showing 5 changed files with 437 additions and 364 deletions.
21 changes: 14 additions & 7 deletions rattlepy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,17 @@ def greet(name):
"""

from .templating import (
escapeHtmlEntities, Element,
SelfClosedElement, text, node,
closed, html, head, body, a,
span, main, header, footer,
article, ul, ol, li, h1, h2,
h3, h4, h5, h6, title,
script, style)
escapeHtmlEntities, Element,
SelfClosedElement, text, node,
closed)

from .elements import (
a, article, body,
div, footer, h1, h2,
h3, h4, h5, h6, head,
header, hr, html, img,
li, link, main, meta,
ol, p, script, span,
style, title, ul, setTitle)

from .utils import createHeader
50 changes: 27 additions & 23 deletions rattlepy/__main__.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
"""
Basic usage of Rattle.py
"""
from .templating import (
html, head, text,
body, title, h1,
span, style, link,
meta, img)
from .templating import text
from .elements import (
html, body, link, style,
h1, img, span)
from .utils import createHeader

with html(lang="ja") as elem:
with head():
link(rel="stylesheet", href="main.css")
meta(name="viewport", content="initial-scale=1.0;width=device-width")
with title():
text("Test HTML")
with style():
text("""\
|a {
| color: blue;
|}\
""")
with body():
with h1():
text("It works!")
text("I'm feeling happy!")
img(src="logo.png", alt="logo image", title="logo")
with span(className="red large", id="abcd"):
text("This text will be large and red.")
with createHeader(
[{"name": "viewport", "content": "initial-scale=1.0,width=device-width"}],
"Hello, Rattle.py"):

link(rel="stylesheet", href="main.css")
with style():
text("""\
|a {
| color: blue;
|}\
""")

with body():
with h1():
text("It works!")

text("I'm feeling happy!")
img(src="logo.png", alt="logo image", title="logo")

with span(className="red large", id="abcd"):
text("This text will be large and red.")

print(elem)
222 changes: 222 additions & 0 deletions rattlepy/elements.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
"""
HTML element short-handing functions
"""

from .templating import Element, SelfClosedElement, text

__all__ = [
'a', 'article', 'body', 'div',
'footer', 'h1', 'h2', 'h3',
'h4', 'h5', 'h6', 'head', 'header',
'hr', 'html', 'img', 'li', 'link',
'main', 'meta', 'ol', 'p', 'script',
'span', 'style', 'title', 'ul', 'setTitle'
]

def html(**kwargs):
"""
Create html node and return it.
Equivalent to `return Element("html", attributes...)`.
"""
return Element("html", **kwargs)

def head(**kwargs):
"""
Create head node and return it.
Equivalent to `return Element("head", attributes...)`.
"""
return Element("head", **kwargs)

def body(**kwargs):
"""
Create body node and return it.
Equivalent to `return Element("body", attributes...)`.
"""
return Element("body", **kwargs)

def div(**kwargs):
"""
Create div node and return it.
Equivalent to `return Element("div", attributes...)`.
"""
return Element("div", **kwargs)

def a(**kwargs):
"""
Create hyper-link and return it.
Equivalent to `return Element("a", attributes...)`.
"""
return Element("a", **kwargs)

def span(**kwargs):
"""
Create span node and return it.
Equivalent to `return Element("span", attributes...)`.
"""
return Element("span", **kwargs)

def main(**kwargs):
"""
Create main node and return it.
Equivalent to `return Element("main", attributes...)`.
"""
return Element("main", **kwargs)

def p(**kwargs):
"""
Create paragraph node and return it.
Equivalent to `return Element("p", attributes...)`.
"""
return Element("p", **kwargs)

def footer(**kwargs):
"""
Create footer node and return it.
Equivalent to `return Element("footer", attributes...)`.
"""
return Element("footer", **kwargs)

def header(**kwargs):
"""
Create header node and return it.
Equivalent to `return Element("header", attributes...)`.
"""
return Element("header", **kwargs)

def article(**kwargs):
"""
Create article node and return it.
Equivalent to `return Element("article", attributes...)`.
"""
return Element("article", **kwargs)

def ul(**kwargs):
"""
Create ul node and return it.
Equivalent to `return Element("ul", attributes...)`.
"""
return Element("ul", **kwargs)

def ol(**kwargs):
"""
Create ol node and return it.
Equivalent to `return Element("ol", attributes...)`.
"""
return Element("ol", **kwargs)

def li(**kwargs):
"""
Create li node and return it.
Equivalent to `return Element("li", attributes...)`.
"""
return Element("li", **kwargs)

def h1(**kwargs):
"""
Create h1 node and return it.
Equivalent to `return Element("h1", attributes...)`.
"""
return Element("h1", **kwargs)

def h2(**kwargs):
"""
Create h2 node and return it.
Equivalent to `return Element("h2", attributes...)`.
"""
return Element("h2", **kwargs)

def h3(**kwargs):
"""
Create h3 node and return it.
Equivalent to `return Element("h3", attributes...)`.
"""
return Element("h3", **kwargs)

def h4(**kwargs):
"""
Create h4 node and return it.
Equivalent to `return Element("h4", attributes...)`.
"""
return Element("h4", **kwargs)

def h5(**kwargs):
"""
Create h5 node and return it.
Equivalent to `return Element("h5", attributes...)`.
"""
return Element("h5", **kwargs)

def h6(**kwargs):
"""
Create h6 node and return it.
Equivalent to `return Element("h6", attributes...)`.
"""
return Element("h6", **kwargs)

def title(**kwargs):
"""
Create title node and return it.
Equivalent to `return Element("title", attributes...)`.
"""
return Element("title", **kwargs)

def setTitle(string):
"""
Set the document title.
This function is as same as
```
with title():
text(string)
```
YOU MUST USE IN WITH EXPRESSION:
```
with setTitle("HogeHoge Page"): pass
```
"""
with title() as elem:
text(string)
return elem

def script(**kwargs):
"""
Create script node and return it.
Equivalent to `return Element("script", attributes...)`.
"""
return Element("script", **kwargs)

def style(**kwargs):
"""
Create style node and return it.
Equivalent to `return Element("style", attributes...)`.
"""
return Element("style", **kwargs)

def img(**kwargs):
"""
Create img node and return it.
Equivalent to `return Element("img", attributes...)`.
"""
return SelfClosedElement("img", _outer=3, **kwargs)

def meta(**kwargs):
"""
Create meta node and return it.
Equivalent to `return Element("meta", attributes...)`.
"""
return SelfClosedElement("meta", _outer=3, **kwargs)

def link(**kwargs):
"""
Create link node and return it.
Equivalent to `return Element("link", attributes...)`.
"""
return SelfClosedElement("link", _outer=3, **kwargs)

def hr(**kwargs):
"""
Create hr node and return it.
Equivalent to `return Element("hr", attributes...)`.
"""
return SelfClosedElement("hr", _outer=3, **kwargs)

0 comments on commit 354c616

Please sign in to comment.