import re


def to_slug(inp, lowercase=True):
    """Implements Aloe Slugify.to_slug
    """
    # https://bitbucket.org/tacc-cic/aloe/src/master/aloe-jobslib/src/main/java/edu/utexas/tacc/aloe/jobs/utils/Slug.java
    # Remove single quote characters
    inp = re.sub("'", '', inp)
    # Remove non-ASCII characters
    inp = re.sub(r'[^\x00-\x7F]', '', inp)
    # Whitespace characters reduced to single -
    inp = re.sub('[\s]+', '-', inp)
    inp = re.sub('[^a-zA-Z0-9_]+', '-', inp)
    inp = re.sub('[-]+', '-', inp)
    inp = re.sub('^-', '', inp)
    inp = re.sub('-$', '', inp)
    if lowercase:
        inp = inp.lower()
    return inp