forked from robert-dodier/maxima-read-wxmx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
maxima-read-wxmx.lisp
61 lines (52 loc) · 2.01 KB
/
maxima-read-wxmx.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
;; maxima_read_wxmx.lisp -- load Maxima code from wxmx file
;; copyright 2016 Robert Dodier
;; I release this work under terms of the GNU General Public License
;; Quick-lisp-less version [uncompressed wxmx only] by Gunter Königsmann in 2018
(defun $load_stream (in)
(catch 'macsyma-quit (continue in t)))
;; Convert wxmx back to ASCII
(defun wxmx-unquote-xml (x)
(let* ((tmp-x (string-substitute #\& "&" x))
(tmp-x (string-substitute #\< "<" tmp-x))
(tmp-x (string-substitute #\> ">" tmp-x))
(tmp-x (string-substitute #\Return " " tmp-x))
(tmp-x (string-substitute #\Linefeed " " tmp-x))
(tmp-x (string-substitute #\Newline " " tmp-x)))))
;; returns a string containing the Maxima code from the content.xml entry in FILE-NAME-WXMX
;;
;; Todo: An error message in the case that no<wxMaximaDocument and no </wxMaximaDocument>
;; are found.
(defun $read_content_xml (filename)
(with-open-file (stream filename)
;; Skip to the first line of the wxmx file's contents
(loop for line = (read-line stream nil)
while (and
line
(not
(search "<wxMaximaDocument" line))))
;; Extract all lines containing a <line> and a </line> tag.
(loop for line = (read-line stream nil)
while (and
line
(not
(search "</wxMaximaDocument>" line)))
do (
let (
(linestart (search "<line>" line))
(lineend (search "</line>" line))
)
(if (and linestart lineend)
(collect
(wxmx-unquote-xml
(subseq line
(+ linestart 6)
(- lineend 0)))))))))
(defun $load_wxmx (file-name-wxmx)
($load_stream (make-string-input-stream ($read_content_xml file-name-wxmx))))
(defun $parse_content_xml (file-name-wxmx)
(declare (special *mread-prompt*))
(let*
((content-string ($read_content_xml file-name-wxmx))
(input-stream (make-string-input-stream content-string))
(*mread-prompt*))
(cons '(mlist) (loop for x = (mread input-stream) while x collect (third x)))))