Skip to content

Instantly share code, notes, and snippets.

@araujo88
Last active May 1, 2024 18:53
Show Gist options
  • Save araujo88/c60e657df5e152bc68b7ca7ed970b9e7 to your computer and use it in GitHub Desktop.
Save araujo88/c60e657df5e152bc68b7ca7ed970b9e7 to your computer and use it in GitHub Desktop.
Convert .tex file to .html (simple cases)
#!/bin/bash
# Input and output files
input_file="input.tex"
output_file="output.html"
# Function to remove curly braces
remove_braces() {
echo "$1" | sed 's/[{}]//g'
}
# Flag to track if we are inside a tcblisting block
inside_tcblisting=0
# Process the LaTeX file
while IFS= read -r line
do
# Check for tcblisting environment
if [[ "$line" == "\\begin{tcblisting}"* ]]; then
inside_tcblisting=1
continue
elif [[ "$line" == "\\end{tcblisting}" ]]; then
inside_tcblisting=0
continue
fi
# Skip processing lines inside tcblisting blocks
if [ $inside_tcblisting -eq 1 ]; then
continue
fi
# Remove braces
line=$(remove_braces "$line")
# Replace \chapter, \section, and \subsection with corresponding HTML tags
if [[ "$line" =~ ^\\chapter\*(.*) ]]; then
echo "<h1>${BASH_REMATCH[1]}</h1>" >> "$output_file"
elif [[ "$line" =~ ^\\chapter(.*) ]]; then
echo "<h1>${BASH_REMATCH[1]}</h1>" >> "$output_file"
elif [[ "$line" =~ ^\\section(.*) ]]; then
echo "<h2>${BASH_REMATCH[1]}</h2>" >> "$output_file"
elif [[ "$line" =~ ^\\subsection(.*) ]]; then
echo "<h3>${BASH_REMATCH[1]}</h3>" >> "$output_file"
elif [[ "$line" =~ ^\\subsubsection(.*) ]]; then
echo "<h4>${BASH_REMATCH[1]}</h4>" >> "$output_file"
else
# Check if the line starts with "-" and wrap it with <li> tags
if [[ "$line" == " - "* || "$line" == "-"* || "$line" == " -"* ]]; then
# Add <li> tags around the line, removing the leading "-" character
echo "<li>${line:3}</li>" >> "$output_file"
else
# Wrap other lines in <p> tags if they're not empty
if [[ ! -z "$line" ]]; then
echo "$line" >> "$output_file"
fi
fi
fi
done < "$input_file"
echo "Conversion complete. Output is in $output_file"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment