Skip to content

Commit a4872f4

Browse files
committed
Launch modal when command is run without latex
1 parent 002512a commit a4872f4

File tree

4 files changed

+86
-87
lines changed

4 files changed

+86
-87
lines changed

commands-tex.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
"options": [
55
{
66
"name": "latex",
7-
"description": "LaTeX compatible math expression",
7+
"description": "LaTeX compatible math expression. If not provided, will launch a modal.",
88
"type": 3,
9-
"required": true
9+
"required": false
1010
}
1111
]
1212
}

commands-tex.sh

+56-65
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44

55
BOUNDARY="boundary"
66
TEMPDIR=$(mktemp -d)
7-
TEXFILE=$TEMPDIR/file.tex
7+
88

99
API_ENDPOINT="https://discord.com/api/v8"
1010
TOKEN=$(jq '.token' "$1" -r)
11+
LATEX_LENGTH=$(jq '.data.options[] | select(.name=="latex").value' "$1" -c | wc)
1112

1213
putboundary(){
1314
echo -e "\r\n--${BOUNDARY}$1"
@@ -17,74 +18,64 @@ echo "Content-Type: multipart/form-data; boundary=$BOUNDARY"
1718
echo "Status: 200 OK"
1819
echo
1920

20-
21-
22-
23-
cat <<END > $TEXFILE
24-
\documentclass[border=2pt]{standalone}
25-
\usepackage{amsmath}
26-
\usepackage{amsfonts}
27-
\usepackage{varwidth}
28-
\begin{document}
29-
\begin{varwidth}{\linewidth}
30-
\[
31-
END
32-
33-
jq '.data.options[] | select(.name=="latex").value' "$1" -r >> $TEXFILE
34-
35-
cat <<END >> $TEXFILE
36-
\]
37-
\end{varwidth}
38-
\end{document}
39-
END
40-
41-
pdflatex --output-directory $TEMPDIR $TEXFILE >$TEMPDIR/tex.log 2>$TEMPDIR/tex-err.log && \
42-
convert -density 300 -background white -alpha remove -quality 50 -colorspace RGB $TEMPDIR/file.pdf $TEMPDIR/file.png >$TEMPDIR/con.log 2>$TEMPDIR/con-err.log
43-
44-
if [ $? -eq 0 ]; then
45-
# Success!
46-
47-
# Send through the JSON
48-
putboundary
49-
echo 'Content-Disposition: form-data; name="payload_json"'
50-
echo "Content-Type: application/json"
51-
echo
52-
echo '{"type": 4, "data":{"embeds":[{"image":{"url":"attachment://tex.png"}}]}}'
53-
54-
putboundary
55-
# Send through the image
56-
echo 'Content-Disposition: form-data; name="files[0]"; filename="tex.png"'
57-
echo "Content-Type: image/png"
58-
echo
59-
cat $TEMPDIR/file.png
60-
61-
# Done
62-
putboundary "--"
63-
else
21+
if [[ $LATEX_LENGTH -eq 0 ]]; then
22+
# Launch a modal
6423
putboundary
6524
echo 'Content-Disposition: form-data; name="payload_json"'
6625
echo "Content-Type: application/json"
6726
echo
68-
echo '{"type": 4, "data":{"embeds":[{"title":"Failed to Render"}], "attachments": [{"id":0, "description":"Error Logs", "filename":"error.log"}]}}'
69-
70-
# Send through the logs
71-
putboundary
72-
echo 'Content-Disposition: form-data; name="files[0]"; filename="error.log"'
73-
echo "Content-Type: text/plain"
74-
echo
75-
76-
echo "===pdf2png stdout==="
77-
cat $TEMPDIR/con.log || echo "[empty]"
78-
79-
echo "===pdf2png stderr==="
80-
cat $TEMPDIR/con-err.log || echo "[empty]"
81-
82-
echo "===pdflatex stdout==="
83-
cat $TEMPDIR/tex.log || echo "[empty]"
84-
85-
echo "===pdflatex stderr==="
86-
cat $TEMPDIR/tex-err.log || echo "[empty]"
87-
27+
echo '{"type": 9, "data":{"custom_id": "tex-render", "title": "Render LaTeX w/ HeXTeX", "components": ['
28+
echo '{"type": 4, "custom_id": "latex", "style": 2, "label":"LaTeX Code"}'
29+
echo ']}}'
30+
else
31+
32+
jq '.data.options[] | select(.name=="latex").value' "$1" -r >> $TEMPDIR/src.tex
33+
34+
bash latex-render.sh "$TEMPDIR"
35+
36+
if [ $? -eq 0 ]; then
37+
# Success!
38+
39+
# Send through the JSON
40+
putboundary
41+
echo 'Content-Disposition: form-data; name="payload_json"'
42+
echo "Content-Type: application/json"
43+
echo
44+
echo '{"type": 4, "data":{"embeds":[{"image":{"url":"attachment://tex.png"}}]}}'
45+
46+
putboundary
47+
# Send through the image
48+
echo 'Content-Disposition: form-data; name="files[0]"; filename="tex.png"'
49+
echo "Content-Type: image/png"
50+
echo
51+
cat $TEMPDIR/file.png
52+
53+
else
54+
putboundary
55+
echo 'Content-Disposition: form-data; name="payload_json"'
56+
echo "Content-Type: application/json"
57+
echo
58+
echo '{"type": 4, "data":{"embeds":[{"title":"Failed to Render"}], "attachments": [{"id":0, "description":"Error Logs", "filename":"error.log"}]}}'
59+
60+
# Send through the logs
61+
putboundary
62+
echo 'Content-Disposition: form-data; name="files[0]"; filename="error.log"'
63+
echo "Content-Type: text/plain"
64+
echo
65+
66+
echo "===pdf2png stdout==="
67+
cat $TEMPDIR/con.log || echo "[empty]"
68+
69+
echo "===pdf2png stderr==="
70+
cat $TEMPDIR/con-err.log || echo "[empty]"
71+
72+
echo "===pdflatex stdout==="
73+
cat $TEMPDIR/tex.log || echo "[empty]"
74+
75+
echo "===pdflatex stderr==="
76+
cat $TEMPDIR/tex-err.log || echo "[empty]"
77+
78+
fi
8879
fi
8980

9081
putboundary "--"

latex-render.sh

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
TEMPDIR=$1
2+
TEXFILE=$TEMPDIR/file.tex
3+
4+
5+
cat <<END > $TEXFILE
6+
\documentclass[border=2pt]{standalone}
7+
\usepackage{amsmath}
8+
\usepackage{amsfonts}
9+
\usepackage{varwidth}
10+
\begin{document}
11+
\begin{varwidth}{\linewidth}
12+
\[
13+
END
14+
15+
cat $TEMPDIR/src.tex
16+
17+
cat <<END >> $TEXFILE
18+
\]
19+
\end{varwidth}
20+
\end{document}
21+
END
22+
23+
pdflatex --output-directory $TEMPDIR $TEXFILE >$TEMPDIR/tex.log 2>$TEMPDIR/tex-err.log && \
24+
convert -density 300 -background white -alpha remove -quality 50 -colorspace RGB $TEMPDIR/file.pdf $TEMPDIR/file.png >$TEMPDIR/con.log 2>$TEMPDIR/con-err.log

tex-eqtopng.sh

+4-20
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,14 @@
33
TEMPDIR=$(mktemp -d)
44
TEXFILE=$TEMPDIR/file.tex
55

6-
cat <<END > $TEXFILE
7-
\documentclass[border=2pt]{standalone}
8-
\usepackage{amsmath}
9-
\usepackage{amsfonts}
10-
\usepackage{varwidth}
11-
\begin{document}
12-
\begin{varwidth}{\linewidth}
13-
\[
14-
END
15-
16-
echo $QUERY_STRING | base64 -d >> $TEXFILE
17-
18-
cat <<END >> $TEXFILE
19-
\]
20-
\end{varwidth}
21-
\end{document}
22-
END
23-
24-
pdflatex --output-directory $TEMPDIR $TEXFILE > /dev/null
6+
echo $QUERY_STRING | base64 -d >> $TEMPDIR/src.tex
7+
8+
bash latex-render.sh "$TEMPDIR"
259

2610
echo "Content-Type: image/png"
2711
echo
2812

29-
convert -density 300 -background white -alpha remove -quality 50 -colorspace RGB $TEMPDIR/file.pdf png:-
13+
cat $TEMPDIR/file.png
3014

3115
rm -rf $TEMPDIR
3216

0 commit comments

Comments
 (0)