From ccaf0577ca501b51ab081e1b7f84af68919f6738 Mon Sep 17 00:00:00 2001 From: "uni@bor.mac" Date: Sun, 17 Nov 2024 20:42:10 +0100 Subject: [PATCH] first working sakara.py draft --- README.md | 1 + conf.yaml | 22 ++++++++ sakara.py | 158 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 181 insertions(+) create mode 100644 conf.yaml create mode 100755 sakara.py diff --git a/README.md b/README.md index 5f9d332..80d4eb7 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,7 @@ Dependencies: - python3 - pypdf (e.g. `python3 -m venv .venv && . .venv/bin/activate && pip install pypdf`) - xelatex (e.g. `apt install texlive-xetex`) +- yaml --- diff --git a/conf.yaml b/conf.yaml new file mode 100644 index 0000000..2d54d29 --- /dev/null +++ b/conf.yaml @@ -0,0 +1,22 @@ +latex-options: + latex-engine: "xelatex" + compilation-runs: "1" + +document-options: + doc-class: "memoir" + doc-class-options: "twoside" + fontsize: "10" + pagesize: "a4" + +project-options: + language: 'en' # see chapter 18.20 in https://texdoc.org/serve/memman.pdf/0 + project: "asem" + title: "Nsɛm wɔ Twi ne Brɔfo" + subtitle: "Texts in Twi and English" + author: "Yɛn Ara" + publisher: "TwiTeX" + edition: "First Edition" + infiles: + - testfiles/parallel-ak.md + - testfiles/parallel-en.md + diff --git a/sakara.py b/sakara.py new file mode 100755 index 0000000..be17b58 --- /dev/null +++ b/sakara.py @@ -0,0 +1,158 @@ +#!/usr/bin/env python3 + +import os +import shutil + +# from command line, yaml file or env vars +def parse_options(filename="conf.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{{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') + # include tex + 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 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() +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)