|
|
|
|
@@ -5,8 +5,8 @@
|
|
|
|
|
;; Author: Igor Shymko <igor.shimko@gmail.com>
|
|
|
|
|
;; URL: https://github.com/ancane/markdown-preview-mode
|
|
|
|
|
;; Keywords: markdown, gfm, convenience
|
|
|
|
|
;; Version: 0.9
|
|
|
|
|
;; Package-Requires: ((emacs "24.3") (websocket "1.6") (markdown-mode "2.0") (cl-lib "0.5") (web-server "0.1.1") (uuidgen "0.3"))
|
|
|
|
|
;; Version: 0.9.2
|
|
|
|
|
;; 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.
|
|
|
|
|
|
|
|
|
|
@@ -30,11 +30,11 @@
|
|
|
|
|
;;
|
|
|
|
|
;;; Code:
|
|
|
|
|
|
|
|
|
|
(eval-when-compile (require 'cl))
|
|
|
|
|
(require 'cl-lib)
|
|
|
|
|
(require 'websocket)
|
|
|
|
|
(require 'markdown-mode)
|
|
|
|
|
(require 'web-server)
|
|
|
|
|
(require 'uuidgen)
|
|
|
|
|
|
|
|
|
|
(defgroup markdown-preview nil
|
|
|
|
|
"Markdown preview mode."
|
|
|
|
|
@@ -96,7 +96,7 @@
|
|
|
|
|
"`markdown-preview' local client.")
|
|
|
|
|
|
|
|
|
|
(defvar markdown-preview--remote-clients (make-hash-table :test 'equal)
|
|
|
|
|
"Remote clients hashtable. UUID -> WS")
|
|
|
|
|
"Remote clients hashtable. UUID -> WS.")
|
|
|
|
|
|
|
|
|
|
(defvar markdown-preview--home-dir (file-name-directory load-file-name)
|
|
|
|
|
"`markdown-preview-mode' home directory.")
|
|
|
|
|
@@ -108,21 +108,23 @@
|
|
|
|
|
"Unique preview identifier.")
|
|
|
|
|
|
|
|
|
|
(defvar markdown-preview--preview-buffers (make-hash-table :test 'equal)
|
|
|
|
|
"Preview buffers hashtable. UUID -> buffer-name.")
|
|
|
|
|
"Preview buffers hashtable. UUID -> buffer name.")
|
|
|
|
|
|
|
|
|
|
(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 ()
|
|
|
|
|
(defun markdown-preview--css ()
|
|
|
|
|
"Get list of styles for preview in backward compatible way."
|
|
|
|
|
(let* ((custom-style (list markdown-preview-style))
|
|
|
|
|
(all-styles
|
|
|
|
|
(mapc (lambda (x) (add-to-list 'custom-style x t)) markdown-preview-stylesheets)))
|
|
|
|
|
(mapconcat
|
|
|
|
|
(lambda (x)
|
|
|
|
|
(concat "<link rel=\"stylesheet\" type=\"text/css\" href=\"" x "\">"))
|
|
|
|
|
(if (string-match-p "^[\n\t ]*<style" x)
|
|
|
|
|
x
|
|
|
|
|
(concat "<link rel=\"stylesheet\" type=\"text/css\" href=\"" x "\">")))
|
|
|
|
|
all-styles
|
|
|
|
|
"\n")))
|
|
|
|
|
|
|
|
|
|
@@ -130,20 +132,21 @@
|
|
|
|
|
"Get list of javascript script tags for preview."
|
|
|
|
|
(mapconcat
|
|
|
|
|
(lambda (x)
|
|
|
|
|
(concat
|
|
|
|
|
"<script src=\"" (if (consp x) (car x) x) "\""
|
|
|
|
|
(if (consp x) (format " %s" (cdr x)))
|
|
|
|
|
"></script>"))
|
|
|
|
|
(if (string-match-p "^[\n\t ]*<script" x)
|
|
|
|
|
x
|
|
|
|
|
(concat
|
|
|
|
|
"<script src=\"" (if (consp x) (car x) x) "\""
|
|
|
|
|
(if (consp x) (format " %s" (cdr x)))
|
|
|
|
|
"></script>")))
|
|
|
|
|
markdown-preview-javascript
|
|
|
|
|
"\n"))
|
|
|
|
|
|
|
|
|
|
(defun markdown-preview--read-preview-template (preview-uuid preview-file)
|
|
|
|
|
"Read preview template and writes identified by PREVIEW-UUID
|
|
|
|
|
rendered copy to PREVIEW-FILE, ready to be open in browser."
|
|
|
|
|
"Read preview template and writes identified by PREVIEW-UUID rendered copy to PREVIEW-FILE, ready to be open in browser."
|
|
|
|
|
(with-temp-file preview-file
|
|
|
|
|
(insert-file-contents (expand-file-name "preview.html" markdown-preview--home-dir))
|
|
|
|
|
(when (search-forward "${MD_STYLE}" nil t)
|
|
|
|
|
(replace-match (markdown-preview--css-links) t))
|
|
|
|
|
(replace-match (markdown-preview--css) t))
|
|
|
|
|
(when (search-forward "${MD_JS}" nil t)
|
|
|
|
|
(replace-match (markdown-preview--scripts) t))
|
|
|
|
|
(when (search-forward "${WS_HOST}" nil t)
|
|
|
|
|
@@ -154,10 +157,21 @@ rendered copy to PREVIEW-FILE, ready to be open in browser."
|
|
|
|
|
(replace-match (format "%s" preview-uuid) t))
|
|
|
|
|
(buffer-string)))
|
|
|
|
|
|
|
|
|
|
;; Emacs 26 async network workaround
|
|
|
|
|
(defun markdown-preview--fix-network-process-wait (plist)
|
|
|
|
|
"Ensure PLIST contain :nowait nil."
|
|
|
|
|
(if (and (>= emacs-major-version 26)
|
|
|
|
|
(equal (plist-get plist :name) "ws-server")
|
|
|
|
|
(plist-get plist :server)
|
|
|
|
|
(plist-get plist :nowait))
|
|
|
|
|
(plist-put plist :nowait nil)
|
|
|
|
|
plist))
|
|
|
|
|
|
|
|
|
|
(defun markdown-preview--start-http-server (port)
|
|
|
|
|
"Start http server at PORT to serve preview file via http."
|
|
|
|
|
(unless markdown-preview--http-server
|
|
|
|
|
(lexical-let ((docroot default-directory))
|
|
|
|
|
(advice-add 'make-network-process :filter-args #'markdown-preview--fix-network-process-wait)
|
|
|
|
|
(setq markdown-preview--http-server
|
|
|
|
|
(ws-start
|
|
|
|
|
(lambda (request)
|
|
|
|
|
@@ -181,7 +195,8 @@ rendered copy to PREVIEW-FILE, ready to be open in browser."
|
|
|
|
|
(ws-send-file process filename)
|
|
|
|
|
(ws-send-404 process)
|
|
|
|
|
))))))
|
|
|
|
|
markdown-preview-http-port nil :host markdown-preview-http-host)))))
|
|
|
|
|
markdown-preview-http-port nil :host markdown-preview-http-host))
|
|
|
|
|
(advice-remove 'make-network-process #'my-filter))))
|
|
|
|
|
|
|
|
|
|
(defun markdown-preview--parse-uuid (headers)
|
|
|
|
|
"Find uuid query param in HEADERS."
|
|
|
|
|
@@ -267,12 +282,12 @@ rendered copy to PREVIEW-FILE, ready to be open in browser."
|
|
|
|
|
(setq markdown-preview--local-client nil))))))
|
|
|
|
|
|
|
|
|
|
(defun markdown-preview--send-preview (preview-uuid)
|
|
|
|
|
"Send the `markdown-preview' preview to clients."
|
|
|
|
|
"Send the `markdown-preview' with PREVIEW-UUID preview to clients."
|
|
|
|
|
(when (bound-and-true-p markdown-preview-mode)
|
|
|
|
|
(markdown-preview--send-preview-to markdown-preview--local-client preview-uuid)))
|
|
|
|
|
|
|
|
|
|
(defun markdown-preview--send-preview-to (websocket preview-uuid)
|
|
|
|
|
"Send the `markdown-preview' to a specific WEBSOCKET."
|
|
|
|
|
"Send the `markdown-preview' with PREVIEW-UUID to a specific WEBSOCKET."
|
|
|
|
|
(let ((mark-position-percent
|
|
|
|
|
(number-to-string
|
|
|
|
|
(truncate
|
|
|
|
|
@@ -302,7 +317,7 @@ rendered copy to PREVIEW-FILE, ready to be open in browser."
|
|
|
|
|
|
|
|
|
|
(defun markdown-preview--start ()
|
|
|
|
|
"Start `markdown-preview' mode."
|
|
|
|
|
(setq-local markdown-preview--uuid (uuidgen-1))
|
|
|
|
|
(setq-local markdown-preview--uuid (markdown-preview--random-uuid))
|
|
|
|
|
(puthash markdown-preview--uuid (buffer-name) markdown-preview--preview-buffers)
|
|
|
|
|
;; (gethash markdown-preview--uuid markdown-preview--preview-buffers)
|
|
|
|
|
(markdown-preview--read-preview-template
|
|
|
|
|
@@ -328,6 +343,37 @@ rendered copy to PREVIEW-FILE, ready to be open in browser."
|
|
|
|
|
(if (file-exists-p preview-file)
|
|
|
|
|
(delete-file preview-file))))
|
|
|
|
|
|
|
|
|
|
(defun markdown-preview--random-uuid ()
|
|
|
|
|
"Insert a UUID using a simple hashing of variable data.
|
|
|
|
|
Example of a UUID: 1df63142-a513-c850-31a3-535fc3520c3d
|
|
|
|
|
Note: this code uses https://en.wikipedia.org/wiki/Md5,
|
|
|
|
|
which is not cryptographically safe. I'm not sure what's
|
|
|
|
|
the implication of its use here.
|
|
|
|
|
Version 2015-01-30
|
|
|
|
|
URL `http://ergoemacs.org/emacs/elisp_generate_uuid.html'"
|
|
|
|
|
;; by Christopher Wellons, 2011-11-18. Editted by Xah Lee.
|
|
|
|
|
;; Edited by Hideki Saito further to generate all valid variants for "N" in xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx format.
|
|
|
|
|
;; (interactive)
|
|
|
|
|
(let ((myStr (md5 (format "%s%s%s%s%s%s%s%s%s%s"
|
|
|
|
|
(user-uid)
|
|
|
|
|
(emacs-pid)
|
|
|
|
|
(system-name)
|
|
|
|
|
(user-full-name)
|
|
|
|
|
(current-time)
|
|
|
|
|
(emacs-uptime)
|
|
|
|
|
(garbage-collect)
|
|
|
|
|
(buffer-string)
|
|
|
|
|
(random)
|
|
|
|
|
(recent-keys)))))
|
|
|
|
|
|
|
|
|
|
(format "%s-%s-4%s-%s%s-%s"
|
|
|
|
|
(substring myStr 0 8)
|
|
|
|
|
(substring myStr 8 12)
|
|
|
|
|
(substring myStr 13 16)
|
|
|
|
|
(format "%x" (+ 8 (random 4)))
|
|
|
|
|
(substring myStr 17 20)
|
|
|
|
|
(substring myStr 20 32))))
|
|
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
|
(defun markdown-preview-open-browser ()
|
|
|
|
|
"Open the `markdown-preview' in the browser."
|
|
|
|
|
|