diff --git a/.gitignore b/.gitignore index 5663c89..04b8fd9 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,7 @@ public/ *.jpeg *.svg !test/1px.png +**/.mypy_cache +**/*.egg-info +build/ +dist/ diff --git a/formatitko.py b/formatitko.py deleted file mode 100755 index 17d072d..0000000 --- a/formatitko.py +++ /dev/null @@ -1,62 +0,0 @@ -#!/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 context import Context, Group -from katex import KatexClient -from html import html -from tex import tex -from images import ImageProcessor - -from mj_show import show - -# Initialize command line arguments -parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter) -parser.add_argument("-l", "--img-lookup-dirs", help="Image lookup directories. When processing images, the program will try to find the image in them first. Always looks for images in the same folder as the markdown file.", nargs="+", default=[]) -parser.add_argument("-p", "--img-public-dir", help="Directory to put processed images into. The program will not overwrite existing images.", default="public") -parser.add_argument("-i", "--img-web-path", help="Path where the processed images are available on the website.", default="/") -parser.add_argument("-w", "--output-html", help="The HTML file (for Web) to write into.", default="output.html") -parser.add_argument("-t", "--output-tex", help="The TEX file to write into.", default="output.tex") -parser.add_argument("input_filename", help="The markdown file to process.") -parser.add_argument("--debug", action='store_true') -args = parser.parse_args() -# TODO: Accept path to unix socket for katexClient, then don't init our own, -# just connect to an existing one. For formátíking many files in a row. - -# Use panflute to parse the input MD file -doc = import_md(open(args.input_filename, "r").read()) - -if args.debug: - print(show(doc)) - -# The language metadatum is important, so it's read before transformation and -# then attached to a group inside the Doc -language = doc.get_metadata("language", None, True) -context = Context(doc, args.input_filename) - -# Transform the document. This includes all the fancy formatting this software does. -doc = doc.walk(transform, context) - -# Now wrap the document contents in a group, which is able to pop its language -# setting out to TeX -doc.content = [Group(*doc.content, metadata={"language":language})] - -# Initialize the image processor (this just keeps some basic state) -imageProcessor = ImageProcessor(args.img_public_dir, args.img_web_path, *args.img_lookup_dirs) - -# Initialize KaTeX client (this runs the node app and connects to a unix socket) -with KatexClient() as katexClient: - # Generate HTML and TeX out of the transformed document - open(args.output_html, "w").write(html(doc, katexClient, imageProcessor)) - open(args.output_tex, "w").write(tex(doc, imageProcessor)) - -if args.debug: - print(show(doc)) - diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..801ebd2 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,26 @@ +[build-system] +requires = ["setuptools>=61.0"] +build-backend = "setuptools.build_meta" + +[project] +name = "formatitko" +version = "0.0.1" +authors = [ + { name="Greenscreener ", email="gs@grsc.cz" }, + { name="Organizers of KSP", email="ksp-tech@ksp.mff.cuni.cz" } +] +description = "A python program based on pandoc and its python library panflute for converting from markdown to TeX and HTML with added fancy features like image processing, python-based macros and much more." +readme = "README.md" +requires-python = ">=3.9" +classifiers = [ + "Programming Language :: Python :: 3", + "License :: OSI Approved :: MIT License", + "Operating System :: Linux", +] + +[project.urls] +"Homepage" = "https://gitea.ks.matfyz.cz/KSP/formatitko" +"Bug Tracker" = "https://gitea.ks.matfyz.cz/KSP/formatitko/issues" + +[project.scripts] +formatitko = "formatitko.formatitko:main" diff --git a/src/formatitko/__init__.py b/src/formatitko/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/command.py b/src/formatitko/command.py similarity index 97% rename from command.py rename to src/formatitko/command.py index 4e89390..d9831c9 100644 --- a/command.py +++ b/src/formatitko/command.py @@ -2,9 +2,9 @@ from panflute import Div,Span,Para from typing import List # Import local files -from util import * -from context import Context -from mj_show import show +from .util import * +from .context import Context +from .mj_show import show class Command: pass diff --git a/context.py b/src/formatitko/context.py similarity index 100% rename from context.py rename to src/formatitko/context.py diff --git a/src/formatitko/formatitko.py b/src/formatitko/formatitko.py new file mode 100755 index 0000000..b2ee9e9 --- /dev/null +++ b/src/formatitko/formatitko.py @@ -0,0 +1,67 @@ +#!/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 .context import Context, Group +from .katex import KatexClient +from .html import html +from .tex import tex +from .images import ImageProcessor + +from .mj_show import show + +def main(): + # Initialize command line arguments + parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter) + parser.add_argument("-l", "--img-lookup-dirs", help="Image lookup directories. When processing images, the program will try to find the image in them first. Always looks for images in the same folder as the markdown file.", nargs="+", default=[]) + parser.add_argument("-p", "--img-public-dir", help="Directory to put processed images into. The program will not overwrite existing images.", default="public") + parser.add_argument("-i", "--img-web-path", help="Path where the processed images are available on the website.", default="/") + parser.add_argument("-w", "--output-html", help="The HTML file (for Web) to write into.", default="output.html") + parser.add_argument("-t", "--output-tex", help="The TEX file to write into.", default="output.tex") + parser.add_argument("input_filename", help="The markdown file to process.") + parser.add_argument("--debug", action='store_true') + args = parser.parse_args() + # TODO: Accept path to unix socket for katexClient, then don't init our own, + # just connect to an existing one. For formátíking many files in a row. + + # Use panflute to parse the input MD file + doc = import_md(open(args.input_filename, "r").read()) + + if args.debug: + print(show(doc)) + + # The language metadatum is important, so it's read before transformation and + # then attached to a group inside the Doc + language = doc.get_metadata("language", None, True) + context = Context(doc, args.input_filename) + + # Transform the document. This includes all the fancy formatting this software does. + doc = doc.walk(transform, context) + + # Now wrap the document contents in a group, which is able to pop its language + # setting out to TeX + doc.content = [Group(*doc.content, metadata={"language":language})] + + # Initialize the image processor (this just keeps some basic state) + imageProcessor = ImageProcessor(args.img_public_dir, args.img_web_path, *args.img_lookup_dirs) + + # Initialize KaTeX client (this runs the node app and connects to a unix socket) + with KatexClient() as katexClient: + # Generate HTML and TeX out of the transformed document + open(args.output_html, "w").write(html(doc, katexClient, imageProcessor)) + open(args.output_tex, "w").write(tex(doc, imageProcessor)) + + if args.debug: + print(show(doc)) + + +if __name__ == "__main__": + main() + diff --git a/html.py b/src/formatitko/html.py similarity index 98% rename from html.py rename to src/formatitko/html.py index 17267ed..87d1dc7 100644 --- a/html.py +++ b/src/formatitko/html.py @@ -5,12 +5,12 @@ 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 context import Group -from images import ImageProcessor +from .whitespace import NBSP +from .transform import FQuoted +from .katex import KatexClient +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: diff --git a/images.py b/src/formatitko/images.py similarity index 100% rename from images.py rename to src/formatitko/images.py diff --git a/katex.py b/src/formatitko/katex.py similarity index 100% rename from katex.py rename to src/formatitko/katex.py diff --git a/mj_show.py b/src/formatitko/mj_show.py similarity index 100% rename from mj_show.py rename to src/formatitko/mj_show.py diff --git a/tex.py b/src/formatitko/tex.py similarity index 98% rename from tex.py rename to src/formatitko/tex.py index 238255e..6d6a674 100644 --- a/tex.py +++ b/src/formatitko/tex.py @@ -1,11 +1,11 @@ from panflute import * import os -from whitespace import NBSP -from transform import FQuoted -from util import inlinify -from context import Group -from images import ImageProcessor +from .whitespace import NBSP +from .transform import FQuoted +from .util import inlinify +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: diff --git a/transform.py b/src/formatitko/transform.py similarity index 98% rename from transform.py rename to src/formatitko/transform.py index c9ada1c..7b1c332 100644 --- a/transform.py +++ b/src/formatitko/transform.py @@ -2,10 +2,10 @@ from panflute import * import re # Import local files -from whitespace import * -from command import * -from util import * -from context import * +from .whitespace import * +from .command import * +from .util import * +from .context import * # This is a small extension to the Quoted panflute elements which allows to diff --git a/util.py b/src/formatitko/util.py similarity index 100% rename from util.py rename to src/formatitko/util.py diff --git a/whitespace.py b/src/formatitko/whitespace.py similarity index 98% rename from whitespace.py rename to src/formatitko/whitespace.py index 928f94b..155f5e5 100644 --- a/whitespace.py +++ b/src/formatitko/whitespace.py @@ -2,7 +2,7 @@ from panflute import Space, SoftBreak, Str, Math from typing import Union # Import local files -from context import Context +from .context import Context Whitespace = Union[Space,SoftBreak]