diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bee8a64 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +__pycache__ diff --git a/live-testing/sublime_merge.png b/live-testing/sublime_merge.png new file mode 100644 index 0000000..d94427e Binary files /dev/null and b/live-testing/sublime_merge.png differ diff --git a/live-testing/test.md b/live-testing/test.md index cc20e53..50be7cf 100644 --- a/live-testing/test.md +++ b/live-testing/test.md @@ -2,18 +2,24 @@ This is a *test*. +``` +import this +# you should try this +``` + I'm not sure that it **actually** going to work, but it seems nicer than the [previous version][prev] -This is the first image from the local file system (absolute path, sorry): +This is the first image from the local file system (absolute path, sorry, it's not going +to work on your system unless your username is math2001): ![The sublime text logo!](file:///home/math2001/.config/sublime-text-3/Packages/MarkdownLivePreview2/live-testing/sublime_text.png) This is the first image from the local file system, *relative* path! -![The sublime text logo!](sublime_text.png) +![The sublime text logo!](sublime_merge.png) This is the first image from the internet! -![some paysage](https://4.bp.blogspot.com/-RHTlwmd9EBw/Tn84-JEW8NI/AAAAAAAAAD0/6ugjklUMbtY/s1600/acapulco-8-704125.jpg) +![math2001's logo](https://avatars1.githubusercontent.com/u/15224242?s=400&u=53324cf4e303d15032ba53aa41673a2046b3284b&v=4) [prev]: https://github.com/math2001/MarkdownLivePreview/tree/d4c477749ce7e77b8e9fc85464a2488f003c45bc \ No newline at end of file diff --git a/markdown2html.py b/markdown2html.py index b90c211..ad16814 100644 --- a/markdown2html.py +++ b/markdown2html.py @@ -1,15 +1,16 @@ +import os.path import concurrent.futures import urllib.request import base64 -import os.path +import bs4 from functools import lru_cache, partial -from bs4 import BeautifulSoup + from .lib.markdown2 import Markdown __all__ = ('markdown2html', ) -markdowner = Markdown() +markdowner = Markdown(extras=['fenced-code-blocks']) # FIXME: how do I choose how many workers I want? Does thread pool reuse threads or # does it stupidly throw them out? (we could implement something of our own) @@ -32,7 +33,7 @@ def markdown2html(markdown, basepath, re_render): """ html = markdowner.convert(markdown) - soup = BeautifulSoup(html, "html.parser") + soup = bs4.BeautifulSoup(html, "html.parser") for img_element in soup.find_all('img'): src = img_element['src'] @@ -54,7 +55,6 @@ def markdown2html(markdown, basepath, re_render): try: base64 = get_base64_image(path, re_render) except FileNotFoundError as e: - print("{!r} not found {!r}".format(path, e)) base64 = BASE64_404_IMAGE except LoadingError: # the image is loading @@ -64,6 +64,12 @@ def markdown2html(markdown, basepath, re_render): # FIXME: how do tables look? should we use ascii tables? + # FIXME: pre aren't handled by ST3. The require manual adjustment + + # FIXME: include a stylesheet + + # FIXME: remove the comments, because they pollute the console with error messages + return str(soup) def get_base64_image(path, re_render): @@ -83,7 +89,7 @@ def get_base64_image(path, re_render): if path in images_cache: return images_cache[path] executor.submit(load_image, path).add_done_callback(partial(callback, path)) - return 'loading of the internet!' + raise LoadingError() with open(path, 'rb') as fp: return 'data:image/png;base64,' + base64.b64encode(fp.read()).decode('utf-8') @@ -98,4 +104,4 @@ def load_image(url): content_type = conn.info().get_content_type() if 'image' not in content_type: raise ValueError("{!r} doesn't point to an image, but to a {!r}".format(url, content_type)) - return 'data:image/png;base64,' + base64.b64encode(conn.read()).decode('utf-8') \ No newline at end of file + return 'data:image/png;base64,' + base64.b64encode(conn.read()).decode('utf-8') diff --git a/utils.py b/utils.py index 6504b05..e0661d1 100644 --- a/utils.py +++ b/utils.py @@ -21,7 +21,3 @@ def min_time_between_call(timeout, on_block=lambda *args, **kwargs: None): return func(*args, **kwargs) return wrapper return outer - -@min_time_between_call(1) -def hello(): - print(time.time()) \ No newline at end of file