canon 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 508c18c9a150606b4008700a0f699f0b6099a7109a3ee6c83ec60d67d0624b34
4
+ data.tar.gz: 732c33be770d64d4189fc34e7fb7845756f71b1d18c64ce6d1c9379d37336403
5
+ SHA512:
6
+ metadata.gz: 8ac3823a7ab276239611acc4d6c597cf17dafb79380c31556a1899332b47ca7ccd31a84db4ff91d528503bd0738112f974dd29675d9c12e208359e2687459293
7
+ data.tar.gz: f77d314d83d3de095e9234e7623bbb2dab8aa492a96a80346094ba57dfc4fb533d072f88a0d5873b8b888be227be8fd626522c5d29308239a9a20bdb0a6b4456
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,11 @@
1
+ # Auto-generated by Cimas: Do not edit it manually!
2
+ # See https://github.com/metanorma/cimas
3
+ inherit_from:
4
+ - .rubocop_todo.yml
5
+ - https://raw.githubusercontent.com/riboseinc/oss-guides/master/ci/rubocop.yml
6
+
7
+ # local repo-specific modifications
8
+ # ...
9
+
10
+ AllCops:
11
+ TargetRubyVersion: 2.7
data/.rubocop_todo.yml ADDED
@@ -0,0 +1,40 @@
1
+ # This configuration was generated by
2
+ # `rubocop --auto-gen-config`
3
+ # on 2025-07-29 05:56:24 UTC using RuboCop version 1.79.0.
4
+ # The point is for the user to remove these configuration records
5
+ # one by one as the offenses are removed from the code base.
6
+ # Note that changes in the inspected code, or installation of new
7
+ # versions of RuboCop, may require this file to be generated again.
8
+
9
+ # Offense count: 2
10
+ # This cop supports safe autocorrection (--autocorrect).
11
+ # Configuration parameters: Max, AllowHeredoc, AllowURI, AllowQualifiedName, URISchemes, IgnoreCopDirectives, AllowedPatterns, SplitStrings.
12
+ # URISchemes: http, https
13
+ Layout/LineLength:
14
+ Exclude:
15
+ - 'canon.gemspec'
16
+ - 'spec/canon/xml/formatter_spec.rb'
17
+
18
+ # Offense count: 1
19
+ # This cop supports safe autocorrection (--autocorrect).
20
+ # Configuration parameters: EnforcedStyleForMultiline.
21
+ # SupportedStylesForMultiline: comma, consistent_comma, no_comma
22
+ Style/TrailingCommaInArguments:
23
+ Exclude:
24
+ - 'lib/canon/rspec_matchers.rb'
25
+
26
+ # Offense count: 1
27
+ # This cop supports safe autocorrection (--autocorrect).
28
+ # Configuration parameters: EnforcedStyleForMultiline.
29
+ # SupportedStylesForMultiline: comma, consistent_comma, diff_comma, no_comma
30
+ Style/TrailingCommaInArrayLiteral:
31
+ Exclude:
32
+ - 'lib/canon/rspec_matchers.rb'
33
+
34
+ # Offense count: 1
35
+ # This cop supports safe autocorrection (--autocorrect).
36
+ # Configuration parameters: EnforcedStyleForMultiline.
37
+ # SupportedStylesForMultiline: comma, consistent_comma, diff_comma, no_comma
38
+ Style/TrailingCommaInHashLiteral:
39
+ Exclude:
40
+ - 'lib/canon/rspec_matchers.rb'
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at ronald.tse@ribose.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/README.adoc ADDED
@@ -0,0 +1,169 @@
1
+ = Canon: Canonicalization for serialization formats
2
+
3
+ A Ruby library for canonicalizing and pretty-printing XML, YAML, and JSON with
4
+ RSpec matchers for equivalence testing.
5
+
6
+
7
+ == Purpose
8
+
9
+ This gem provides `Canon` which is a library for canonicalizing and
10
+ pretty-printing various serialization formats (XML, YAML, JSON). It provides a
11
+ standardized form suitable for comparison and testing.
12
+
13
+
14
+ == Features
15
+
16
+ XML canonicalization::
17
+ Format XML documents according to the https://www.w3.org/TR/xml-c14n11/[W3C
18
+ Canonicalized XML] format, with consistent indentation and ordering.
19
+
20
+ YAML canonicalization::
21
+ Format YAML documents with keys sorted alphabetically in a recursive manner at
22
+ all levels of the YAML structure, with consistent indentation.
23
+
24
+ JSON canonicalization::
25
+ Format JSON documents with keys sorted alphabetically in a recursive manner at
26
+ all levels of the JSON structure, with consistent indentation.
27
+
28
+ RSpec matchers::
29
+ Provides matchers for testing equivalence between serialized formats.
30
+
31
+ Unified interface::
32
+ Single API for working with all three formats.
33
+
34
+
35
+ == Installation
36
+
37
+ Add this line to your application's Gemfile:
38
+
39
+ [source,ruby]
40
+ ----
41
+ gem 'canon'
42
+ ----
43
+
44
+ And then execute:
45
+
46
+ [source,bash]
47
+ ----
48
+ $ bundle install
49
+ ----
50
+
51
+ Or install it yourself as:
52
+
53
+ [source,bash]
54
+ ----
55
+ $ gem install canon
56
+ ----
57
+
58
+
59
+ == Usage
60
+
61
+ === Formatting and canonicalization
62
+
63
+ Canon provides a unified interface for formatting and canonicalizing XML,
64
+ YAML, and JSON.
65
+
66
+ The `format` method is used to pretty-print and canonicalize the input data,
67
+ with the arguments of the method taking the content and the format type as
68
+ arguments.
69
+
70
+ [source,ruby]
71
+ ----
72
+ require 'canon'
73
+
74
+ # XML formatting
75
+ xml_input = '<root><b>2</b><a>1</a></root>'
76
+ formatted_xml = Canon.format(xml_input, :xml)
77
+ # => Pretty-printed XML with consistent formatting
78
+
79
+ # YAML formatting
80
+ yaml_input = "---\nz: 3\na: 1\nb: 2\n"
81
+ formatted_yaml = Canon.format(yaml_input, :yaml)
82
+ # => YAML with keys sorted alphabetically
83
+
84
+ # JSON formatting
85
+ json_input = '{"z":3,"a":1,"b":2}'
86
+ formatted_json = Canon.format(json_input, :json)
87
+ # => Pretty-printed JSON with keys sorted alphabetically
88
+ ----
89
+
90
+
91
+ === Parsing
92
+
93
+ Canon can also parse XML, YAML, and JSON strings into Ruby objects. The `parse`
94
+ method takes the content and the format type as arguments, returning a Ruby
95
+ object (Hash, Array, etc.) for YAML and JSON, or a Nokogiri XML document for XML.
96
+
97
+ [source,ruby]
98
+ ----
99
+ # Parse XML
100
+ xml_doc = Canon.parse(xml_input, :xml)
101
+ # => Nokogiri::XML::Document
102
+
103
+ # Parse YAML
104
+ yaml_obj = Canon.parse(yaml_input, :yaml)
105
+ # => Ruby object (Hash, Array, etc.)
106
+
107
+ # Parse JSON
108
+ json_obj = Canon.parse(json_input, :json)
109
+ # => Ruby object (Hash, Array, etc.)
110
+ ----
111
+
112
+ === RSpec matchers
113
+
114
+ The library provides RSpec matchers for testing equivalence between serialized
115
+ formats:
116
+
117
+ [source,ruby]
118
+ ----
119
+ require 'rspec'
120
+ require 'canon'
121
+
122
+ RSpec.describe 'Serialization tests' do
123
+ # Unified matcher with format parameter
124
+ it 'compares equivalent XML' do
125
+ xml1 = '<root><a>1</a><b>2</b></root>'
126
+ xml2 = '<root><b>2</b><a>1</a></root>'
127
+ expect(xml1).to be_serialization_equivalent_to(xml2, format: :xml)
128
+ end
129
+
130
+ it 'compares equivalent YAML' do
131
+ yaml1 = "---\na: 1\nb: 2\n"
132
+ yaml2 = "---\nb: 2\na: 1\n"
133
+ expect(yaml1).to be_serialization_equivalent_to(yaml2, format: :yaml)
134
+ end
135
+
136
+ it 'compares equivalent JSON' do
137
+ json1 = '{"a":1,"b":2}'
138
+ json2 = '{"b":2,"a":1}'
139
+ expect(json1).to be_serialization_equivalent_to(json2, format: :json)
140
+ end
141
+
142
+ # Format-specific matchers
143
+ it 'uses format-specific matchers' do
144
+ expect(xml1).to be_xml_equivalent_to(xml2) # XML
145
+ expect(xml1).to be_analogous_with(xml2) # XML (legacy matcher)
146
+ expect(yaml1).to be_yaml_equivalent_to(yaml2) # YAML
147
+ expect(json1).to be_json_equivalent_to(json2) # JSON
148
+ end
149
+ end
150
+ ----
151
+
152
+
153
+ == Development
154
+
155
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run
156
+ `rake spec` to run the tests. You can also run `bin/console` for an interactive
157
+ prompt that will allow you to experiment.
158
+
159
+
160
+ == Contributing
161
+
162
+ Bug reports and pull requests are welcome on GitHub at
163
+ https://github.com/lutaml/canon.
164
+
165
+
166
+ == Copyright and license
167
+
168
+ Copyright Ribose. https://opensource.org/licenses/BSD-2-Clause[BSD-2-Clause License].
169
+
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+
5
+ module Canon
6
+ module Formatters
7
+ # JSON formatter for canonicalization
8
+ class JsonFormatter
9
+ def self.format(json)
10
+ parsed = parse(json)
11
+ JSON.pretty_generate(sort_json_keys(parsed))
12
+ end
13
+
14
+ def self.parse(json)
15
+ JSON.parse(json)
16
+ end
17
+
18
+ def self.sort_json_keys(obj)
19
+ case obj
20
+ when Hash
21
+ obj.transform_values { |v| sort_json_keys(v) }
22
+ .sort.to_h
23
+ when Array
24
+ obj.map { |item| sort_json_keys(item) }
25
+ else
26
+ obj
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,70 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "nokogiri"
4
+
5
+ module Canon
6
+ module Formatters
7
+ # XML formatter for canonicalization
8
+ class XmlFormatter
9
+ # Source of XSLT
10
+ # https://emmanueloga.wordpress.com/2009/09/29/pretty-printing-xhtml-with-nokogiri-and-xslt/
11
+ NOKOGIRI_C14N_XSL = <<~XSL
12
+ <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
13
+ <xsl:output method="xml" encoding="ISO-8859-1"/>
14
+ <xsl:param name="indent-increment" select="' '"/>
15
+ <xsl:template name="newline">
16
+ <xsl:text disable-output-escaping="yes">
17
+ </xsl:text>
18
+ </xsl:template>
19
+
20
+ <xsl:template match="comment() | processing-instruction()">
21
+ <xsl:param name="indent" select="''"/>
22
+ <xsl:call-template name="newline"/>
23
+ <xsl:value-of select="$indent"/>
24
+ <xsl:copy />
25
+ </xsl:template>
26
+
27
+ <xsl:template match="text()">
28
+ <xsl:param name="indent" select="''"/>
29
+ <xsl:call-template name="newline"/>
30
+ <xsl:value-of select="$indent"/>
31
+ <xsl:value-of select="normalize-space(.)"/>
32
+ </xsl:template>
33
+
34
+ <xsl:template match="text()[normalize-space(.)='']"/>
35
+
36
+ <xsl:template match="*">
37
+ <xsl:param name="indent" select="''"/>
38
+ <xsl:call-template name="newline"/>
39
+ <xsl:value-of select="$indent"/>
40
+ <xsl:choose>
41
+ <xsl:when test="count(child::*) > 0">
42
+ <xsl:copy>
43
+ <xsl:copy-of select="@*"/>
44
+ <xsl:apply-templates select="*|text()">
45
+ <xsl:with-param name="indent" select="concat ($indent, $indent-increment)"/>
46
+ </xsl:apply-templates>
47
+ <xsl:call-template name="newline"/>
48
+ <xsl:value-of select="$indent"/>
49
+ </xsl:copy>
50
+ </xsl:when>
51
+ <xsl:otherwise>
52
+ <xsl:copy-of select="."/>
53
+ </xsl:otherwise>
54
+ </xsl:choose>
55
+ </xsl:template>
56
+ </xsl:stylesheet>
57
+ XSL
58
+
59
+ def self.format(xml)
60
+ Nokogiri::XSLT(NOKOGIRI_C14N_XSL)
61
+ .transform(Nokogiri::XML(xml, &:noblanks))
62
+ .to_xml(indent: 2, pretty: true, encoding: "UTF-8")
63
+ end
64
+
65
+ def self.parse(xml)
66
+ Nokogiri::XML(xml)
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "yaml"
4
+
5
+ module Canon
6
+ module Formatters
7
+ # YAML formatter for canonicalization
8
+ class YamlFormatter
9
+ def self.format(yaml)
10
+ parsed = parse(yaml)
11
+ sort_yaml_keys(parsed).to_yaml
12
+ end
13
+
14
+ def self.parse(yaml)
15
+ YAML.safe_load(yaml)
16
+ end
17
+
18
+ def self.sort_yaml_keys(obj)
19
+ case obj
20
+ when Hash
21
+ obj.transform_values { |v| sort_yaml_keys(v) }
22
+ .sort.to_h
23
+ when Array
24
+ obj.map { |item| sort_yaml_keys(item) }
25
+ else
26
+ obj
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,138 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "canon" unless defined?(::Canon)
4
+ require "compare-xml"
5
+ require "diffy"
6
+
7
+ begin
8
+ require "rspec/expectations"
9
+ rescue LoadError
10
+ end
11
+
12
+ module Canon
13
+ module RSpecMatchers
14
+ # Base matcher class for serialization equivalence
15
+ class SerializationMatcher
16
+ def initialize(expected, format = :xml)
17
+ @expected = expected
18
+ @format = format.to_sym
19
+ @result = nil
20
+ end
21
+
22
+ def matches?(target)
23
+ @target = target
24
+ send("match_#{@format}") ||
25
+ raise(Canon::Error, "Unsupported format: #{@format}")
26
+ rescue NoMethodError
27
+ raise Canon::Error, "Unsupported format: #{@format}"
28
+ end
29
+
30
+ def match_xml
31
+ @result = CompareXML.equivalent?(
32
+ Nokogiri::XML(@target),
33
+ Nokogiri::XML(@expected),
34
+ {
35
+ collapse_whitespace: true,
36
+ ignore_attr_order: true,
37
+ verbose: true,
38
+ },
39
+ )
40
+
41
+ @result.empty?
42
+ end
43
+
44
+ # Canonicalize and check string equivalence for YAML/JSON
45
+ def match_yaml
46
+ canonicalize_and_compare(:yaml)
47
+ end
48
+
49
+ def match_json
50
+ canonicalize_and_compare(:json)
51
+ end
52
+
53
+ private
54
+
55
+ def canonicalize_and_compare(format)
56
+ @actual_sorted = Canon.format(@target, format)
57
+ @expected_sorted = Canon.format(@expected, format)
58
+ @actual_sorted == @expected_sorted
59
+ end
60
+
61
+ def failure_message
62
+ case @format
63
+ when :xml
64
+ xml_failure_message
65
+ when :yaml, :json
66
+ generic_failure_message
67
+ end
68
+ end
69
+
70
+ def xml_failure_message
71
+ index = 0
72
+ @result.map do |hash|
73
+ index += 1
74
+ index_str = index.to_s.rjust(2, "0")
75
+ "DIFF #{index_str}:\n" \
76
+ " expected node: #{hash[:node1]}\n" \
77
+ " actual node : #{hash[:node2]}\n" \
78
+ " diff from : #{hash[:diff1]}\n" \
79
+ " diff to : #{hash[:diff2]}\n"
80
+ end.join("\n")
81
+ end
82
+
83
+ def generic_failure_message
84
+ diff = Diffy::Diff.new(
85
+ @expected_sorted,
86
+ @actual_sorted,
87
+ include_diff_info: false,
88
+ include_plus_and_minus_in_html: true,
89
+ diff_options: "-u",
90
+ )
91
+
92
+ "expected #{@format.to_s.upcase} to be equivalent\n\n" \
93
+ "Diff:\n" +
94
+ diff.to_s(:color)
95
+ end
96
+
97
+ def failure_message_when_negated
98
+ [
99
+ "expected:",
100
+ @target.to_s,
101
+ "not be equivalent to:",
102
+ @expected.to_s,
103
+ ].join("\n")
104
+ end
105
+
106
+ def diffable
107
+ true
108
+ end
109
+ end
110
+
111
+ # Matcher methods
112
+ def be_serialization_equivalent_to(expected, format: :xml)
113
+ SerializationMatcher.new(expected, format)
114
+ end
115
+
116
+ def be_analogous_with(expected)
117
+ SerializationMatcher.new(expected, :xml)
118
+ end
119
+
120
+ def be_xml_equivalent_to(expected)
121
+ SerializationMatcher.new(expected, :xml)
122
+ end
123
+
124
+ def be_yaml_equivalent_to(expected)
125
+ SerializationMatcher.new(expected, :yaml)
126
+ end
127
+
128
+ def be_json_equivalent_to(expected)
129
+ SerializationMatcher.new(expected, :json)
130
+ end
131
+
132
+ if defined?(::RSpec)
133
+ RSpec.configure do |config|
134
+ config.include(Canon::RSpecMatchers)
135
+ end
136
+ end
137
+ end
138
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Canon
4
+ VERSION = "0.1.0"
5
+ end
data/lib/canon.rb ADDED
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "canon/version"
4
+ require_relative "canon/formatters/xml_formatter"
5
+ require_relative "canon/formatters/yaml_formatter"
6
+ require_relative "canon/formatters/json_formatter"
7
+
8
+ require_relative "canon/rspec_matchers" if defined?(::RSpec)
9
+
10
+ module Canon
11
+ # Format content based on the specified format type
12
+ # @param content [String] The content to format
13
+ # @param format [Symbol] The format type (:xml, :yaml, :json)
14
+ # @return [String] The formatted content
15
+ def self.format(content, format = :xml)
16
+ get_formatter(format).format(content)
17
+ end
18
+
19
+ # Parse content based on the specified format type
20
+ # @param content [String] The content to parse
21
+ # @param format [Symbol] The format type (:xml, :yaml, :json)
22
+ # @return [Object] The parsed content
23
+ def self.parse(content, format = :xml)
24
+ get_formatter(format).parse(content)
25
+ end
26
+
27
+ def self.get_formatter(format)
28
+ case format.to_sym
29
+ when :xml
30
+ Formatters::XmlFormatter
31
+ when :yaml
32
+ Formatters::YamlFormatter
33
+ when :json
34
+ Formatters::JsonFormatter
35
+ else
36
+ raise Error, "Unsupported format: #{format}"
37
+ end
38
+ end
39
+
40
+ class Error < StandardError; end
41
+ end
data/lib/xml-c14n.rb ADDED
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This file provides backward compatibility for users of the xml-c14n gem.
4
+ # It redirects to the new canon gem.
5
+
6
+ require_relative "canon"
7
+
8
+ # For backward compatibility
9
+ module Xml
10
+ C14n = Canon
11
+ end
data/sig/xml/c14n.rbs ADDED
@@ -0,0 +1,6 @@
1
+ module Xml
2
+ module C14n
3
+ VERSION: String
4
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
5
+ end
6
+ end
metadata ADDED
@@ -0,0 +1,173 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: canon
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ribose Inc.
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2025-07-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: compare-xml
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: diffy
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: json
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: nokogiri
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rubocop-performance
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: Library for canonicalizing and pretty-printing XML, YAML, and JSON with
126
+ RSpec matchers for equivalence testing
127
+ email:
128
+ - open.source@ribose.com
129
+ executables: []
130
+ extensions: []
131
+ extra_rdoc_files: []
132
+ files:
133
+ - ".rspec"
134
+ - ".rubocop.yml"
135
+ - ".rubocop_todo.yml"
136
+ - CODE_OF_CONDUCT.md
137
+ - README.adoc
138
+ - Rakefile
139
+ - lib/canon.rb
140
+ - lib/canon/formatters/json_formatter.rb
141
+ - lib/canon/formatters/xml_formatter.rb
142
+ - lib/canon/formatters/yaml_formatter.rb
143
+ - lib/canon/rspec_matchers.rb
144
+ - lib/canon/version.rb
145
+ - lib/xml-c14n.rb
146
+ - sig/xml/c14n.rbs
147
+ homepage: https://github.com/metanorma/canon
148
+ licenses:
149
+ - BSD-2-Clause
150
+ metadata:
151
+ homepage_uri: https://github.com/metanorma/canon
152
+ source_code_uri: https://github.com/metanorma/canon
153
+ changelog_uri: https://github.com/metanorma/canon
154
+ post_install_message:
155
+ rdoc_options: []
156
+ require_paths:
157
+ - lib
158
+ required_ruby_version: !ruby/object:Gem::Requirement
159
+ requirements:
160
+ - - ">="
161
+ - !ruby/object:Gem::Version
162
+ version: 2.7.0
163
+ required_rubygems_version: !ruby/object:Gem::Requirement
164
+ requirements:
165
+ - - ">="
166
+ - !ruby/object:Gem::Version
167
+ version: '0'
168
+ requirements: []
169
+ rubygems_version: 3.5.22
170
+ signing_key:
171
+ specification_version: 4
172
+ summary: Library for canonicalization of serialization formats
173
+ test_files: []