from panflute import *
from pygments import highlight
from pygments.lexers import get_lexer_by_name
from pygments.formatters import HtmlFormatter
from pygments.util import ClassNotFound
import os
from whitespace import NBSP
from transform import FQuoted
from katex import KatexClient
from util import inlinify
from group import Group
from images import ImageProcessor
def html(e: Element, k: KatexClient, i: ImageProcessor, indent_level: int=0, indent_str: str="\t") -> str:
if hasattr(e, "attributes") and "only" in e.attributes and e.attributes["only"] != "html":
return ""
if isinstance(e, ListContainer):
return ''.join([html(child, k, i, indent_level, indent_str) for child in e])
tag = e.tag.lower()
attributes = ""
content_foot = ""
content_head = ""
if isinstance(e, Str):
return e.text.replace(" ", " ")
tags = {
BulletList: "ul",
Doc: "main",
Emph: "em",
Caption: "figcaption",
Para: "p",
Header: "h"+str(e.level) if hasattr(e, "level") else "",
LineBlock: "p",
ListItem: "li",
SmallCaps: "span",
Strikeout: "strike",
Subscript: "sub",
Superscript: "sup",
Underline: "u",
TableBody: "tbody",
TableHead: "thead",
TableFoot: "tfoot",
TableRow: "tr",
TableCell: "td",
}
if type(e) in tags:
tag = tags[type(e)]
not_implemented = {
Citation: True,
Cite: True,
Definition: True,
DefinitionItem: True,
DefinitionList: True
}
if type(e) in not_implemented:
return f''
simple_string = {
NBSP: " ",
Space: " ",
Null: "",
LineBreak: f"\n{indent_level*indent_str}
\n{indent_level*indent_str}",
SoftBreak: f" ",
HorizontalRule: f"{indent_level*indent_str}
{e.text}' if isinstance(e, Figure): content_foot = html(e.caption, k, i, indent_level+1, indent_str) if isinstance(e, Caption): tag = "figcaption" if isinstance(e, Image): url = e.url source_dir = e.attributes["source_dir"] _, ext = os.path.splitext(url) ext = ext[1:] if ext in ["svg", "png", "jpeg", "gif"]: url = i.process_image(url, ext, source_dir) elif ext in ["pdf", "epdf"]: url = i.process_image(url, "png", source_dir, dpi=300) elif ext in ["jpg"]: url = i.process_image(url, "jpeg", source_dir) else: url = i.process_image(url, "png", source_dir) _, ext = os.path.splitext(url) ext = ext[1:] srcset = [] if ext in ["png", "jpeg"] and (not "no-srcset" in e.attributes or e.attributes["no-srcset"] == False or e.attributes["no-srcset"] == 'False'): # This is inspired by @vojta001's blogPhoto shortcode he made for # patek.cz: # https://gitlab.com/patek-devs/patek.cz/-/blob/master/themes/patek/layouts/shortcodes/blogPhoto.html width, height = i.get_image_size(url, [source_dir, i.public_dir]) sizes = [(640, 360, 85), (1280, 720, 85), (1920, 1080, 90)] # (widht, height, quality) for size in sizes: if width <= size[0] and height <= size[1]: srcset.append((f'{i.web_path}/{url}', f'{width}w')) break quality = size[2] if ext == "jpeg" else None srcset.append((f'{i.web_path}/{i.process_image(url, ext, source_dir, width=size[0], height=size[1], quality=quality)}', f'{size[0]}w')) url = i.web_path + "/" + url attributes = f'{" style=width:"+e.attributes["width"] if "width" in e.attributes else ""} alt="{e.title or html(e.content, k, i, 0, "")}"' if len(srcset) != 0: return f'' else: return f'' if isinstance(e, Header): tag = "h"+str(e.level) if isinstance(e, Link): tag = "a" attributes += f' href="{e.url}"' if e.title: attributes += f' title="{e.title}"' if isinstance(e, LineItem): return indent_level*indent_str + html(e.content, k, i) + "