import re from typing import List, Optional from markdown2 import markdown # It's part of # https://github.com/constantAmateur/markdown2Mathjax/blob/master/lib/markdown2Mathjax.py def break_tie(inline, equation): """If one of the delimiters is a substring of the other (e.g., $ and $$) it is possible that the two will begin at the same location. In this case we need some criteria to break the tie and decide which operation takes precedence. I've gone with the longer of the two delimiters takes priority (for example, $$ over $). This function should return a 2 for the equation block taking precedence, a 1 for the inline block. The magic looking return statement is to map 0->2 and 1->1. """ tmp = inline.end() - inline.start() > equation.end() - equation.start() return (tmp * 3 + 2) % 4 def markdown_safe(placeholder): """Is the placeholder changed by markdown? If it is, this isn't a valid placeholder.""" mdstrip = re.compile("
(.*)
\n") md = markdown(placeholder) mdp = mdstrip.match(md) if mdp and mdp.group(1) == placeholder: return True return False def sanitizeInput( string, inline_delims: Optional[List[str]] = None, equation_delims: Optional[List[str]] = None, placeholder="$0$", ): """Given a string that will be passed to markdown, the content of the different math blocks is stripped out and replaced by a placeholder which MUST be ignored by markdown. A list is returned containing the text with placeholders and a list of the stripped out equations. Note that any pre-existing instances of the placeholder are "replaced" with themselves and a corresponding dummy entry is placed in the returned codeblock. The sanitized string can then be passed safety through markdown and then reconstructed with reconstructMath. There are potential four delimiters that can be specified. The left and right delimiters for inline and equation mode math. These can potentially be anything that isn't already used by markdown and is compatible with mathjax (see documentation for both). """ inline_delims = inline_delims or ["$", "$"] equation_delims = equation_delims or ["$$", "$$"] # Check placeholder is valid. if not markdown_safe(placeholder): raise ValueError("Placeholder %s altered by markdown processing." % placeholder) # really what we want is a reverse markdown function, but as that's too much work, this will do inline_left = re.compile("(?= 0) : tmp] # Set the new post post = tmp # Back to start! continue elif startmatches[1] is None and startmatches[2] is None: # No more blocks, add in the rest of string and be done with it... sanitizedString = sanitizedString + string[post * (post >= 0) :] return (sanitizedString, codeblocks) elif startmatches[1] is None: inBlock = 2 elif startmatches[2] is None: inBlock = 1 else: inBlock = (startpoints[1] < startpoints[2]) + (startpoints[1] > startpoints[2]) * 2 if not inBlock: inBlock = break_tie(startmatches[1], startmatches[2]) # Magic to ensure minimum index is 0 sanitizedString = sanitizedString + string[(post * (post >= 0)) : startpoints[inBlock]] post = startmatches[inBlock].end() # Now find the matching end... while terminator < post: endpoint = scanners[inBlock][1].search() # If we run out of terminators before ending this loop, we're done if endpoint is None: # Add the unterminated codeblock to the sanitized string sanitizedString = sanitizedString + string[startpoints[inBlock] :] return (sanitizedString, codeblocks) terminator = endpoint.start() # We fonud a matching endpoint, add the bit to the appropriate codeblock... codeblocks.append(str(inBlock) + string[post : endpoint.start()]) # Now add in the appropriate placeholder sanitizedString = sanitizedString + placeholder # Fabulous. Now we can start again once we update post... post = endpoint.end() def reconstructMath( processedString, codeblocks, inline_delims: Optional[List[str]] = None, equation_delims: Optional[List[str]] = None, placeholder="$0$", ): """This is usually the output of sanitizeInput, after having passed the output string through markdown. The delimiters given to this function should match those used to construct the string to begin with. This will output a string containing html suitable to use with mathjax. "<" and ">" "&" symbols in math can confuse the html interpreter because they mark the beginning and end of definition blocks. To avoid issues, if htmlSafe is set to True these symbols will be replaced by ascii codes in the math blocks. The downside to this is that if anyone is already doing this, there already formatted text might be mangled (I think I've taken steps to make sure it won't but not extensively tested...) """ inline_delims = inline_delims or ['\\(', '\\)'] equation_delims = equation_delims or ['\\[', '\\]'] delims = [['', ''], inline_delims, equation_delims] placeholder_re = re.compile("(?",">") # Step through the codeblocks one at a time and replace the next occurrence of the placeholder. # Extra placeholders are invalid math blocks and ignored... outString = '' scan = placeholder_re.scanner(processedString) post = 0 for i in range(len(codeblocks)): inBlock = int(codeblocks[i][0]) match = scan.search() if not match: raise ValueError("More codeblocks given than valid placeholders in text.") outString = ( outString + processedString[post : match.start()] + delims[inBlock][0] + codeblocks[i][1:].strip() + delims[inBlock][1] ) post = match.end() # Add the rest of the string (if we need to) if post < len(processedString): outString = outString + processedString[post:] return outString