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)
This commit is contained in:
Jan Černohorský 2023-02-05 01:15:08 +01:00
parent 241e001023
commit da30967d12
5 changed files with 48 additions and 11 deletions

View file

@ -15,9 +15,11 @@
\it{}#1% \it{}#1%
}} }}
\def\superscript#1{\leavevmode\raise3pt\hbox{\fiverm#1}}
\newcount\fncount \newcount\fncount
\fncount=1 \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\fn#1{\footnote\fnmark{#1}\advance\fncount by 1}
\def\hA#1{{\parskip1em\settextsize{14}\bf #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}}} \long\def\blockquote#1{\vskip\lineskip\vskip\parskip\hbox{\vrule\hskip5pt\vbox{#1}}}
\def\code#1{{\tt #1}} \def\code#1{{\tt #1}}
\let\codeblock\verbatim \let\codeblock\verbatim
\def\figure#1{#1} \def\subscript#1{\leavevmode\lower1pt\hbox{\fiverm#1}}
\def\image#1{#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\table#1{#1}
\def\tablebody#1{#1} \def\tablebody#1{#1}
\def\tablerow#1{#1} \def\tablerow#1{#1}

View file

@ -7,6 +7,7 @@ from pygments.util import ClassNotFound
from whitespace import NBSP from whitespace import NBSP
from transform import FQuoted from transform import FQuoted
from katex import KatexClient from katex import KatexClient
from util import inlinify
def html(e: Element, k: KatexClient, indent_level: int=0, indent_str: str="\t") -> str: 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: if hasattr(e, "classes") and len(e.classes) != 0:
attributes += f' class="{" ".join(e.classes)}"' attributes += f' class="{" ".join(e.classes)}"'
# TODO: Pass attributes down to HTML too
if isinstance(e, CodeBlock): if isinstance(e, CodeBlock):
if e.attributes["highlight"] == True or e.attributes["highlight"] == 'True': if e.attributes["highlight"] == True or e.attributes["highlight"] == 'True':
for cl in e.classes: 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): if isinstance(e, Note):
content_head = "(" content_head = "("
content_foot = ")" content_foot = ")"
if len(e.content) == 1 and isinstance(e.content[0], Para): if inlinify(e) is not None:
return f' <note>({html(e.content[0].content, k, 0, "")})</note>' return f' <note>({html(inlinify(e), k, 0, "")})</note>'
if isinstance(e, OrderedList): if isinstance(e, OrderedList):
tag = "ol" tag = "ol"

12
test.md
View file

@ -46,7 +46,13 @@ This should only be shown to cats the second time
# [$are_we_there_yet]{} # [$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} ```python {.run}
ctx.set_metadata("language", "cs") 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 text with some inline math: $\sum_{i=1}^nn^2$. Plus some display math:
A link with the link in the link: <https://bruh.com>
H~2~O is a liquid. 2^10^ is 1024.
[Underline]{.underline}
:::{only=tex} :::{only=tex}
$$ $$

22
tex.py
View file

@ -2,6 +2,7 @@ from panflute import *
from whitespace import NBSP from whitespace import NBSP
from transform import FQuoted from transform import FQuoted
from util import inlinify
# Heavily inspired by: git://git.ucw.cz/labsconf2022.git # Heavily inspired by: git://git.ucw.cz/labsconf2022.git
def tex(e, indent_level: int=0, indent_str: str="\t") -> str: 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" close = "\\endlist"
# FIXME: Starting number of list # 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): if isinstance(e, ListItem):
tag = ":" tag = ":"
if isinstance(e, Link): if isinstance(e, Link):
tag = "linkurl" if len(e.content) == 1 and isinstance(e.content[0], Str) and e.content[0].text == e.url:
arguments = f'{{{e.url}}}' tag = "url"
else:
tag = "linkurl"
arguments = f'{{{e.url}}}'
if isinstance(e, Math): if isinstance(e, Math):
if e.format == "DisplayMath": if e.format == "DisplayMath":
@ -118,8 +132,8 @@ def tex(e, indent_level: int=0, indent_str: str="\t") -> str:
if isinstance(e, Note): if isinstance(e, Note):
tag = "fn" tag = "fn"
if len(e.content) == 1 and isinstance(e.content[0], Para): if inlinify(e) is not None:
return f'\\fn{{{tex(e.content[0].content, 0, "")}}}' return f'\\fn{{{tex(inlinify(e), 0, "")}}}'
if isinstance(e, RawInline): if isinstance(e, RawInline):
if e.format == "tex": if e.format == "tex":

View file

@ -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 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: def replaceEl(e: Element, r: Element) -> Element:
parent = e.parent parent = e.parent
parent.content[e.index] = r parent.content[e.index] = r