-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsvTable.scm
More file actions
65 lines (52 loc) · 2.12 KB
/
csvTable.scm
File metadata and controls
65 lines (52 loc) · 2.12 KB
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
(texmacs-module (tables simple csvTable))
;; ;; string-split exists in Guile but not in Mit Scheme
;; ;; I assume I will be able to write the code for Guile without the following function
;; ;; https://codereview.stackexchange.com/questions/75172/split-string-for-r7rs
;; ;; https://codereview.stackexchange.com/a/75178 (solution of Chris Jester-Young)
;; (define (string-split str delim)
;; (define in (open-input-string str))
;; (let recur ((out (open-output-string)))
;; (define c (read-char in))
;; (cond ((eof-object? c)
;; (list (get-output-string out)))
;; ((char=? c delim)
;; (cons (get-output-string out)
;; (recur (open-output-string))))
;; (else
;; (write-char c out)
;; (recur out)))))
(define (addLineRecursive dataPort lines)
(let ((lineIn (read-line dataPort)))
(if (eof-object? lineIn)
lines
(begin
; (bkpt lines "test") ; breakpoint: http://www.cs.toronto.edu/~sheila/324/w07/assns/A3/debug.pdf
(addLineRecursive dataPort (cons lineIn lines))
))))
(define (readTable dataPort)
(reverse (addLineRecursive dataPort '())))
(define (row->Cells row)
(string-split row #\,))
(define (table->Scheme table)
(map row->Cells table))
(define (cell->TeXmacs cellContent)
`(cell ,cellContent))
(define (row->TeXmacs rowContent)
(append `(row) (map cell->TeXmacs rowContent)))
(define (table->TeXmacs tableContent)
(append `(table) (map row->TeXmacs tableContent)))
(define (file->TeXmacsTable dataPort)
(table->TeXmacs (table->Scheme (readTable dataPort))))
;; adapted example of https://ds26gte.github.io/tyscheme/index-Z-H-9.html
(tm-define (csvTable filename)
(set! filename (tree->stree filename))
(call-with-input-file filename
(lambda (dataPort)
(let* ((table (file->TeXmacsTable dataPort)))
(stree->tree `(wide-tabular ,table))))))
(tm-define (insert-csvTable filename)
(set! filename (url->system filename))
(insert (call-with-input-file filename
(lambda (dataPort)
(let* ((table (file->TeXmacsTable dataPort)))
(stree->tree `(wide-tabular ,table)))))))