File: printer.jl

package info (click to toggle)
librep 0.90.2-1.3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 3,940 kB
  • sloc: ansic: 32,948; lisp: 11,025; sh: 9,844; makefile: 545; sed: 93
file content (70 lines) | stat: -rw-r--r-- 2,010 bytes parent folder | download | duplicates (3)
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
#| rep.xml.printer -- companion XML printer to rep.xml.reader

   $Id$

   Copyright (C) 2002 John Harper <jsh@unfactored.org>

   This file is part of librep.

   librep 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 2, or (at your option)
   any later version.

   librep 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 librep; see the file COPYING.  If not, write to
   the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
|#

(define-structure rep.xml.printer

    (export make-xml-output
	    print-xml-item)

    (open rep
	  rep.regexp)

  (define make-xml-output identity)

  (define (substitute-entities string)
    (string-replace "[<&'\"]"
		    (lambda ()
		      (car (rassoc (expand-last-match "\\0")
				   '(("lt" . "<")
				     ("amp" . "&")
				     ("apos" . "'")
				     ("quot" . "\"")))))
		    string))

  (define (print-params stream params)
    (mapc (lambda (cell)
	    (format stream " %s=\"%s\""
		    (car cell) (substitute-entities (cdr cell))))
	  params))

  (define (print-xml-item stream item)
    (cond ((stringp item)
	   (write stream (substitute-entities item)))

	  ((eq (car item) '!)
	   (format stream "<!%s>" (nth 1 stream)))

	  ((symbolp (car item))
	   (format stream "<%s" (car item))
	   (print-params stream (nth 1 item))
	   (cond ((string-match "^\\?" (symbol-name (car item)))
		  (write stream "?>"))
		 ((null (nthcdr 2 item))
		  (write stream "/>"))
		 (t
		  (write stream #\>)
		  (mapc (lambda (x)
			  (print-xml-item stream x)) (nthcdr 2 item))
		  (format stream "</%s>" (car item)))))

	  (t (error "Unknown item type: %s" item)))))