Skip to content

Commit d2d9649

Browse files
authored
Merge pull request #1 from treatmesubj/live-md
Live markdown rendering
2 parents d9e8c17 + c224a36 commit d2d9649

File tree

4 files changed

+23
-10
lines changed

4 files changed

+23
-10
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ It makes for a simple way to write up some Markdown with a terminal & a web brow
1515
## Usage
1616
### Markdown-to-HTML Server
1717
```
18-
python -m httpmdhtml.server -b 127.0.0.1 -d . --css_file ../mystyle.css
18+
python -m httpmdhtml.server -b 127.0.0.1 -d . --css_file ../mystyle.css -l 5000
1919
---
20-
usage: server.py [-h] [--cgi] [--bind ADDRESS] [--directory DIRECTORY] [--css_file CSS_FILE] [port]
20+
usage: server.py [-h] [--cgi] [--bind ADDRESS] [--directory DIRECTORY] [--css_file CSS_FILE]
21+
[--live_md_rr LIVE_MD_RR]
22+
[port]
2123
2224
positional arguments:
2325
port Specify alternate port [default: 8000]
@@ -30,6 +32,8 @@ optional arguments:
3032
--directory DIRECTORY, -d DIRECTORY
3133
Specify alternative directory [default:current directory]
3234
--css_file CSS_FILE css-file-path; its content will be written to the <style> element
35+
--live_md_rr LIVE_MD_RR, -l LIVE_MD_RR
36+
continuous refresh rate of MD page, in ms
3337
```
3438

3539
### Markdown-to-HTML Out-File

httpmdhtml/md_to_html.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313

1414
def markdown_to_html(MarkdownIt_obj, in_file_path, out_file_path="tmp.html",
15-
encode_local_images=False, css_file=None):
15+
encode_local_images=False, css_file=None, live_md_rr=None):
1616

1717
text = open(in_file_path, "r").read()
1818
tokens = MarkdownIt_obj.parse(text)
@@ -31,6 +31,10 @@ def markdown_to_html(MarkdownIt_obj, in_file_path, out_file_path="tmp.html",
3131
code { color: #ae81ff; background-color: #272b33; border-radius: 6px; }
3232
table, th, td { border: 1px solid; border-collapse: collapse; padding-left: 4px; padding-right: 4px; }"""
3333
soup.select_one("style").string = css
34+
if live_md_rr:
35+
script = f"setTimeout(function(){{ document.location.reload(); }}, {live_md_rr});"
36+
soup.select_one('head').append(soup.new_tag("script"))
37+
soup.select_one("script").string = script
3438
if encode_local_images:
3539
img_elems = soup.select("img")
3640
url_pattern = "^https?:\\/\\/(?:www\\.)?[-a-zA-Z0-9@:%._\\+~#=]{1,256}\\.[a-zA-Z0-9()]{1,6}\\b(?:[-a-zA-Z0-9()@:%_\\+.~#?&\\/=]*)$"

httpmdhtml/server.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,25 @@
3131

3232

3333
class md_to_html_SimpleHTTPRequestHandler(SimpleHTTPRequestHandler):
34-
def __init__(self, *args, MarkdownIt_obj=None, css_file=None, **kwargs):
34+
def __init__(self, *args, MarkdownIt_obj=None, css_file=None, live_md_rr=False, **kwargs):
3535
self.MarkdownIt_obj = MarkdownIt_obj
3636
self.css_file = css_file
37+
self.live_md_rr = live_md_rr
3738
super().__init__(*args, **kwargs)
3839

3940

4041
def do_GET(self, rm_temp_html=False):
4142
"""Serve a GET request."""
4243
self.url_dc_path = urllib.parse.unquote(urllib.parse.urlsplit(self.path).path) # url decode, strip query params for file check
43-
if self.url_dc_path.endswith(".md") and os.path.exists(os.path.join(self.directory, f".{self.url_dc_path}")): # check for markdown file request
44+
if self.MarkdownIt_obj and self.url_dc_path.endswith(".md") and os.path.exists(os.path.join(self.directory, f".{self.url_dc_path}")): # check for markdown file request
4445
in_file_path=os.path.join(self.directory, f".{self.url_dc_path}")
4546
out_file_path=os.path.join(self.directory, f".{os.path.splitext(self.url_dc_path)[0]}.html")
4647
md_to_html.markdown_to_html(
4748
self.MarkdownIt_obj,
4849
in_file_path=in_file_path,
4950
out_file_path=out_file_path,
50-
css_file=self.css_file)
51+
css_file=self.css_file,
52+
live_md_rr=self.live_md_rr)
5153
self.path = f"{os.path.splitext(self.path)[0]}.html"
5254
rm_temp_html = True
5355
f = self.send_head()
@@ -73,6 +75,8 @@ def do_GET(self, rm_temp_html=False):
7375
'[default:current directory]')
7476
parser.add_argument('--css_file', default=None,
7577
help='css-file-path; its content will be written to the <style> element')
78+
parser.add_argument('--live_md_rr', '-l', action='store', type=int, default=None,
79+
help='continuous refresh rate of MD page, in ms')
7680
parser.add_argument('port', action='store',
7781
default=8000, type=int,
7882
nargs='?',
@@ -83,11 +87,12 @@ def do_GET(self, rm_temp_html=False):
8387
else:
8488
MarkdownIt_obj = MarkdownIt("commonmark").enable("table").enable("strikethrough")
8589
if args.css_file and not os.path.isfile(args.css_file):
86-
raise FileNotFoundError(f"looks like the given `css_file` argument's value - {args.css_file} - is not actually a file")
90+
raise FileNotFoundError(f"looks like the given `css_file` argument's value - {args.css_file} - cannot be found")
8791
handler_class = partial(md_to_html_SimpleHTTPRequestHandler,
8892
directory=args.directory,
8993
MarkdownIt_obj=MarkdownIt_obj,
90-
css_file=args.css_file)
94+
css_file=args.css_file,
95+
live_md_rr=args.live_md_rr)
9196

9297
# ensure dual-stack is not disabled; ref #38907
9398
class DualStackServer(ThreadingHTTPServer):

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66

77
setup(
88
name="httpmdhtml",
9-
version="0.0.6",
9+
version="0.0.7",
1010
license="gpl-3.0",
1111
author="John Hupperts",
1212
author_email="[email protected]",
1313
description="HTTP server that converts markdown to HTML",
1414
long_description=long_description,
1515
long_description_content_type="text/markdown",
1616
url="https://github.com/treatmesubj/python-md-to-html-server",
17-
download_url="https://github.com/treatmesubj/python-md-to-html-server/archive/refs/tags/v0.0.6.tar.gz",
17+
download_url="https://github.com/treatmesubj/python-md-to-html-server/archive/refs/tags/v0.0.7.tar.gz",
1818
packages=["httpmdhtml"],
1919
package_dir={"python-md-to-html-server": "httpmdhtml"},
2020
project_urls={

0 commit comments

Comments
 (0)