Skip to content

Useful Functions

agus makmun edited this page Oct 22, 2017 · 5 revisions

These useful functions to find the images and usernames inside the markdown content, provides specialy for martor.

import re
from bs4 import BeautifulSoup
from martor.utils import markdownify


def markdown_find_images(markdown_text):
    """
    return list of image urls inside `markdown_text`.
    :param `markdown_text` is markdown text to find.

    example markdown text:
        Hello ![title](/path/to/image.png)
    provides for:
        jpeg|jpg|png|gif
    demo:
        https://goo.gl/3LEXom
    """
    # findgex = r"[^(\s]+\.(?:jpeg|jpg|png|gif)(?=\b[+^\)])"
    findgex = r"[^(\s]+\.(?:jpeg|jpg|png|gif)(?=\))"
    return re.findall(findgex, markdown_text)


def markdown_find_mentions(markdown_text):
    """
    To find the users that mentioned
    on markdown content using `BeautifulShoup`.

    input  : `markdown_text` or markdown content.
    return : `list` of usernames.
    """
    mark = markdownify(markdown_text)
    soup = BeautifulSoup(mark, 'html.parser')
    return list(set(
        username.text[1::] for username in
        soup.findAll('a', {'class': 'direct-mention-link'})
    ))
Clone this wiki locally