2014-09-15 07:30:13 +02:00
|
|
|
;;; markdown-preview-mode.el --- markdown realtime preview minor mode.
|
2014-09-14 11:18:24 +02:00
|
|
|
|
|
|
|
;; Copyright (C) 2014 <igor.shimko@gmail.com>
|
|
|
|
|
2014-09-14 14:39:16 +02:00
|
|
|
;; Author: Igor Shymko <igor.shimko@gmail.com>
|
2014-09-14 11:18:24 +02:00
|
|
|
;; URL: https://github.com/ancane/markdown-preview-mode
|
|
|
|
;; Keywords: markdown, preview
|
2016-08-29 18:33:19 +02:00
|
|
|
;; Version: 0.5
|
2016-06-04 13:17:56 +02:00
|
|
|
;; Package-Requires: ((websocket "1.6") (markdown-mode "2.1") (cl-lib "0.5"))
|
2014-09-14 11:18:24 +02:00
|
|
|
|
|
|
|
;; 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
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;; 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 <http://www.gnu.org/licenses/>.
|
|
|
|
|
2016-01-11 09:18:32 +01:00
|
|
|
;;; Commentary:
|
|
|
|
;;
|
|
|
|
;; This package makes use of websockets to deliver rendered markdown to a web browser.
|
|
|
|
;; Updates happen upon buffer save or on idle.
|
|
|
|
;;
|
2016-01-06 06:02:31 +01:00
|
|
|
;;; Code:
|
2014-09-14 11:18:24 +02:00
|
|
|
|
2016-01-06 06:02:31 +01:00
|
|
|
(require 'cl-lib)
|
2014-09-14 11:18:24 +02:00
|
|
|
(require 'websocket)
|
|
|
|
(require 'markdown-mode)
|
|
|
|
|
2014-09-15 21:39:14 +02:00
|
|
|
(defgroup markdown-preview nil
|
2016-01-15 08:30:58 +01:00
|
|
|
"Markdown preview mode."
|
2014-09-14 11:18:24 +02:00
|
|
|
:group 'text
|
2016-01-11 05:53:03 +01:00
|
|
|
:prefix "markdown-preview-"
|
2014-09-15 21:39:14 +02:00
|
|
|
:link '(url-link "https://github.com/ancane/markdown-preview-mode"))
|
|
|
|
|
|
|
|
(defcustom markdown-preview-port 7379
|
2016-01-11 09:18:32 +01:00
|
|
|
"Markdown preview websocket server port."
|
2014-09-15 21:39:14 +02:00
|
|
|
:group 'markdown-preview
|
|
|
|
:type 'integer)
|
|
|
|
|
2016-01-15 08:30:58 +01:00
|
|
|
(defcustom markdown-preview-style
|
|
|
|
"http://thomasf.github.io/solarized-css/solarized-dark.min.css"
|
2016-01-11 09:18:32 +01:00
|
|
|
"Markdown preview style URI."
|
2014-09-15 21:39:14 +02:00
|
|
|
:group 'markdown-preview
|
|
|
|
:type 'string)
|
2014-09-14 11:18:24 +02:00
|
|
|
|
2016-08-29 18:33:19 +02:00
|
|
|
(defcustom markdown-preview-file-name
|
|
|
|
".markdown-preview.html"
|
|
|
|
"Markdown preview file name"
|
|
|
|
:group 'markdown-preview
|
|
|
|
:type 'string)
|
|
|
|
|
2016-01-11 08:34:24 +01:00
|
|
|
(defvar markdown-preview--websocket-server nil
|
|
|
|
"`markdown-preview' Websocket server.")
|
2016-01-11 09:18:32 +01:00
|
|
|
|
2016-01-11 08:34:24 +01:00
|
|
|
(defvar markdown-preview--local-client nil
|
|
|
|
"`markdown-preview' local client.")
|
2016-01-11 09:18:32 +01:00
|
|
|
|
2016-01-11 08:34:24 +01:00
|
|
|
(defvar markdown-preview--remote-clients nil
|
|
|
|
"List of `markdown-preview' websocket remote clients.")
|
2016-01-11 09:18:32 +01:00
|
|
|
|
2016-01-11 08:34:24 +01:00
|
|
|
(defvar markdown-preview--preview-url
|
|
|
|
(concat (file-name-directory load-file-name) "preview.html")
|
|
|
|
"Location of `markdown-preview' html.")
|
2016-01-11 09:18:32 +01:00
|
|
|
|
|
|
|
(defvar markdown-preview--idle-timer nil
|
|
|
|
"Preview idle timer.")
|
2016-01-11 05:53:03 +01:00
|
|
|
|
|
|
|
(defun markdown-preview--stop-idle-timer ()
|
2016-01-11 08:34:24 +01:00
|
|
|
"Stop the `markdown-preview' idle timer."
|
2016-01-11 05:53:03 +01:00
|
|
|
(when (timerp markdown-preview--idle-timer)
|
|
|
|
(cancel-timer markdown-preview--idle-timer)))
|
|
|
|
|
|
|
|
(defun markdown-preview--open-browser-preview ()
|
2016-01-11 08:34:24 +01:00
|
|
|
"Open the markdown preview in the browser."
|
2016-08-29 18:33:19 +02:00
|
|
|
(let* ((dir-of-buffer-to-preview (file-name-directory (buffer-file-name)))
|
|
|
|
(preview-file (concat dir-of-buffer-to-preview markdown-preview-file-name)))
|
2016-08-23 09:48:37 +02:00
|
|
|
(if (not (file-exists-p preview-file))
|
|
|
|
(copy-file markdown-preview--preview-url preview-file))
|
|
|
|
(browse-url preview-file)))
|
2016-01-11 05:53:03 +01:00
|
|
|
|
|
|
|
(defun markdown-preview--stop-websocket-server ()
|
2016-01-11 08:34:24 +01:00
|
|
|
"Stop the `markdown-preview' websocket server."
|
2016-01-11 05:53:03 +01:00
|
|
|
(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 ()
|
2016-01-11 08:34:24 +01:00
|
|
|
"Clean closed clients in `markdown-preview--remote-clients' list."
|
2016-01-11 05:53:03 +01:00
|
|
|
(setq markdown-preview--remote-clients
|
|
|
|
(cl-remove-if-not #'websocket-openp markdown-preview--remote-clients)))
|
|
|
|
|
|
|
|
(defun markdown-preview--start-websocket-server ()
|
2016-01-11 08:34:24 +01:00
|
|
|
"Start `markdown-preview' websocket server."
|
2016-01-11 05:53:03 +01:00
|
|
|
(when (not markdown-preview--websocket-server)
|
|
|
|
(setq markdown-preview--websocket-server
|
2014-09-14 11:18:24 +02:00
|
|
|
(websocket-server
|
2014-09-15 21:39:14 +02:00
|
|
|
markdown-preview-port
|
2014-09-14 11:18:24 +02:00
|
|
|
:on-message (lambda (websocket frame)
|
2016-02-15 17:49:40 +01:00
|
|
|
(mapc (lambda (ws) (websocket-send ws frame))
|
2016-01-11 05:53:03 +01:00
|
|
|
markdown-preview--remote-clients))
|
2014-09-14 17:06:33 +02:00
|
|
|
:on-open (lambda (websocket)
|
2016-01-11 05:53:03 +01:00
|
|
|
(push websocket markdown-preview--remote-clients)
|
|
|
|
(markdown-preview--send-preview-to websocket))
|
2014-09-14 16:41:52 +02:00
|
|
|
:on-error (lambda (websocket type err) (message (concat "====> Error:" err)))
|
2016-01-11 05:53:03 +01:00
|
|
|
:on-close (lambda (websocket) (markdown-preview--drop-closed-clients))))
|
|
|
|
(add-hook 'kill-emacs-hook 'markdown-preview--stop-websocket-server)
|
|
|
|
(markdown-preview--open-browser-preview)))
|
2014-09-14 14:39:16 +02:00
|
|
|
|
2016-01-11 05:53:03 +01:00
|
|
|
(defun markdown-preview--start-local-client ()
|
2016-01-11 08:34:24 +01:00
|
|
|
"Start the `markdown-preview' local client."
|
2016-01-11 05:53:03 +01:00
|
|
|
(when (not markdown-preview--local-client)
|
|
|
|
(setq markdown-preview--local-client
|
2014-09-14 14:39:16 +02:00
|
|
|
(websocket-open
|
2014-09-15 21:39:14 +02:00
|
|
|
(format "ws://localhost:%d" markdown-preview-port)
|
2014-09-14 14:39:16 +02:00
|
|
|
:on-error (lambda (ws type err)
|
|
|
|
(message "error connecting"))
|
|
|
|
:on-close (lambda (websocket)
|
2016-01-11 05:53:03 +01:00
|
|
|
(setq markdown-preview--local-client nil))))))
|
2014-09-14 11:18:24 +02:00
|
|
|
|
2016-01-11 05:53:03 +01:00
|
|
|
(defun markdown-preview--send-preview ()
|
2016-01-11 08:34:24 +01:00
|
|
|
"Send the `markdown-preview' preview to clients."
|
2014-10-27 12:11:21 +01:00
|
|
|
(when (bound-and-true-p markdown-preview-mode)
|
2016-01-11 05:53:03 +01:00
|
|
|
(markdown-preview--send-preview-to markdown-preview--local-client)))
|
2014-09-14 17:06:33 +02:00
|
|
|
|
2016-01-11 05:53:03 +01:00
|
|
|
(defun markdown-preview--send-preview-to (websocket)
|
2016-01-11 08:34:24 +01:00
|
|
|
"Send the `markdown-preview' to a specific WEBSOCKET."
|
2014-10-23 01:14:12 +02:00
|
|
|
(let ((mark-position-percent
|
|
|
|
(number-to-string
|
|
|
|
(truncate
|
|
|
|
(* 100
|
|
|
|
(/
|
2014-10-27 12:11:21 +01:00
|
|
|
(float (- (line-number-at-pos) (/ (count-screen-lines (window-start) (point)) 2)))
|
2014-10-23 01:14:12 +02:00
|
|
|
(count-lines (point-min) (point-max))))))))
|
2016-01-11 09:18:32 +01:00
|
|
|
(markdown markdown-output-buffer-name)
|
2014-10-23 01:14:12 +02:00
|
|
|
(with-current-buffer (get-buffer markdown-output-buffer-name)
|
|
|
|
(websocket-send-text websocket
|
|
|
|
(concat
|
|
|
|
"<div>"
|
|
|
|
"<span id='style'>"
|
|
|
|
markdown-preview-style
|
|
|
|
"</span>"
|
|
|
|
"<span id='position-percentage'>"
|
|
|
|
mark-position-percent
|
|
|
|
"</span>"
|
|
|
|
"<div id='content'>"
|
|
|
|
(buffer-substring-no-properties (point-min) (point-max))
|
|
|
|
"</div>"
|
|
|
|
"</div>")
|
|
|
|
))))
|
2014-09-14 11:18:24 +02:00
|
|
|
|
2016-01-11 05:53:03 +01:00
|
|
|
(defun markdown-preview--start ()
|
2016-01-11 08:34:24 +01:00
|
|
|
"Start `markdown-preview' mode."
|
2016-01-11 05:53:03 +01:00
|
|
|
(markdown-preview--start-websocket-server)
|
|
|
|
(markdown-preview--start-local-client)
|
|
|
|
(setq markdown-preview--idle-timer
|
2016-02-15 17:49:40 +01:00
|
|
|
(run-with-idle-timer 2 t (lambda () (markdown-preview--send-preview))))
|
2016-06-04 13:17:56 +02:00
|
|
|
(add-hook 'after-save-hook 'markdown-preview--send-preview nil t))
|
2014-09-14 11:18:24 +02:00
|
|
|
|
2016-01-11 05:53:03 +01:00
|
|
|
(defun markdown-preview--stop ()
|
2016-01-11 08:34:24 +01:00
|
|
|
"Stop `markdown-preview' mode."
|
2016-01-11 05:53:03 +01:00
|
|
|
(remove-hook 'after-save-hook 'markdown-preview--send-preview t)
|
2016-08-29 22:58:12 +02:00
|
|
|
(markdown-preview--stop-idle-timer)
|
|
|
|
(let ((preview-file (concat (file-name-directory (buffer-file-name)) markdown-preview-file-name)))
|
|
|
|
(if (file-exists-p preview-file)
|
|
|
|
(delete-file preview-file)))
|
|
|
|
)
|
2014-09-14 11:18:24 +02:00
|
|
|
|
2016-01-11 09:18:32 +01:00
|
|
|
;;;###autoload
|
2014-09-14 12:19:03 +02:00
|
|
|
(defun markdown-preview-open-browser ()
|
2016-01-11 08:34:24 +01:00
|
|
|
"Open the `markdown-preview' in the browser."
|
2014-09-14 12:19:03 +02:00
|
|
|
(interactive)
|
2016-01-11 05:53:03 +01:00
|
|
|
(markdown-preview--open-browser-preview))
|
2014-09-14 12:19:03 +02:00
|
|
|
|
2016-01-11 09:18:32 +01:00
|
|
|
;;;###autoload
|
2014-09-14 17:06:33 +02:00
|
|
|
(defun markdown-preview-cleanup ()
|
2016-01-11 08:34:24 +01:00
|
|
|
"Cleanup `markdown-preview' mode."
|
2014-09-14 12:19:03 +02:00
|
|
|
(interactive)
|
2016-01-11 05:53:03 +01:00
|
|
|
(markdown-preview--stop-websocket-server))
|
2014-09-14 12:19:03 +02:00
|
|
|
|
2016-01-06 08:30:37 +01:00
|
|
|
;;;###autoload
|
2014-09-14 11:18:24 +02:00
|
|
|
(define-minor-mode markdown-preview-mode
|
2016-01-11 08:34:24 +01:00
|
|
|
"Markdown preview mode."
|
2016-01-11 09:18:32 +01:00
|
|
|
:group 'markdown-preview
|
2014-09-14 11:18:24 +02:00
|
|
|
:init-value nil
|
2015-03-31 23:01:37 +02:00
|
|
|
(when (not (or
|
|
|
|
(equal major-mode 'markdown-mode)
|
|
|
|
(equal major-mode 'gfm-mode)))
|
2014-09-14 11:18:24 +02:00
|
|
|
(markdown-mode))
|
2014-09-14 12:19:03 +02:00
|
|
|
(if markdown-preview-mode
|
2016-01-11 05:53:03 +01:00
|
|
|
(markdown-preview--start)
|
|
|
|
(markdown-preview--stop)))
|
2014-09-14 11:18:24 +02:00
|
|
|
|
|
|
|
(provide 'markdown-preview-mode)
|
|
|
|
|
|
|
|
;;; markdown-preview-mode.el ends here
|