#!/usr/bin/env python3 import os import shutil ### USAGE # from command line, yaml file or env vars def parse_options(filename="anansesem.yaml"): import yaml with open(filename) as stream: try: conf=yaml.safe_load(stream) except yaml.YAMLError as exec: print(exec) return conf # create tex files from md files def md2tex(infile, outdir): intext=read(infile) outtext=exec("pandoc -i inifile -f md -t tex") def convert_text_format(infiles, informat='md', outformat='tex'): # from md/adoc to html/tex # md2parallel for file in infiles: if path(file) is not absolute: # os.path.isabs(my_path) path()+file md2tex(infile, outfile) def create_project_dir(conf): project=conf['project-options']['project'] cwd=os.getcwd() if os.path.exists(project): shutil.rmtree(project) # https://stackoverflow.com/a/814170 # print(project, "already exist. Please delete or move the folder") # exit(1) os.makedirs(project) return os.path.realpath(project) def create_skel(project_dir,conf): proj=conf['project-options']['project'] os.makedirs(os.path.join(proj,'chapters')) # create makefile # r'' https://stackoverflow.com/a/46011113 # '{{{}}}'.format() https://stackoverflow.com/a/10112665 makefile_content=r''' basename := {project} LATEX := {latex_engine} test: $(basename).tex $(LATEX) $(basename).tex full: $(basename).tex $(LATEX) $(basename).tex $(LATEX) $(basename).tex clear: clean rm -f $(basename).pdf clean: ls | egrep "$(basename).[0-9]+" | xargs rm -f rm -f $(basename).*R \ $(basename).Aend \ $(basename).Bend \ $(basename).Cend \ $(basename).Dend \ $(basename).Eend \ $(basename).aux \ $(basename).eled* \ $(basename).eled* \ $(basename).log \ $(basename).maf \ $(basename).mtc* \ $(basename).glg \ $(basename).glo \ $(basename).gls \ $(basename).ist \ $(basename).mw \ $(basename).toc '''.format(project=conf['project-options']['project'], latex_engine=conf['latex-options']['latex-engine']) makefile_path=os.path.join(project_dir,'Makefile') with open(makefile_path, 'w') as makefile: # https://www.geeksforgeeks.org/writing-to-file-in-python/#with-statement makefile.write(makefile_content) # create main latex file mainfile_content=r''' \documentclass[twoside,{pagesize}paper]{{memoir}} \usepackage[fontsize={fontsize}pt]{{fontsize}} \usepackage{{libertine}} % also loads fontspec which is needed for ɔ and ɛ %\usepackage{{minitoc}} % make mini table of content for each chapter \usepackage{{graphicx}} % include graphics \usepackage{{tcolorbox}} % https://en.wikipedia.org/wiki/Colophon_(publishing) \usepackage{{placeins}} % https://tex.stackexchange.com/a/88659/264579 \usepackage{{csquotes}} % \enquote \usepackage{{hyperref}} % https://tex.stackexchange.com/a/643590/264579 \usepackage{{reledmac}} % needed for repedpar \usepackage{{reledpar}} % parallel text \firstlinenum*{{1000}} \linenumincrement*{{1000}} \newcommand{{\pagesinclude}}[2]{{ \begin{{pages}} \begin{{Leftside}} \beginnumbering \pstart\input{{#1}}\pend \endnumbering \end{{Leftside}} \begin{{Rightside}} \beginnumbering \pstart\input{{#2}}\pend \endnumbering \end{{Rightside}} \end{{pages}} \Pages }} \title{{{title}}} \author{{{author}}} %\usepackage[showframe]{{geometry}} \begin{{document}} '''.format(pagesize=conf['document-options']['pagesize'], fontsize=conf['document-options']['fontsize'], title=conf['project-options']['title'], author=conf['project-options']['author']) mainfile_path=os.path.join(project_dir,proj+'.tex') with open(mainfile_path, 'w') as maintex: maintex.write(mainfile_content) maintex.write('\n') # title page titlepage=gettitlepage(conf) maintex.write(titlepage) # include chapters for infile in conf['project-options']['infiles']: # maintex.write(infile) f=os.path.join("chapters", os.path.basename(infile).split('.')[0]+".tex") maintex.write(r'\input{{{f}}}'.format(f=f)) maintex.write('\n') # end document maintex.write('\n') maintex.write(r'\end{document}') def gettitlepage(conf): title_page=r''' \begin{titlingpage} %This starts the title page % center textblock on page % https://tex.stackexchange.com/a/378157/264579 \setlrmarginsandblock{2.5cm}{*}{1} % this is manual and only works for a5 \setulmarginsandblock{2.5cm}{*}{1} \checkandfixthelayout \begin{flushright} \hrule width \hsize height 2pt \kern 1mm \hrule width \hsize \vspace{ 1.5em} \Huge title\par % \vspace{ 0.5em} \huge subtitlep osttitle\par \vspace{ 2em} \Large author \vspace{ 1.5em} \hrule width \hsize \kern 1mm \hrule width \hsize height 2pt \vspace*{\fill} \LARGE SAKARA % \vspace{ 2em} \end{flushright} \end{titlingpage} ''' return title_page def make_pdf(conf): os.system("cd "+conf['project-options']['project']+" && make") # https://stackoverflow.com/a/92395 # subprocess.call("make") def convert_text_format(conf): proj=conf['project-options']['project'] infiles=conf['project-options']['infiles'] for infile in infiles: os.system("pandoc -f markdown -t latex -i {infile} -o {outfile}".format( infile=infile, outfile=os.path.join( proj, "chapters", os.path.basename(infile).split('.')[0]+'.tex' ) ) ) print(infile) ## MAIN conf=parse_options("anansesem.yaml") project_dir=create_project_dir(conf) create_skel(project_dir, conf) infiles=conf['project-options']['infiles'] convert_text_format(conf) make_pdf(conf) exit(0)