Skip to content

Commit

Permalink
syntax highlightling
Browse files Browse the repository at this point in the history
  • Loading branch information
treatmesubj committed Jan 19, 2024
1 parent 53d2d76 commit 502f8d7
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions httpmdhtml/md_to_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@
just convert a markdown file to HTML and write it out to a file
"""
import os
import sys
import base64
from pathlib import Path
import argparse
import re
from markdown_it import MarkdownIt
from bs4 import BeautifulSoup, element
from bs4 import BeautifulSoup


def markdown_to_html(
Expand All @@ -26,9 +25,17 @@ def markdown_to_html(
soup = BeautifulSoup(html_text, "html5lib") # adds <html>, <head>, <body>
soup.select_one("head").append(soup.new_tag("meta"))
soup.select_one("meta").attrs["charset"] = "UTF-8"
# if lots of images, caching is preferable more often than not
# soup.select_one('meta').attrs['http-equiv'] = "Cache-control"
# soup.select_one('meta').attrs['content'] = "no-cache"

# syntax highlighting
soup.select_one('head').append(soup.new_tag('link'))
soup.select_one("link").attrs["rel"] = "stylesheet"
soup.select_one("link").attrs["href"] = "https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/monokai.min.css"

soup.select_one('head').append(soup.new_tag('script'))
soup.select("script")[0].attrs['src'] ='https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js'
soup.select_one('head').append(soup.new_tag('script'))
soup.select("script")[1].string = 'hljs.highlightAll();'

soup.select_one("head").append(soup.new_tag("style"))
if css_file:
if css_file == "none":
Expand All @@ -52,7 +59,7 @@ def markdown_to_html(
f"setTimeout(function(){{ document.location.reload(); }}, {live_md_rr});"
)
soup.select_one("head").append(soup.new_tag("script"))
soup.select_one("script").string = script
soup.select("script")[-1].string = script
if encode_local_images:
img_elems = soup.select("img")
url_pattern = "^https?:\\/\\/(?:www\\.)?[-a-zA-Z0-9@:%._\\+~#=]{1,256}\\.[a-zA-Z0-9()]{1,6}\\b(?:[-a-zA-Z0-9()@:%_\\+.~#?&\\/=]*)$"
Expand All @@ -77,7 +84,7 @@ def markdown_to_html(
parser.add_argument(
"--out_file_path",
"-o",
default="tmp.html",
default=None,
help="out-file-path; your HTML file to be created",
)
parser.add_argument(
Expand All @@ -93,6 +100,9 @@ def markdown_to_html(
)
args = parser.parse_args()

if not args.out_file_path:
args.out_file_path = str(Path(args.in_file_path).with_suffix('.html'))

markdown_to_html(
MarkdownIt_obj=MarkdownIt_obj,
in_file_path=args.in_file_path,
Expand Down

0 comments on commit 502f8d7

Please sign in to comment.