Skip to content

Commit

Permalink
add html-buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
cxxxr committed Aug 31, 2024
1 parent df86b03 commit ffb97bd
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 20 deletions.
28 changes: 25 additions & 3 deletions extensions/markdown-mode/internal.lisp
Original file line number Diff line number Diff line change
@@ -1,8 +1,30 @@
(defpackage :lem-markdown-mode/internal
(:use :cl)
(:export :on-save
:on-kill))
:on-kill
:on-change
:preview
:on-save-default
:on-kill-default
:on-change-default
:preview-default))
(in-package :lem-markdown-mode/internal)

(defgeneric on-save (buffer))
(defgeneric on-kill (buffer))
(defparameter *view-type* :html-buffer)

(defgeneric on-save (buffer view-type))
(defgeneric on-kill (buffer view-type))
(defgeneric on-change (buffer view-type))
(defgeneric preview (buffer view-type))

(defun on-save-default (buffer)
(on-save buffer *view-type*))

(defun on-kill-default (buffer)
(on-kill buffer *view-type*))

(defun on-change-default (buffer)
(on-change buffer *view-type*))

(defun preview-default (buffer)
(preview buffer *view-type*))
6 changes: 5 additions & 1 deletion extensions/markdown-mode/lem-markdown-mode.asd
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@
(:file "languages")
(:file "syntax-parser")
(:file "markdown-mode")
(:file "preview")))
(:module "preview"
:serial t
:components ((:file "preview")
(:file "external-browser")
(:file "html-buffer")))))
8 changes: 6 additions & 2 deletions extensions/markdown-mode/markdown-mode.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@
(variable-value 'tab-width) 4
(variable-value 'calc-indent-function) 'markdown-calc-indent)
(add-hook (variable-value 'after-save-hook :buffer (current-buffer))
'lem-markdown-mode/internal:on-save)
'lem-markdown-mode/internal:on-save-default)
(add-hook (variable-value 'kill-buffer-hook :buffer (current-buffer))
'lem-markdown-mode/internal:on-kill))
'lem-markdown-mode/internal:on-kill-default)
(add-hook (variable-value 'after-change-functions :buffer (current-buffer))
(lambda (start end old-len)
(declare (ignore end old-len))
(lem-markdown-mode/internal:on-change-default (point-buffer start)))))

(define-key *markdown-mode-keymap* "C-c C-l" 'markdown-insert-link)

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
(defpackage :lem-markdown-mode/preview
(:use #:cl
#:lem)
(defpackage :lem-markdown-mode/preview/external-browser
(:use :cl
:lem)
(:import-from :lem-markdown-mode/preview/preview
:render)
(:documentation "Live rendering of markdown documents to a browser.
This package defines the command M-x markdown-preview that opens a browser window, connects to it via a websocket, and renders and updates the markdown in the browser at each file save.
The WS connection is closed when the markdown file is closed."))

(in-package :lem-markdown-mode/preview)
(in-package :lem-markdown-mode/preview/external-browser)

(defclass server (trivial-ws:server)
((handler :initform nil
Expand All @@ -30,14 +32,9 @@ The WS connection is closed when the markdown file is closed."))

(defvar *template*
(lisp-preprocessor:compile-template
(asdf:system-relative-pathname :lem-markdown-mode "index.html")
(asdf:system-relative-pathname :lem-markdown-mode "preview/external-browser-preview.html")
:arguments '($websocket-url $body)))

(defun render (string)
(let ((3bmd-code-blocks:*code-blocks* t))
(with-output-to-string (stream)
(3bmd:parse-string-and-print-to-stream string stream))))

(defun generate-html (buffer websocket-port)
(uiop:with-temporary-file (:stream out
:pathname html-file
Expand Down Expand Up @@ -84,15 +81,18 @@ The WS connection is closed when the markdown file is closed."))
(error (e)
(log:error e)))))))

(defmethod lem-markdown-mode/internal:on-save (buffer)
(defmethod lem-markdown-mode/internal:on-save (buffer (view-type (eql :external-browser)))
(refresh buffer))

(defmethod lem-markdown-mode/internal:on-kill (buffer)
(defmethod lem-markdown-mode/internal:on-kill (buffer (view-type (eql :external-browser)))
(alexandria:when-let* ((server (buffer-server buffer))
(handler (server-handler server)))
(trivial-ws:stop handler)))

(define-command markdown-preview () ()
(defmethod lem-markdown-mode/internal:on-change (buffer (view-type (eql :external-browser)))
)

(defmethod lem-markdown-mode/internal:preview (buffer (view-type (eql :external-browser)))
"Render the markdown of the current buffer to a browser window. The preview is refreshed when the file is saved.
The connection is closed when the file is closed."
Expand Down
26 changes: 26 additions & 0 deletions extensions/markdown-mode/preview/html-buffer.lisp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
(uiop:define-package :lem-markdown-mode/preview/html-buffer
(:use :cl :lem)
(:import-from :lem-markdown-mode/preview/preview
:render))
(in-package :lem-markdown-mode/preview/html-buffer)

(defun preview-buffer-name (buffer)
(format nil
"*Markdown Preview ~A*"
(buffer-name buffer)))

(defmethod lem-markdown-mode/internal:preview (buffer (view-type (eql :html-buffer)))
(let* ((html (render (buffer-text buffer)))
(html-buffer (lem:make-html-buffer (preview-buffer-name buffer)
html)))
(pop-to-buffer html-buffer)))

(defmethod lem-markdown-mode/internal:on-save (buffer (view-type (eql :html-buffer)))
(lem-markdown-mode/internal:preview buffer :html-buffer))

(defmethod lem-markdown-mode/internal:on-kill (buffer (view-type (eql :html-buffer)))
)

(defmethod lem-markdown-mode/internal:on-change (buffer (view-type (eql :html-buffer)))
(when (get-buffer (preview-buffer-name buffer))
(lem-markdown-mode/internal:preview buffer :html-buffer)))
11 changes: 11 additions & 0 deletions extensions/markdown-mode/preview/preview.lisp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
(uiop:define-package :lem-markdown-mode/preview/preview
(:use :cl :lem))
(in-package :lem-markdown-mode/preview/preview)

(define-command markdown-preview () ()
(lem-markdown-mode/internal:preview (current-buffer) :html-buffer))

(defun render (string)
(let ((3bmd-code-blocks:*code-blocks* t))
(with-output-to-string (stream)
(3bmd:parse-string-and-print-to-stream string stream))))
3 changes: 2 additions & 1 deletion lem.asd
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@
(:file "command-advices")
(:file "interface")
(:file "highlight-line")
(:file "html-buffer")
(:file "site-init")
(:file "lem")

Expand Down Expand Up @@ -204,7 +205,7 @@
(:file "deepl")
(:file "themes")
(:file "detective")))

(:module "ui"
:serial t
:components ((:file "theme-list")))))
Expand Down
10 changes: 10 additions & 0 deletions src/html-buffer.lisp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
(in-package :lem-core)

(defclass html-buffer (text-buffer)
((html :initarg :html
:reader html-buffer-html)))

(defun make-html-buffer (buffer-name html)
(let ((buffer (make-buffer buffer-name)))
(change-class buffer 'html-buffer :html html)
buffer))
5 changes: 5 additions & 0 deletions src/internal-packages.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,11 @@
:register-formatter
:register-formatters
:format-buffer)
;; html-buffer.lisp
(:export
:html-buffer
:html-buffer-html
:make-html-buffer)
;; site-init.lisp
(:export
:*inits-directory-name*
Expand Down

0 comments on commit ffb97bd

Please sign in to comment.