Browse Source

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)
pull/28/head
Jan Černohorský 2 years ago
parent
commit
da30967d12
  1. 12
      formatitko.tex
  2. 7
      html.py
  3. 12
      test.md
  4. 18
      tex.py
  5. 6
      util.py

12
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}

7
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' <note>({html(e.content[0].content, k, 0, "")})</note>'
if inlinify(e) is not None:
return f' <note>({html(inlinify(e), k, 0, "")})</note>'
if isinstance(e, OrderedList):
tag = "ol"

12
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: <https://bruh.com>
H~2~O is a liquid. 2^10^ is 1024.
[Underline]{.underline}
:::{only=tex}
$$

18
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,10 +104,23 @@ 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):
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}}}'
@ -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":

6
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

Loading…
Cancel
Save