-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcj-sxml-serializer.scm
744 lines (623 loc) · 22.1 KB
/
cj-sxml-serializer.scm
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
;;; Copyright 2005-2017 by Christian Jaeger <[email protected]>
;;; This file is free software; you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License (GPL) as published
;;; by the Free Software Foundation, either version 2 of the License, or
;;; (at your option) any later version.
;; BUGS: The non-fast non-pretty versions (with the line-wrapping
;; inside tags) give blank page in chromium (related to js?).
;; ** NOTE: this version just includes all dependencies so that it
;; does not require chjmodule **. It's not such a good idea modifying
;; code here. But if you need to do anyway, still feed the changes
;; upstream to Christian please.
;; http://okmij.org/ftp/Scheme/xml.html
;; https://en.wikipedia.org/wiki/SXML
(require easy
(srfi-1 last-pair append!)
cj-env ;; warn, for debgging ah and now thunk
(cj-sxml sxml-element? sxml-begin? sxml-begin
with-sxml-element-attributes/else
sxml-element-attribute-ref
sxml-element:add-attributes-unless-present)
(stream stream-for-each)
(cj-gambit-sys-0 @vector-ref)
(test TEST)
cj-warn
srfi-1-macros
debuggable-promise)
(export sxml>>html-fast
sxml>>xhtml-fast
sxml>>xml-fast
;; drop the -fast here, take those as the normal ones, OK?
(method sxml.print-xhtml
sxml.print-html
sxml.print-xml)
;; XX clean these up and not actually use as such?
sxml>>xml-file
sxml>>fast-xml-file
sxml>>xhtml-file
sxml>>fast-xhtml-file
sxml>>html-file
sxml>>fast-html-file
sxml>>pretty-xml-file
sxml>>pretty-xhtml-file
sxml>>xhtml
sxml>>html
sxml>>xml
sxml->html-string-fragment
sxml->xml-string-fragment
(method sxml.xml-string-fragment
sxml.html-string-fragment)
sxml->xml-string
sxml->html-string
sxml->xhtml-string
(method sxml-element.xml-string
sxml-element.html-string
sxml-element.xhtml-string))
(include "cj-standarddeclares.scm")
(possibly-use-debuggable-promise)
(include "gambit-io-fast--include.scm")
(declare (not safe)) ;; XXX remove again!!!
(def unbound 'unbound)
(define-macro (%with-sxml-element/else elt
cont-name-attrs-body
cont-else)
`(if (pair? ,elt)
(let ((maybe-name (car ,elt)))
(if (symbol? maybe-name)
(let ((has-attrs (lambda (attrs rest)
(,cont-name-attrs-body maybe-name attrs rest)))
(no-attrs (lambda (rest)
(,cont-name-attrs-body maybe-name '(@) rest))))
(let ((maybe-2nd (cdr ,elt)))
(if (pair? maybe-2nd)
(let ((2nd-val (car maybe-2nd)))
(if (and (pair? 2nd-val)
(eq? (car 2nd-val)
'@))
(has-attrs 2nd-val
(cdr maybe-2nd))
(no-attrs maybe-2nd)))
(no-attrs maybe-2nd))))
(,cont-else)))
(,cont-else)))
(define-macro (%with-sxml-element elt cont-name-attrs-body)
`(%with-sxml-element/else ,elt
,cont-name-attrs-body
(lambda ()
(error "not an sxml element:" ,elt))))
(define-macro (%with-sxml-element-attributes/else element yes no)
`(%with-sxml-element ,element
(lambda (name attrs body)
(let ((alis (cdr attrs)))
(if (pair? alis)
(,yes attrs)
(,no))))))
(def (sxml-element-attribute-ref element #(symbol? attrname)
#!optional (missing unbound))
(%with-sxml-element-attributes/else
element
(lambda (attrs)
(cond ((assoc attrname (cdr attrs))
=> cdr)
(else #f)))
(lambda ()
(if (eq? missing unbound)
(error "missing sxml-attribute:" attrname)
missing))))
(def (maybe-sxml-element-attribute-alist element)
(%with-sxml-element-attributes/else element
cdr
(lambda ()
#f)))
(def (sxml-element:add-attributes-unless-present element alis)
(let ((newattrs
(cons '@
(cond ((maybe-sxml-element-attribute-alist element)
=> (lambda (present-alis)
(let lp ((alis alis)
(prepend '()))
(if (null? alis)
(begin
;;(step)
(append! prepend present-alis))
(let* ((p (car alis))
(newkey (car p)))
(if (assoc newkey present-alis)
(lp (cdr alis)
prepend)
(lp (cdr alis)
(cons p prepend))))))))
(else
alis)))))
;; now check (todo: make namespaceetc aware!..) if attributes
;; are already present
(let* ((nam (car element))
(r1 (cdr element))
(possibly-oldattrs (and (pair? r1)
(car r1)))
(have-oldattrs (and (pair? possibly-oldattrs)
(eq? '@ (car possibly-oldattrs))))
(body (if have-oldattrs
(cdr r1)
r1)))
(cons nam (cons newattrs body)))))
;; ----------------------------------------------------------------------
(def (@sxml-attributes l)
;; l must be an sxml-element; returns the *rest* of the attribute
;; list, or #f
(let ((2ndpair (##cdr l)))
(if (pair? 2ndpair)
(let ((2nd (##car 2ndpair)))
(if (and (pair? 2nd)
(eq? (##car 2nd) '@))
(##cdr 2nd)
#f))
#f)))
(def @string>> ##write-string)
(def (@char>> c port)
(macro-write-char-neverlock c port))
(def (@symbol>> sym port)
(@string>> (##symbol->string sym) port))
(def @car ##car)
(def @cdr ##cdr)
(def @cddr ##cddr)
(def cj-sxml-serializer:atom>>
(lambda (val key xml? port)
(and val
(begin
(@char>> #\space port)
(@symbol>> key port)
(@char>> #\= port)
(@char>> #\" port)
(let ()
(define-macro (piece>> val)
`(atom>>htmlquoted ,val port #f xml? #t))
(cond ((sxml-begin? val)
(%for-each piece>> (cdr val)))
((pair? val)
(%for-each piece>> val))
(else
(piece>> val))))
(@char>> #\" port)))))
(def (@attrlist>> attrlist port xml?)
(cond ((pair? attrlist)
(let ((d (@car attrlist)))
(when (pair? d)
(let ((key (@car d))
(val (@cdr d)))
(if (symbol? key)
(cond ((pair? val)
(cj-sxml-serializer:atom>> (@car val) key xml? port))
((null? val)
;; empty attribute value. ok for html.
(if xml?
(error "missing value for attribute:" key)
(begin
(@char>> #\space port)
(@symbol>> key port))))
(else
(cj-sxml-serializer:atom>> val key xml? port)))
;; error; or treat the whole thing like elements,
;; e.g. accept listwraps? :
(error "attribute key is not a symbol:" key)))
;; else something is strange.
;;(error "attrlist-display: invalid attrlist:" attrlist)))
;; no, accept it, like in '(img (@ (src
;; "http://www.ethlife.ethz.ch/pix/favicon.png") (align
;; "absbottom") (alt "") #f (border 0)))
))
(@attrlist>> (@cdr attrlist) port xml?))
((null? attrlist))
(else
(error "@attrlist>>: not a list:" attrlist))))
(TEST
> (sxml->html-string-fragment '(p (@ (title (##begin 123 4 5 56)))))
"<p title=\"1234556\">")
(def (@string>>htmlquoted str port count-chars? in-attributes?)
(let ((len (##string-length str)))
(let loop ((pos 0))
(when (##fixnum.< pos len)
(begin
(@char>>htmlquoted (##string-ref str pos)
port count-chars? in-attributes?)
(loop (##fixnum.+ pos 1)))))))
(def (html-whitespace? char)
(case char
((#\space #\newline #\return #\tab #\page) #t)
(else #f)))
(def (html-hyphenationchar? char)
(case char
((#\- #\~ ;; todo weitere.
) #t)
(else #f)))
;; hmm. basically everything that's not a word char is a word break.
;; XX proper unicode handling?...
(def (html-wordchar? char)
(or (and (char>=? char #\a)
(char<=? char #\z))
(and (char>=? char #\A)
(char<=? char #\Z))
(case char
((#\ä #\ö #\ü #\Ä #\Ö #\Ü #\Ç #\ç #\_ #\É #\é #\È #\è #\à #\À) #t)
(else #f))))
(def (@char>>htmlquoted char port count-chars? in-attributes?)
;; (if count-chars?
;; (if (html-wordchar? char)
;; (serializeprocess-string-nextchar! port)
;; (serializeprocess-string-reset!)))
(cond ((case char
((#\<) "<")
((#\>) ">")
((#\&) "&")
((#\") (if in-attributes?
"""
(begin (@char>> char port) #f)))
(else
(@char>> char port)
#f))
=> (lambda (str)
(@string>> str port)))))
(def (atom-or-list>>htmlquoted value
port
count-chars?
xml?
in-attributes?
maybe-level)
(define-macro (subproc item)
`(sxml>>fast ,item port xml? maybe-level))
(cond ((pair? value)
(%for-each subproc value))
((promise? value)
(%stream-for-each subproc value))
(else
(atom>>htmlquoted value port count-chars? xml? in-attributes?))))
(def (atom>>htmlquoted atom port count-chars? xml? in-attributes?)
(let self ((atom atom))
(cond ((string? atom)
(@string>>htmlquoted atom port count-chars? in-attributes?))
((char? atom)
(@char>>htmlquoted atom port count-chars? in-attributes?))
((null? atom)
#|nothing|#)
((number? atom)
(##display atom port))
((boolean? atom)
#|nothing|#)
;; text (or more general: atom) lists or streams: (no
;; subelements!)
((pair? atom)
(%for-each self atom))
((promise? atom)
(%stream-for-each self atom))
(else
(error "atom>>htmlquoted: unknown type of:" atom)))))
(TEST
> (%try-error (.xml-string-fragment '(p | |)))
[error "atom>>htmlquoted: unknown type of:" | |]
> (%try-error (.xml-string-fragment '(p (@ (class | |)))))
[error "atom>>htmlquoted: unknown type of:" | |])
(def indentation-width 1)
(def (@sxml-element>> l port xml? maybe-level)
;; (serializeprocess-string-reset!)
(let* ((next-level (and maybe-level
(+ maybe-level indentation-width)))
(body->>> ;; proxy back to sxml>>fast
(lambda (body)
(%for-each (lambda (item)
(sxml>>fast item port xml? next-level))
body)))
(nam (@car l))
(is-comment
;; upcase version is the one shown in the reference
(or (eq? nam '*COMMENT*)
(eq? nam '*comment*))))
(if is-comment
(@string>> "<!--" port)
(begin
(@char>> #\< port)
(@symbol>> nam port)))
(let ((content (cond ((@sxml-attributes l)
;; XX what should happen with *COMMENT*
;; elements having attributes? being handled
;; strangely currently, probably.
=> (lambda (attrlist)
(@attrlist>> attrlist port xml?)
(@cddr l)))
(else (@cdr l)))))
(if is-comment
(cond ((pair? content)
(body->>> content)
(@string>> "-->" port))
((null? content)
(@string>> "-->" port))
(else
(error "@sxml-element>>: improper list: " l)))
(let* ((maybe-indent>> (lambda (maybe-level)
(when maybe-level
(begin
(@char>> #\newline port)
(let lp ((i maybe-level))
(when (> i 0)
(begin
(@char>> #\space port)
(lp (- i 1)))))))))
(out>>/body>> (lambda (body>>)
(maybe-indent>> next-level)
(@char>> #\> port)
(body>>)
(@char>> #\< port)
(@char>> #\/ port)
(@symbol>> (@car l) port)
(maybe-indent>> maybe-level)
(@char>> #\> port)))
(end>> (&
(when xml? (begin
(@char>> #\space port)
(@char>> #\/ port)))
(@char>> #\> port))))
;; XX use improper-for-each instead
(cond ((pair? content)
(out>>/body>> (& (body->>> content))))
((null? content)
(end>>))
(else
(out>>/body>> (& (sxml>>fast content port xml? next-level)))
;; (end>>) hm why do I not need this?
)))))))
;; item may be an atom, element, or list containing elements (tree).
(def (sxml>>fast item port xml? maybe-level)
(if (and (port? port)
(or (not maybe-level)
(##fixnum? maybe-level)))
(@sxml>>fast item port xml? maybe-level)
(error "invalid input type(s):" port maybe-level)))
(def *warn-thunk-evaluation* #f)
(set! *warn-thunk-evaluation* #f)
(def (@sxml>>fast item port xml? maybe-level)
(let self ((item item))
;;(paramdata (make-paramdata 0));;UNFINISHED! not threadsafe like this.
;; XX why is all this logic here; @sxml-element>> has it again?
;; rewrite really.
(cond
((pair? item)
(cond ((sxml-begin? item)
(%for-each self (cdr item)))
((sxml-element? item)
(@sxml-element>> item port xml? maybe-level))
(else
(%for-each self item))))
((vector? item)
(vector-for-each (lambda (v)
(self v)) item))
((promise? item)
(let ((item (force item)))
(if (pair? item)
(%stream-for-each self item)
(self item))))
((null? item))
((procedure? item)
(when *warn-thunk-evaluation*
(warn "evaluating procedure" item))
(@sxml>>fast (item) port xml? maybe-level))
(else
(atom-or-list>>htmlquoted item port #t xml? #f maybe-level)))))
(def (sxml-element:add-attribute el attributes)
;; XX do it right? currently cheap and simple.
`(,(car el)
,attributes
,@(cdr el)))
(def (sxml>>html* item
#!optional
(port (current-output-port))
maybe-level)
(sxml>>fast item port #f (and maybe-level
(if (boolean? maybe-level)
0
maybe-level))))
(def (sxml>>xhtml* item
#!optional
(port (current-output-port))
maybe-level)
(display "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" port)
(display "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n" port)
(let ((lang (or (sxml-element-attribute-ref item 'xml:lang #f)
(sxml-element-attribute-ref item 'lang #f)
"en")))
(sxml>>fast (sxml-element:add-attributes-unless-present
item
`((xmlns "http://www.w3.org/1999/xhtml")
(xml:lang ,lang)
(lang ,lang)))
port
#t
(and maybe-level
(if (boolean? maybe-level)
0
maybe-level)))))
(def (sxml>>xml* item
#!optional
(port (current-output-port))
maybe-level)
;; XX find out which output encoding the port is using?
(display "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" port)
(sxml>>fast item port #t (and maybe-level
(if (boolean? maybe-level)
0
maybe-level))))
(def (make-sxml>> sxml>>* maybe-level)
(lambda (item port)
(sxml>>* item
port
maybe-level)))
(def (sxml>>html-fast [sxml-element? item]
#!optional (port (current-output-port)))
((make-sxml>> sxml>>html* #f) item port))
(def. sxml.print-html sxml>>html-fast)
(def (sxml>>html [sxml-element? item]
#!optional (port (current-output-port)))
((make-sxml>> sxml>>html* #t) item port))
(def (sxml>>xhtml-fast [sxml-element? item]
#!optional (port (current-output-port)))
((make-sxml>> sxml>>xhtml* #f) item port))
(def. sxml.print-xhtml sxml>>xhtml-fast)
(def (sxml>>xhtml [sxml-element? item]
#!optional (port (current-output-port)))
((make-sxml>> sxml>>xhtml* #t) item port))
(def (sxml>>xml-fast [sxml-element? item]
#!optional (port (current-output-port)))
((make-sxml>> sxml>>xml* #f) item port))
(def. sxml.print-xml sxml>>xml-fast)
(def (sxml>>xml [sxml-element? item]
#!optional (port (current-output-port)))
((make-sxml>> sxml>>xml* #t) item port))
(def (sxml>>xml-file item path)
(call-with-output-file path (cut sxml>>xml item <>)))
(def (sxml>>fast-xml-file item path)
(call-with-output-file path (cut sxml>>xml-fast item <>)))
(def (sxml>>xhtml-file item path)
(call-with-output-file path (cut sxml>>xhtml item <>)))
(def (sxml>>fast-xhtml-file item path)
(call-with-output-file path (cut sxml>>xhtml-fast item <>)))
(def (sxml>>html-file item path)
(call-with-output-file path (cut sxml>>html item <>)))
(def (sxml>>fast-html-file item path)
(call-with-output-file path (cut sxml>>html-fast item <>)))
(def (pretty-filer serialize)
(lambda (item outpath #!optional noblanks) ;; XX noblanks unused now
(let ((tmppath (string-append outpath "~"
(number->string (random-integer 100000)))))
(let ((p (open-process
`(path: "xmllint"
arguments:
("--output" ,tmppath
,@(if noblanks
'()
'(
;; "--format"
;; breaks embedded perltidy output!
))
;;"--noblanks"
"-")
char-encoding: UTF-8
buffering: #t))))
(with-exception-catcher
(lambda (e)
(with-exception-catcher
(lambda (e2)
(warn "delete-file didn't succeed on: "tmppath))
(thunk
(delete-file tmppath)
(warn "successfully deleted " tmppath)))
(raise e))
(thunk
(serialize item p)
(close-output-port p)
(let ((r (process-status p)))
(if (= r 0)
(rename-file tmppath outpath)
(error "sxml>>pretty-..-file: xmllint gave exit status: "
r)))))))))
(def sxml>>pretty-xml-file (pretty-filer sxml>>xml-fast))
(def sxml>>pretty-xhtml-file (pretty-filer sxml>>xhtml-fast))
;; 'fragment' means, that it's output doesn't include a <?..?> line.
(def (sxml:fragment-stringifyer xml?)
(lambda (item #!optional maybe-level)
(let ((port (open-output-string '())))
(sxml>>fast item port xml? maybe-level)
(get-output-string port))))
(def sxml->html-string-fragment (sxml:fragment-stringifyer #f))
(def. sxml.html-string-fragment sxml->html-string-fragment)
(def sxml->xml-string-fragment (sxml:fragment-stringifyer #t))
(def. sxml.xml-string-fragment sxml->xml-string-fragment)
(TEST
> (sxml->xml-string-fragment '(a (@ (href "ha")) . "welt"))
"<a href=\"ha\">welt</a>"
> (sxml->xml-string-fragment '(a (@ (href "ha")) "welt"))
"<a href=\"ha\">welt</a>"
> (sxml->xml-string-fragment '(a (@ (href . "ha")) "welt"))
"<a href=\"ha\">welt</a>")
(def (sxml:stringifyer >>)
(lambda (item)
(let ((port (open-output-string '())))
(>> item port)
(get-output-string port))))
(def sxml->xml-string (sxml:stringifyer sxml>>xml-fast))
(def. sxml-element.xml-string sxml->xml-string)
(def sxml->html-string (sxml:stringifyer sxml>>html-fast))
(def. sxml-element.html-string sxml->html-string)
(def sxml->xhtml-string (sxml:stringifyer sxml>>xhtml-fast))
(def. sxml-element.xhtml-string sxml->xhtml-string)
(TEST
> (def v '(##begin (a (@ (href "ha")) . "welt") (br)))
> (map (C _ v) (list .xml-string-fragment
.html-string-fragment))
("<a href=\"ha\">welt</a><br />"
"<a href=\"ha\">welt</a><br>")
> (%try (.xml-string v))
(exception
text:
"no method found for generic .xml-string for value: (##begin (a (@ (href \"ha\")) . \"welt\") (br))\n")
> (%try (sxml-element.xml-string v))
(exception
text:
"item does not match sxml-element?: (##begin (a (@ (href \"ha\")) . \"welt\") (br))\n")
> (def v '(div (a (@ (href "ha")) . "welt") (br)))
> (.xml-string v)
"<?xml version=\"1.0\" encoding=\"utf-8\"?>
<div><a href=\"ha\">welt</a><br /></div>"
> (.xhtml-string v)
"<?xml version=\"1.0\" encoding=\"utf-8\"?>
<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
<div xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\"><a href=\"ha\">welt</a><br /></div>"
> (.html-string v)
"<div><a href=\"ha\">welt</a><br></div>")
;; ---------------------------------------------------------
;; idea to offer some serializer helper
;; XX remove all of that again, right?
; Idee 1: refactor. Cleaner einhaken.
; Idee 2: stubs. drin lassen als gleichnamig quasi aber einfach emptymachen. hm wie gleich alles aufsmal?. Oder: wie kann man ein SET von variablen um lenken?. im obigen code?.
; Na: mal so. serializeprocess-string-reset! und eben.
;(define serializeprocess-string-reset! paramdata/chars-in-a-row-reset!);; kein param
;(define serializeprocess-string-nextchar! paramdata/chars-in-a-row-inc!);; port param heh.
; ps JA staun obiges geht sogar obwohl pre definition und som it auch runtimig eigtl nicht geht aber es geht.
(define serializeprocess-string-reset! (lambda()#f));; kein param
(define serializeprocess-string-nextchar! (lambda(port)#f));; port param heh.
(define chars-in-a-row-allowed 5)
;(define chars-in-a-row-allowed 50000)
(define-structure paramdata
chars-in-a-row ; integer, chars seen since the last element.
)
(define paramdata (make-parameter #f))
; (define (paramdata-chars-in-a-row-add! n)
; (...
; (define (paramdata/chars-in-a-row)
; (paramdata-chars-in-a-row (paramdata)))
; (define (paramdata/chars-in-a-row-set! n)
; (paramdata-chars-in-a-row-set! (paramdata) n))
(define (paramdata/chars-in-a-row-reset!)
(paramdata-chars-in-a-row-set! (paramdata) 0))
(define (paramdata/chars-in-a-row-inc! port)
(let* ((p (paramdata))
(chars-in-a-row (paramdata-chars-in-a-row p)))
;; now I could use references. and an incf which then deref-inkrements.
(if (>= chars-in-a-row chars-in-a-row-allowed)
(begin
;;(@string>> "­" port)
;;(@string>> "­" port)
;;(@string>> "-" port)
(@string>> (next-separator) port)
(paramdata-chars-in-a-row-set! p 0))
(paramdata-chars-in-a-row-set! p (+ chars-in-a-row 1)))))
(define next-separator
(let* ((vec (vector
;;"­" ;todo entities hum?. also nbsp 'sadly'.
"­"
;;"-"
))
(pos 0)
(len (vector-length vec)))
(lambda ()
(set! pos (+ pos 1))
(when (>= pos len)
(set! pos 0))
(vector-ref vec pos))))