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
2023-01-24 03:17:30 +01:00
import sys
2023-01-29 19:21:03 +01:00
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
2023-01-31 14:35:44 +01:00
from transform import transform
2023-01-30 01:46:50 +01:00
from util import *
2023-01-31 14:35:44 +01:00
from context import Context
2023-02-05 17:13:50 +01:00
from group import Group
2023-02-02 18:48:48 +01:00
from katex import KatexClient
2023-02-04 00:29:45 +01:00
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 ( )
2023-02-06 16:56:52 +01:00
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 " )
2023-02-06 16:56:52 +01:00
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 ( ) )
2023-02-05 17:13:50 +01:00
language = doc . get_metadata ( " language " , None , True )
2023-02-06 16:56:52 +01:00
context = Context ( doc , args . input_filename )
2023-02-06 01:00:45 +01:00
2023-01-31 13:38:12 +01:00
doc = doc . walk ( transform , context )
2023-02-06 01:00:45 +01:00
2023-02-05 17:13:50 +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 ( )
2023-02-06 16:56:52 +01:00
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 ) )