Last active
June 10, 2021 09:55
-
-
Save airglow923/46f8c9bb89c1ba49f093dc9918b7a819 to your computer and use it in GitHub Desktop.
Convert LaTeX code into URI so as to embed it in websites
This file contains hidden or 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
const latex = Deno.args[0]; | |
const latexRenderer = 'https://latex.codecogs.com/svg.latex?'; | |
if (latex === undefined || typeof latex !== 'string') { | |
const errMsg = 'Invalid argument'; | |
writeStdStream(errMsg, Deno.stderr); | |
Deno.exit(1); | |
} | |
async function writeStdStream(str, stream = Deno.stdout) { | |
return stream.write(new TextEncoder().encode(str)); | |
} | |
function asciiToUTF(str) { | |
str = str.replace(/\(/g, '%28'); | |
str = str.replace(/\)/g, '%29'); | |
return str; | |
} | |
const encodedLatex = encodeURI(latex); | |
const normalizedLatex = asciiToUTF(encodedLatex); | |
writeStdStream(latexRenderer + normalizedLatex + '\n'); |
This file contains hidden or 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
const latex = process.argv[2]; | |
const latexRenderer = 'https://latex.codecogs.com/svg.latex?'; | |
if (latex === undefined || typeof latex !== 'string') { | |
const errMsg = 'Invalid argument'; | |
process.stderr.write(errMsg); | |
process.exit(1); | |
} | |
function asciiToUTF(str) { | |
str = str.replace(/\(/g, '%28'); | |
str = str.replace(/\)/g, '%29'); | |
return str; | |
} | |
const encodedLatex = encodeURI(latex); | |
const normalizedLatex = asciiToUTF(encodedLatex); | |
writeStdStream(latexRenderer + normalizedLatex + '\n'); |
This file contains hidden or 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
#!/bin/sh | |
CURRENT_DIR=$(dirname $0) | |
COMMAND="" | |
usage() { | |
# additional error messages | |
if [ "$#" -ne 0 ]; then | |
printf "$@\n" >&2; | |
fi | |
printf "Usage: $0 LATEX\n" >&2; | |
} | |
if [ "$#" -eq 0 ]; then | |
usage | |
exit 1; | |
fi | |
if command -v deno > /dev/null; then | |
COMMAND="deno run $CURRENT_DIR/deno-to-latex-uri.js" | |
elif command -v node > /dev/null; then | |
COMMAND="node $CURRENT_DIR/node-to-latex-uri.js" | |
else | |
usage "No JavaScript runtime found" | |
exit 1; | |
fi | |
$COMMAND "$1" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
deno-to-latex-uri.js
,node-to-latex-uri.js
, andto-latex-uri.sh
in the same directory.chmod +x to-latex-uri-.sh
./to-latex-uri.sh YOUR_LATEX
Example:
Results in
This can be used as follows:
This shows
To simplify the execution, one can
PATH
; or.bashrc
,.zshrc
) that executes the script.In my case, I added the following lines to
.zshrc
:and it can be called without the extension (
.sh
):