<commit_before>"""

Many things copied from <NAME>'s `MoinMoin Latex support`_

.. `MoinMoin Latex support`: http://johannes.sipsolutions.net/Projects/new-moinmoin-latex

"""

import subprocess, os, sys, shutil, tempfile, resource, md5
import Image

OUT_PATH = "/var/www-pub/root/pTSc0V/testwiki/math"
OUT_URI_BASE = "http://192.168.0.100/pTSc0V/testwiki/math/"

DVIPNG = "dvipng"
LIMITED_LATEX = os.path.join(os.path.abspath(os.path.dirname(__file__)),
                             "limited-latex.py")
LATEX_TEMPLATE = r"""
\documentclass[10pt]{article}
\pagestyle{empty}
\usepackage[utf8]{inputenc}
\usepackage{amsmath,amssymb}
%(prologue)s
\begin{document}
%(raw)s
\end{document}
"""
LATEX_ARGS = ["--interaction=nonstopmode"]
DVIPNG_ARGS = ["-bgTransparent", "-Ttight", "--noghostscript", "-l1",
               "--truecolor"
               # NOTE: PIL doesn't handle indexed PNG alpha gracefully,
               #       hence truecolor...
               ]

# -----------------------------------------------------------------------------
# Running LaTeX safely
# -----------------------------------------------------------------------------

def exec_cmd(cmd, ok_return_value=0, show_cmd=True, echo=False, **kw):
    """
    Run given command and check return value.
    Return concatenated input and output.
    """
    try:
        if show_cmd:
            print '%s$' % os.getcwd(),
            if isinstance(cmd, str):
                print cmd
            else:
                print ' '.join(cmd)
            print '[running ...]'
        p = subprocess.Popen(cmd,
                             stdout=subprocess.PIPE,
                             stderr=subprocess.PIPE,
                             stdin=subprocess.PIPE, **kw)
        out, err = p.communicate()
    except OSError, e:
        raise RuntimeError("Command %s failed: %s" % (' '.join(cmd), e))
    
    if ok_return_value is not None and p.returncode != ok_return_value:
        raise RuntimeError("Command %s failed (code %d): %s"
                           % (' '.join(cmd), p.returncode, out + err))
    if echo: print out + err
    return out + err

def extract_baseline(png_fn):
    img = Image.open(png_fn).convert('RGBA')
    red, green, blue, alpha = img.split()
    baseline_offset = 0
    for y in xrange(1, img.size[1]):
        if alpha.getpixel((0, img.size[1]-1 - y)) != 0:
            baseline_offset = y-1
            break
    right_edge = 0
    for x in xrange(img.size[0]):
        if alpha.getpixel((x, baseline_offset)) == 0:
            right_edge = x + 1
            break
    img2 = img.crop((right_edge, 0, img.size[0], img.size[1]))
    img2.save(png_fn, 'PNG')
    return baseline_offset

def latex_to_png(prologue, in_text, out_png, with_baseline=False):
    out_png = os.path.abspath(out_png)<commit_msg>moin-rst-latex: Better cropping after baseline calculation<commit_after>"""

Many things copied from Joh<NAME>'s `MoinMoin Latex support`_

.. `MoinMoin Latex support`: http://johannes.sipsolutions.net/Projects/new-moinmoin-latex

"""

import subprocess, os, sys, shutil, tempfile, resource, md5
import Image

OUT_PATH = "/var/www-pub/root/pTSc0V/testwiki/math"
OUT_URI_BASE = "http://192.168.0.100/pTSc0V/testwiki/math/"

DVIPNG = "dvipng"
LIMITED_LATEX = os.path.join(os.path.abspath(os.path.dirname(__file__)),
                             "limited-latex.py")
LATEX_TEMPLATE = r"""
\documentclass[10pt]{article}
\pagestyle{empty}
\usepackage[utf8]{inputenc}
\usepackage{amsmath,amssymb}
%(prologue)s
\begin{document}
%(raw)s
\end{document}
"""
LATEX_ARGS = ["--interaction=nonstopmode"]
DVIPNG_ARGS = ["-bgTransparent", "-Ttight", "--noghostscript", "-l1",
               "--truecolor"
               # NOTE: PIL doesn't handle indexed PNG alpha gracefully,
               #       hence truecolor...
               ]

# -----------------------------------------------------------------------------
# Running LaTeX safely
# -----------------------------------------------------------------------------

def exec_cmd(cmd, ok_return_value=0, show_cmd=True, echo=False, **kw):
    """
    Run given command and check return value.
    Return concatenated input and output.
    """
    try:
        if show_cmd:
            print '%s$' % os.getcwd(),
            if isinstance(cmd, str):
                print cmd
            else:
                print ' '.join(cmd)
            print '[running ...]'
        p = subprocess.Popen(cmd,
                             stdout=subprocess.PIPE,
                             stderr=subprocess.PIPE,
                             stdin=subprocess.PIPE, **kw)
        out, err = p.communicate()
    except OSError, e:
        raise RuntimeError("Command %s failed: %s" % (' '.join(cmd), e))
    
    if ok_return_value is not None and p.returncode != ok_return_value:
        raise RuntimeError("Command %s failed (code %d): %s"
                           % (' '.join(cmd), p.returncode, out + err))
    if echo: print out + err
    return out + err

def extract_baseline(png_fn):
    img = Image.open(png_fn).convert('RGBA')
    red, green, blue, alpha = img.split()
    baseline_offset = 0
    for y in xrange(1, img.size[1]):
        if alpha.getpixel((0, img.size[1]-1 - y)) != 0:
            baseline_offset = y-1
            break
    right_edge = 0
    for x in xrange(img.size[0]):
        if alpha.getpixel((x, baseline_offset)) == 0:
            right_edge = x + 1
            break
    img2 = img.crop((right_edge, 0, img.size[0], img.size[1]))
    red2, green2, blue2, alpha2 = img2.split()
    img2 = img2.crop(alpha2.getbbox())
    img2.save(png_fn, 'PNG')
    return baseline_offset

def latex_to_png(prologue, in_text, out_png, with_baseline=False):
    out_png = os.path.abspath(out_png)
{'fraction_non_alphanumeric': 0.18417391304347827, 'fraction_numerical': 0.013565217391304348, 'mean_word_length': 1.9401840490797546, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1188795', 'n_tokens_mistral': 1909, 'n_tokens_neox': 1822, 'n_words': 405}