4
4
5
5
BOUNDARY=" boundary"
6
6
TEMPDIR=$( mktemp -d)
7
- TEXFILE= $TEMPDIR /file.tex
7
+
8
8
9
9
API_ENDPOINT=" https://discord.com/api/v8"
10
10
TOKEN=$( jq ' .token' " $1 " -r)
11
+ LATEX_LENGTH=$( jq ' .data.options[] | select(.name=="latex").value' " $1 " -c | wc)
11
12
12
13
putboundary (){
13
14
echo -e " \r\n--${BOUNDARY} $1 "
@@ -17,74 +18,64 @@ echo "Content-Type: multipart/form-data; boundary=$BOUNDARY"
17
18
echo " Status: 200 OK"
18
19
echo
19
20
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
64
23
putboundary
65
24
echo ' Content-Disposition: form-data; name="payload_json"'
66
25
echo " Content-Type: application/json"
67
26
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
88
79
fi
89
80
90
81
putboundary " --"
0 commit comments