Created
April 3, 2024 19:53
-
-
Save Flecart/8155daa72d92110f1e22a47d93b30daf to your computer and use it in GitHub Desktop.
Small script to convert a markdown file into a tex file using some template and copying onto the clipboard.
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 python3 | |
import sys | |
import os | |
import re | |
import subprocess | |
# Function to display help information | |
def display_help(): | |
print("A script to build tex files for your papers") | |
print("Usage: {} [OPTION]... INPUT".format(sys.argv[0])) | |
print("Options:") | |
print(" -h, --help Display this help message") | |
sys.exit(1) | |
def to_kebab_case(s : str): | |
return s.lower().replace(' ', '-') | |
# Parse command line arguments | |
input1 = None | |
for arg in sys.argv[1:]: | |
if arg in ("-h", "--help"): | |
display_help() | |
else: | |
if input1 is None: | |
input1 = arg | |
else: | |
print("Error: Too many arguments.") | |
display_help() | |
if input1 is None: | |
print("Error: Inputs are required.") | |
display_help() | |
# Get script directory | |
script_dir = os.path.dirname(os.path.realpath(__file__)) | |
# Read the input file and apply regex substitutions | |
with open(os.path.join(script_dir, input1), 'r') as f: | |
content = f.read() | |
# Apply regex substitution for removing escaped brackets | |
# content = re.sub(r'\\\[(.*?)\\\]', r'\1', content) | |
# Find internal references and substitute with \ref command | |
# e.g. [[#Section]] -> ~\ref{section}, and use kebab case for the label | |
content = re.sub(r'\[\[#(.*?)\]\]', lambda x: '\\ \\ref{' + to_kebab_case(x.group(1)) + '}', content) | |
# Apply pandoc conversion with the specified template | |
proc = subprocess.Popen(['pandoc', '-f', 'markdown+wikilinks_title_after_pipe+hard_line_breaks', '-t', 'latex', '--template', os.path.join(script_dir, 'projects', 'Tesi', 'template.tex')], stdin=subprocess.PIPE, stdout=subprocess.PIPE) | |
converted_content, _ = proc.communicate(input=content.encode()) | |
converted_content = converted_content.decode() | |
# Apply regex substitution for URL citations | |
converted_content = re.sub(r'\\url{@([^}]*)}', r'\\cite{\1}', converted_content) | |
# Join multiple citations into one | |
# e.g. \cite{a}\cite{b} -> \cite{a,b} | |
# e.g. \cite{a}\cite{b}\cite{c} -> \cite{a,b,c} | |
while re.search(r'\\cite{([^}]*)}\\cite{([^}]*)}', converted_content): | |
converted_content = re.sub(r'\\cite{([^}]*)}\\cite{([^}]*)}', r'\\cite{\1,\2}', converted_content) | |
# Add ~ before \cite to avoid line breaks | |
converted_content = re.sub(r'\s+\\cite{', r'~\\cite{', converted_content) | |
# Copy the converted content to clipboard | |
proc = subprocess.Popen(['xclip', '-sel', 'c'], stdin=subprocess.PIPE) | |
proc.communicate(input=converted_content.encode()) | |
# print(converted_content) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This assumes you are using wikilinks linking style, for example
[[link]]