Skip to content

Commit

Permalink
Merge pull request #27 from anusii/zy/26_multiline_comment
Browse files Browse the repository at this point in the history
support multiline comment in the fields
  • Loading branch information
zheyxu authored Nov 25, 2023
2 parents de9df94 + a49e65b commit b1c6a8b
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 4 deletions.
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
## 0.2.6

- `pubspec.yaml`
- update version of package [petitparser]
- `graph.dart`
- new [_preprocessTurtleContent] method to to handle multiline string literals
- update [parseTurtle] to work when the content with multiline comments
- Examples
- add a example to parse files with multiline field


## 0.2.5

- `pubspec.yaml`
- update version of package [http]

## 0.2.4

- `graph.dart`
Expand Down
27 changes: 27 additions & 0 deletions example/example_5.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import 'dart:io';

import 'package:rdflib/rdflib.dart';

main() async {
String filePath = 'example/sample_ttl_5.ttl';
// Read file content to a local String
String fileContents = await File(filePath).readAsStringSync();

print('-------Original file-------\n$fileContents');

// create a graph to read turtle file and store info
Graph g = Graph();

// Parse with the new method [Graph.parseTurtle] instead of [Graph.parse] (deprecated)
g.parseTurtle(fileContents);

// Serialize the Graph for output
g.serialize(format: 'ttl', abbr: 'short');
print('-------Serialized String--------\n${g.serializedString}');

// Print out full format of triples (will use shorthand in serialization/export)
print('--------All triples in the graph-------');
for (Triple t in g.triples) {
print(t);
}
}
11 changes: 11 additions & 0 deletions example/sample_ttl_5.ttl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix sh_data: <http://yarrabah.net/data/solid-health#> .
@prefix sh_onto: <http://sii.cecs.anu.edu.au/onto/solid-health#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
sh_data:Info-17809g89a a sh_onto:InfoTab,
owl:NamedIndividual ;
rdfs:label "label_name"^^xsd:string ;
sh_onto:commentP """1. First comment.
2. Second comment.
3. Third comment."""^^xsd:string .
32 changes: 30 additions & 2 deletions lib/src/graph.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class Graph {
/// print(t);
/// }
/// ```
void addTripleToGroups(dynamic s, dynamic p, dynamic o) {
void addTripleToGroups(dynamic s, dynamic p, dynamic o) {
// TODO: subject as a BlankNode
try {
URIRef sub = (s.runtimeType == URIRef) ? s : item(s) as URIRef;
Expand Down Expand Up @@ -572,8 +572,11 @@ class Graph {
///
/// Updates [Graph.ctx], [Graph.groups] and [Graph.triples] in the process.
void parseTurtle(String fileContent) {
final String content = _removeComments(fileContent);
String processedContent = _preprocessTurtleContent(fileContent);
final String content = _removeComments(processedContent);

List parsedList = parser.parse(content).value;

for (List tripleList in parsedList) {
_saveToContext(tripleList);
}
Expand Down Expand Up @@ -929,4 +932,29 @@ class Graph {
}
return rtnStr;
}

/// Preprocesses Turtle content to handle multiline string literals.
///
/// This function addresses the issue of multiline literals in Turtle syntax,
/// which are enclosed within triple quotes `"""`.
String _preprocessTurtleContent(String turtleContent) {
// Regular expression to match multiline literals.

final multilineLiteralRegex = RegExp(r'"""(.*?)"""', dotAll: true);

// Replace each multiline literal with a processed version.

return turtleContent.replaceAllMapped(multilineLiteralRegex, (match) {
// Get the multiline literal, excluding the triple quotes.

String multilineLiteral = match.group(1)!;

// Process the multiline literal as needed.
// Example: Replace line breaks with a special sequence.
String processedLiteral = multilineLiteral.replaceAll('\n', '\\n');

// Return the processed literal with the original triple quotes
return '"$processedLiteral"';
});
}
}
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: rdflib
description: A pure Dart package for working with RDF (resource description framework).
version: 0.2.5
version: 0.2.6
homepage: https://github.com/anusii/rdflib
repository: https://github.com/anusii/rdflib

Expand All @@ -9,7 +9,7 @@ environment:

dependencies:
logging: ^1.1.0
petitparser: ^5.1.0
petitparser: ^6.0.1
http: ^1.1.0

dev_dependencies:
Expand Down

0 comments on commit b1c6a8b

Please sign in to comment.