2
2
just convert a markdown file to HTML and write it out to a file
3
3
"""
4
4
import os
5
- import sys
6
5
import base64
7
6
from pathlib import Path
8
7
import argparse
9
8
import re
10
9
from markdown_it import MarkdownIt
11
- from bs4 import BeautifulSoup , element
10
+ from bs4 import BeautifulSoup
12
11
13
12
14
13
def markdown_to_html (
@@ -26,9 +25,17 @@ def markdown_to_html(
26
25
soup = BeautifulSoup (html_text , "html5lib" ) # adds <html>, <head>, <body>
27
26
soup .select_one ("head" ).append (soup .new_tag ("meta" ))
28
27
soup .select_one ("meta" ).attrs ["charset" ] = "UTF-8"
29
- # if lots of images, caching is preferable more often than not
30
- # soup.select_one('meta').attrs['http-equiv'] = "Cache-control"
31
- # soup.select_one('meta').attrs['content'] = "no-cache"
28
+
29
+ # syntax highlighting
30
+ soup .select_one ('head' ).append (soup .new_tag ('link' ))
31
+ soup .select_one ("link" ).attrs ["rel" ] = "stylesheet"
32
+ soup .select_one ("link" ).attrs ["href" ] = "https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/monokai.min.css"
33
+
34
+ soup .select_one ('head' ).append (soup .new_tag ('script' ))
35
+ soup .select ("script" )[0 ].attrs ['src' ] = 'https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js'
36
+ soup .select_one ('head' ).append (soup .new_tag ('script' ))
37
+ soup .select ("script" )[1 ].string = 'hljs.highlightAll();'
38
+
32
39
soup .select_one ("head" ).append (soup .new_tag ("style" ))
33
40
if css_file :
34
41
if css_file == "none" :
@@ -52,7 +59,7 @@ def markdown_to_html(
52
59
f"setTimeout(function(){{ document.location.reload(); }}, { live_md_rr } );"
53
60
)
54
61
soup .select_one ("head" ).append (soup .new_tag ("script" ))
55
- soup .select_one ("script" ).string = script
62
+ soup .select ("script" )[ - 1 ] .string = script
56
63
if encode_local_images :
57
64
img_elems = soup .select ("img" )
58
65
url_pattern = "^https?:\\ /\\ /(?:www\\ .)?[-a-zA-Z0-9@:%._\\ +~#=]{1,256}\\ .[a-zA-Z0-9()]{1,6}\\ b(?:[-a-zA-Z0-9()@:%_\\ +.~#?&\\ /=]*)$"
@@ -77,7 +84,7 @@ def markdown_to_html(
77
84
parser .add_argument (
78
85
"--out_file_path" ,
79
86
"-o" ,
80
- default = "tmp.html" ,
87
+ default = None ,
81
88
help = "out-file-path; your HTML file to be created" ,
82
89
)
83
90
parser .add_argument (
@@ -93,6 +100,9 @@ def markdown_to_html(
93
100
)
94
101
args = parser .parse_args ()
95
102
103
+ if not args .out_file_path :
104
+ args .out_file_path = str (Path (args .in_file_path ).with_suffix ('.html' ))
105
+
96
106
markdown_to_html (
97
107
MarkdownIt_obj = MarkdownIt_obj ,
98
108
in_file_path = args .in_file_path ,
0 commit comments