-
Notifications
You must be signed in to change notification settings - Fork 3
/
ob-hurl.el
100 lines (92 loc) · 3.72 KB
/
ob-hurl.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
;;; ob-hurl.el --- Org babel integration for hurl -*- lexical-binding: t; -*-
;;
;; Copyright (C) 2023 Jason Zhen
;;
;; Author: Jason Zhen
;; Maintainer: Jason Zhen
;; Created: December 05, 2023
;; Version: 0.0.1
;; Package-Requires: ((emacs "26.1"))
;; URL: https://github.com/JasZhe/hurl-mode
;;
;; This file is not part of GNU Emacs.
;;
;;; License:
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;
;;; Commentary:
;; Very simple org babel integration for hurl mode
;; a lot taken from the ob-template
;;; Code:
(require 'ob)
(require 'ob-ref)
(require 'ob-comint)
(require 'ob-eval)
;; optionally define a file extension for this language
(add-to-list 'org-babel-tangle-lang-exts '("hurl" . "tmp"))
;; optionally declare default header arguments for this language
(defvar org-babel-default-header-args:hurl '())
;; This function expands the body of a source code block by doing things like
;; prepending argument definitions to the body, it should be called by the
;; `org-babel-execute:hurl' function below. Variables get concatenated in
;; the `mapconcat' form, therefore to change the formatting you can edit the
;; `format' form.
(defun org-babel-expand-body:hurl (body params &optional processed-params)
"Expand BODY according to PARAMS, return the expanded body."
(require 'inf-hurl nil t)
(let ((vars (org-babel--get-vars (or processed-params (org-babel-process-params params)))))
(concat
(mapconcat ;; define any variables
(lambda (pair)
(format "%s=%S"
(car pair) (org-babel-hurl-var-to-hurl (cdr pair))))
vars "\n")
"\n" body "\n")))
;; This is the main function which is called to evaluate a code
;; block.
;;
;; This function will evaluate the body of the source code and
;; return the results as emacs-lisp depending on the value of the
;; :results header argument
;; - output means that the output to STDOUT will be captured and
;; returned
;; - value means that the value of the last statement in the
;; source code block will be returned
;;;###autoload
(defun org-babel-execute:hurl (body params)
"Execute a block of Hurl code with org-babel.
Any variables assigned to the src block get passed into the cli command via the --variable option.
This function is called by `org-babel-execute-src-block'"
(message "executing Hurl source code block")
(let* ((processed-params (org-babel-process-params params))
;; variables assigned for use in the block
(vars (org-babel--get-vars processed-params))
(in-file (org-babel-temp-file "hurl" ".hurl"))
(hurl-vars (cl-reduce
(lambda (acc elem)
(concat acc (format "--variable %s=%s" (car elem) (cdr elem)) " "))
vars :initial-value "")))
(with-temp-file in-file
(insert body))
(org-babel-eval
(format "hurl %s %s" hurl-vars (org-babel-process-file-name in-file))
"")))
(defun org-babel-hurl-var-to-hurl (var)
"Convert an elisp var into a string of hurl source code
specifying a var of the same value."
(format "%S" var))
(provide 'ob-hurl)
;;; ob-hurl.el ends here