From e0daa147f1dada44dc128c2e7bd40ff0300f1a19 Mon Sep 17 00:00:00 2001 From: Christian Morpurgo Date: Thu, 24 Apr 2025 17:06:45 +0200 Subject: [PATCH] fix import --- MarkdownLivePreview.py | 5 +++++ markdown2html.py | 15 +++++++++------ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/MarkdownLivePreview.py b/MarkdownLivePreview.py index 9f3a55f..455413f 100644 --- a/MarkdownLivePreview.py +++ b/MarkdownLivePreview.py @@ -2,6 +2,11 @@ import os import sys import sublime +# Add the package archive path itself to sys.path +package_path = os.path.dirname(__file__) +if package_path not in sys.path: + sys.path.insert(0, package_path) + # --- Add lib to sys.path --- # Get the directory containing this file (MarkdownLivePreview.py) plugin_dir = os.path.dirname(__file__) diff --git a/markdown2html.py b/markdown2html.py index 6aea8b1..73c5d4f 100644 --- a/markdown2html.py +++ b/markdown2html.py @@ -13,15 +13,16 @@ import urllib.request import base64 # --- Bundled library imports --- -# These should now find the libraries in ./lib via the modified sys.path -import bs4 -from .lib.markdown2 import Markdown +# Explicitly import from the 'lib' directory, now that the package root is in sys.path +from lib import bs4 +from lib.markdown2 import Markdown # --- End bundled library imports --- from functools import partial __all__ = ("markdown2html",) +# Use the imported module name markdowner = Markdown(extras=["fenced-code-blocks", "cuddled-lists"]) # FIXME: how do I choose how many workers I want? Does thread pool reuse threads or @@ -36,6 +37,7 @@ def markdown2html(markdown, basepath, re_render, resources, viewport_width, font """ html = markdowner.convert(markdown) + # Use the imported module name soup = bs4.BeautifulSoup(html, "html.parser") for img_element in soup.find_all("img"): src = img_element["src"] @@ -55,14 +57,15 @@ def markdown2html(markdown, basepath, re_render, resources, viewport_width, font # realpath: simplify that paths so that we don't have duplicated caches path = os.path.realpath(os.path.expanduser(os.path.join(basepath, src))) - base64, (width, height) = get_base64_image(path, re_render, resources) + base64_img, (width, height) = get_base64_image(path, re_render, resources) # Renamed local var to avoid conflict - img_element["src"] = base64 + img_element["src"] = base64_img if width > viewport_width: img_element["width"] = viewport_width img_element["height"] = viewport_width * (height / width) # remove comments, because they pollute the console with error messages + # Use the imported module name for comment_element in soup.find_all( text=lambda text: isinstance(text, bs4.Comment) ): @@ -81,7 +84,7 @@ def markdown2html(markdown, basepath, re_render, resources, viewport_width, font .replace(" ", '.') .replace("\n", "
") ) - + # Use the imported module name code_element.replace_with(bs4.BeautifulSoup(fixed_pre, "html.parser")) # FIXME: highlight the code using Sublime's syntax