Last active
April 7, 2021 20:03
-
-
Save mshafae/50548e162fad3878efb6a1fdac064533 to your computer and use it in GitHub Desktop.
LaTeX Math to GitHub Image URL
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
| #!/usr/bin/env python3 | |
| """ | |
| Converts LaTeX math code to a URL that will render an image | |
| via GitHub's math renderer URL. | |
| Inspired by the discussion found at | |
| https://gist.github.com/a-rodin/fef3f543412d6e1ec5b6cf55bf197d7b | |
| """ | |
| # | |
| # Copyright 2020 Michael Shafae | |
| # | |
| # Redistribution and use in source and binary forms, with or without | |
| # modification, are permitted provided that the following conditions | |
| # are met: | |
| # | |
| # 1. Redistributions of source code must retain the above copyright | |
| # notice, this list of conditions and the following disclaimer. | |
| # | |
| # 2. Redistributions in binary form must reproduce the above copyright | |
| # notice, this list of conditions and the following disclaimer in the | |
| # documentation and/or other materials provided with the distribution. | |
| # | |
| # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | |
| # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | |
| # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | |
| # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | |
| # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
| # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | |
| # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
| # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN | |
| # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
| # POSSIBILITY OF SUCH DAMAGE. | |
| # | |
| import urllib.parse | |
| #import sys | |
| import platform | |
| from subprocess import Popen, PIPE, DEVNULL | |
| import argparse | |
| def copy_cmd(): | |
| """ Shell ommand to copy the URL to the pasteboard. """ | |
| p = platform.system( ) | |
| cmd = None | |
| args = [] | |
| if p == 'Linux': | |
| cmd = ['xclip'] | |
| args = ['-selection', 'clipboard', '-f'] | |
| elif p == 'Darwin': | |
| cmd = ['pbcopy'] | |
| else: | |
| print('No copy command available; URL not saved to pastboard.') | |
| return (cmd, args) | |
| def send_to_pasteboard(s, cmd, args): | |
| """ Sends the string s to the pasteboard. """ | |
| c = cmd + args | |
| p = Popen(c, stdin=PIPE, stdout=DEVNULL) | |
| p.communicate(input=s.encode( )) | |
| def mkbigger(n = 0): | |
| """ Return the LaTeX size string relative to the value of n. """ | |
| sizes = [None, '\\larger', '\\Large', '\\LARGE', '\\huge', | |
| '\\Huge', '\\Huge'] | |
| if n > len(sizes): | |
| n = len(sizes) | |
| return sizes[n] | |
| def main( ): | |
| """ Main function, given a LaTeX equation, return a URL for rendering it. """ | |
| parser = argparse.ArgumentParser(prog=__file__, | |
| description='Convert LaTeX math to a render.githubusercontent.com URL.') | |
| parser.add_argument('-b', '--bigger', | |
| help='Need to make it bigger? Specify an integer between 1 and 5.', | |
| dest='bigger', type=int, required=False) | |
| parser.add_argument('equation', help='LaTeX Math equation', type=str) | |
| github_math_render_url = 'https://render.githubusercontent.com/render/math?math={}' | |
| args = parser.parse_args( ) | |
| eqn = args.equation | |
| # The shell replaces all the '\\ ' with '\ '. | |
| # \b is backspace, so before going any further replace the backspaces | |
| # with \\b (ex. \begin{} -> \\begin{}). | |
| eqn = eqn.replace('\b', '\\b') | |
| # Replace the '\\ ' with '\\\\ ' for matrices and other multiline statements | |
| if '\\ ' in eqn and not '\\\n' in eqn: | |
| # There's a bug probably on the GitHub side where it doesn't handle | |
| # more than one matrix correctly. | |
| # Probably can fix it by throwing in a \n in the right spot. | |
| """ | |
| \begin{pmatrix} 1 & 2 & 3\\ a & b & c \end{pmatrix} = \begin{pmatrix} 1 & 2 & 3 \\ a & b & c \end{pmatrix} = \begin{pmatrix} 1 & 2 & 3\\ a & b & c \end{pmatrix} | |
| """ | |
| eqn = eqn.replace('\\ ', '\\\\ ') | |
| # Replace the '\\\n' with '\\\\ ' for matrices and other multiline | |
| # statements | |
| if '\\\n' in eqn: | |
| #This works as exepcted! | |
| """ | |
| \begin{pmatrix} 1 & 2 & 3\\ | |
| a & b & c | |
| \end{pmatrix} = | |
| \begin{pmatrix} | |
| 1 & 2 & 3 \\ | |
| a & b & c | |
| \end{pmatrix} = | |
| \begin{pmatrix} | |
| 1 & 2 & 3\\ | |
| a & b & c | |
| \end{pmatrix} | |
| """ | |
| eqn = eqn.replace('\\\n', '\\\\\n') | |
| size = mkbigger(args.bigger) | |
| if size != "": | |
| eqn = size + "{" + eqn + "}" | |
| eqn_encoded = urllib.parse.quote(eqn) | |
| url = github_math_render_url.format(eqn_encoded) | |
| s = ''.format(eqn, url) | |
| print(s) | |
| cmd, args = copy_cmd( ) | |
| send_to_pasteboard(s, cmd, args) | |
| if __name__ == '__main__': | |
| main( ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment