clean up code; support code block; add few FIXMEs

This commit is contained in:
Mathieu PATUREL
2019-11-14 21:13:16 +11:00
parent ef9b2daf6d
commit cc28bfef96
5 changed files with 23 additions and 14 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
__pycache__

Binary file not shown.

After

Width:  |  Height:  |  Size: 38 KiB

View File

@ -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

View File

@ -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')
return 'data:image/png;base64,' + base64.b64encode(conn.read()).decode('utf-8')

View File

@ -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())