You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

286 lines
11 KiB

8 years ago
;;; markdown-preview-mode.el --- markdown realtime preview minor mode.
;; Copyright (C) 2014 <>
;; Author: Igor Shymko <>
;; URL:
;; Keywords: markdown, gfm, convenience
;; Version: 0.8
;; Package-Requires: ( (emacs "24.3") (websocket "1.6") (markdown-mode "2.0") (cl-lib "0.5") (web-server "0.1.1"))
;; This file is not part of GNU Emacs.
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. If not, see <>.
;;; Commentary:
;; This package makes use of websockets to deliver rendered markdown to a web browser.
;; Updates happen upon buffer save or on idle.
;;; Code:
(require 'cl-lib)
(require 'websocket)
(require 'markdown-mode)
(require 'web-server)
(defgroup markdown-preview nil
"Markdown preview mode."
:group 'text
:prefix "markdown-preview-"
:link '(url-link ""))
(defcustom markdown-preview-ws-host "localhost"
"Markdown preview websocket server address."
:group 'markdown-preview
:type 'string)
(defcustom markdown-preview-ws-port 7379
"Markdown preview websocket server port."
:group 'markdown-preview
:type 'integer)
(defcustom markdown-preview-http-port 9000
"Markdown preview http server port."
:group 'markdown-preview
:type 'integer)
(defcustom markdown-preview-style nil
"Deprecated. Use `markdown-preview-stylesheets'."
:group 'markdown-preview
:type 'string)
(defcustom markdown-preview-file-name ".markdown-preview.html"
"Markdown preview file name."
6 years ago
:group 'markdown-preview
:type 'string)
(defcustom markdown-preview-auto-open 'http
"Markdown preview websocket server address."
:group 'markdown-preview
:type '(choice (const :tag "As local file" file)
6 years ago
(const :tag "Via http" http)
(const :tag "Off" nil)))
(defvar markdown-preview-javascript '()
"List of client javascript libs for preview.")
(defvar markdown-preview-stylesheets
(list "")
"List of client stylesheets for preview.")
(defvar markdown-preview--websocket-server nil
"`markdown-preview' Websocket server.")
(defvar markdown-preview--local-client nil
"`markdown-preview' local client.")
(defvar markdown-preview--remote-clients nil
"List of `markdown-preview' websocket remote clients.")
6 years ago
(defvar markdown-preview--home-dir (file-name-directory load-file-name)
"`markdown-preview-mode' home directory.")
(defvar markdown-preview--idle-timer nil
"Preview idle timer.")
(defun markdown-preview--stop-idle-timer ()
"Stop the `markdown-preview' idle timer."
(when (timerp markdown-preview--idle-timer)
(cancel-timer markdown-preview--idle-timer)))
(defun markdown-preview--css-links ()
"Get list of styles for preview in backward compatible way."
(let* ((custom-style (list markdown-preview-style))
(mapc (lambda (x) (add-to-list 'custom-style x t)) markdown-preview-stylesheets)))
(lambda (x)
(concat "<link rel=\"stylesheet\" type=\"text/css\" href=\"" x "\">"))
(defun markdown-preview--scripts ()
"Get list of javascript script tags for preview."
(lambda (x)
"<script src=\"" (if (consp x) (car x) x) "\""
(if (consp x) (format " %s" (cdr x)))
(defun markdown-preview--read-preview-template (preview-file)
6 years ago
"Read preview template and writes rendered copy to PREVIEW-FILE, ready to be open in browser."
(with-temp-file preview-file
6 years ago
(insert-file-contents (expand-file-name "preview.html" markdown-preview--home-dir))
(if (search-forward "${MD_STYLE}" nil t)
(replace-match (markdown-preview--css-links) t))
(if (search-forward "${MD_JS}" nil t)
(replace-match (markdown-preview--scripts) t))
(if (search-forward "${WS_HOST}" nil t)
(replace-match markdown-preview-ws-host t))
(if (search-forward "${WS_PORT}" nil t)
(replace-match (format "%s" markdown-preview-ws-port) t))
(defun markdown-preview--start-http-server (port)
6 years ago
"Start http server at PORT to serve preview file via http."
(lexical-let ((docroot default-directory))
(lambda (request)
(with-slots (process headers) request
6 years ago
(let* ((path (substring (cdr (assoc :GET headers)) 1))
(filename (expand-file-name path docroot)))
;;(message (format "===> Path: [%s]" path))
(if (string= path "")
(ws-send-file process (expand-file-name markdown-preview-file-name docroot))
(if (string= path "favicon.ico")
(ws-send-file process (expand-file-name path markdown-preview--home-dir))
(if (and (not (file-directory-p filename)) (file-exists-p filename))
(ws-send-file process filename)
(ws-send-404 process)
(defun markdown-preview--open-browser-preview ()
"Open the markdown preview in the browser."
(when (eq markdown-preview-auto-open 'file)
(browse-url (expand-file-name markdown-preview-file-name default-directory)))
(when (eq markdown-preview-auto-open 'http)
(browse-url (format "http://localhost:%d" markdown-preview-http-port)))
(unless markdown-preview-auto-open
(message (format "Preview address:" markdown-preview-http-port))))
(defun markdown-preview--stop-websocket-server ()
"Stop the `markdown-preview' websocket server."
(when markdown-preview--local-client
(websocket-close markdown-preview--local-client))
(when markdown-preview--websocket-server
(delete-process markdown-preview--websocket-server)
(setq markdown-preview--websocket-server nil
markdown-preview--remote-clients nil)))
(defun markdown-preview--drop-closed-clients ()
"Clean closed clients in `markdown-preview--remote-clients' list."
(setq markdown-preview--remote-clients
(cl-remove-if-not #'websocket-openp markdown-preview--remote-clients)))
(defun markdown-preview--start-websocket-server ()
"Start `markdown-preview' websocket server."
(when (not markdown-preview--websocket-server)
(setq markdown-preview--websocket-server
:host markdown-preview-ws-host
:on-message (lambda (websocket frame)
(mapc (lambda (ws) (websocket-send ws frame))
:on-open (lambda (websocket)
(push websocket markdown-preview--remote-clients)
(markdown-preview--send-preview-to websocket))
:on-error (lambda (websocket type err) (message (concat "====> Error:" err)))
:on-close (lambda (websocket) (markdown-preview--drop-closed-clients))))
(add-hook 'kill-emacs-hook 'markdown-preview--stop-websocket-server)
(defun markdown-preview--start-local-client ()
"Start the `markdown-preview' local client."
(when (not markdown-preview--local-client)
(setq markdown-preview--local-client
(format "ws://%s:%d" markdown-preview-ws-host markdown-preview-ws-port)
:on-error (lambda (ws type err)
(message "error connecting"))
:on-close (lambda (websocket)
(setq markdown-preview--local-client nil))))))
(defun markdown-preview--send-preview ()
"Send the `markdown-preview' preview to clients."
(when (bound-and-true-p markdown-preview-mode)
(markdown-preview--send-preview-to markdown-preview--local-client)))
(defun markdown-preview--send-preview-to (websocket)
"Send the `markdown-preview' to a specific WEBSOCKET."
(let ((mark-position-percent
(* 100
(float (- (line-number-at-pos) (/ (count-screen-lines (window-start) (point)) 2)))
(count-lines (point-min) (point-max))))))))
(markdown markdown-output-buffer-name)
(with-current-buffer (get-buffer markdown-output-buffer-name)
(websocket-send-text websocket
"<span id='position-percentage'>"
"<div id='content'>"
(buffer-substring-no-properties (point-min) (point-max))
(defun markdown-preview--start ()
"Start `markdown-preview' mode."
(expand-file-name markdown-preview-file-name default-directory))
(markdown-preview--start-http-server markdown-preview-http-port)
(setq markdown-preview--idle-timer
(run-with-idle-timer 2 t (lambda () (markdown-preview--send-preview))))
(add-hook 'after-save-hook 'markdown-preview--send-preview nil t))
(defun markdown-preview--stop ()
"Stop `markdown-preview' mode."
(remove-hook 'after-save-hook 'markdown-preview--send-preview t)
(let ((preview-file (concat (file-name-directory (buffer-file-name)) markdown-preview-file-name)))
(if (file-exists-p preview-file)
(delete-file preview-file))))
(defun markdown-preview-open-browser ()
"Open the `markdown-preview' in the browser."
(defun markdown-preview-cleanup ()
"Cleanup `markdown-preview' mode."
(define-minor-mode markdown-preview-mode
"Markdown preview mode."
:group 'markdown-preview
:init-value nil
(when (not (or
(equal major-mode 'markdown-mode)
(equal major-mode 'gfm-mode)))
(if markdown-preview-mode
(provide 'markdown-preview-mode)
;;; markdown-preview-mode.el ends here