From 9478b592a0100f83558fb3f7e92e1c25ba740020 Mon Sep 17 00:00:00 2001 From: Igor Shymko Date: Sun, 14 Sep 2014 12:18:24 +0300 Subject: [PATCH] markdown preview mode initial commit --- README.md | 9 +++- markdown-preview-mode.el | 94 ++++++++++++++++++++++++++++++++++++++++ preview.html | 33 ++++++++++++++ 3 files changed, 134 insertions(+), 2 deletions(-) create mode 100644 markdown-preview-mode.el create mode 100644 preview.html diff --git a/README.md b/README.md index 6aff381..2a90868 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,9 @@ -Markdown preview minor mode +Markdown preview mode =========================== -Markdown mode extention with realtime preview +Markdown mode extension with realtime preview. Minor mode. + +Depends on: + +* markdown-mode +* websocket.el diff --git a/markdown-preview-mode.el b/markdown-preview-mode.el new file mode 100644 index 0000000..f2f36bc --- /dev/null +++ b/markdown-preview-mode.el @@ -0,0 +1,94 @@ +;;; markdown-preview-mode.el --- markdown realtime preview. + +;; Copyright (C) 2014 + +;; Author: Kostafey +;; URL: https://github.com/ancane/markdown-preview-mode +;; Keywords: markdown, preview +;; Package-Requires: ((websocket "1.3")) + +;; 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 . + +(eval-when-compile (require 'cl)) + +(require 'websocket) +(require 'markdown-mode) + +(defgroup markdown-preview-mode nil + "Markdown preview mode" + :group 'text + :prefix "mdpm:") + +(defvar mdpm:websocket-port 7379) +(defvar mdpm:websocket-server nil) +(defvar mdpm:websocket-clients nil) +(defvar mdpm:directory (file-name-directory load-file-name)) + +;; (setq mdpm:websocket-server (get-process "websocket server on port 7379")) + +(defun mdpm:start-websocket-server () + (when (not mdpm:websocket-server) + (setq mdpm:websocket-server + (websocket-server + mdpm:websocket-port + :on-message (lambda (websocket frame) + (mapc (lambda (ws) + ;; send frame only if ws != websocket + (websocket-send-text + ws + (websocket-frame-payload frame)))) + mdpm:websocket-clients) + :on-open (lambda (websocket) + (push websocket mdpm:websocket-clients)) + :on-close (lambda (websocket) + (delete websocket mdpm:websocket-clients)))))) + +(defun mdpm:stop-websocket-server () + (when mdpm:websocket-server + (delete-process mdpm:websocket-server) + (setq mdpm:websocket-server nil + mdpm:websocket-clients nil))) + +(defun mdpm:send ()) + +(defun mdpm:start () + (message "MDP: Start") + (mdpm:start-websocket-server) + (browse-url (concat mdpm:directory "preview.html")) + (add-hook 'kill-emacs-hook 'mdpm:stop-websocket-server) + (add-hook 'after-save-hook 'mdpm:send nil t)) + +(defun mdpm:stop () + (message "MDP: Stop") + (mdpm:stop-websocket-server) + (remove-hook 'kill-emacs-hook 'mdpm:stop-websocket-server) + (remove-hook 'after-save-hook 'mdpm:send)) + +(define-minor-mode markdown-preview-mode + "Markdown preview mode" + :group 'markdown-preview-mode + :init-value nil + + (when (not (equal major-mode 'markdown-mode)) + (markdown-mode)) + + (if (not markdown-preview-mode) + (mdpm:start) + (mdpm:stop))) + +(provide 'markdown-preview-mode) + +;;; markdown-preview-mode.el ends here diff --git a/preview.html b/preview.html new file mode 100644 index 0000000..a58d33b --- /dev/null +++ b/preview.html @@ -0,0 +1,33 @@ + + + + + + Markdown preview + + + +
+

Markdown preview

+
+ +