13
13
on the start of the next line
14
14
* uses default encoding (encode()) is used instead of "latin-1"
15
15
16
- - Nicholas Car, 2021
16
+ - Nicholas Car, 2023
17
17
"""
18
18
19
19
from rdflib .exceptions import Error
@@ -101,7 +101,6 @@ def serialize(self, stream, base=None, encoding=None, spacious=None, **args):
101
101
self .write ("\n " )
102
102
103
103
self .endDocument ()
104
- self .write ("\n " )
105
104
106
105
self .base = None
107
106
@@ -168,21 +167,20 @@ def s_default(self, subject):
168
167
self .path (subject , SUBJECT )
169
168
self .write ("\n " + self .indent ())
170
169
self .predicateList (subject )
171
- self .write (" ; \n ." )
170
+ self .write ("\n ." )
172
171
return True
173
172
174
173
def s_squared (self , subject ):
175
174
if (self ._references [subject ] > 0 ) or not isinstance (subject , BNode ):
176
175
return False
177
176
self .write ("\n " + self .indent () + "[]" )
178
- self .predicateList (subject )
177
+ self .predicateList (subject , newline = False )
179
178
self .write (" ;\n ." )
180
179
return True
181
180
182
181
def path (self , node , position , newline = False ):
183
182
if not (
184
- self .p_squared (node , position , newline )
185
- or self .p_default (node , position , newline )
183
+ self .p_squared (node , position ) or self .p_default (node , position , newline )
186
184
):
187
185
raise Error ("Cannot serialize node '%s'" % (node ,))
188
186
@@ -207,7 +205,11 @@ def label(self, node, position):
207
205
208
206
return self .getQName (node , position == VERB ) or node .n3 ()
209
207
210
- def p_squared (self , node , position , newline = False ):
208
+ def p_squared (
209
+ self ,
210
+ node ,
211
+ position ,
212
+ ):
211
213
if (
212
214
not isinstance (node , BNode )
213
215
or node in self ._serialized
@@ -216,23 +218,19 @@ def p_squared(self, node, position, newline=False):
216
218
):
217
219
return False
218
220
219
- if not newline :
220
- self .write (" " )
221
-
222
221
if self .isValidList (node ):
223
222
# this is a list
224
223
self .depth += 2
225
- self .write ("(\n " )
226
- self .depth -= 1
224
+ self .write (" (\n " )
225
+ self .depth -= 2
227
226
self .doList (node )
228
- self .depth -= 1
229
- self .write ("\n " + self .indent (1 ) + ")" )
227
+ self .write ("\n " + self .indent () + ")" )
230
228
else :
229
+ # this is a Blank Node
231
230
self .subjectDone (node )
232
- self .depth += 2
233
- self .write ("[\n " )
234
- self .depth -= 1
235
- self .predicateList (node , newline = False )
231
+ self .write ("\n " + self .indent (1 ) + "[\n " )
232
+ self .depth += 1
233
+ self .predicateList (node )
236
234
self .depth -= 1
237
235
self .write ("\n " + self .indent (1 ) + "]" )
238
236
@@ -279,6 +277,7 @@ def predicateList(self, subject, newline=False):
279
277
self .write (" ;\n " + self .indent (1 ))
280
278
self .verb (predicate , newline = True )
281
279
self .objectList (properties [predicate ])
280
+ self .write (" ;" )
282
281
283
282
def verb (self , node , newline = False ):
284
283
self .path (node , VERB , newline )
@@ -291,11 +290,13 @@ def objectList(self, objects):
291
290
self .depth += depthmod
292
291
first_nl = False
293
292
if count > 1 :
294
- self .write ("\n " + self .indent (1 ))
293
+ if not isinstance (objects [0 ], BNode ):
294
+ self .write ("\n " + self .indent (1 ))
295
295
first_nl = True
296
296
self .path (objects [0 ], OBJECT , newline = first_nl )
297
297
for obj in objects [1 :]:
298
- self .write (" ,\n " )
299
- self .write (self .indent (1 ))
298
+ self .write (" ," )
299
+ if not isinstance (obj , BNode ):
300
+ self .write ("\n " + self .indent (1 ))
300
301
self .path (obj , OBJECT , newline = True )
301
302
self .depth -= depthmod
0 commit comments