formatitko/formatitko.py

44 lines
1.7 KiB
Python
Raw Normal View History

2022-11-20 00:20:41 +01:00
#!/usr/bin/env python3
2023-02-06 01:00:45 +01:00
import argparse
2022-11-24 16:57:54 +01:00
import re
import sys
from typing import List
2023-02-06 01:00:45 +01:00
import os
2022-11-20 00:20:41 +01:00
2023-01-30 01:46:50 +01:00
# Import local files
from transform import transform
2023-01-30 01:46:50 +01:00
from util import *
from context import Context
from group import Group
2023-02-02 18:48:48 +01:00
from katex import KatexClient
from html import html
from tex import tex
2023-02-06 01:00:45 +01:00
from images import ImageProcessor
2023-01-30 01:46:50 +01:00
2022-11-20 00:37:03 +01:00
from mj_show import show
2022-11-20 00:20:41 +01:00
2023-02-06 01:00:45 +01:00
parser = argparse.ArgumentParser()
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. By default contains the directory of each MarkDown file.", nargs="+", default=[])
2023-02-06 01:00:45 +01:00
parser.add_argument("-p", "--img-public-dir", help="Directory to put processed images into. The program will not overwrite existing images.", nargs=1, default="public")
parser.add_argument("-i", "--img-web-path", help="Path where the processed images are available on the website.", nargs=1, default="/")
2023-02-06 01:00:45 +01:00
parser.add_argument("-w", "--output-html", help="The HTML file (for Web) to write into.", nargs=1, default="output.html")
parser.add_argument("-t", "--output-tex", help="The TEX file to write into.", nargs=1, default="output.tex")
parser.add_argument("input_filename", help="The MarkDown file to process.")
args = parser.parse_args()
doc = import_md(open(args.input_filename, "r").read())
language = doc.get_metadata("language", None, True)
context = Context(doc, args.input_filename)
2023-02-06 01:00:45 +01:00
doc = doc.walk(transform, context)
2023-02-06 01:00:45 +01:00
doc.content = [Group(*doc.content, metadata={"language":language})]
2023-02-06 01:00:45 +01:00
2023-02-03 14:54:16 +01:00
katexClient = KatexClient()
imageProcessor = ImageProcessor(args.img_public_dir, args.img_web_path, *args.img_lookup_dirs)
2023-02-06 01:00:45 +01:00
open(args.output_html, "w").write(html(doc, katexClient, imageProcessor))
open(args.output_tex, "w").write(tex(doc, imageProcessor))