fix import

This commit is contained in:
2025-04-24 17:06:45 +02:00
parent 0034602fff
commit e0daa147f1
2 changed files with 14 additions and 6 deletions

View File

@ -2,6 +2,11 @@ import os
import sys import sys
import sublime 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 --- # --- Add lib to sys.path ---
# Get the directory containing this file (MarkdownLivePreview.py) # Get the directory containing this file (MarkdownLivePreview.py)
plugin_dir = os.path.dirname(__file__) plugin_dir = os.path.dirname(__file__)

View File

@ -13,15 +13,16 @@ import urllib.request
import base64 import base64
# --- Bundled library imports --- # --- Bundled library imports ---
# These should now find the libraries in ./lib via the modified sys.path # Explicitly import from the 'lib' directory, now that the package root is in sys.path
import bs4 from lib import bs4
from .lib.markdown2 import Markdown from lib.markdown2 import Markdown
# --- End bundled library imports --- # --- End bundled library imports ---
from functools import partial from functools import partial
__all__ = ("markdown2html",) __all__ = ("markdown2html",)
# Use the imported module name
markdowner = Markdown(extras=["fenced-code-blocks", "cuddled-lists"]) markdowner = Markdown(extras=["fenced-code-blocks", "cuddled-lists"])
# FIXME: how do I choose how many workers I want? Does thread pool reuse threads or # 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) html = markdowner.convert(markdown)
# Use the imported module name
soup = bs4.BeautifulSoup(html, "html.parser") soup = bs4.BeautifulSoup(html, "html.parser")
for img_element in soup.find_all("img"): for img_element in soup.find_all("img"):
src = img_element["src"] 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 # realpath: simplify that paths so that we don't have duplicated caches
path = os.path.realpath(os.path.expanduser(os.path.join(basepath, src))) 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: if width > viewport_width:
img_element["width"] = viewport_width img_element["width"] = viewport_width
img_element["height"] = viewport_width * (height / width) img_element["height"] = viewport_width * (height / width)
# remove comments, because they pollute the console with error messages # remove comments, because they pollute the console with error messages
# Use the imported module name
for comment_element in soup.find_all( for comment_element in soup.find_all(
text=lambda text: isinstance(text, bs4.Comment) text=lambda text: isinstance(text, bs4.Comment)
): ):
@ -81,7 +84,7 @@ def markdown2html(markdown, basepath, re_render, resources, viewport_width, font
.replace(" ", '<i class="space">.</i>') .replace(" ", '<i class="space">.</i>')
.replace("\n", "<br />") .replace("\n", "<br />")
) )
# Use the imported module name
code_element.replace_with(bs4.BeautifulSoup(fixed_pre, "html.parser")) code_element.replace_with(bs4.BeautifulSoup(fixed_pre, "html.parser"))
# FIXME: highlight the code using Sublime's syntax # FIXME: highlight the code using Sublime's syntax