...

Markdown to HTML convertor

Although you may find some tools for viewing markdown documents, but I coudn't find any simple python script that can do the job for me.

To launch this tool, pandoc utility has to be installed on your linux system.

Script will display the content of markdown file in firefox browser. When you save any change in markdown file, then script will reload the browser, but it goes to the top of html in the browser.

Script Syntax

script_name /path/to/markdownfile.md

from inspect import getblock
import os, sys, hashlib, signal, time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

arg_markdown_file = sys.argv[1]
markdown_file = os.path.abspath(arg_markdown_file)

def create_css():
    with open('/tmp/mystyles.css', "w") as cssfile:
        cssfile.write('''
body {
    margin: 50px;
}

code, pre {
  display: block;
  padding-block: 5px 5px;
  padding-right: 5px;
  padding-left: 5px;
  font-family: Courier New, monospace;
  font-size: inherit;
  /* background: #B0E0E6; */
  background-color: #eeeeee;
  white-space: pre; 
  -webkit-overflow-scrolling: touch;
  overflow-x: scroll;
  max-width: 100%;
  min-width: 100px;
}

code:empty {
    background-color: white;
}

''')

def create_header():
    with open('/tmp/header.tex', "w") as header:
        header.write('''
\\let\\OldRule\\rule
\\renewcommand{\\rule}[2]{\\OldRule{\\linewidth}{#2}}

''')

def sha1(filename):
    BUF_SIZE = 65536  # read stuff in 64kb chunks!
    sha1 = hashlib.sha1()
    with open(filename, 'rb') as f:
        while True:
            data = f.read(BUF_SIZE)
            if not data:
                break
            sha1.update(data)
    return sha1.hexdigest()


def handler(signum, frame):
    res = input("\nCtrl-c was pressed. Do you really want to exit? y/n ")
    if res != 'n':
        if os.path.exists('geckodriver.log'):
                os.remove('geckodriver.log')
        exit(0)

signal.signal(signal.SIGINT, handler)

def run_pandoc():
    """
        Check continuously changes in markdown file, and for any change, 
        convert markdown to html, refresh the html page.
    """
    driver = webdriver.Firefox()
    driver.get('file:///tmp/latest.html')
    initial_hash = sha1(markdown_file)

    while True:
        new_hash = sha1(markdown_file)
        if new_hash != initial_hash:
            print(markdown_file, 'modified!')
            
            os.rename('/tmp/latest.html', '/tmp/_latest.html')
            cmd = "pandoc -f markdown -t html " + markdown_file + " -o /tmp/latest.html --self-contained --css=/tmp/mystyles.css --metadata pagetitle='converted markdown to html' --toc"
            os.system(cmd)

            driver.refresh()
            driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
            
            initial_hash = new_hash
            time.sleep(5)

if __name__ == '__main__':
    # os.system('touch /tmp/latest.html')
    create_css()
    create_header()
    cmd = "pandoc -f markdown -t html " + markdown_file + " -o /tmp/latest.html --self-contained --css=/tmp/mystyles.css --metadata pagetitle='converted markdown to html' --toc"
    os.system(cmd)
    run_pandoc()