-
Notifications
You must be signed in to change notification settings - Fork 11
/
haml_templates.rb
267 lines (257 loc) · 11.6 KB
/
haml_templates.rb
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
# Default HAML templates used for generating output from the writer
module RDF::RDFa
class Writer
# The default set of HAML templates used for RDFa code generation
BASE_HAML = {
identifier: "base",
# Document
# Locals: language, title, prefix, base, subjects
# Yield: subjects.each
doc: %q(
!!! XML
!!! 5
%html{**{xmlns: "http://www.w3.org/1999/xhtml", lang: lang, prefix: prefix}.compact}
- if base || title
%head
- if base
%base{**{href: base}.compact}
- if title
%title= title
%body
- subjects.map do |subject|
!= yield(subject)
),
# Output for non-leaf resources
# Note that @about may be omitted for Nodes that are not referenced
#
# If _rel_ and _resource_ are not nil, the tag will be written relative
# to a previous subject. If _element_ is :li, the tag will be written
# with <li> instead of <div>.
#
# Locals: subject, typeof, predicates, rel, element, inlist
# Yield: predicates.each
subject: %q(
- if element == :li
%li{**{rel: rel, resource: (about || resource), typeof: typeof, inlist: inlist}.compact}
- if typeof
%span.type!= typeof
- predicates.each do |predicate|
!= yield(predicate)
- else
%div{**{rel: rel, resource: (about || resource), typeof: typeof, inlist: inlist}.compact}
- if typeof
%span.type!= typeof
- predicates.each do |predicate|
!= yield(predicate)
),
# Output for single-valued properties
# Locals: predicate, object, inlist
# Yields: object
# If nil is returned, render as a leaf
# Otherwise, render result
property_value: %q(
- if heading_predicates.include?(predicate) && object.literal?
%h1{**{property: get_curie(predicate), content: get_content(object), lang: get_lang(object), datatype: get_dt_curie(object), inlist: inlist}.compact}= escape_entities(get_value(object))
- else
%div.property
%span.label
= get_predicate_name(predicate)
- if res = yield(object)
!= res
- elsif get_curie(object) == 'rdf:nil'
%span{rel: get_curie(predicate), inlist: ''}
- elsif object.node?
%span{**{property: get_curie(predicate), resource: get_curie(object), inlist: inlist}.compact}= get_curie(object)
- elsif object.uri?
%a{**{property: get_curie(predicate), href: object.to_s, inlist: inlist}.compact}= object.to_s
- elsif object.datatype == RDF.XMLLiteral
%span{**{property: get_curie(predicate), lang: get_lang(object), datatype: get_dt_curie(object), inlist: inlist}.compact}<!= get_value(object)
- else
%span{**{property: get_curie(predicate), content: get_content(object), lang: get_lang(object), datatype: get_dt_curie(object), inlist: inlist}.compact}= escape_entities(get_value(object))
),
# Output for multi-valued properties
# Locals: predicate, :objects, :inlist
# Yields: object for leaf resource rendering
property_values: %q(
%div.property
%span.label
= get_predicate_name(predicate)
%ul
- objects.each do |object|
- if res = yield(object)
!= res
- elsif object.node?
%li{**{property: get_curie(predicate), resource: get_curie(object), inlist: inlist}.compact}= get_curie(object)
- elsif object.uri?
%li
%a{**{property: get_curie(predicate), href: object.to_s, inlist: inlist}.compact}= object.to_s
- elsif object.datatype == RDF.XMLLiteral
%li{**{property: get_curie(predicate), lang: get_lang(object), datatype: get_curie(object.datatype), inlist: inlist}.compact}<!= get_value(object)
- else
%li{**{property: get_curie(predicate), content: get_content(object), lang: get_lang(object), datatype: get_dt_curie(object), inlist: inlist}.compact}= escape_entities(get_value(object))
),
}
# An alternative, minimal HAML template for RDFa code generation.
# This version does not perform recursive object generation and does not attempt
# to create human readable output.
MIN_HAML = {
identifier: "min",
# Document
# Locals: language, title, prefix, base, subjects
# Yield: subjects.each
doc: %q(
!!! XML
!!! 5
%html{**{xmlns: "http://www.w3.org/1999/xhtml", lang: lang, prefix: prefix}.compact}
- if base
%head
%base{*{href: base}.compact}
%body
- subjects.each do |subject|
!= yield(subject)
),
# Output for non-leaf resources
# Note that @about may be omitted for Nodes that are not referenced
#
# Locals: about, typeof, predicates, :inlist
# Yield: predicates.each
subject: %q(
%div{**{rel: rel, resource: (about || resource), typeof: typeof}.compact}
- predicates.each do |predicate|
!= yield(predicate)
),
# Output for single-valued properties.
# This version does not perform a recursive call, and renders all objects as leafs.
# Locals: predicate, object, inlist
# Yields: object
# If nil is returned, render as a leaf
# Otherwise, render result
property_value: %q(
- if res = yield(object)
!= res
- elsif get_curie(object) == 'rdf:nil'
%span{rel: get_curie(predicate), inlist: ''}
- elsif object.node?
%span{**{property: get_curie(predicate), resource: get_curie(object), inlist: inlist}.compact}= get_curie(object)
- elsif object.uri?
%a{**{property: get_curie(predicate), href: object.to_s, inlist: inlist}.compact}= object.to_s
- elsif object.datatype == RDF.XMLLiteral
%span{**{property: get_curie(predicate), lang: get_lang(object), datatype: get_dt_curie(object), inlist: inlist}.compact}<!= get_value(object)
- else
%span{**{property: get_curie(predicate), content: get_content(object), lang: get_lang(object), datatype: get_dt_curie(object), inlist: inlist}.compact}= escape_entities(get_value(object))
)
}
DISTILLER_HAML = {
identifier: "distiller",
# Document
# Locals: language, title, prefix, base, subjects
# Yield: subjects.each
doc: %q(
!!! XML
!!! 5
%html{**{xmlns: "http://www.w3.org/1999/xhtml", lang: lang, prefix: prefix}.compact}
- if base || title
%head
- if base
%base{href: base}
- if title
%title= title
%link{rel: "stylesheet", href: "http://rdf.greggkellogg.net/css/distiller.css", type: "text/css"}
%script{src: "https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js", type: "text/javascript"}
%script{src: "http://rdf.greggkellogg.net/js/distiller.js", type: "text/javascript"}
%body
- if base
%p= "RDFa serialization URI base: <#{base}>"
- subjects.each do |subject|
!= yield(subject)
%footer
%p= "Written by <a href='https://rubygems.org/gems/rdf-rdfa'>RDF::RDFa</a> version #{RDF::RDFa::VERSION}"
),
# Output for non-leaf resources
# Note that @about may be omitted for Nodes that are not referenced
#
# If _rel_ and _resource_ are not nil, the tag will be written relative
# to a previous subject. If _element_ is :li, the tag will be written
# with <li> instead of <div>.
#
# Note that @rel and @resource can be used together, or @about and @typeof, but
# not both.
#
# Locals: subject, typeof, predicates, rel, element, inlist
# Yield: predicates.each
subject: %q(
- if element == :li
%li{**{rel: rel, resource: (about || resource), typeof: typeof, inlist: inlist}.compact}
- if typeof
%span.type!= typeof
%table.properties
- predicates.each do |predicate|
!= yield(predicate)
- elsif rel
%td{**{rel: rel, resource: (about || resource), typeof: typeof, inlist: inlist}.compact}
- if typeof
%span.type!= typeof
%table.properties
- predicates.each do |predicate|
!= yield(predicate)
- else
%div{**{resource: (about || resource), typeof: typeof, inlist: inlist}.compact}
- if typeof
%span.type!= typeof
%table.properties
- predicates.each do |predicate|
!= yield(predicate)
),
# Output for single-valued properties
# Locals: predicate, object, inlist
# Yields: object
# If nil is returned, render as a leaf
# Otherwise, render result
property_value: %q(
- if heading_predicates.include?(predicate) && object.literal?
%h1{**{property: get_curie(predicate), content: get_content(object), lang: get_lang(object), datatype: get_dt_curie(object), inlist: inlist}.compact}= escape_entities(get_value(object))
- else
%tr.property
%td.label
= get_predicate_name(predicate)
- if res = yield(object)
!= res
- elsif get_curie(object) == 'rdf:nil'
%td{rel: get_curie(predicate), inlist: ''}= "Empty"
- elsif object.node?
%td{**{property: get_curie(predicate), resource: get_curie(object), inlist: inlist}.compact}= get_curie(object)
- elsif object.uri?
%td
%a{**{property: get_curie(predicate), href: object.to_s, inlist: inlist}.compact}= object.to_s
- elsif object.datatype == RDF.XMLLiteral
%td{**{property: get_curie(predicate), lang: get_lang(object), datatype: get_dt_curie(object), inlist: inlist}.compact}<!= get_value(object)
- else
%td{**{property: get_curie(predicate), content: get_content(object), lang: get_lang(object), datatype: get_dt_curie(object), inlist: inlist}.compact}= escape_entities(get_value(object))
),
# Output for multi-valued properties
# Locals: predicate, objects, inliste
# Yields: object for leaf resource rendering
property_values: %q(
%tr.property
%td.label
= get_predicate_name(predicate)
%td
%ul
- objects.each do |object|
- if res = yield(object)
!= res
- elsif object.node?
%li{**{property: get_curie(predicate), resource: get_curie(object), inlist: inlist}.compact}= get_curie(object)
- elsif object.uri?
%li
%a{**{property: get_curie(predicate), href: object.to_s, inlist: inlist}.compact}= object.to_s
- elsif object.datatype == RDF.XMLLiteral
%li{**{property: get_curie(predicate), lang: get_lang(object), datatype: get_curie(object.datatype), inlist: inlist}.compact}<!= get_value(object)
- else
%li{**{property: get_curie(predicate), content: get_content(object), lang: get_lang(object), datatype: get_dt_curie(object), inlist: inlist}.compact}= escape_entities(get_value(object))
),
}
HAML_TEMPLATES = {base: BASE_HAML, min: MIN_HAML, distiller: DISTILLER_HAML}
DEFAULT_HAML = BASE_HAML
end
end