Skip to content

Commit

Permalink
done custom_tags
Browse files Browse the repository at this point in the history
  • Loading branch information
pranavr2003 committed Jul 17, 2021
1 parent 5a37c0e commit e12bcdb
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 25 deletions.
79 changes: 54 additions & 25 deletions src/sierra/custom_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def tag(func):
\nUse `kwargs` to add tag attributes. Python-conflicting attribute names like `class` and `for` to entered in as `_class` and `_for`
"""
@functools.wraps(func)
def wrapper(*args, **kwargs):
def wrapper(**kwargs):

name = func.__name__

Expand All @@ -45,10 +45,10 @@ def wrapper(*args, **kwargs):
}

all_attr = f"<{name} ", *(f' {key}="{value}"' for key, value in kwargs.items()), ">"
print(join_attr(all_attr))
open('index.html', 'a+').write(f"\n{join_attr(all_attr)}")

print(check_text)
print(f"</{name}>")
open('index.html', 'a+').write(f"\n{check_text}")
open('index.html', 'a+').write(f"\n</{name}>")

except KeyError:

Expand All @@ -57,17 +57,19 @@ def wrapper(*args, **kwargs):
}

all_attr = f"<{name} ", *(f' {key}="{value}"' for key, value in kwargs.items()), ">"
print(join_attr(all_attr))
open('index.html', 'a+').write(f"\n{join_attr(all_attr)}")

else:

open('index.html', 'a+').write(f"\n<{name}>")

warnings.showwarning(r'''
Execution not possible if you're not using **kwargs. please use Tag() or @CmTag() instead of
@tag()''',
Execution not viable if you're not using **kwargs. Use this only if you're using a tag like <br/>.
Please use Tag() or @CmTag() instead of @tag()''',
UserWarning, 'custom_tags.py', '@tag')


func(*args, **kwargs)
func(**kwargs)

return wrapper

Expand All @@ -93,23 +95,30 @@ def __exit__(self, exc_type, exc_value, tb):
traceback.print_exception(exc_type, exc_value, tb)
else:
name = self.cm_tag_func.__name__
print(name)
open('index.html', 'a+').write(f"\n</{name}>")

def __call__(self, **kwargs):

name = self.cm_tag_func.__name__
print(name)
print(kwargs)
self.cm_tag_func(**kwargs)
return self

# @CmTag
# def testingg(**kwargs):
# pass
if kwargs:

kwargs = {
k.replace("__", "").replace("_", "-"): v for k, v in kwargs.items()
}


all_attr = f"<{name} ", *(f' {key}="{value}"' for key, value in kwargs.items()), ">"
open('index.html', 'a+').write(f"\n{join_attr(all_attr)}")

# with testingg(foo='bar') as a:
# print('a test')
else:

open('index.html', 'a+').write(f"\n<{name}>")

self.cm_tag_func(**kwargs)
return self

### Some tests

@tag
def meta(**kwargs):
Expand All @@ -128,22 +137,42 @@ def script(**kwargs):
script(text=someJStext, __async="", src="some_src")

# <script async="", src="some_src">

# someJStext

# </script>

# OR use @CmTag (work-in-progress)
# OR use @CmTag

# @CmTag
# def script(**kwargs):
# pass
@CmTag
def script(**kwargs):
pass

# with script(_async="", src="some_src"):
# print(someJStext)
with script():
open('index.html', 'a+').write(someJStext)

# <script async="", src="some_src">

# someJStext

# </script>
print('haha')

@tag
def br():
pass
br()

# <br/>

@CmTag
def test(**kwargs):
pass

with test(some='attr'):
pass

# <test some="attr">
# </test>



Expand Down
8 changes: 8 additions & 0 deletions workspace.code-workspace
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"folders": [
{
"path": "."
}
],
"settings": {}
}

0 comments on commit e12bcdb

Please sign in to comment.