Skip to content

Commit

Permalink
Fleshed out bundling
Browse files Browse the repository at this point in the history
  • Loading branch information
Istvan Bozso committed Feb 17, 2020
1 parent aa573a3 commit 49779a4
Showing 1 changed file with 36 additions and 23 deletions.
59 changes: 36 additions & 23 deletions utils/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""

import base64
import os.path as path

from utils.utils import export
from utils.simpledoc import SimpleDoc, _attributes
Expand Down Expand Up @@ -41,7 +42,7 @@ def inner(self, *args, **kwargs):
return inner

stags = {
"meta", "link", "source", "iframe",
"meta", "link", "iframe",
}

for stag in stags:
Expand Down Expand Up @@ -120,9 +121,6 @@ def __call__(self, media_path):

encoder = Encoder()

encodable = {
"img", "video",
}

class ImagePaths(object):
__slots__ = ("paths", "doc", "width",)
Expand Down Expand Up @@ -181,21 +179,23 @@ def img(self, name, *args, **kwargs):

self.doc.img(*args, **kwargs)

def noencode(path):
return path

@export
class HTML(SimpleDoc):
__slots__ = (
"bundle", "encoder",
"encoder",
)

def __init__(self, *args, **kwargs):
kwargs.setdefault("stag_end", ">")

self.bundle, self.encoder = (
bool(kwargs.pop("bundle", False)),
kwargs.pop("encoder", encoder),
)
if bool(kwargs.pop("bundle", False)):
self.encoder = kwargs.pop("encoder", encoder)
else:
self.encoder = noencode

SimpleDoc.__init__(self, *args, **kwargs)

@staticmethod
Expand All @@ -210,18 +210,6 @@ def math(self, txt):

def imath(self, txt):
self._append("\( %s \)" % (txt))

def img(self, *args, **kwargs):
path = kwargs.pop("src")

if self.bundle:
src = self.encoder(path)
else:
src = path

kwargs["src"] = src

self.stag("img", *args, **kwargs)

def image_paths(self, width, *paths):
return ImagePaths(self, width, *paths)
Expand All @@ -239,7 +227,32 @@ def url(self, address, txt, **kwargs):

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




def make_encodable(name, mode):
if mode == "stag":
def inner(self, *args, **kwargs):
kwargs["src"] = self.encoder(kwargs.pop("src"))

self.stag(name, *args, **kwargs)
elif mode == "tag":
def inner(self, *args, **kwargs):
kwargs["src"] = self.encoder(kwargs.pop("src"))

return self.tag(self, name, *args, **kwargs)

return inner

encodable = {
"img": "stag",
"source": "stag",
}


for enc, mode in encodable.items():
setattr(HTML, enc, make_encodable(enc, mode))


yt_opts = {
"autoplay": False,
Expand Down

0 comments on commit 49779a4

Please sign in to comment.