-
Notifications
You must be signed in to change notification settings - Fork 0
/
mkns
executable file
·207 lines (195 loc) · 8.99 KB
/
mkns
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
#!/usr/bin/env racket
#lang racket/base
(require racket/bool
racket/cmdline
racket/contract
racket/list
racket/string
net/url-string
rdf/core/name
rdf/core/namespace
rdf/core/nsmap)
(define iso-8601-date-pattern (pregexp "\\d{4}-[01]\\-[0-3]\\d"))
(define iso-8601-partial-date-pattern (pregexp "(?:\\d{4}(-[01]\\d(-[0-3]\\d)?)?)"))
(define (date-string? str)
(regexp-match? iso-8601-date-pattern str))
(define (partial-date-string? str)
(regexp-match? iso-8601-partial-date-pattern str))
(struct vocabulary (namespace
namespace-prefix
name
description
specification-uri
specification-date
names)
#:transparent
#:constructor-name make-vocabulary
#:guard (struct-guard/c namespace?
prefix-string?
(or/c string? #f)
(or/c string? #f)
(or/c url-absolute? #f)
(or/c partial-date-string? #f)
(listof local-name-string?)))
(define output-width (make-parameter 100))
(define (--write-line-and-wrap words (length 0) (prefix "") (spacer " ") (op (current-output-port)))
(cond
((empty? words)
(when (> length 0) (displayln "" op))
words)
((and (= length 0) (non-empty-string? prefix))
(display prefix op)
(--write-line-and-wrap words
(string-length prefix)
prefix
spacer
op))
(else (let ((next (car words)))
(if (>= (+ length (string-length next)) (output-width))
(begin
(displayln "" op)
(--write-line-and-wrap words 0 prefix spacer op))
(begin
(display (format "~a~a" next spacer) op)
(--write-line-and-wrap (cdr words)
(+ length (string-length next) 1)
prefix
spacer
op)))))))
(define (write-line-and-wrap words #:prefix (prefix "") #:spacer (spacer " ") #:output-port (op (current-output-port)))
(--write-line-and-wrap words 0 prefix spacer op))
(define (write-vocabulary vocabulary (op (current-output-port)))
(displayln "#lang racket/base" op)
(displayln ";;" op)
(when (vocabulary-name vocabulary)
(displayln (format ";; Name: ~a" (vocabulary-name vocabulary)) op)
(displayln ";;" op))
(when (vocabulary-description vocabulary)
(let ((words (string-split (string-normalize-spaces (vocabulary-description vocabulary)))))
(write-line-and-wrap words #:prefix ";; " #:output-port op))
(displayln ";;" op))
(when (or (vocabulary-specification-uri vocabulary)
(vocabulary-specification-date vocabulary))
(when (vocabulary-specification-uri vocabulary)
(displayln (format ";; Specification URI: ~a" (url->string (vocabulary-specification-uri vocabulary))) op))
(when (vocabulary-specification-date vocabulary)
(displayln (format ";; Specification Date: ~a" (vocabulary-specification-date vocabulary)) op))
(displayln ";;" op))
(displayln ";; Status: not set" op)
(displayln ";;" op)
(displayln "" op)
(displayln "(require (only-in rdf/core/name" op)
(displayln " string->local-name)" op)
(displayln "(require (only-in rdf/core/namespace" op)
(displayln " string->namespace" op)
(displayln " namespace+name->nsname)" op)
(displayln " (only-in rdf/core/nsmap" op)
(displayln " string->prefix-name" op)
(displayln " nsmap-set!))" op)
(displayln "" op)
(displayln "(provide (all-defined-out))" op)
(displayln "" op)
(let ((prefix (vocabulary-namespace-prefix vocabulary))
(uri (vocabulary-namespace vocabulary))
(separator (make-string (- (output-width) 4) #\=)))
(displayln (format ";; ~a" separator) op)
(displayln ";; Namespace definition" op)
(displayln (format ";; ~a" separator) op)
(displayln "" op)
(displayln (format "(define ~a-prefix-string \"~a\")" prefix prefix) op)
(displayln (format "(define ~a-namespace-string \"~a\")" prefix (namespace->string uri)) op)
(displayln "" op)
(displayln (format "(define ~a: " prefix) op)
(displayln (format " (string->namespace \"~a\"))" (namespace->string uri)) op)
(displayln "" op)
(displayln (format "(define (nsmap-add-~a map)" prefix) op)
(displayln (format " (nsmap-set! map") op)
(displayln (format " (string->prefix-name= ~a-prefix-string)" prefix) op)
(displayln (format " (string->namespace \"~a\")))" (namespace->string uri)) op)
(displayln "" op)
(displayln (format ";; ~a" separator) op)
(displayln ";; Name Definitions" op)
(displayln (format ";; ~a" separator) op)
(when (not (empty? (vocabulary-names vocabulary)))
(displayln "" op)
(for-each
(λ (name)
(displayln (format "(define ~a:~a"
prefix name) op)
(displayln (format " (namespace+name->nsname ~a: (string->local-name \"~a\")))"
prefix name) op))
(vocabulary-names vocabulary)))))
(define (parse-command-line)
(let* ((args (make-hash `((name . #f)
(desc . #f)
(specuri . #f)
(specdate . #f)
(names . ())
(out-file . #f))))
(uri+prefix (command-line
#:usage-help ""
#:usage-help "Create a new Racket module for an RDF vocabulary."
#:once-each
(("-n" "--name")
name
"This vocabulary's name"
(hash-set! args 'name name))
(("-d" "--description")
text
"A description of this vocabulary"
(hash-set! args 'desc text))
(("-s" "--spec-uri")
uri
"A Specification URI describing this vocabulary"
(hash-set! args 'specuri uri))
(("-p" "--spec-published")
date
"The publication date of the vocabulary specification"
(hash-set! args 'specdate date))
(("-w" "--width")
width
"The width of the output written (default: 100)"
(output-width (string->number width)))
#:once-any
(("-o" "--output-file")
racket-file
"The name of a file to write output to (default: stdout)"
(hash-set! args 'out-file racket-file))
(("-r" "--output-racket-module")
module
"The name of a module to write output to"
(hash-set! args 'out-file (format "~a.rkt" module)))
(("-u" "--use-prefix-name")
"Use the namespace prefix to determine the module name"
(hash-set! args 'out-file #t))
#:multi
(("-m" "--member")
member-names
"The name of a member, or list of members, within this vocabulary"
(hash-set! args 'names (cons member-names (hash-ref args 'names))))
#:args (uri prefix)
(let ((file-arg (hash-ref args 'out-file)))
(when (and (boolean? file-arg) (boolean=? file-arg #t))
(hash-set! args 'out-file (format "~a.rkt" prefix))))
(list uri prefix))))
(displayln (format "~s // ~a" args uri+prefix))
(values
(make-vocabulary (string->namespace (car uri+prefix))
(cadr uri+prefix)
(hash-ref args 'name)
(hash-ref args 'desc)
(if (hash-ref args 'specuri)
(string->url (hash-ref args 'specuri))
#f)
(hash-ref args 'specdate)
(flatten
(map (λ (member) (string-split member ","))
(hash-ref args 'names))))
(hash-ref args 'out-file))))
(let-values (((vocabulary output-file) (parse-command-line)))
(write-vocabulary vocabulary
(if output-file
(begin
(displayln (format "Writing to file ~a" output-file))
(open-output-file #:exists 'replace output-file))
(current-output-port))))