xml_in_kdl 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: fd2877c988925b9a88007a1903e8e37af9467b41b42291b2be12558b00e286d4
4
+ data.tar.gz: 63e89b10da00d6891f5a42e784dfdcc928968cc191cc8d8c5e552c7fe31ca300
5
+ SHA512:
6
+ metadata.gz: 752285a593c4becb8b57f3fda7a61d54a7db7f0e0ef1afb424ae287e84fd228617df9ab93390f2b11084ee540cdb77e9a2a83641ce1835dc9d8c9cba29c6fea2
7
+ data.tar.gz: ad06c71e423144d38da8e9e3e8b1117d0dc724b916129bd32ed9a51016b1fc9a266e183ca3a177fb040faa3d2ea055bd7677f05393f1052b6f1979abbb793c00
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in xml_in_kdl.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "minitest", "~> 5.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,33 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ xml_in_kdl (0.1.0)
5
+ kdl (~> 1.0)
6
+ nokogiri (~> 1.13)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ kdl (1.0.3)
12
+ simpleidn (~> 0.2.1)
13
+ minitest (5.16.1)
14
+ nokogiri (1.13.6-x86_64-linux)
15
+ racc (~> 1.4)
16
+ racc (1.6.0)
17
+ rake (13.0.6)
18
+ simpleidn (0.2.1)
19
+ unf (~> 0.1.4)
20
+ unf (0.1.4)
21
+ unf_ext
22
+ unf_ext (0.0.8.2)
23
+
24
+ PLATFORMS
25
+ x86_64-linux
26
+
27
+ DEPENDENCIES
28
+ minitest (~> 5.0)
29
+ rake (~> 13.0)
30
+ xml_in_kdl!
31
+
32
+ BUNDLED WITH
33
+ 2.3.14
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 TODO: Write your name
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,33 @@
1
+ # XmlInKdl
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/xml_in_kdl`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Install the gem and add to the application's Gemfile by executing:
10
+
11
+ $ bundle add xml_in_kdl
12
+
13
+ If bundler is not being used to manage dependencies, install the gem by executing:
14
+
15
+ $ gem install xml_in_kdl
16
+
17
+ ## Usage
18
+
19
+ TODO: Write usage instructions here
20
+
21
+ ## Development
22
+
23
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
24
+
25
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
26
+
27
+ ## Contributing
28
+
29
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/xml_in_kdl.
30
+
31
+ ## License
32
+
33
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/test_*.rb"]
10
+ end
11
+
12
+ task default: :test
@@ -0,0 +1,112 @@
1
+ # frozen_string_literal: true
2
+
3
+ module XmlInKdl
4
+ class Decoder
5
+ def initialize(kdl)
6
+ @kdl = kdl
7
+ @xml_doc = ::Nokogiri::XML::Document.new
8
+ end
9
+
10
+ def self.decode(kdl)
11
+ new(kdl).decode
12
+ end
13
+
14
+ def decode
15
+ @kdl.nodes.each do |kdl_node|
16
+ xml_node = decode_node(kdl_node)
17
+ @xml_doc.add_child(xml_node) unless xml_node.nil?
18
+ end
19
+ @xml_doc
20
+ end
21
+
22
+ private
23
+
24
+ def decode_node(node)
25
+ case node.name
26
+ when /^\?/
27
+ decode_directive(node)
28
+ when '!'
29
+ decode_comment(node)
30
+ when /^!/
31
+ decode_doctype(node)
32
+ when '-'
33
+ decode_text(node)
34
+ else
35
+ decode_element(node)
36
+ end
37
+ end
38
+
39
+ DIRECTIVE_ERROR_STRING = "XiK directive nodes must only contain properties"
40
+ def decode_directive(directive)
41
+ raise Error, DIRECTIVE_ERROR_STRING unless directive.children.empty? && directive.arguments.empty?
42
+ raise Error, DIRECTIVE_ERROR_STRING if directive.properties.values.any? { |v| !v.is_a?(::KDL::Value::String) }
43
+
44
+ if directive.properties['version']
45
+ @xml_doc = ::Nokogiri::XML::Document.new(directive.properties['version'].value)
46
+ end
47
+ if directive.properties['encoding']
48
+ @xml_doc.encoding = directive.properties['encoding'].value
49
+ end
50
+ # TODO: more directives, e.g. "standalone"
51
+ nil
52
+ end
53
+
54
+ COMMENT_ERROR_STRING = "XiK comment nodes must contain exactly one string argument and nothing else"
55
+ def decode_comment(comment)
56
+ raise Error, COMMENT_ERROR_STRING unless comment.children.empty? && comment.properties.empty?
57
+ raise Error, COMMENT_ERROR_STRING unless comment.arguments.size == 1
58
+ raise Error, COMMENT_ERROR_STRING unless comment.arguments.first.is_a?(::KDL::Value::String)
59
+
60
+ ::Nokogiri::XML::Comment.new(@xml_doc, comment.arguments.first.value)
61
+ end
62
+
63
+ DOCTYPE_ERORR_STRING = "XiK doctype nodes must contain 1 to 3 string arguments and nothing else"
64
+ def decode_doctype(doctype)
65
+ raise Error, DOCTYPE_ERORR_STRING unless doctype.children.empty? && doctype.properties.empty?
66
+ raise Error, DOCTYPE_ERORR_STRING unless doctype.arguments.size >= 1 && doctype.arguments.size <= 3
67
+ raise Error, DOCTYPE_ERORR_STRING unless doctype.arguments.all? { |arg| arg.is_a?(::KDL::Value::String) }
68
+
69
+ args = doctype.arguments.map(&:value)
70
+ args << nil while args.length < 3
71
+ @xml_doc.create_internal_subset(*args)
72
+ nil
73
+ end
74
+
75
+ TEXT_ERORR_STRING = "XiK text nodes must contain exactly one string argument and nothing else"
76
+ def decode_text(text)
77
+ raise Error, TEXT_ERORR_STRING unless text.children.empty? && text.properties.empty?
78
+ raise Error, TEXT_ERORR_STRING unless text.arguments.size == 1
79
+ raise Error, TEXT_ERORR_STRING unless text.arguments.first.is_a?(::KDL::Value::String)
80
+
81
+ decode_string(text.arguments.first)
82
+ end
83
+
84
+ STRING_ERROR_STRING = "XiK values must be strings"
85
+ def decode_string(string)
86
+ raise Error, STRING_ERROR_STRING unless string.is_a?(::KDL::Value::String)
87
+
88
+ ::Nokogiri::XML::Text.new(string.value, @xml_doc)
89
+ end
90
+
91
+ ELEMENT_ERROR_STRING = "XiK elements must have either one string argument OR any number of children"
92
+ def decode_element(element)
93
+ if element.children.empty?
94
+ raise Error, ELEMENT_ERROR_STRING unless element.arguments.size <= 1
95
+ else
96
+ raise Error, ELEMENT_ERROR_STRING unless element.arguments.empty?
97
+ end
98
+ raise Error, STRING_ERROR_STRING if element.properties.values.any? { |v| !v.is_a?(::KDL::Value::String) }
99
+
100
+ node = @xml_doc.create_element(element.name, element.properties.transform_values { |v| v.value.to_s })
101
+ if element.arguments.empty?
102
+ element.children.each do |kdl_child|
103
+ xml_child = decode_node(kdl_child)
104
+ node.add_child(xml_child) unless xml_child.nil?
105
+ end
106
+ else
107
+ node.add_child(decode_string(element.arguments.first)) unless element.arguments.empty?
108
+ end
109
+ node
110
+ end
111
+ end
112
+ end
@@ -0,0 +1,65 @@
1
+ module XmlInKdl
2
+ module Encoder
3
+ class << self
4
+ def encode(xml)
5
+ encoding_node = encode_encoding(xml.encoding)
6
+ children = xml.children.map { |c| encode_node(c) }
7
+ ::KDL::Document.new([encoding_node, *children].compact)
8
+ end
9
+
10
+ private
11
+
12
+ def encode_node(node)
13
+ case node
14
+ when Nokogiri::XML::Element
15
+ encode_element(node)
16
+ when Nokogiri::XML::Text
17
+ encode_text(node)
18
+ when Nokogiri::XML::DTD
19
+ encode_dtd(node)
20
+ when Nokogiri::XML::Comment
21
+ encode_comment(node)
22
+ end
23
+ end
24
+
25
+ def encode_element(element)
26
+ arguments = []
27
+ children = []
28
+ properties = element.attributes.map { |key, value| [key, ::KDL::Value.from(value.value)] }.to_h
29
+ if !properties.empty? || element.children.any? { |c| c.is_a?(Nokogiri::XML::Element) }
30
+ children = element.children.map { |c| encode_node(c) }.compact
31
+ elsif element.children.first.is_a?(Nokogiri::XML::Text)
32
+ content = element.children.first.content.strip
33
+ arguments = [::KDL::Value.from(element.children.first.content.strip)] unless content.empty?
34
+ end
35
+ ::KDL::Node.new(element.name, arguments, properties, children)
36
+ end
37
+
38
+ def encode_text(text)
39
+ content = squish(text.content)
40
+ return nil if content.strip.empty?
41
+
42
+ ::KDL::Node.new('-', [::KDL::Value.from(content)])
43
+ end
44
+
45
+ def encode_dtd(dtd)
46
+ args = [dtd.name, dtd.external_id, dtd.system_id].compact.map { |x| ::KDL::Value.from(x) }
47
+ ::KDL::Node.new('!doctype', args)
48
+ end
49
+
50
+ def encode_encoding(encoding)
51
+ return nil if encoding.nil?
52
+
53
+ ::KDL::Node.new('?xml', [], { 'encoding' => KDL::Value.from(encoding) })
54
+ end
55
+
56
+ def squish(string)
57
+ string.gsub(/(^\s+|\s+$)/, ' ')
58
+ end
59
+
60
+ def encode_comment(comment)
61
+ ::KDL::Node.new('!', [::KDL::Value.from(comment.content)])
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module XmlInKdl
4
+ VERSION = "0.1.0"
5
+ end
data/lib/xml_in_kdl.rb ADDED
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "kdl"
4
+ require "nokogiri"
5
+ require_relative "xml_in_kdl/version"
6
+ require_relative "xml_in_kdl/encoder"
7
+ require_relative "xml_in_kdl/decoder"
8
+
9
+ module XmlInKdl
10
+ class Error < StandardError; end
11
+
12
+ class << self
13
+ def encode(xml)
14
+ Encoder.encode(xml)
15
+ end
16
+
17
+ def encode_string(xml)
18
+ encode(::Nokogiri::XML(xml))
19
+ end
20
+
21
+ def decode(kdl)
22
+ Decoder.decode(kdl)
23
+ end
24
+
25
+ def decode_string(kdl)
26
+ decode(::KDL.parse_document(kdl))
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,4 @@
1
+ module XmlInKdl
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/xml_in_kdl/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "xml_in_kdl"
7
+ spec.version = XmlInKdl::VERSION
8
+ spec.authors = ["Danielle Smith"]
9
+ spec.email = ["danini@hey.com"]
10
+
11
+ spec.summary = "XML-in-KDL (aka XiK)"
12
+ spec.description = "Allows XML to be encoded as KDL"
13
+ spec.homepage = "https://github.com/kdl-org/kdl/blob/main/XML-IN-KDL.md"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 2.6.0"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = "https://github.com/danini-the-panini/xml-in-kdl-rb"
19
+ spec.metadata["changelog_uri"] = "https://github.com/danini-the-panini/xml-in-kdl-rb/releases"
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(__dir__) do
24
+ `git ls-files -z`.split("\x0").reject do |f|
25
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
26
+ end
27
+ end
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ # Uncomment to register a new dependency of your gem
33
+ spec.add_dependency "kdl", "~> 1.0"
34
+ spec.add_dependency "nokogiri", "~> 1.13"
35
+
36
+ # For more information and examples about making a new gem, check out our
37
+ # guide at: https://bundler.io/guides/creating_gem.html
38
+ end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: xml_in_kdl
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Danielle Smith
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-07-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: kdl
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: nokogiri
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.13'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.13'
41
+ description: Allows XML to be encoded as KDL
42
+ email:
43
+ - danini@hey.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - Gemfile
49
+ - Gemfile.lock
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - lib/xml_in_kdl.rb
54
+ - lib/xml_in_kdl/decoder.rb
55
+ - lib/xml_in_kdl/encoder.rb
56
+ - lib/xml_in_kdl/version.rb
57
+ - sig/xml_in_kdl.rbs
58
+ - xml_in_kdl.gemspec
59
+ homepage: https://github.com/kdl-org/kdl/blob/main/XML-IN-KDL.md
60
+ licenses:
61
+ - MIT
62
+ metadata:
63
+ homepage_uri: https://github.com/kdl-org/kdl/blob/main/XML-IN-KDL.md
64
+ source_code_uri: https://github.com/danini-the-panini/xml-in-kdl-rb
65
+ changelog_uri: https://github.com/danini-the-panini/xml-in-kdl-rb/releases
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: 2.6.0
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubygems_version: 3.3.7
82
+ signing_key:
83
+ specification_version: 4
84
+ summary: XML-in-KDL (aka XiK)
85
+ test_files: []