From a146b9d3dd16ab981f5e9ceeeddd39ec2cadd905 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Istv=C3=A1n=20Bozs=C3=B3?= Date: Wed, 4 Mar 2020 16:46:41 +0100 Subject: [PATCH] Some bugfixes and polish of the html generation library --- css/notes.css | 40 +++++++--------------------------------- utils/base.py | 3 +++ utils/html/doc.py | 12 +++++------- utils/html/jslibs.py | 9 ++++----- utils/html/tag.py | 35 +++++++++++++++++++++++++++++------ utils/path.py | 5 +++++ 6 files changed, 53 insertions(+), 51 deletions(-) diff --git a/css/notes.css b/css/notes.css index f3660af..3edf13a 100644 --- a/css/notes.css +++ b/css/notes.css @@ -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; @@ -293,6 +284,7 @@ table caption { table { border-collapse: collapse; + text-align: center; } th, td { @@ -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; -} -*/ diff --git a/utils/base.py b/utils/base.py index 8ceda8b..18a34f1 100644 --- a/utils/base.py +++ b/utils/base.py @@ -11,6 +11,9 @@ py3 = sys.version_info[0] == 3 +from collections.abc import Iterable + + if py3: str_t = str else: diff --git a/utils/html/doc.py b/utils/html/doc.py index c58b1db..e666c44 100644 --- a/utils/html/doc.py +++ b/utils/html/doc.py @@ -1,4 +1,4 @@ -from utils.html import Children, st, Options +from utils.html import Children, st, Options, t __all__ = ( "Doc", @@ -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 = [""] + list(args) @@ -44,6 +44,7 @@ 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), @@ -51,9 +52,6 @@ def meta(self): 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 diff --git a/utils/html/jslibs.py b/utils/html/jslibs.py index 8a9a1cb..6204e42 100644 --- a/utils/html/jslibs.py +++ b/utils/html/jslibs.py @@ -1,5 +1,5 @@ from utils import namespace -from utils.html import Library +from utils.html import Library, t __all__ = ( "libs", "plotly", "Plotly", @@ -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( diff --git a/utils/html/tag.py b/utils/html/tag.py index 61a640b..d570a5c 100644 --- a/utils/html/tag.py +++ b/utils/html/tag.py @@ -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", ) @@ -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() @@ -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" % ( name, self.parse_options(), @@ -80,7 +96,7 @@ def render(self): class SelfClosingTag(BaseTag): def render(self): return "<%s %s>" % ( - self.__class__.__name__, + self.name, self.parse_options() ) @@ -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 } ) @@ -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 } ) @@ -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 = "
", thematic_break = "
", diff --git a/utils/path.py b/utils/path.py index 11365f9..f4ffbaf 100644 --- a/utils/path.py +++ b/utils/path.py @@ -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)) @@ -96,3 +100,4 @@ def __str__(self): def __fspath__(self): return self._path +