clean up code; support code block; add few FIXMEs
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
__pycache__
|
||||
BIN
live-testing/sublime_merge.png
Normal file
BIN
live-testing/sublime_merge.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 38 KiB |
@ -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):
|
||||
|
||||

|
||||
|
||||
This is the first image from the local file system, *relative* path!
|
||||
|
||||

|
||||

|
||||
|
||||
This is the first image from the internet!
|
||||
|
||||

|
||||

|
||||
|
||||
[prev]: https://github.com/math2001/MarkdownLivePreview/tree/d4c477749ce7e77b8e9fc85464a2488f003c45bc
|
||||
@ -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')
|
||||
|
||||
Reference in New Issue
Block a user