diff --git a/CHANGELOG.md b/CHANGELOG.md index f42024f..106965d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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` diff --git a/example/example_5.dart b/example/example_5.dart new file mode 100644 index 0000000..a966e66 --- /dev/null +++ b/example/example_5.dart @@ -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); + } +} diff --git a/example/sample_ttl_5.ttl b/example/sample_ttl_5.ttl new file mode 100644 index 0000000..e6b55d9 --- /dev/null +++ b/example/sample_ttl_5.ttl @@ -0,0 +1,11 @@ +@prefix owl: . +@prefix rdfs: . +@prefix sh_data: . +@prefix sh_onto: . +@prefix xsd: . +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 . \ No newline at end of file diff --git a/lib/src/graph.dart b/lib/src/graph.dart index 86d3665..1893c4d 100644 --- a/lib/src/graph.dart +++ b/lib/src/graph.dart @@ -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; @@ -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); } @@ -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"'; + }); + } } diff --git a/pubspec.yaml b/pubspec.yaml index e3a6214..b6381ea 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -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 @@ -9,7 +9,7 @@ environment: dependencies: logging: ^1.1.0 - petitparser: ^5.1.0 + petitparser: ^6.0.1 http: ^1.1.0 dev_dependencies: