'How to find image with its source in python markdown2

I am trying to make a static site generator with Python. I coded most of it. But there is a problem.

When I add an image to a Markdown file (like this: ![Python](python.png)) it embeds it to an HTML file. But when I process it and save the Markdown file's HTML to output folder, it should also move the used images to the same directory for proper displaying of image.

I don't want to move unnecessary images to the output directory.

  • I am using markdown2 for processing the Markdown files
  • I am using jinja for templating the HTML files

solution i tried:

from bs4 import BeautifulSoup as bs

def get_images(html_path) :
    with open(html_path) as file :
        soup = bs(file.read(), "html.parser")
        images = []
        for img in soup.find_all("img"):
            img_url = img.attrs.get("src")
            if img_url:
                images.append(img_url)
    return images


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source