emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] externals/dash 3cfbebf 293/316: Port more Texinfo generation chan


From: ELPA Syncer
Subject: [elpa] externals/dash 3cfbebf 293/316: Port more Texinfo generation changes to Markdown
Date: Mon, 15 Feb 2021 15:58:19 -0500 (EST)

branch: externals/dash
commit 3cfbebfe3eb1007564f4a972169194455c3f2d91
Author: Basil L. Contovounesios <contovob@tcd.ie>
Commit: Basil L. Contovounesios <contovob@tcd.ie>

    Port more Texinfo generation changes to Markdown
    
    * dev/examples-to-docs.el (create-docs-file): Move 'nil -> '()
    replacement from here...
    (dash--print-lisp-as-md): ...to this new function.
    (example-to-string): Use it to print Lisp in a buffer rather than
    manipulating strings.
    (defexamples): Move example-to-string call from here...
    (function-to-md): ...to here, where it's needed.  Replace s-replace
    with more correct code.
    (s-replace): Remove; no longer used.
    
    * README.md: Regenerate.
---
 README.md               |  2 +-
 dev/examples-to-docs.el | 50 ++++++++++++++++++++++++++-----------------------
 2 files changed, 28 insertions(+), 24 deletions(-)

diff --git a/README.md b/README.md
index 3fa054a..52de5e8 100644
--- a/README.md
+++ b/README.md
@@ -2868,7 +2868,7 @@ See `srfi-26` for detailed description.
 ```el
 (funcall (-cut list 1 <> 3 <> 5) 2 4) ;; => '(1 2 3 4 5)
 (-map (-cut funcall <> 5) `(1+ 1- ,(lambda (x) (/ 1.0 x)))) ;; => '(6 4 0.2)
-(-map (-cut <> 1 2 3) '(list vector string)) ;; => '((1 2 3) [1 2 3] 
"\^A\^B\^C")
+(-map (-cut <> 1 2 3) '(list vector string)) ;; => '((1 2 3) [1 2 3] "\1\2\3")
 ```
 
 #### -not `(pred)`
diff --git a/dev/examples-to-docs.el b/dev/examples-to-docs.el
index 430b46f..8beeddc 100644
--- a/dev/examples-to-docs.el
+++ b/dev/examples-to-docs.el
@@ -28,20 +28,29 @@
 
 (defvar functions ())
 
+(defun dash--print-lisp-as-md (obj)
+  "Print Lisp OBJ suitably for Markdown."
+  (let ((print-quoted t)
+        (print-escape-control-characters t))
+    (save-excursion (prin1 obj)))
+  (while (re-search-forward
+          (rx (| (group ?\' symbol-start "nil" symbol-end) "\\?")) nil 'move)
+    ;; 'nil -> '(), `-any\?' -> `-any?'.
+    (replace-match (if (match-beginning 1) "'()" "?") t t)))
+
 (defun example-to-string (example)
-  (-let (((actual sym expected) example)
-         (print-quoted t))
-    (--> (cond
-          ((eq sym '=>) (format "=> %S" expected))
-          ((eq sym '~>) (format "~> %S" expected))
-          ((eq sym '!!>) "Error")
-          ((error "Invalid test case: %S" example)))
-      (format "%S ;; %s" actual it)
-      (replace-regexp-in-string "\\\\\\?" "?" it t t)
-      (replace-regexp-in-string
-       "[^\n[:print:]]"
-       (lambda (s) (concat "\\" (text-char-description (string-to-char s))))
-       it t t))))
+  (pcase-let ((`(,actual ,sym ,expected) example)
+              (print-quoted t))
+    (with-output-to-string
+      (with-current-buffer standard-output
+        (dash--print-lisp-as-md actual)
+        (insert " ;; ")
+        (cond ((memq sym '(=> ~>))
+               (princ sym)
+               (insert ?\s)
+               (dash--print-lisp-as-md expected))
+              ((eq sym '!!>) (insert "Error"))
+              ((error "Invalid test case: %S" example)))))))
 
 (defun dash--describe (fn)
   "Return the (ARGLIST DOCSTRING) of FN symbol.
@@ -62,8 +71,7 @@ Based on `describe-function-1'."
 (defmacro defexamples (cmd &rest examples)
   `(push (cons ',cmd
                (nconc (dash--describe ',cmd)
-                      (list (mapcar #'example-to-string
-                                    (-partition 3 ',examples)))))
+                      (list (-partition 3 ',examples))))
          functions))
 
 (defmacro def-example-group (group desc &rest examples)
@@ -128,13 +136,14 @@ Based on `describe-function-1'."
 
 (defun function-to-md (function)
   (if (stringp function)
-      (concat "\n" (s-replace "### " "## " function) "\n")
+      (concat "\n" (replace-regexp-in-string (rx bos "### ") "## " function)
+              "\n")
     (-let [(command-name signature docstring examples) function]
       (format "#### %s `%s`\n\n%s\n\n```el\n%s\n```\n"
               command-name
               signature
               (dash--format-docstring docstring)
-              (mapconcat #'identity (-take 3 examples) "\n")))))
+              (mapconcat #'example-to-string (-take 3 examples) "\n")))))
 
 (defun docs--chop-prefix (prefix s)
   "Remove PREFIX if it is at the start of S."
@@ -159,10 +168,6 @@ Based on `describe-function-1'."
                                                    "!"
                                                    (format "%S %S" 
command-name signature)))))
 
-(defun s-replace (old new s)
-  "Replace OLD with NEW in S."
-  (replace-regexp-in-string (regexp-quote old) new s t t))
-
 (defun function-summary (function)
   (if (stringp function)
       (concat "\n" function "\n")
@@ -186,7 +191,6 @@ Based on `describe-function-1'."
       (dash--replace-all "[[ function-list ]]"
                          (mapconcat #'function-summary functions "\n"))
       (dash--replace-all "[[ function-docs ]]"
-                         (mapconcat #'function-to-md functions "\n"))
-      (dash--replace-all "'nil" "'()"))))
+                         (mapconcat #'function-to-md functions "\n")))))
 
 ;;; examples-to-docs.el ends here



reply via email to

[Prev in Thread] Current Thread [Next in Thread]