2017-12-21 9 views
2

인라인 자바 스크립트를 생성하려고하는데, parenscript 코드를 (:script)(str) 태그 안에 cl-who를 사용해야합니다. ps, ps*, ps-inlineps-inline*은 생성 된 js와별로 차이가없는 것 같습니다.cl-who, parenscript 및 hunchentoot을 사용하여 인라인 자바 스크립트 생성하기

코드 중복을 피하기 위해 매크로를 작성하는 일반적인 방법입니까 아니면 더 좋은 방법입니까?

(in-package #:ps-test) 

(defmacro standard-page ((&key title) &body body) 
    `(with-html-output-to-string (*standard-output* nil :prologue t :indent t) 
    (:html 
     :lang "en" 
     (:head 
     (:meta :http-equiv "Content-Type" 
      :content "text/html;charset=utf-8") 
     (:title ,title) 
      (:link :type "text/css" 
       :rel "stylesheet" 
       :href "/style.css")) 
     (:body 
     ,@body))))  

(defun main() 
    (with-html-output (*standard-output* nil :indent t :prologue nil) 
    (standard-page (:title "Parenscript test") 
     (:div (str "Hello worldzors")) 
     (:script :type "text/javascript" 
      (str (ps (alert "Hello world as well"))))))) 

(define-easy-handler (docroot :uri "/")() 
    (main)) 

(defun start-ps-test() 
    (setf (html-mode) :html5) 
    (setf *js-string-delimiter* #\") 
    (start (make-instance 'hunchentoot:easy-acceptor :port 8080))) 

(defun stop-ps-test() 
    (stop *server*)) 

(defvar *server* (start-ps-test)) 

답변

2

매크로이 사용 사례에서 잘 수 있습니다

여기 내 프로그램입니다. 트릭은 매크로가 특정 순서로 확장된다는 것입니다. 사용자가 정의한 말하는 js 매크로 : macroexpansion이 매크로에 with-html-output, 내부 전화를 발견 한 경우 (js (alert "Ho Ho Ho")) 함수 호출처럼 보이는, 그리고 생성 된 코드에 그대로 남아 있습니다. js 매크로 다음에(:script ...)으로 확장되면 은 실제로 그런 함수 이름을 지정하지 않았다고 가정하면 :script은 알 수없는 함수입니다. 코드 워커를 사용하여 코드를 해석하려면 (who:htm ...) 표현을 포함하는 을 내야합니다.

(defmacro js (code) 
    `(who:htm 
    (:script :type "text/javascript" (who:str (ps:ps ,code))))) 

이것은 단지 with-html-output의 컨텍스트에서 작동합니다.

인라인 자바 스크립트의 경우, 주위 을 <script> 태그를 싶지 않아 당신은 일반적으로 단순히 ps-inline 사용할 수 있습니다

(who:with-html-output (*standard-output*) 
    (:a :href (ps:ps-inline (void 0)) 
    "A link where the usual HREF behavior is canceled.")) 

;; prints: 
;; 
;; <a href='javascript:void(0)'>A link where the usual HREF behavior is canceled.</a> 

을하지만 종종 같은 행동을 할 경우 매크로를 사용하여 주시기 바랍니다 물건 :

(defmacro link (&body body) 
    `(who:htm (:a :href #.(ps:ps-inline (void 0)) ,@body))) 

(who:with-html-output (*standard-output*) (link "Link")) 

;; prints: 
;; 
;; <a href='javascript:void(0)'>Link</a> 
관련 문제