-
Notifications
You must be signed in to change notification settings - Fork 0
/
clisptex.lisp
98 lines (83 loc) · 2.74 KB
/
clisptex.lisp
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
;;;; clisptex.lisp
(in-package #:clisptex)
;;; basic facilites to process "tex-s-expressions"
(defun tex-to-string (input)
(cond
((null input) nil)
((listp input) (concatenate 'string (tex-to-string (car input)) (tex-to-string (cdr input))))
((stringp input) input)
;; ((not (null input)) (format nil "~(~A~)" input))))
((not (null input)) (format nil "~(~A~)" input))))
(defun tex-eval (content)
(cond
;;"base cases"
((null content) nil)
((numberp content) content)
((atom content) (tex-to-string content))
;;"special forms"
((and (listp content) (eq (car content) 'list)) (apply #'list
(mapcar #'tex-eval
(cdr content))))
((and (listp content) (eq (car content) 'group)) (apply #'concat-space
(mapcar #'tex-eval
(cdr content))))
;;"handle nested lists"
;; ((listp content) (apply (car content) (remove nil (tex-expand (cdr content)))))))
((listp content) (apply (car content) (tex-expand (cdr content))))))
(defun test-fn (&optional arg1 arg2)
(if arg1 "arg1"
"no arg1"))
(defun tex-expand (content-list)
(when content-list (cons (tex-eval (car content-list)) (tex-expand (cdr content-list)))))
(defun tex-eval-list (content)
(if (listp (car content))
(extract-list-of-strings (mapcar #'tex-eval content))
(tex-eval content)))
(defun list-if-not-already (input)
"writes input into a lisp unless it is already a list"
(if (listp input)
input
(list input)))
(defun extract-list-of-strings (string-list &optional pre post)
"extract a list of strings and frame each item by pre and post"
(cond ((not (null string-list))
(concatenate 'string
(tex-to-string pre)
(tex-to-string (car string-list))
(tex-to-string post)
(extract-list-of-strings (cdr string-list) pre post)))))
(defun concat-space (&rest input)
(if (and input (car input))
(concatenate 'string
(tex-to-string (car input))
" "
(apply #'concat-space (cdr input)))
""))
(defun repeat-string (string count)
(when (> count 0)
(concat-space
(tex-to-string string)
(repeat-string string (- count 1)))))
(defun wrap (inner deliml delimr)
(concatenate 'string (tex-to-string deliml)
(tex-to-string inner)
(tex-to-string delimr)))
(defun wrap-parens (&rest inner)
(wrap (concat-space inner)
(tex-cmd "left(")
(tex-cmd "right)")))
(defun wrap-braces (&rest inner)
(wrap (concat-space inner)
"{"
"}"))
(defun wrap-brackets (&rest inner)
(wrap (concat-space inner)
(tex-cmd "left[")
(tex-cmd "right]")))
(defun write-tex-file (filename content)
"write content to file"
(with-open-file (output filename
:direction :output
:if-exists :supersede
:if-does-not-exist :create)
(format output "~a~%" content)))