Skip to content

Commit

Permalink
docstrings improved, all attr replaced with kwargs, cTags depr, write…
Browse files Browse the repository at this point in the history
…CSS to kwargs
  • Loading branch information
pranavr2003 committed Jul 19, 2021
1 parent 3b07acf commit 1c2dc74
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 96 deletions.
8 changes: 2 additions & 6 deletions src/sierra/__init__.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
from sierra.cutom_tags import *
from sierra.custom_tags import *
from sierra.bullets_and_lists import *
from sierra.cTags import *
from sierra.directory_structure import *
from sierra.main_htm import *
from sierra.table import *
from sierra.tags import *
from sierra.tTags import *
from sierra.write import *

# from custom_tags import *
# from bullets_and_lists import *
# from cTags import *
# from directory_structure import * # Import these if you're cloning the repo to test/work with Sierra
# from bullets_and_lists import * # Import these if you're cloning the repo to test/work with Sierra
# from main_htm import *
# from table import *
# from tags import *
Expand Down
26 changes: 0 additions & 26 deletions src/sierra/cTags.py

This file was deleted.

73 changes: 43 additions & 30 deletions src/sierra/main_htm.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@
from bs4 import BeautifulSoup
import traceback

def title(Title, icon=False, templating=False):
"""
def title(Title, icon=False):
"""Adds a title to the webpage, an icon (if entered)
\nThis must be the first function called after `from sierr import *`
\nCalling it initiates the HTML and CSS (already linked) file that is created in the working directory upon running of the code
Args:
Title(str, compulsory) : Title of the HTML file.
icon(str, optional) : Icon to be displayed. Should be a .ico file. Defaults to no icon.
Expand All @@ -14,25 +18,18 @@ def title(Title, icon=False, templating=False):
<html lang="en">
<meta charset="utf-8">
<head>
<title>{Title}</title>''')
<title>{Title}</title>
<link rel="stylesheet" href="style.css">''')

if templating == False:
f.write(f'\n<link rel="stylesheet" href="style.css">')
else:
f.write(f'''\n<link rel="stylesheet" href="/static/style.css">''')

if type(icon) == str and icon.split('.')[-1] == '.ico':
f.write(f'''\n<link rel="shortcut icon" href={icon}>''')
if type(icon) == str and icon.split('.')[-1] == 'ico':
f.write(f'\n<link rel="shortcut icon" href={icon}>')
open('style.css', 'w').write('')


# def href(link:str, text:str):
# with open('index.html', 'a+') as f:
# f.write(f'''\n<a href="{link}">{text}</a>''')


def addFont(font_link):
"""Give the font link to add different fonts to your webpage.
\nAdd only the `href` attribute to `font_link`
Args:
font_link (str, compulsory): The font link.
Expand All @@ -41,22 +38,18 @@ def addFont(font_link):
with open("index.html", 'a') as f:
f.write(f'\n<link href="{font_link}" rel="stylesheet">')

# def code(codeblock):
# """Give the codeblock as a text to display the code in your webpage.

# Args:
# codeblock (str, compulsory): The code block.
# """

# with open("index.html", 'a') as f:
# f.write(f"\n<code>{codeblock}</code>")

def head(Head, type='header', **kwargs):
"""
Args:
Head (str, compulsory) : Caption header.
type (str, optional) : Header type. Anything from 'h1' to 'h6'
**kwargs (optional) : CSS styling arguments
"""Adds a header
\n`kwargs` is used to add CSS styling attributes to the `type` mentioned
\nUse underscores instead of hyphens when adding arguments
\nFor example, the styling attribute `font-size` must be entered in as `font_size`
\nArgs:
Head (str, compulsory) : Caption header.
type (str, optional) : Header type. Anything from 'h1' to 'h6'
**kwargs (optional) : CSS styling arguments
"""

# Use underscores for hyphens in **kwags for styling args
Expand All @@ -81,7 +74,20 @@ def join_attr(tup):
return string

class image():

def __init__(self, src:str, **kwargs):
"""Adds an image to the webpage. Use `kwargs` to add tag attributes
\nUse `.show()` to display the image and `.css()` to style it
\nUse `kwargs` to add tag attributes. Python-conflicting attribute names like `class` and `for` to be prefixed by a double underscore, that is, to be entered in as `__class` and `__for`
\nUse a single underscore in place of a hyphen in the `key` of `kwargs`, which is the tag atrribute name.
\neg. Tag attribute `initial-scale` must be `initial_scale` as the `key`
\nArgs:
\nsrc (str, compulsory) : The location of the image file
\nkwargs (optional) : Add tag attributes to `<img>`
"""
self.src = src
self.kwargs = kwargs

Expand Down Expand Up @@ -114,7 +120,14 @@ def show(self):

def css(self, **kwargs):
"""Writes the given parameters to the CSS file.
Args:
\nUse underscores instead of hyphens
\nFor example, styling attribute `font-size` must be entered in as `font_size`
\nUsing this adds CSS directly to `<img>` irrespective of an image `class` or `id` mentioned
\nUse `writeCSS()` to add CSS to a specific `class` or `id`
\nArgs:
**kwargs (optional) : CSS parameters.
"""

Expand Down
67 changes: 45 additions & 22 deletions src/sierra/tags.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,35 @@
import traceback

def join_attr(tup):
string = ''
for item in tup:
string = string + item
string = string.replace(' ', ' ')
return string

class Tag:
def __init__(self, tag, attr=None):
"""Opens any tag."""

def __init__(self, tag, **kwargs):
"""Opens any tag.
\nUse `kwargs` to add tag attributes. Python-conflicting attribute names like `class` and `for` to be prefixed by a double underscore, that is, to be entered in as `__class` and `__for`
\nUse a single underscore in place of a hyphen in the `key` of `kwargs`, which is the tag atrribute name.
\neg. Tag attribute `initial-scale` must be `initial_scale` as the `key`
"""

self.tag = tag
self.attr = attr
self.kwargs = kwargs

def __enter__(self):

if self.kwargs:
# open("index.html", 'a+').write(f"\n<{self.tag}>")
all_attr = f"<{self.tag} ", *(f' {key.replace("__", "").replace("_", "-")}="{value}"' for key, value in self.kwargs.items()), ">"
open('index.html', 'a+').write(f"\n{join_attr(all_attr)}")

if self.attr == None:
open("index.html", 'a+').write(f"<{self.tag}>")
else:
open("index.html", 'a+').write(f"<{self.tag} {self.attr}>")
open("index.html", 'a+').write(f"\n<{self.tag}>")

def __enter__(self):
return self

def __exit__(self, exc_type, exc_value, tb):
Expand All @@ -22,34 +39,40 @@ def __exit__(self, exc_type, exc_value, tb):
open("index.html", 'a+').write(f"\n</{self.tag}>")

def css(self, **kwargs):
"""Writes the given parameters to the CSS file.
"""Adds the given styling attributes to the `Tag` mentioned
Args:
**kwargs (optional) : CSS parameters.
\nUse `kwargs` to add styling attributes to the `Tag` mentioned
\nThis adds CSS directly to the tag mentioned, irrespective of any `__class` or `id` mentioned
\nUse `writeCSS()` instead to add CSS to a specific `class` or `id`
\nUse underscores instead of hyphens when adding styling attributes
\nFor example, the attribute `font-size` must be entered in as `font_size`
\nArgs:
\n **kwargs (optional) : CSS parameters.
"""

with open("style.css", 'a+') as s:
s.write("\n\nbody {{")
for key, value in kwargs:
s.write(f"\n\n{self.tag} {{")

for key, value in kwargs.items():
s.write(f"\n\t{key.replace('_', '-')}: {value};")

s.write("\n}")

def openBody(**kwargs):
"""Opens the body tag and adds the required CSS.
Args:
**kwargs (optional) : CSS parameters.
\nArgs:
**kwargs (optional) : CSS parameters.
"""

open("index.html", 'a+').write('\n<body>')
with open("style.css", 'a+') as s:
for key, value in kwargs:
s.write(f"\n\t{key.replace('_', '-')}: {value};")
s.write("\nbody {")

def closeHTML():
"""Closes the <HTML> tag."""
open("index.html", 'a+').write("\n</html>")
for key, value in kwargs.items():
s.write(f"\n\t{key.replace('_', '-')}: {value};")

def closeBody():
"""Closes the body tag."""
open("index.html", 'a+').write('\n</body>')
s.write("\n}")
34 changes: 22 additions & 12 deletions src/sierra/write.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,39 @@ def writeWA(text):
text (str, compulsory): HTML code snippet.
"""

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


def writeCSS(tag, *args):
def writeCSS(tag, **kwargs):
"""
Writes the given code to the CSS file.
Enter in a dictionary
Adds the given styling attributes to the tag mentioned
\nUse `kwargs` to add styling attributes to the `tag` mentioned
\nUse underscores instead of hyphens when adding styling attributes.
\nFor example, the attribute `font-size` must be entered in as `font_size`
\nAnother example:
\n`writeCSS('.some_class', font-size='20px')` adds the attributes `font-size=20px` to the class `some_class`
\nArgs:
\ntag (str, compulsory) : Tag to add the CSS to. Could be a `class`, `id` or anything
\nkwargs : CSS styling attributes to add to the `tag` mentioned
"""

with open('style.css', 'a+') as s:
s.write(f"""\n\n{tag} {{""")
for arg in args:
for parameter, value in arg.items():
s.write(f"""\n\t{parameter}: {value};""")
s.write(f"\n\n{tag} {{")

for key, value in kwargs.items():
s.write(f"\n\t{key.replace('_', '-')}: {value};")

s.write("\n}")


def writeCSS_raw(text):
"""Writes the given code to the CSS file.
"""Writes the given text to the CSS file.
Args:
text (str, compulsory): CSS style sheet.
"""

open("style.css", 'a+').write(text)

open("style.css", 'a+').write(f"\n{text}")

0 comments on commit 1c2dc74

Please sign in to comment.