From da30967d121257e2ae265b7e78cb7b88680ececb Mon Sep 17 00:00:00 2001 From: Greenscreener Date: Sun, 5 Feb 2023 01:15:08 +0100 Subject: [PATCH] Figures and misc. TODO: - Custom implementation of attributes, classes and functions for math - Image processing - Tables for TeX - KaTeX math context (defs work in subsequent math blocks) --- formatitko.tex | 12 +++++++++--- html.py | 7 +++++-- test.md | 12 +++++++++++- tex.py | 22 ++++++++++++++++++---- util.py | 6 +++++- 5 files changed, 48 insertions(+), 11 deletions(-) diff --git a/formatitko.tex b/formatitko.tex index fbcf448..8a09b54 100644 --- a/formatitko.tex +++ b/formatitko.tex @@ -15,9 +15,11 @@ \it{}#1% }} + +\def\superscript#1{\leavevmode\raise3pt\hbox{\fiverm#1}} \newcount\fncount \fncount=1 -\def\fnmark{\leavevmode\raise3pt\hbox{\fiverm\the\fncount}} +\def\fnmark{\superscript{\the\fncount}} \def\fn#1{\footnote\fnmark{#1}\advance\fncount by 1} \def\hA#1{{\parskip1em\settextsize{14}\bf #1}} @@ -27,8 +29,12 @@ \long\def\blockquote#1{\vskip\lineskip\vskip\parskip\hbox{\vrule\hskip5pt\vbox{#1}}} \def\code#1{{\tt #1}} \let\codeblock\verbatim -\def\figure#1{#1} -\def\image#1{#1} +\def\subscript#1{\leavevmode\lower1pt\hbox{\fiverm#1}} +\def\strikeout#1{FIXME: Strikeout not implemented} +\def\underline#1{FIXME: Underline not implemented} +\def\figure#1#2{\vskip5pt\centerline{#1}\centerline{#2}\vskip5pt} +\def\caption#1{{\it #1}} +\let\image\putimage \def\table#1{#1} \def\tablebody#1{#1} \def\tablerow#1{#1} diff --git a/html.py b/html.py index 3074d26..0c6b689 100644 --- a/html.py +++ b/html.py @@ -7,6 +7,7 @@ from pygments.util import ClassNotFound from whitespace import NBSP from transform import FQuoted from katex import KatexClient +from util import inlinify def html(e: Element, k: KatexClient, indent_level: int=0, indent_str: str="\t") -> str: @@ -74,6 +75,8 @@ def html(e: Element, k: KatexClient, indent_level: int=0, indent_str: str="\t") if hasattr(e, "classes") and len(e.classes) != 0: attributes += f' class="{" ".join(e.classes)}"' + # TODO: Pass attributes down to HTML too + if isinstance(e, CodeBlock): if e.attributes["highlight"] == True or e.attributes["highlight"] == 'True': for cl in e.classes: @@ -115,8 +118,8 @@ def html(e: Element, k: KatexClient, indent_level: int=0, indent_str: str="\t") if isinstance(e, Note): content_head = "(" content_foot = ")" - if len(e.content) == 1 and isinstance(e.content[0], Para): - return f' ({html(e.content[0].content, k, 0, "")})' + if inlinify(e) is not None: + return f' ({html(inlinify(e), k, 0, "")})' if isinstance(e, OrderedList): tag = "ol" diff --git a/test.md b/test.md index f74b8c0..0a52e8f 100644 --- a/test.md +++ b/test.md @@ -46,7 +46,13 @@ This should only be shown to cats the second time # [$are_we_there_yet]{} -![This is a figure, go figure...](/this/image/does/not/exist.jpg) +![This is a figure, go figure...](/tmp/logo.pdf){width=10em} + +![This is a figure, go figure...](/tmp/logo.jpg){width=10em} + +![This is a figure, go figure...](/tmp/logo.png){width=10em} + +![This is a figure, go figure...](/tmp/logo.svg){width=10em} ```python {.run} ctx.set_metadata("language", "cs") @@ -74,7 +80,11 @@ A lot of spaces A text with some inline math: $\sum_{i=1}^nn^2$. Plus some display math: +A link with the link in the link: + +H~2~O is a liquid. 2^10^ is 1024. +[Underline]{.underline} :::{only=tex} $$ diff --git a/tex.py b/tex.py index 2c9e849..a9fd0ce 100644 --- a/tex.py +++ b/tex.py @@ -2,6 +2,7 @@ from panflute import * from whitespace import NBSP from transform import FQuoted +from util import inlinify # Heavily inspired by: git://git.ucw.cz/labsconf2022.git def tex(e, indent_level: int=0, indent_str: str="\t") -> str: @@ -103,12 +104,25 @@ def tex(e, indent_level: int=0, indent_str: str="\t") -> str: close = "\\endlist" # FIXME: Starting number of list + if isinstance(e, Image): + return f'\\image{{width {e.attributes["width"] if "width" in e.attributes else ""}}}{{{e.url}}}' + + if isinstance(e, Figure): + return f'\\figure{{{tex(e.content, indent_level+1, indent_str)}}}{{{tex(e.caption, indent_level+1, indent_str)}}}\n\n' + + if isinstance(e, Caption): + if inlinify(e) is not None: + return f'\\caption{{{tex(e.content, 0, "")}}}' + if isinstance(e, ListItem): tag = ":" if isinstance(e, Link): - tag = "linkurl" - arguments = f'{{{e.url}}}' + if len(e.content) == 1 and isinstance(e.content[0], Str) and e.content[0].text == e.url: + tag = "url" + else: + tag = "linkurl" + arguments = f'{{{e.url}}}' if isinstance(e, Math): if e.format == "DisplayMath": @@ -118,8 +132,8 @@ def tex(e, indent_level: int=0, indent_str: str="\t") -> str: if isinstance(e, Note): tag = "fn" - if len(e.content) == 1 and isinstance(e.content[0], Para): - return f'\\fn{{{tex(e.content[0].content, 0, "")}}}' + if inlinify(e) is not None: + return f'\\fn{{{tex(inlinify(e), 0, "")}}}' if isinstance(e, RawInline): if e.format == "tex": diff --git a/util.py b/util.py index ed0173a..683c519 100644 --- a/util.py +++ b/util.py @@ -1,6 +1,10 @@ -from panflute import Element, Block, Inline, Null, Str, Doc, convert_text +from panflute import Element, Block, Inline, Null, Str, Doc, convert_text, Para, Plain import re +def inlinify(e: Element) -> Element: + if len(e.content) == 1 and (isinstance(e.content[0], Para) or isinstance(e.content[0], Plain)): + return e.content[0].content + def replaceEl(e: Element, r: Element) -> Element: parent = e.parent parent.content[e.index] = r