9
9
import posixpath
10
10
import select
11
11
import shutil
12
- import socket # For gethostbyaddr()
12
+ import socket # For gethostbyaddr()
13
13
import socketserver
14
14
import sys
15
15
import time
31
31
32
32
33
33
class md_to_html_SimpleHTTPRequestHandler (SimpleHTTPRequestHandler ):
34
- def __init__ (self , * args , MarkdownIt_obj = None , css_file = None , live_md_rr = False , ** kwargs ):
34
+ def __init__ (
35
+ self , * args , MarkdownIt_obj = None , css_file = None , live_md_rr = False , ** kwargs
36
+ ):
35
37
self .MarkdownIt_obj = MarkdownIt_obj
36
38
self .css_file = css_file
37
39
self .live_md_rr = live_md_rr
38
40
super ().__init__ (* args , ** kwargs )
39
41
40
-
41
42
def do_GET (self , rm_temp_html = False ):
42
43
"""Serve a GET request."""
43
- self .url_dc_path = urllib .parse .unquote (urllib .parse .urlsplit (self .path ).path ) # url decode, strip query params for file check
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
45
- in_file_path = os .path .join (self .directory , f".{ self .url_dc_path } " )
46
- out_file_path = os .path .join (self .directory , f".{ os .path .splitext (self .url_dc_path )[0 ]} .html" )
44
+ self .url_dc_path = urllib .parse .unquote (
45
+ urllib .parse .urlsplit (self .path ).path
46
+ ) # url decode, strip query params for file check
47
+ if (
48
+ self .MarkdownIt_obj
49
+ and self .url_dc_path .endswith (".md" )
50
+ and os .path .exists (os .path .join (self .directory , f".{ self .url_dc_path } " ))
51
+ ): # check for markdown file request
52
+ in_file_path = os .path .join (self .directory , f".{ self .url_dc_path } " )
53
+ out_file_path = os .path .join (
54
+ self .directory , f".{ os .path .splitext (self .url_dc_path )[0 ]} .html"
55
+ )
47
56
md_to_html .markdown_to_html (
48
- self .MarkdownIt_obj ,
49
- in_file_path = in_file_path ,
50
- out_file_path = out_file_path ,
51
- css_file = self .css_file ,
52
- live_md_rr = self .live_md_rr )
57
+ self .MarkdownIt_obj ,
58
+ in_file_path = in_file_path ,
59
+ out_file_path = out_file_path ,
60
+ css_file = self .css_file ,
61
+ live_md_rr = self .live_md_rr ,
62
+ )
53
63
self .path = f"{ os .path .splitext (self .path )[0 ]} .html"
54
64
rm_temp_html = True
55
65
f = self .send_head ()
@@ -63,44 +73,66 @@ def do_GET(self, rm_temp_html=False):
63
73
64
74
65
75
if __name__ == "__main__" :
66
-
67
76
parser = argparse .ArgumentParser ()
68
- parser .add_argument ('--cgi' , action = 'store_true' ,
69
- help = 'Run as CGI Server' )
70
- parser .add_argument ('--bind' , '-b' , metavar = 'ADDRESS' ,
71
- help = 'Specify alternate bind address '
72
- '[default: all interfaces]' )
73
- parser .add_argument ('--directory' , '-d' , default = os .getcwd (),
74
- help = 'Specify alternative directory '
75
- '[default:current directory]' )
76
- parser .add_argument ('--css_file' , default = None ,
77
- 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. Respects cache' )
80
- parser .add_argument ('port' , action = 'store' ,
81
- default = 8000 , type = int ,
82
- nargs = '?' ,
83
- help = 'Specify alternate port [default: 8000]' )
77
+ parser .add_argument ("--cgi" , action = "store_true" , help = "Run as CGI Server" )
78
+ parser .add_argument (
79
+ "--bind" ,
80
+ "-b" ,
81
+ metavar = "ADDRESS" ,
82
+ help = "Specify alternate bind address " "[default: all interfaces]" ,
83
+ )
84
+ parser .add_argument (
85
+ "--directory" ,
86
+ "-d" ,
87
+ default = os .getcwd (),
88
+ help = "Specify alternative directory " "[default:current directory]" ,
89
+ )
90
+ parser .add_argument (
91
+ "--css_file" ,
92
+ default = None ,
93
+ help = "css-file-path; its content will be written to the <style> element" ,
94
+ )
95
+ parser .add_argument (
96
+ "--live_md_rr" ,
97
+ "-l" ,
98
+ action = "store" ,
99
+ type = int ,
100
+ default = None ,
101
+ help = "Continuous refresh rate of MD page, in ms. Respects cache" ,
102
+ )
103
+ parser .add_argument (
104
+ "port" ,
105
+ action = "store" ,
106
+ default = 8000 ,
107
+ type = int ,
108
+ nargs = "?" ,
109
+ help = "Specify alternate port [default: 8000]" ,
110
+ )
84
111
args = parser .parse_args ()
85
112
if args .cgi :
86
113
handler_class = CGIHTTPRequestHandler
87
114
else :
88
- MarkdownIt_obj = MarkdownIt ("commonmark" ).enable ("table" ).enable ("strikethrough" )
115
+ MarkdownIt_obj = (
116
+ MarkdownIt ("commonmark" ).enable ("table" ).enable ("strikethrough" )
117
+ )
89
118
if args .css_file and not os .path .isfile (args .css_file ):
90
- raise FileNotFoundError (f"looks like the given `css_file` argument's value - { args .css_file } - cannot be found" )
91
- handler_class = partial (md_to_html_SimpleHTTPRequestHandler ,
92
- directory = args .directory ,
93
- MarkdownIt_obj = MarkdownIt_obj ,
94
- css_file = args .css_file ,
95
- live_md_rr = args .live_md_rr )
119
+ raise FileNotFoundError (
120
+ f"looks like the given `css_file` argument's value - { args .css_file } - cannot be found"
121
+ )
122
+ handler_class = partial (
123
+ md_to_html_SimpleHTTPRequestHandler ,
124
+ directory = args .directory ,
125
+ MarkdownIt_obj = MarkdownIt_obj ,
126
+ css_file = args .css_file ,
127
+ live_md_rr = args .live_md_rr ,
128
+ )
96
129
97
130
# ensure dual-stack is not disabled; ref #38907
98
131
class DualStackServer (ThreadingHTTPServer ):
99
132
def server_bind (self ):
100
133
# suppress exception when protocol is IPv4
101
134
with contextlib .suppress (Exception ):
102
- self .socket .setsockopt (
103
- socket .IPPROTO_IPV6 , socket .IPV6_V6ONLY , 0 )
135
+ self .socket .setsockopt (socket .IPPROTO_IPV6 , socket .IPV6_V6ONLY , 0 )
104
136
return super ().server_bind ()
105
137
106
138
http_server_test (
0 commit comments