Extra stylesheets for preview. Fixes #20

This commit is contained in:
Igor Shymko 2016-12-11 13:55:24 +02:00
parent 322825b9e5
commit 191365929d
2 changed files with 56 additions and 43 deletions

View File

@ -34,7 +34,6 @@
(require 'websocket) (require 'websocket)
(require 'markdown-mode) (require 'markdown-mode)
(require 'web-server) (require 'web-server)
(require 'url-util)
(defgroup markdown-preview nil (defgroup markdown-preview nil
"Markdown preview mode." "Markdown preview mode."
@ -57,9 +56,8 @@
:group 'markdown-preview :group 'markdown-preview
:type 'integer) :type 'integer)
(defcustom markdown-preview-style (defcustom markdown-preview-style nil
"http://thomasf.github.io/solarized-css/solarized-dark.min.css" "Deprecated. Use `markdown-preview-stylesheets'."
"Markdown preview style URI."
:group 'markdown-preview :group 'markdown-preview
:type 'string) :type 'string)
@ -75,8 +73,12 @@
(const :tag "Via http" http) (const :tag "Via http" http)
(const :tag "Off" nil))) (const :tag "Off" nil)))
(defvar markdown-preview-javascript (list "http://code.jquery.com/jquery-1.11.0.min.js") (defvar markdown-preview-javascript '()
"List of javascript libs for preview.") "List of client javascript libs for preview.")
(defvar markdown-preview-stylesheets
(list "http://thomasf.github.io/solarized-css/solarized-dark.min.css")
"List of client stylesheets for preview.")
(defvar markdown-preview--websocket-server nil (defvar markdown-preview--websocket-server nil
"`markdown-preview' Websocket server.") "`markdown-preview' Websocket server.")
@ -98,22 +100,36 @@
(when (timerp markdown-preview--idle-timer) (when (timerp markdown-preview--idle-timer)
(cancel-timer 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))
(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 "\">"))
all-styles
"\n")))
(defun markdown-preview--scripts ()
"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>"))
markdown-preview-javascript
"\n"))
(defun markdown-preview--read-preview-template (preview-file) (defun markdown-preview--read-preview-template (preview-file)
"Read preview template and writes rendered copy to PREVIEW-FILE, ready to be open in browser." "Read preview template and writes rendered copy to PREVIEW-FILE, ready to be open in browser."
(with-temp-file preview-file (with-temp-file preview-file
(insert-file-contents (expand-file-name "preview.html" markdown-preview--home-dir)) (insert-file-contents (expand-file-name "preview.html" markdown-preview--home-dir))
(if (search-forward "${MD_STYLE}" nil t) (if (search-forward "${MD_STYLE}" nil t)
(replace-match markdown-preview-style t)) (replace-match (markdown-preview--css-links) t))
(if (search-forward "${MD_JS}" nil t) (if (search-forward "${MD_JS}" nil t)
(replace-match (replace-match (markdown-preview--scripts) t))
(mapconcat (lambda (x)
(concat
"<script src=\"" (if (consp x) (car x) x) "\""
(if (consp x) (format " %s" (cdr x)))
"></script>"))
markdown-preview-javascript
"\n")
t))
(if (search-forward "${WS_HOST}" nil t) (if (search-forward "${WS_HOST}" nil t)
(replace-match markdown-preview-ws-host t)) (replace-match markdown-preview-ws-host t))
(if (search-forward "${WS_PORT}" nil t) (if (search-forward "${WS_PORT}" nil t)
@ -127,27 +143,26 @@
(lambda (request) (lambda (request)
(with-slots (process headers) request (with-slots (process headers) request
(let* ((path (substring (cdr (assoc :GET headers)) 1)) (let* ((path (substring (cdr (assoc :GET headers)) 1))
(filename (expand-file-name path docroot))) (filename (expand-file-name path docroot)))
(if (string= path "favicon.ico") ;;(message (format "===> Path: [%s]" path))
(ws-send-file process (expand-file-name path markdown-preview--home-dir)) (if (string= path "")
(if (and (not (file-directory-p filename)) (file-exists-p filename)) (ws-send-file process (expand-file-name markdown-preview-file-name docroot))
(ws-send-file process filename) (if (string= path "favicon.ico")
(ws-send-404 process) (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)
))))))
markdown-preview-http-port))) markdown-preview-http-port)))
(defun markdown-preview--open-browser-preview () (defun markdown-preview--open-browser-preview ()
"Open the markdown preview in the browser." "Open the markdown preview in the browser."
(let* ((preview-file (expand-file-name markdown-preview-file-name default-directory)) (when (eq markdown-preview-auto-open 'file)
(preview-address (url-encode-url (browse-url (expand-file-name markdown-preview-file-name default-directory)))
(format "http://localhost:%d/%s" markdown-preview-http-port markdown-preview-file-name)))) (when (eq markdown-preview-auto-open 'http)
(markdown-preview--read-preview-template preview-file) (browse-url (format "http://localhost:%d" markdown-preview-http-port)))
(when (eq markdown-preview-auto-open 'file) (unless markdown-preview-auto-open
(browse-url preview-file)) (message (format "Preview address: http://0.0.0.0:%d" markdown-preview-http-port))))
(when (eq markdown-preview-auto-open 'http)
(browse-url preview-address))
(unless markdown-preview-auto-open
(message (format "Preview address: %s" preview-address)))))
(defun markdown-preview--stop-websocket-server () (defun markdown-preview--stop-websocket-server ()
"Stop the `markdown-preview' websocket server." "Stop the `markdown-preview' websocket server."
@ -222,6 +237,8 @@
(defun markdown-preview--start () (defun markdown-preview--start ()
"Start `markdown-preview' mode." "Start `markdown-preview' mode."
(markdown-preview--read-preview-template
(expand-file-name markdown-preview-file-name default-directory))
(markdown-preview--start-websocket-server) (markdown-preview--start-websocket-server)
(markdown-preview--start-local-client) (markdown-preview--start-local-client)
(markdown-preview--start-http-server markdown-preview-http-port) (markdown-preview--start-http-server markdown-preview-http-port)

View File

@ -4,12 +4,8 @@
<meta charset="utf-8"> <meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, minimal-ui"> <meta name="viewport" content="width=device-width, initial-scale=1, minimal-ui">
<title>Markdown preview</title> <title>Markdown preview</title>
<link href="${MD_STYLE}" rel="stylesheet"></link> ${MD_STYLE}
<style> <script src="http://code.jquery.com/jquery-1.12.4.min.js"></script>
code.json {
line-height: 1.2em;
}
</style>
${MD_JS} ${MD_JS}
<script> <script>
(function($, undefined) { (function($, undefined) {
@ -19,9 +15,9 @@
}; };
socket.onclose = function(event) { socket.onclose = function(event) {
if (event.wasClean) { if (event.wasClean) {
alert('Connection closed gracefully.'); console.log('Connection closed gracefully.');
} else { } else {
alert('Connection terminated.'); console.log('Connection terminated.');
} }
console.log('Code: ' + event.code + ' reason: ' + event.reason); console.log('Code: ' + event.code + ' reason: ' + event.reason);
}; };
@ -31,7 +27,7 @@
$("html, body").animate({ scrollTop: scroll }, 600); $("html, body").animate({ scrollTop: scroll }, 600);
}; };
socket.onerror = function(error) { socket.onerror = function(error) {
alert("Error: " + error.message); console.log("Error: " + error.message);
}; };
})(jQuery); })(jQuery);
</script> </script>