import re


def preprocess_mako(text):
    """
    Mako template rendering treats line-initial /##+/ as comments that
    get ignored, but these are important for Markdown. Replace them with
    a substitution ${ s } where s is the hash sequence.
    """
    return re.sub(
        r'(^|(?<=\n))(?P<lead>\s*)(?P<hash>#+)(?P<trail>\s*)',
        r'\g<lead>${"\g<hash>"}\g<trail>',
        text
    )