CoCalc Public Filestmp / wiki.sagewsOpen in with one click!
Authors: Harald Schilly, ℏal Snyder, William A. Stein
def sanitize_math_input(s): from markdown2Mathjax import sanitizeInput # it's critical that $$ be first! delims = [('$$','$$'), ('\\(','\\)'), ('\\[','\\]'), ('\\begin{equation}', '\\end{equation}'), ('\\begin{equation*}', '\\end{equation*}'), ('\\begin{align}', '\\end{align}'), ('\\begin{align*}', '\\end{align*}'), ('\\begin{eqnarray}', '\\end{eqnarray}'), ('\\begin{eqnarray*}', '\\end{eqnarray*}'), ('\\begin{math}', '\\end{math}'), ('\\begin{displaymath}', '\\end{displaymath}') ] tmp = [((s,None),None)] for d in delims: tmp.append((sanitizeInput(tmp[-1][0][0], equation_delims=d), d)) return tmp def reconstruct_math(s, tmp): from markdown2Mathjax import reconstructMath while len(tmp) > 1: s = reconstructMath(s, tmp[-1][0][1], equation_delims=tmp[-1][1]) del tmp[-1] return s def pandoc(infmt, doc=None): if doc is None: def f(s): salvus.html(pandoc(infmt, s)) return f import subprocess p = subprocess.Popen(['pandoc', '-f', infmt], stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE) tmp = sanitize_math_input(doc) p.stdin.write(tmp[-1][0][0]) p.stdin.close() err = p.stderr.read() if err: raise RuntimeError(err) out = p.stdout.read() return out, tmp return reconstruct_math(out, tmp)
1302f9cc-49c5-477e-a79e-570440ee6188s def pandoc(fmt, hide=True, doc=None): """ INPUT: - fmt -- one of 'docbook', 'haddock', 'html', 'json', 'latex', 'markdown', 'markdown_github', 'markdown_mmd', 'markdown_phpextra', 'markdown_strict', 'mediawiki', 'native', 'opml', 'rst', 'textile' - doc -- a string in the given format OUTPUT: - Called directly, you get the HTML rendered version of doc as a string. - If you use this as a cell decorator, it displays the HTML output, e.g., %pandoc('mediawiki') * ''Unordered lists'' are easy to do: ** Start every line with a star. *** More stars indicate a deeper level. """ if doc is None: return lambda x : salvus.html(hide=hide)(pandoc(fmt, x)) import subprocess p = subprocess.Popen(['pandoc', '-f', fmt, '--mathjax'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE) p.stdin.write(doc) p.stdin.close() err = p.stderr.read() if err: raise RuntimeError(err) return p.stdout.read()
html('foo', hide=False)
foo
%pandoc('mediawiki') For $x^3$ and $$a^3 + b^3 + \int_0^1 f(x^3)dx$$ Consider <em>foo</em></h1> * ''Unordered lists'' are easy to do: ** Start every line with a star. *** More stars indicate a deeper level. *: Previous item continues. ** A newline * in a list marks the end of the list. *Of course you can start again.
Error in lines 1-1 Traceback (most recent call last): File "/projects/4a5f0542-5873-4eed-a85c-a18c706e8bcd/.sagemathcloud/sage_server.py", line 864, in execute exec compile(block+'\n', '', 'single') in namespace, locals File "", line 1, in <module> File "/projects/4a5f0542-5873-4eed-a85c-a18c706e8bcd/.sagemathcloud/sage_server.py", line 905, in execute_with_code_decorators code = code_decorator(code) File "", line 24, in <lambda> TypeError: html() got an unexpected keyword argument 'hide'
%default_mode pandoc('mediawiki')
* ''Unordered lists'' are easy to do: ** Start every line with a star. *** More stars indicate a deeper level. *: Previous item continues. ** A newline * in a list marks the end of the list. *Of course you can start again.
  • Unordered lists are easy to do:
    • Start every line with a star.
      • More stars indicate a deeper level.
    Previous item continues.
    • A newline
  • in a list

marks the end of the list.

  • Of course you can start again.
%default_mode sage