Removed compile-time constant string concatenation from the Parenscript printer,...
[clinton/parenscript.git] / docs / reference.lisp
index 3865afb..8ead719 100644 (file)
@@ -130,9 +130,10 @@ WHEN WHILE WITH WITH-SLOTS
 
 "bratzel bub" => 'bratzel bub'
 
-;;; Escapes in Lisp are not converted to JavaScript escapes. However,
-;;; to avoid having to use double backslashes when constructing a
-;;; string, you can use the CL-INTERPOL library by Edi Weitz.
+;;; Special characters such as newline and backspace are converted
+;;; into their corresponding JavaScript escape sequences.
+
+"      "   => '\\t'
 
 ;;;## Array literals
 ;;;t \index{array}
@@ -335,22 +336,21 @@ a-variable  => aVariable
 (foobar (blorg 1 2) (blabla 3 4) (array 2 3 4))
 => foobar(blorg(1, 2), blabla(3, 4), [ 2, 3, 4 ])
 
+((slot-value this 'blorg) 1 2) => this.blorg(1, 2)
+
 ((aref foo i) 1 2) => foo[i](1, 2)
 
-;;; A method call is a function call where the function name is a
-;;; symbol and begins with a "." . In a method call, the name of the
-;;; function is append to its first argument, thus reflecting the
-;;; method call syntax of JavaScript. Please note that most method
-;;; calls can be abbreviated using the "." trick in symbol names (see
-;;; "Symbol Conversion" above).
+((slot-value (aref foobar 1) 'blorg) NIL T) => foobar[1].blorg(null, true)
 
-(.blorg this 1 2) => this.blorg(1, 2)
+;;; Note that while most method calls can be abbreviated using the "."
+;;; trick in symbol names (see "Symbol Conversion" above), this is not
+;;; advised due to the fact that "object.function" is treated as a
+;;; symbol distinct from both "object" and "function," which will
+;;; cause problems if Parenscript package prefixes or package
+;;; obfuscation is used.
 
 (this.blorg 1 2) => this.blorg(1, 2)
 
-(.blorg (aref foobar 1) NIL T)
-=> foobar[1].blorg(null, true)
-
 ;;;# Operator Expressions
 ;;;t \index{operator}
 ;;;t \index{operator expression}
@@ -1083,8 +1083,8 @@ a-variable  => aVariable
 
 (document.write
   (ps-html ((:a :href "#"
-                :onclick (lisp (ps-inline (transport)))) "link")))
-=> document.write('<A HREF=\"#\" ONCLICK=\"' + 'javascript:transport()' + '\">link</A>')
+                :onclick (ps-inline (transport))) "link")))
+=> document.write('<A HREF=\"#\" ONCLICK=\"' + ('javascript:' + 'transport' + '(' + ')') + '\">link</A>')
 
 ;;; Forms may be used in attribute lists to conditionally generate
 ;;; the next attribute. In this example the textarea is sometimes disabled.
@@ -1213,11 +1213,11 @@ a-variable  => aVariable
 ;;; a particular package receive a prefix when translated to
 ;;; JavaScript with the `PS-PACKAGE-PREFIX' place.
 
-(defpackage "MY-LIBRARY"
-  (:use #:parenscript))
-(setf (ps-package-prefix :my-library) "my_library_")
+(defpackage "PS-REF.MY-LIBRARY"
+  (:use "PARENSCRIPT"))
+(setf (ps-package-prefix "PS-REF.MY-LIBRARY") "my_library_")
 
-(defun my-library::library-function (x y)
+(defun ps-ref.my-library::library-function (x y)
   (return (+ x y)))
   -> function my_library_libraryFunction(x, y) {
         return x + y;
@@ -1229,18 +1229,30 @@ a-variable  => aVariable
 ;;;t \index{OBFUSCATE-PACKAGE}
 ;;;t \index{UNOBFUSCATE-PACKAGE}
 
-; (OBFUSCATE-PACKAGE package-designator)
+; (OBFUSCATE-PACKAGE package-designator &optional symbol-map)
 ; (UNOBFUSCATE-PACKAGE package-designator)
 
 ;;; Similar to the namespace mechanism, Parenscript provides a
-;;; facility to generate obfuscated identifiers in certain Lisp
-;;; packages.
-
-(defpackage "OBFUSCATE-ME")
-(obfuscate-package :obfuscate-me)
-
-(defun obfuscate-me::library-function2 (a b obfuscate-me::foo)
-  (+ a (my-library::library-function b obfuscate-me::foo)))
+;;; facility to generate obfuscated identifiers in specified CL
+;;; packages. The function `OBFUSCATE-PACKAGE' may optionally be
+;;; passed a hash-table or a closure that maps symbols to their
+;;; obfuscated counterparts. By default, the mapping is done using
+;;; `PS-GENSYM'.
+
+(defpackage "PS-REF.OBFUSCATE-ME")
+(obfuscate-package "PS-REF.OBFUSCATE-ME"
+  (let ((code-pt-counter #x8CF0)
+        (symbol-map (make-hash-table)))
+    (lambda (symbol)
+      (or (gethash symbol symbol-map)
+          (setf (gethash symbol symbol-map)
+                (make-symbol (string (code-char (incf code-pt-counter)))))))))
+
+(defun ps-ref.obfuscate-me::a-function (a b ps-ref.obfuscate-me::foo)
+  (+ a (ps-ref.my-library::library-function b ps-ref.obfuscate-me::foo)))
+  -> function 賱(a, b, 賲) {
+       a + my_library_libraryFunction(b, 賲);
+     }
 
 ;;; The obfuscation and namespace facilities can be used on packages
 ;;; at the same time.