03-variable-injection.md done
This commit is contained in:
95
ob-elixir.el
95
ob-elixir.el
@@ -67,6 +67,95 @@ Can be a full path or command name if in PATH."
|
||||
(with-eval-after-load 'org-src
|
||||
(add-to-list 'org-src-lang-modes '("elixir" . elixir)))
|
||||
|
||||
;;; Type Conversion
|
||||
|
||||
(defun ob-elixir--escape-string (str)
|
||||
"Escape special characters in STR for Elixir string literal."
|
||||
(let ((result str))
|
||||
;; Escape backslashes first
|
||||
(setq result (replace-regexp-in-string "\\\\" "\\\\\\\\" result))
|
||||
;; Escape double quotes
|
||||
(setq result (replace-regexp-in-string "\"" "\\\\\"" result))
|
||||
;; Escape newlines
|
||||
(setq result (replace-regexp-in-string "\n" "\\\\n" result))
|
||||
;; Escape tabs
|
||||
(setq result (replace-regexp-in-string "\t" "\\\\t" result))
|
||||
result))
|
||||
|
||||
(defun ob-elixir--elisp-to-elixir (value)
|
||||
"Convert Elisp VALUE to Elixir literal syntax.
|
||||
|
||||
Handles:
|
||||
- nil -> nil
|
||||
- t -> true
|
||||
- numbers -> numbers
|
||||
- strings -> quoted strings
|
||||
- symbols -> atoms
|
||||
- lists -> Elixir lists
|
||||
- vectors -> tuples"
|
||||
(cond
|
||||
;; nil
|
||||
((null value) "nil")
|
||||
|
||||
;; Boolean true
|
||||
((eq value t) "true")
|
||||
|
||||
;; Numbers
|
||||
((numberp value)
|
||||
(number-to-string value))
|
||||
|
||||
;; Strings
|
||||
((stringp value)
|
||||
(format "\"%s\"" (ob-elixir--escape-string value)))
|
||||
|
||||
;; Symbols become atoms (except special ones)
|
||||
((symbolp value)
|
||||
(let ((name (symbol-name value)))
|
||||
(cond
|
||||
((string= name "hline") ":hline")
|
||||
((string-match-p "^[a-z_][a-zA-Z0-9_]*[?!]?$" name)
|
||||
(format ":%s" name))
|
||||
(t (format ":\"%s\"" name)))))
|
||||
|
||||
;; Vectors become tuples
|
||||
((vectorp value)
|
||||
(format "{%s}"
|
||||
(mapconcat #'ob-elixir--elisp-to-elixir
|
||||
(append value nil) ", ")))
|
||||
|
||||
;; Lists
|
||||
((listp value)
|
||||
(format "[%s]"
|
||||
(mapconcat #'ob-elixir--elisp-to-elixir value ", ")))
|
||||
|
||||
;; Fallback
|
||||
(t (format "%S" value))))
|
||||
|
||||
;;; Variable Handling
|
||||
|
||||
(defun ob-elixir--var-name (name)
|
||||
"Convert NAME to a valid Elixir variable name.
|
||||
|
||||
Elixir variables must start with lowercase or underscore."
|
||||
(let ((str (if (symbolp name) (symbol-name name) name)))
|
||||
;; Ensure starts with lowercase or underscore
|
||||
(if (string-match-p "^[a-z_]" str)
|
||||
str
|
||||
(concat "_" str))))
|
||||
|
||||
(defun org-babel-variable-assignments:elixir (params)
|
||||
"Return list of Elixir statements assigning variables from PARAMS.
|
||||
|
||||
Each statement has the form: var_name = value"
|
||||
(mapcar
|
||||
(lambda (pair)
|
||||
(let ((name (car pair))
|
||||
(value (cdr pair)))
|
||||
(format "%s = %s"
|
||||
(ob-elixir--var-name name)
|
||||
(ob-elixir--elisp-to-elixir value))))
|
||||
(org-babel--get-vars params)))
|
||||
|
||||
;;; Execution
|
||||
|
||||
(defconst ob-elixir--value-wrapper
|
||||
@@ -115,7 +204,11 @@ PARAMS is an alist of header arguments.
|
||||
This function is called by `org-babel-execute-src-block'."
|
||||
(let* ((result-type (cdr (assq :result-type params)))
|
||||
(result-params (cdr (assq :result-params params)))
|
||||
(result (ob-elixir--execute body result-type)))
|
||||
;; Expand body with variable assignments
|
||||
(full-body (org-babel-expand-body:generic
|
||||
body params
|
||||
(org-babel-variable-assignments:elixir params)))
|
||||
(result (ob-elixir--execute full-body result-type)))
|
||||
(org-babel-reassemble-table
|
||||
(org-babel-result-cond result-params
|
||||
result
|
||||
|
||||
Reference in New Issue
Block a user