Skip to content

Commit

Permalink
Some bugfixes and polish of the html generation library
Browse files Browse the repository at this point in the history
  • Loading branch information
István Bozsó committed Mar 4, 2020
1 parent 80efab7 commit a146b9d
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 51 deletions.
40 changes: 7 additions & 33 deletions css/notes.css
Original file line number Diff line number Diff line change
Expand Up @@ -56,25 +56,16 @@ h3 {
}

.center {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
text-align: center;
margin-left: auto;
margin-right: auto;
}

table.center {
position: absolute;
top: 25px;
bottom: 25px;
left: 25px;
right: 25px;
margin: 10px;
width: 90%;
margin: 0px auto;

}


li {
margin-bottom: 6px;
margin-top: 10px;
Expand Down Expand Up @@ -293,6 +284,7 @@ table caption {

table {
border-collapse: collapse;
text-align: center;
}

th, td {
Expand All @@ -302,27 +294,9 @@ th, td {

th {
font-weight: bold;
background-color: bbbb77;
background-color: #bbbb77;
}

tr:nth-child(even) {
background-color: #f2f2f2;
}


/*Targets the headings and first column*/
/*
th, td:nth-child(1) {
font-size: 1.1em;
font-weight: 500;
}
*/

/*Targets footer explanation and attribution*/
/*
tfoot tr:nth-child(1n+2) {
font-size: 0.9em;
font-weight: normal;
text-align: left;
}
*/
3 changes: 3 additions & 0 deletions utils/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
py3 = sys.version_info[0] == 3


from collections.abc import Iterable


if py3:
str_t = str
else:
Expand Down
12 changes: 5 additions & 7 deletions utils/html/doc.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from utils.html import Children, st, Options
from utils.html import Children, st, Options, t

__all__ = (
"Doc",
Expand All @@ -25,12 +25,12 @@ class Doc(Children):
)

def __init__(self, *args, filename=None, desc="", keywords=[],
author="", viewport={}, encoder=None):
author="", viewport={}, encoder=None, title=""):

self.filename, self.description, self.keywords, self.author, \
self.viewport, self.encoder = (
self.viewport, self.encoder, self.title = (
filename, desc, keywords, author,
Viewport(**viewport), encoder
Viewport(**viewport), encoder, title
)

self.children = ["<!DOCTYPE html>"] + list(args)
Expand All @@ -44,16 +44,14 @@ def meta(self):
kw = ",".join(kw)

return (
t.title(self.title),
st.meta(charset="utf-8"),
st.meta(name="description", content=self.description),
st.meta(name="keywords", content=kw),
st.meta(name="author", content=self.author),
st.meta(name="viewport", content=self.viewport.parse_options()),
)

def append(self, *args):
self.children.append(args)

def write_to(self, writer):
enc = self.encoder

Expand Down
9 changes: 4 additions & 5 deletions utils/html/jslibs.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from utils import namespace
from utils.html import Library
from utils.html import Library, t

__all__ = (
"libs", "plotly", "Plotly",
Expand All @@ -15,16 +15,15 @@ def __init__(self, *args, **kwargs):

Library.__init__(self, kwargs["path"])

def add(self, doc, *args, **kwargs):
def add(self, *args, **kwargs):
args = set(args)

if self._async:
args.add("async")

kwargs["src"] = self.path

with doc.tag("script", *args, **kwargs):
pass

return t.script(**kwargs)


libs = namespace(
Expand Down
35 changes: 29 additions & 6 deletions utils/html/tag.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from utils import str_t, namespace
from utils import str_t, namespace, isiter
from inspect import isgenerator

__all__ = (
"tags", "t", "stags", "st", "Children", "Options",
"tags", "t", "stags", "st", "Children", "Options", "url", "doi",
)


Expand Down Expand Up @@ -51,6 +52,21 @@ def With(cls, **kwargs):


class Children(object):
def __call__(self, *items):
if isgenerator(items[0]):
items = tuple(items[0])

self.children.extend(items)

def append(self, item):
self.children.append(item)

# @staticmethod
# def sum_impl(self):
# for child in self.children:
# try:


def render_children(self):
return "".join(
child.render()
Expand All @@ -70,7 +86,7 @@ def __init__(self, *args, **kwargs):
self.children = list(args)

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

return "<%s %s>%s</%s>" % (
name, self.parse_options(),
Expand All @@ -80,7 +96,7 @@ def render(self):
class SelfClosingTag(BaseTag):
def render(self):
return "<%s %s>" % (
self.__class__.__name__,
self.name,
self.parse_options()
)

Expand Down Expand Up @@ -113,7 +129,7 @@ def render(self):

tags = namespace(_name_="Tags",
**{
key: type(key, (Tag,), {})
key: type(key, (Tag,), {"name": key})
for key in _tags
}
)
Expand All @@ -128,7 +144,7 @@ def render(self):

stags = namespace(_name_="SelfClosingTags",
**{
key: type(key, (SelfClosingTag,), {})
key: type(key, (SelfClosingTag,), {"name": key})
for key in _stags
}
)
Expand All @@ -139,6 +155,13 @@ def render(self):

st = stags

def url(address, txt, **kwargs):
return t.a(txt, href=address, **kwargs)

def doi(number, **kwargs):
return url("https://doi.org/%s" % number, **kwargs)


symbols = namespace(
linebreak = "<br>",
thematic_break = "<hr>",
Expand Down
5 changes: 5 additions & 0 deletions utils/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ class Path(object):
def __init__(self, path):
self._path = path

@classmethod
def expanduser(cls, p):
return cls(path.expanduser(p))

@classmethod
def joined(cls, *args):
return cls(path.join(*args))
Expand Down Expand Up @@ -96,3 +100,4 @@ def __str__(self):

def __fspath__(self):
return self._path

0 comments on commit a146b9d

Please sign in to comment.