Skip to content

Commit

Permalink
spinneret-tags(:ncode): Separate printed code from htmlized one.
Browse files Browse the repository at this point in the history
This way, copying, RELP-editing, and config appending use the printed code,
while display is the HTMLized one.
  • Loading branch information
aartaka committed Jan 19, 2023
1 parent f29a785 commit 201d59a
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions source/spinneret-tags.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -243,41 +243,45 @@ code is at the very least readable.
Forms in BODY can be quoted, in which case Spinneret won't even try to look at
its contents (useful if there are forms that start with a keyword, Spinneret
unconditionally converts those to tags unless the whole form is quoted.)"
(labels ((prini-listing (form)
(let ((listing (nyxt:prini-to-string form :readably t :right-margin 70))
(*suppress-inserted-spaces* t)
(labels ((prini* (object)
(nyxt:prini-to-string object :readably t :right-margin 70 :package package))
(htmlize-body (form listing)
(let ((*suppress-inserted-spaces* t)
(*html-style* :tree)
(*print-pretty* nil))
(when (listp form)
(multiple-value-bind (functions variables linkable-strings)
(resolve-linkable-symbols form)
(dolist (function functions)
(let ((fun-listing (prini-listing function)))
(let ((fun-listing (prini* function)))
(setf listing (str:replace-all
(str:concat "(" fun-listing)
(str:concat
"(" (with-html-string
(:nxref :function function fun-listing)))
listing))))
(dolist (var variables)
(let ((var-listing (prini-listing var)))
(let ((var-listing (prini* var)))
(setf listing (str:replace-all var-listing
(with-html-string
(:nxref :variable var var-listing))
listing))))
(dolist (string linkable-strings)
(setf listing (str:replace-all (prini-listing string)
(prini-listing
(setf listing (str:replace-all (prini* string)
(prini*
(nyxt:resolve-backtick-quote-links string package))
listing)))))
listing)))
(let* ((code (if literal-p
(let* ((printed-body
(mapcar #'prini* (mapcar #'remove-smart-quoting body)))
(code (if literal-p
(first body)
(let ((*package* (find-package package)))
(serapeum:mapconcat
#'prini-listing
(mapcar #'remove-smart-quoting body)
(make-string 2 :initial-element #\newline)))))
(str:join (make-string 2 :initial-element #\newline) printed-body)))
(htmlized-code
(unless literal-p
(str:join
(make-string 2 :initial-element #\newline)
(mapcar #'htmlize-body body printed-body))))
(*print-escape* nil)
(id (nyxt:prini-to-string (gensym)))
(select-code
Expand Down Expand Up @@ -321,7 +325,7 @@ unconditionally converts those to tags unless the whole form is quoted.)"
(injectable-code
(if literal-p
`(the string ,code)
`(:raw (the string ,code)))))
`(:raw (the string ,htmlized-code)))))
(if inline-p
`(:span (:code ,@attrs ,injectable-code) ,select-code)
;; https://spdevuk.com/how-to-create-code-copy-button/
Expand Down

0 comments on commit 201d59a

Please sign in to comment.