diff --git a/pyproject.toml b/pyproject.toml index 91ed4d6..f4436d1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -28,8 +28,9 @@ formatitko = "formatitko.formatitko:main" [tool.setuptools_scm] - [tool.setuptools.packages.find] where = ["src"] exclude = ["src/formatitko/katex-server/node_modules"] +[tool.pyright] +strictParameterNoneValue = false diff --git a/src/formatitko/command.py b/src/formatitko/command.py index d9831c9..0fd3b4e 100644 --- a/src/formatitko/command.py +++ b/src/formatitko/command.py @@ -1,10 +1,9 @@ -from panflute import Div,Span,Para +from panflute import Div, Span, Para, Element from typing import List # Import local files -from .util import * +from .util import nullify, import_md from .context import Context -from .mj_show import show class Command: pass diff --git a/src/formatitko/context.py b/src/formatitko/context.py index 2f48823..d262ec2 100644 --- a/src/formatitko/context.py +++ b/src/formatitko/context.py @@ -1,6 +1,5 @@ from panflute import Doc, Div -from typing import Dict import os @@ -71,15 +70,15 @@ class Context: if key == "language": print("WARN: Setting language this way doesn't propagate to TeX. Either use the Front Matter or specify it additionally using the \\languagexx macro.") meta = self.doc.metadata - key = key.split(".") - for k in key[:-1]: + keys = key.split(".") + for k in keys[:-1]: meta = meta[k] - meta[key[-1]] = value + meta[keys[-1]] = value def unset_metadata(self, key: str): meta = self.doc.metadata - key = key.split(".") - for k in key[:-1]: + keys = key.split(".") + for k in keys[:-1]: meta = meta[k] del meta.content[key[-1]] # A hack because MetaMap doesn't have a __delitem__ diff --git a/src/formatitko/formatitko.py b/src/formatitko/formatitko.py index b2ee9e9..9465456 100755 --- a/src/formatitko/formatitko.py +++ b/src/formatitko/formatitko.py @@ -1,14 +1,10 @@ #!/usr/bin/env python3 import argparse -import re -import sys -from typing import List -import os # Import local files from .transform import transform -from .util import * +from .util import import_md from .context import Context, Group from .katex import KatexClient from .html import html diff --git a/src/formatitko/html.py b/src/formatitko/html.py index 87d1dc7..55d54b7 100644 --- a/src/formatitko/html.py +++ b/src/formatitko/html.py @@ -4,6 +4,7 @@ from pygments.lexers import get_lexer_by_name from pygments.formatters import HtmlFormatter from pygments.util import ClassNotFound import os +from typing import Union from .whitespace import NBSP from .transform import FQuoted @@ -12,7 +13,7 @@ from .util import inlinify from .context import Group from .images import ImageProcessor -def html(e: Element, k: KatexClient, i: ImageProcessor, indent_level: int=0, indent_str: str="\t") -> str: +def html(e: Union[Element, ListContainer], k: KatexClient, i: ImageProcessor, indent_level: int=0, indent_str: str="\t") -> str: # `only` attribute which makes transformed elements appear only in tex # output or html output @@ -40,7 +41,7 @@ def html(e: Element, k: KatexClient, i: ImageProcessor, indent_level: int=0, ind Emph: "em", Caption: "figcaption", Para: "p", - Header: "h"+str(e.level) if hasattr(e, "level") else "", + Header: "h"+str(e.level) if isinstance(e, Header) else "", LineBlock: "p", ListItem: "li", SmallCaps: "span", diff --git a/src/formatitko/images.py b/src/formatitko/images.py index aee0b4a..6121bb5 100644 --- a/src/formatitko/images.py +++ b/src/formatitko/images.py @@ -1,4 +1,4 @@ -from typing import List +from typing import List, Union import os import shutil import subprocess @@ -74,7 +74,7 @@ class ImageProcessor: return Image.open(full_path).size - def find_image(self, input_filename: str, additional_dirs: List[str]=[]) -> str: + def find_image(self, input_filename: str, additional_dirs: List[str]=[]) -> Union[str, None]: for dir in [*self.lookup_dirs, *additional_dirs]: if os.path.isfile(dir + "/" + input_filename): return dir + "/" + input_filename diff --git a/src/formatitko/katex.py b/src/formatitko/katex.py index fc9ed4f..99a70fc 100644 --- a/src/formatitko/katex.py +++ b/src/formatitko/katex.py @@ -4,7 +4,6 @@ import tempfile import json import os from typing import Dict -import time class KatexError(Exception): @@ -66,5 +65,5 @@ class KatexClient: def __enter__(self): return self - def __exit__(self, type, value, tb): + def __exit__(self): self._server_process.terminate() diff --git a/src/formatitko/mj_show.py b/src/formatitko/mj_show.py index a02f2c1..e4e2541 100644 --- a/src/formatitko/mj_show.py +++ b/src/formatitko/mj_show.py @@ -3,7 +3,7 @@ ### import sys, re -from panflute import * +from panflute import Str, Element, ListContainer, DictContainer avoid_keys = { 'dict', diff --git a/src/formatitko/tex.py b/src/formatitko/tex.py index 6d6a674..3d47568 100644 --- a/src/formatitko/tex.py +++ b/src/formatitko/tex.py @@ -1,5 +1,6 @@ from panflute import * import os +from typing import Union from .whitespace import NBSP from .transform import FQuoted @@ -8,7 +9,7 @@ from .context import Group from .images import ImageProcessor # Heavily inspired by: git://git.ucw.cz/labsconf2022.git -def tex(e: Element, i: ImageProcessor, indent_level: int=0, indent_str: str="\t") -> str: +def tex(e: Union[Element, ListContainer], i: ImageProcessor, indent_level: int=0, indent_str: str="\t") -> str: # `only` attribute which makes transformed elements appear only in tex # output or html output @@ -30,7 +31,7 @@ def tex(e: Element, i: ImageProcessor, indent_level: int=0, indent_str: str="\t" tag = e.tag.lower() tags = { - Header: "h"+chr(64 + e.level) if hasattr(e, "level") else "", + Header: "h"+chr(64 + e.level) if isinstance(e, Header) else "", } if type(e) in tags: tag = tags[type(e)] diff --git a/src/formatitko/transform.py b/src/formatitko/transform.py index 7b1c332..45f64dc 100644 --- a/src/formatitko/transform.py +++ b/src/formatitko/transform.py @@ -1,11 +1,11 @@ -from panflute import * +from panflute import Element, Div, Span, Quoted, Image, CodeBlock, Str, MetaInlines, MetaStr, MetaBool import re # Import local files -from .whitespace import * -from .command import * -from .util import * -from .context import * +from .whitespace import Whitespace, NBSP, bavlna +from .command import Command, BlockCommand, InlineCommand, handle_command_define, executeCommand +from .util import nullify, import_md +from .context import Context, Group # This is a small extension to the Quoted panflute elements which allows to