ruricolist / spinneret

Common Lisp HTML5 generator
MIT License
369 stars 26 forks source link

How to pass variables to with-html-string? #40

Closed farooqkz closed 4 years ago

farooqkz commented 4 years ago

I'm not sure how to use it:

* (ql:quickload :cl-html-parse)
To load "cl-html-parse":
  Load 1 ASDF system:
    cl-html-parse
; Loading "cl-html-parse"

(:CL-HTML-PARSE)
* (ql:quickload :spinneret)
To load "spinneret":
  Load 1 ASDF system:
    spinneret
; Loading "spinneret"
...
Switching to the BALLAND2006 optimizer

(:SPINNERET)
* (setf a (html-parse:parse-html "<p>H<sup>ello</sup><sub>i</sub></p>"))
; in: SETF A
;     (SETF A (NET.HTML.PARSER:PARSE-HTML "<p>H<sup>ello</sup><sub>i</sub></p>"))
; ==>
;   (SETQ A (NET.HTML.PARSER:PARSE-HTML "<p>H<sup>ello</sup><sub>i</sub></p>"))
; 
; caught WARNING:
;   undefined variable: A
; 
; compilation unit finished
;   Undefined variable:
;     A
;   caught 1 WARNING condition

((:P "H" (:SUP "ello") (:SUB "i")))
* (setf a (first a))
; in: SETF A
;     (SETF A (FIRST A))
; ==>
;   (SETQ A (FIRST A))
; 
; caught WARNING:
;   undefined variable: A
; 
; compilation unit finished
;   Undefined variable:
;     A
;   caught 1 WARNING condition

(:P "H" (:SUP "ello") (:SUB "i"))
* (spinneret:with-html-string a)
; in: SPINNERET:WITH-HTML-STRING A
;     (SPINNERET:WITH-HTML
;       A)
; ==>
;   (LET ((SPINNERET:*HTML* (SPINNERET::ENSURE-HTML-STREAM SPINNERET:*HTML*)))
;     A)
; 
; caught WARNING:
;   undefined variable: A
; 
; compilation unit finished
;   Undefined variable:
;     A
;   caught 1 WARNING condition

""
* (spinneret:with-html-string (:P "H" (:SUP "ello") (:SUB "i")))

"<p>H<sup>ello</sup><sub>i</sub>"

As you see (with-html-string a) does not work as expected. Thanks

Edit0: The problem's not with the warnings. The problem's that (with-html-string a) does not return what (with-html-string (:P "H" (:SUP "ello) (:SUB "i"))) returns

ruricolist commented 4 years ago

Normally one uses setf to set the value of a variable that has already been defined using defparameter. That's why you're seeing so many warnings.

farooqkz commented 4 years ago

The problem's not warnings. The problem's that (with-html-string a) returns empty string rather than the generated string of html tags.

farooqkz commented 4 years ago

@ruricolist Hello?

ruricolist commented 4 years ago

@ruricolist Hello?

I haven't forgotten you, but I haven't had a chance to look into this yet.

farooqkz commented 4 years ago

Okay. Look into it whenever you've got some free time, whenever you want :)

ruricolist commented 4 years ago

The short answer is that to do what you want, you need to preface the variable with #.:

(spinneret:with-html-string #.a)

The long answer is that with-html-string is a macro, not a function. It sees its arguments before, not after, they're evaluated.

Try it for yourself:

(defmacro my-quote (arg)
  (print arg))
(my-quote a) ;; Prints A

Evaluation in Lisp happens in phases: first code is read, then it's macro-expanded, and then it's evaluated. If you want a macro to see the value of a variable, you have to move it into an earlier phase. That's what #. does: it evaluates at read time.

(my-quote #.a) 
(:P "H" (:SUP "ello") (:SUB "i"))
farooqkz commented 4 years ago

Thank you very much for solving my problem and for teaching me CL. So I close this now.