first working sakara.py draft
This commit is contained in:
parent
6879d41a4d
commit
ccaf0577ca
@ -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
|
||||
|
||||
---
|
||||
|
||||
|
22
conf.yaml
Normal file
22
conf.yaml
Normal file
@ -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
|
||||
|
158
sakara.py
Executable file
158
sakara.py
Executable file
@ -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)
|
Loading…
Reference in New Issue
Block a user