Last active
April 4, 2023 13:46
-
-
Save RuizSerra/f6d708250c1046a7e0065d5dc7de01e0 to your computer and use it in GitHub Desktop.
Obsidian.md π LaTeX
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# | |
# * Converts citations: | |
# "[[Lastname2019a]]" into "\cite{Lastname2019a}" | |
# | |
# * Interprets code blocks with four back-ticks (````) as comments, | |
# and those with three back-ticks (```) as "lstlisting" environments. | |
# | |
# * Images: | |
# "![[Image Name.png]]" into "\figure (blablablabla)" | |
# or | |
# "" into "\caption{The caption for the image} etc blablabla" | |
import argparse | |
import os | |
import re | |
import subprocess | |
import sys | |
parser = argparse.ArgumentParser(description='Convert .md file to .tex') | |
parser.add_argument('filename', type=str, | |
help='Files to convert') | |
parser.add_argument('-c', '--copy', action='store_true', | |
help='Copy .tex to clipboard') | |
args = parser.parse_args() | |
if len(sys.argv) == 1: | |
raise Exception('Need path to file') | |
with open(args.filename, 'r') as f: | |
data = f.read() | |
# ------------------------------------------------------------------------------ | |
# Markdown quirk replacement | |
# ------------------------------------------------------------------------------ | |
# Images (note, does not support underscores in image filename) | |
figure_format = (r'\\begin{figure}\n' | |
r' \\centering\n' | |
#r' \\includegraphics[width=\\linewidth]{\1.\2}\n' | |
r' \\includegraphics[width=\\textwidth]{\1.\2}\n' | |
r' \\caption{FIXME: \1}\n' | |
r' \\label{fig:\1}\n' | |
r'\\end{figure}\n') | |
data = re.sub(r'!\[\[(.+)\.(png|jpg)\]\]', figure_format, data) | |
# Images with:  | |
figure_format = (r'\\begin{figure}\n' | |
r' \\centering\n' | |
#r' \\includegraphics[width=\\linewidth]{\2.\3}\n' | |
r' \\includegraphics[width=\\textwidth]{\2.\3}\n' | |
r' \\caption{\1}\n' | |
r' \\label{fig:\2}\n' | |
r'\\end{figure}\n') | |
data = re.sub(r'!\[(.+)]\((.+)\.(png|jpg)\)', figure_format, data) | |
# Citations | |
data = re.sub(r'\[\[([A-Z][a-zA-Z\-]+\d{4}\w?)\]\]', r'\\cite{\1}', data, flags=re.M) | |
# Any remaining wiki links | |
# Disable them: | |
data = re.sub(r'\[\[(.*?)\]\]', r'\\iffalse \1 \\fi', data, flags=re.DOTALL) | |
# Ignore the link but keep the text: | |
#data = re.sub(r'\[\[([^|]+)\|?(.*?)\]\]', r'\2', data, flags=re.DOTALL) ## FIXME: needs to use \1 if there is no \2 | |
# Comment blocks | |
data = re.sub(r'````(.*?)````', r'\\iffalse \1 \\fi\n', data, flags=re.DOTALL) | |
# Code blocks | |
data = re.sub(r'```(\w+)\n(.*?)```', r'\\begin{lstlisting}[language=\1]\n\2\n\\end{lstlisting}', data, flags=re.DOTALL) | |
# ------------------------------------------------------------------------------ | |
# Convert to LaTeX and write to file + clipboard | |
# ------------------------------------------------------------------------------ | |
out_filename = os.path.basename(sys.argv[1].replace('.md', '').replace(' ', '')) + '.tex' | |
print('Writing TeX file to {}'.format(out_filename)) | |
process = subprocess.run('pandoc -f markdown -t latex -o Latex/{} << "HEREDOC"\n{}\nHEREDOC'.format(out_filename, data), | |
shell=True, check=True, stdout=subprocess.PIPE, universal_newlines=True) | |
# output = process.stdout | |
if args.copy: | |
process = subprocess.run('cat Latex/{} | pbcopy'.format(out_filename), | |
shell=True, check=True, stdout=subprocess.PIPE, universal_newlines=True) | |
print('Copied contents of Latex/{} to clipboard!'.format(out_filename)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To use run: