mirror of
				https://github.com/zulip/zulip.git
				synced 2025-11-03 21:43:21 +00:00 
			
		
		
		
	Previous cleanups (mostly the removals of Python __future__ imports) were done in a way that introduced leading newlines. Delete leading newlines from all files, except static/assets/zulip-emoji/NOTICE, which is a verbatim copy of the Apache 2.0 license. Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
		
			
				
	
	
		
			44 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			44 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
import logging
 | 
						|
import os
 | 
						|
import subprocess
 | 
						|
from django.conf import settings
 | 
						|
from typing import Optional
 | 
						|
from zerver.lib.storage import static_path
 | 
						|
 | 
						|
def render_tex(tex: str, is_inline: bool=True) -> Optional[str]:
 | 
						|
    r"""Render a TeX string into HTML using KaTeX
 | 
						|
 | 
						|
    Returns the HTML string, or None if there was some error in the TeX syntax
 | 
						|
 | 
						|
    Keyword arguments:
 | 
						|
    tex -- Text string with the TeX to render
 | 
						|
           Don't include delimiters ('$$', '\[ \]', etc.)
 | 
						|
    is_inline -- Boolean setting that indicates whether the render should be
 | 
						|
                 inline (i.e. for embedding it in text) or not. The latter
 | 
						|
                 will show the content centered, and in the "expanded" form
 | 
						|
                 (default True)
 | 
						|
    """
 | 
						|
 | 
						|
    katex_path = (
 | 
						|
        static_path("webpack-bundles/katex-cli.js")
 | 
						|
        if settings.PRODUCTION
 | 
						|
        else os.path.join(settings.DEPLOY_ROOT, "node_modules/katex/cli.js")
 | 
						|
    )
 | 
						|
    if not os.path.isfile(katex_path):
 | 
						|
        logging.error("Cannot find KaTeX for latex rendering!")
 | 
						|
        return None
 | 
						|
    command = ['node', katex_path]
 | 
						|
    if not is_inline:
 | 
						|
        command.extend(['--display-mode'])
 | 
						|
    katex = subprocess.Popen(command,
 | 
						|
                             stdin=subprocess.PIPE,
 | 
						|
                             stdout=subprocess.PIPE,
 | 
						|
                             stderr=subprocess.PIPE)
 | 
						|
    stdout = katex.communicate(input=tex.encode())[0]
 | 
						|
    if katex.returncode == 0:
 | 
						|
        # stdout contains a newline at the end
 | 
						|
        assert stdout is not None
 | 
						|
        return stdout.decode('utf-8').strip()
 | 
						|
    else:
 | 
						|
        return None
 |