View Source RDF.Turtle.Encoder (RDF.ex v2.0.0)

An encoder for Turtle serializations of RDF.ex data structures.

As for all encoders of RDF.Serialization.Formats, you normally won't use these functions directly, but via one of the write_ functions on the RDF.Turtle format module or the generic RDF.Serialization module.

Options

  • :content: Allows specifying the content and structure of the Turtle document to be rendered and defining which parts should be generated in which order. This option accepts lists of the values :base, :prefixes, and :triples. You can also use :directives to specify [:base, :prefixes] as a group. Additionally, arbitrary strings can be included at desired positions to customize the document.

    RDF.Turtle.write_string(graph, content: [
      "# === HEADER ===",
      :directives,
      "\n# === TRIPLES ===",
      :triples
    ])
  • :prefixes: Allows to specify the prefixes to be used as a RDF.PrefixMap or anything from which a RDF.PrefixMap can be created with RDF.PrefixMap.new/1. If not specified the ones from the given graph are used or if these are also not present the RDF.default_prefixes/0. If you explicitly want to omit the prefixes use [] as a value.

  • :base: : Allows to specify the base URI to be used for a @base directive. If not specified the one from the given graph is used or if there is also none specified for the graph the RDF.default_base_iri/0.

  • :directive_style: Specifies the style of directives to be used in the document. Possible values are :turtle and :sparql (default: :turtle).

  • :implicit_base: This boolean flag allows to use a base URI to get relative IRIs without embedding it explicitly in the content with a @base directive, so that the URIs will be resolved according to the remaining strategy specified in section 5.1 of RFC3986 (default: false).

  • :base_description: Allows to provide a description of the resource denoted by the base URI. This option is especially useful when the base URI is actually not specified, e.g. in the common use case of wanting to describe the document itself, which should be denoted by the URL where it is hosted as the implicit base URI.

  • :line_prefix: Allows to specify a function returning prefixes for the encoded lines. When this function is defined, the :single_triple_lines option is implicitly set, so that each line encodes at most one triple. The function receives three arguments:

    1. type - one of the following values specifying the content of the line: :triple, :description, :graph (for named graphs in TriG only), :closing
    2. value - a type specific value for the content in this line
      • for :triple: the RDF.Triple encoded in this line
      • for :description: the subject whose description is started in this line
      • for :graph: the name of the graph, opened in this line
      • for :closing: a value specifying which element is closed in this line
    3. graph_name - the name of graph (in Turtle this is always nil)
  • :single_triple_lines: When set to true each line encodes at most one triple, i.e. no object lists with multiple objects are used and no Turtle list encodings are used. This option is in of itself is not very useful. It is set implicitly when defining a :line_prefix function which depends on this mode to produce useful results.

  • :indent: Allows to specify the number of spaces the output should be indented.

  • :indent_width: Allows to specify the number of spaces that should be used for indentations (default: 4).

  • :pn_local_validation: This option controls how IRIs are validated the check whether they can encoded as a prefixed name. Available settings are:

    • :fast (default): Provides a quick and efficient validation that covers most common cases. It does not handle every possible valid scenario, focusing instead on typical structures encountered in prefixed names.
    • :none: Disables validation entirely. Use this mode if you are confident that all your IRIs are already compliant with prefixed name requirements, allowing you to bypass validation checks for increased performance.
    • Note: Currently, a :strict mode, which would provide comprehensive validation conforming strictly to the Turtle specification, is not implemented. Contributions for implementing this mode are welcome.
  • :rdf_star: Allows to skip a RDF-star related preprocessing step. This can be used to improve performance a little bit when you know the encoded data doesn't include any RDF-star statements. Defaults to the returned value of RDF.star?/0.