-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoverleaf_export.sh
executable file
·54 lines (48 loc) · 1.94 KB
/
overleaf_export.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/bin/bash
bold=$(tput bold)
normal=$(tput sgr0)
# Constants
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# Change to the directory
cd "$DIR"
# Remove overleaf directory
if [ -d "$DIR/overleaf" ]; then
rm -r "$DIR/overleaf"
fi
# Create overleaf directory
mkdir "$DIR/overleaf"
# Export to overleaf
for file in *.md; do
if [[ -f "$file" ]]; then
# Create a new file with a modified name
new_file="${file%.md}-tex.md"
cp "$file" "$new_file"
# Remove markdown links, e.g., [Devops](#devops)
sed -i '' 's/\[\([^]]*\)\](\([^)]*#.*\))/**\1**/g' "$new_file"
# Remove https links, e.g., [Devops](https://example.com)
sed -i '' 's/\[\([^]]*\)\](https:\/\/[^)]*)/**\1**/g' "$new_file"
# Remove email links, e.g., [AnyEmailAccount](mailto: AnyEmailAccount)
sed -i '' 's/\[\([^]]*\)\](mailto:[^)]*)/\1/g' "$new_file"
# Add \url to [email protected]
#sed -i '' -E 's/([A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+)/\\url{\1}/g' "$new_file"
# Remove HTML tags, e.g., <AnyWord>
sed -i '' 's/<[^>]*>//g' "$new_file"
# Remove "Back to" links
grep -v '^Back to top$' "$new_file" > temp.md && mv temp.md "$new_file"
# Remove blank lines at the top
awk 'NF{p=1} p' "$new_file" > temp.md && mv temp.md "$new_file"
# Remove italic markdown footnotes *Anyword.*
awk '/^!\[/ { p = 1; print; next } p && /^\*/ { p = 0; next } { p = 0 } 1' "$new_file" > temp.md && mv temp.md "$new_file"
# Replace ```AnyString``` with bold **AnyString**
sed -i '' 's/```\([^`]*\)```/**\1**/g' "$new_file"
awk '/^```$/{f=!f; next} f{print "* **" $0 "**"; next} 1' "$new_file" > tmpfile && mv tmpfile "$new_file"
# scape * in *.ethz.ch
sed -i '' 's/\*\.ethz\.ch/\\*\.ethz\.ch/g' "$new_file"
# Replace blank spaces
sed -i '' 's/\*\* / **/g' "$new_file"
# Replace ./imgs/ with ./
sed -i '' 's@./imgs/@./@g' "$new_file"
# Move to the tex folder
mv "$new_file" "overleaf/${new_file//-tex/}"
fi
done