Compare commits
	
		
			2 commits
		
	
	
		
			a36da55481
			...
			cd91750c04
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| cd91750c04 | |||
| 51e51ec425 | 
					 3 changed files with 81 additions and 4 deletions
				
			
		
							
								
								
									
										65
									
								
								ksp/ksp_formatitko.py
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										65
									
								
								ksp/ksp_formatitko.py
									
									
									
									
									
										Executable file
									
								
							| 
						 | 
					@ -0,0 +1,65 @@
 | 
				
			||||||
 | 
					#!/usr/bin/env python3
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import argparse
 | 
				
			||||||
 | 
					import sys
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Import local files
 | 
				
			||||||
 | 
					from formatitko.transform import transform
 | 
				
			||||||
 | 
					from formatitko.util import import_md
 | 
				
			||||||
 | 
					from formatitko.context import Context, Group
 | 
				
			||||||
 | 
					from formatitko.katex import KatexClient
 | 
				
			||||||
 | 
					from formatitko.images import ImageProcessor
 | 
				
			||||||
 | 
					from ksp_html_generator import KSPHTMLGenerator
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					from formatitko.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 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.", 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_cache_dir, *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))
 | 
				
			||||||
 | 
							KSPHTMLGenerator(sys.stdout, katexClient, imageProcessor).generate(doc)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if args.debug:
 | 
				
			||||||
 | 
							print(show(doc))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					if __name__ == "__main__":
 | 
				
			||||||
 | 
					    main()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
							
								
								
									
										11
									
								
								ksp/ksp_html_generator.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								ksp/ksp_html_generator.py
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,11 @@
 | 
				
			||||||
 | 
					# A sample on how a custom html generator might look
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					from formatitko.html_generator import HTMLGenerator
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					from panflute import Link
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class KSPHTMLGenerator(HTMLGenerator):
 | 
				
			||||||
 | 
						def generate_Link(self, e: Link):
 | 
				
			||||||
 | 
							if e.url.startswith("ksp://"):
 | 
				
			||||||
 | 
								e.url = "https://ksp.mff.cuni.cz/viz/" + e.url[len("ksp://"):]
 | 
				
			||||||
 | 
							return super().generate_Link(e)
 | 
				
			||||||
| 
						 | 
					@ -156,7 +156,8 @@ class OutputGenerator:
 | 
				
			||||||
	def generate_simple_tag(self, e: Union[Element, None]=None, tag: str="", attributes: Union[dict[str,str],None]=None, content: Union[ListContainer, Element, list[Union[Element, ListContainer]], str, None]=None, inline: Union[bool, None]=None):
 | 
						def generate_simple_tag(self, e: Union[Element, None]=None, tag: str="", attributes: Union[dict[str,str],None]=None, content: Union[ListContainer, Element, list[Union[Element, ListContainer]], str, None]=None, inline: Union[bool, None]=None):
 | 
				
			||||||
		if not tag and e:
 | 
							if not tag and e:
 | 
				
			||||||
			tag = self.tagname(e)
 | 
								tag = self.tagname(e)
 | 
				
			||||||
		if attributes is None and e:
 | 
							if attributes is None:
 | 
				
			||||||
 | 
								if e:
 | 
				
			||||||
				attributes = self.common_attributes(e)
 | 
									attributes = self.common_attributes(e)
 | 
				
			||||||
			else:
 | 
								else:
 | 
				
			||||||
				attributes = {}
 | 
									attributes = {}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in a new issue