#!/usr/bin/env python3
import argparse
import sys
# Import local files
from . transform import transform
from . util import import_md
from . context import Context , BlockGroup
from . katex import KatexClient
from . html import html
from . tex import tex
from . images import ImageProcessor
from . output_generator import OutputGenerator
from . html_generator import HTMLGenerator
from . transform_processor import TransformProcessor
from . pandoc_processor import PandocProcessor
from panflute import convert_text
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 overwrite images, whose dependencies are newer. " , default = " public " )
parser . add_argument ( " -c " , " --img-cache-dir " , help = " Directory to cache processed images and intermediate products. The program will overwrite files, whose dependencies are newer. " , default = " cache " )
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. " )
parser . add_argument ( " -t " , " --output-tex " , help = " The TEX file to write into. " )
parser . add_argument ( " -m " , " --output-md " , help = " The Markdown file to write into. (Uses pandoc to generate markdown) " )
parser . add_argument ( " -j " , " --output-json " , help = " The JSON file to dump the pandoc-compatible AST into. " )
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 :
OutputGenerator ( sys . stdout ) . generate ( doc )
doc = TransformProcessor ( args . input_filename ) . transform ( doc )
# Initialize the image processor (this just keeps some basic state)
imageProcessor = ImageProcessor ( args . img_public_dir , args . img_web_path , args . img_cache_dir , * args . img_lookup_dirs )
if args . output_html is not None :
# Initialize KaTeX client (this runs the node app and connects to a unix socket)
with KatexClient ( ) as katexClient :
HTMLGenerator ( open ( args . output_html , " w " ) , katexClient , imageProcessor ) . generate ( doc )
if args . output_md is not None :
open ( args . output_md , " w " ) . write ( convert_text ( PandocProcessor ( ) . transform ( doc ) , input_format = " panflute " , output_format = " markdown " ) )
if args . output_json is not None :
open ( args . output_json , " w " ) . write ( convert_text ( PandocProcessor ( ) . transform ( doc ) , input_format = " panflute " , output_format = " json " ) )
if args . debug :
print ( " ----------------------------------- " )
OutputGenerator ( sys . stdout ) . generate ( doc )
if __name__ == " __main__ " :
main ( )