Skip to content

Commit

Permalink
chore: rubocop
Browse files Browse the repository at this point in the history
  • Loading branch information
ronaldtse committed Dec 17, 2024
1 parent b195683 commit 7fd4839
Show file tree
Hide file tree
Showing 65 changed files with 436 additions and 228 deletions.
2 changes: 2 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
inherit_from: .rubocop_todo.yml

AllCops:
TargetRubyVersion: 3.0

Expand Down
86 changes: 86 additions & 0 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2024-12-17 09:38:48 UTC using RuboCop version 1.69.2.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 1
# This cop supports safe autocorrection (--autocorrect).
Layout/ElseAlignment:
Exclude:
- 'lib/moxml/adapter/customized_oga/xml_generator.rb'

# Offense count: 1
# This cop supports safe autocorrection (--autocorrect).
# Configuration parameters: EnforcedStyleAlignWith, Severity.
# SupportedStylesAlignWith: keyword, variable, start_of_line
Layout/EndAlignment:
Exclude:
- 'lib/moxml/adapter/customized_oga/xml_generator.rb'

# Offense count: 2
# This cop supports safe autocorrection (--autocorrect).
# Configuration parameters: Width, AllowedPatterns.
Layout/IndentationWidth:
Exclude:
- 'lib/moxml/adapter/customized_oga/xml_generator.rb'

# Offense count: 1
Lint/IneffectiveAccessModifier:
Exclude:
- 'lib/moxml/node.rb'

# Offense count: 1
# Configuration parameters: MaximumRangeSize.
Lint/MissingCopEnableDirective:
Exclude:
- 'lib/moxml/adapter/customized_oga/xml_generator.rb'

# Offense count: 1
# Configuration parameters: AllowedParentClasses.
Lint/MissingSuper:
Exclude:
- 'lib/moxml/adapter/customized_oga/xml_declaration.rb'

# Offense count: 5
# Configuration parameters: AllowedMethods, AllowedPatterns, CountRepeatedAttributes.
Metrics/AbcSize:
Max: 30

# Offense count: 46
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
# AllowedMethods: refine
Metrics/BlockLength:
Max: 152

# Offense count: 3
# Configuration parameters: CountComments, CountAsOne.
Metrics/ClassLength:
Max: 246

# Offense count: 3
# Configuration parameters: AllowedMethods, AllowedPatterns.
Metrics/CyclomaticComplexity:
Max: 10

# Offense count: 9
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
Metrics/MethodLength:
Max: 20

# Offense count: 4
# Configuration parameters: MinNameLength, AllowNamesEndingInNumbers, AllowedNames, ForbiddenNames.
# AllowedNames: as, at, by, cc, db, id, if, in, io, ip, of, on, os, pp, to
Naming/MethodParameterName:
Exclude:
- 'lib/moxml/adapter/customized_oga/xml_generator.rb'
- 'lib/moxml/adapter/nokogiri.rb'
- 'lib/moxml/adapter/ox.rb'
- 'lib/moxml/attribute.rb'

# Offense count: 27
# Configuration parameters: AllowedConstants.
Style/Documentation:
Enabled: false
10 changes: 5 additions & 5 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ source "https://rubygems.org"
# Specify your gem's dependencies in moxml.gemspec
gemspec

gem "byebug"
gem "get_process_mem", "~> 1.0.0"
gem "nokogiri", "~> 1.15"
gem "oga", "~> 3.4"
gem "ox", "~> 2.14"
gem "rake", "~> 13"
gem "rspec", "~> 3.0"
gem "rubocop", "~> 1.21"
gem "nokogiri", "~> 1.15"
gem "ox", "~> 2.14"
gem "oga", "~> 3.4"
gem 'get_process_mem', "~> 1.0.0"
gem "tempfile", "~> 0.3.1"
gem "byebug"
2 changes: 2 additions & 0 deletions lib/moxml.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Moxml
class << self
def new(adapter = nil, &block)
Expand Down
2 changes: 2 additions & 0 deletions lib/moxml/adapter.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require_relative "adapter/base"

module Moxml
Expand Down
2 changes: 2 additions & 0 deletions lib/moxml/adapter/base.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require_relative "../xml_utils"
require_relative "../document_builder"

Expand Down
4 changes: 3 additions & 1 deletion lib/moxml/adapter/customized_oga/xml_declaration.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# frozen_string_literal: true

require "oga"

module Moxml
module Adapter
module CustomizedOga
class XmlDeclaration < ::Oga::XML::XmlDeclaration
def initialize(options = {})
@version = options[:version] || '1.0'
@version = options[:version] || "1.0"
# encoding is optional, but Oga sets it to UTF-8 by default
@encoding = options[:encoding]
@standalone = options[:standalone]
Expand Down
37 changes: 18 additions & 19 deletions lib/moxml/adapter/customized_oga/xml_generator.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# rubocop:disable Style/FrozenStringLiteralComment

require "oga"

# monkey patch the Oga generator because it's not configurable
Expand All @@ -14,24 +16,23 @@ def self_closing?(_element)
def on_element(element, output)
name = element.expanded_name

namespace_definitions = ''
element.namespaces.values.each do |ns|
namespace_definitions << ' '
namespace_definitions = ""
element.namespaces.each_value do |ns|
namespace_definitions << " "
on_namespace_definition(ns, namespace_definitions)
end

attrs = ''
attrs = ""
element.attributes.each do |attr|
attrs << ' '
attrs << " "
on_attribute(attr, attrs)
end

closing_tag =
if self_closing?(element)
html_void_element?(element) ? '>' : ' />'
else
">"
end
closing_tag = if self_closing?(element)
html_void_element?(element) ? ">" : " />"
else
">"
end

output << "<#{name}#{namespace_definitions}#{attrs}#{closing_tag}"
end
Expand All @@ -40,25 +41,23 @@ def on_namespace_definition(ns, output)
name = "xmlns"
name += ":#{ns.name}" unless ns.name.nil?

output << %Q(#{name}="#{ns.uri}")
output << %(#{name}="#{ns.uri}")
end

def on_attribute(attr, output)
return super unless attr.value&.include?("'")

output << %Q(#{attr.expanded_name}="#{encode(attr.value)}")
output << %(#{attr.expanded_name}="#{encode(attr.value)}")
end

def on_cdata(node, output)
# Escape the end sequence
return super unless node.text.include?("]]>")

chunks = node.text.split(/(\]\]>)/)
if chunks.size == 1
chunks = ["]]", ">"]
end
chunks = ["]]", ">"] if chunks.size == 1

while index = chunks.index("]]>") do
while (index = chunks.index("]]>"))
# the end tag cannot be the first and the last at the same time

if index.zero?
Expand Down Expand Up @@ -92,7 +91,7 @@ def on_processing_instruction(node, output)
def on_xml_declaration(node, output)
super
# remove the space before the closing tag
output.gsub!(/ \?\>$/, '?>')
output.gsub!(/ \?>$/, "?>")
end

protected
Expand Down
36 changes: 19 additions & 17 deletions lib/moxml/adapter/nokogiri.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require_relative "base"
require "nokogiri"

Expand All @@ -11,28 +13,28 @@ def set_root(doc, element)

def parse(xml, options = {})
native_doc = begin
if options[:fragment]
::Nokogiri::XML::DocumentFragment.parse(xml) do |config|
config.strict.nonet
config.recover unless options[:strict]
end
else
::Nokogiri::XML(xml, nil, options[:encoding]) do |config|
config.strict.nonet
config.recover unless options[:strict]
end
if options[:fragment]
::Nokogiri::XML::DocumentFragment.parse(xml) do |config|
config.strict.nonet
config.recover unless options[:strict]
end
else
::Nokogiri::XML(xml, nil, options[:encoding]) do |config|
config.strict.nonet
config.recover unless options[:strict]
end
rescue ::Nokogiri::XML::SyntaxError => e
raise Moxml::ParseError.new(e.message, line: e.line, column: e.column)
end
rescue ::Nokogiri::XML::SyntaxError => e
raise Moxml::ParseError.new(e.message, line: e.line, column: e.column)
end

DocumentBuilder.new(Context.new(:nokogiri)).build(native_doc)
end

def create_document
::Nokogiri::XML::Document.new
end

def create_fragment
# document fragments are weird and should be used with caution:
# https://github.com/sparklemotion/nokogiri/issues/572
Expand Down Expand Up @@ -93,7 +95,7 @@ def set_declaration_attribute(declaration, attr_name, value)
end

declaration.native_content =
attrs.map { |k, v| %{#{k}="#{v}"} }.join(" ")
attrs.map { |k, v| %(#{k}="#{v}") }.join(" ")
end

def set_namespace(element, ns)
Expand Down Expand Up @@ -279,13 +281,13 @@ def serialize(node, options = {})

# Don't force expand empty elements if they're really empty
save_options |= ::Nokogiri::XML::Node::SaveOptions::NO_EMPTY_TAGS if options[:expand_empty]
save_options |= ::Nokogiri::XML::Node::SaveOptions::FORMAT if options[:indent].to_i > 0
save_options |= ::Nokogiri::XML::Node::SaveOptions::FORMAT if options[:indent].to_i.positive?
save_options |= ::Nokogiri::XML::Node::SaveOptions::NO_DECLARATION if options[:no_declaration]

node.to_xml(
indent: options[:indent],
encoding: options[:encoding],
save_with: save_options,
save_with: save_options
)
end

Expand All @@ -295,7 +297,7 @@ def build_declaration_attrs(version, encoding, standalone)
attrs = { "version" => version }
attrs["encoding"] = encoding if encoding
attrs["standalone"] = standalone if standalone
attrs.map { |k, v| %{#{k}="#{v}"} }.join(" ")
attrs.map { |k, v| %(#{k}="#{v}") }.join(" ")
end

def current_declaration_attributes(declaration)
Expand Down
Loading

0 comments on commit 7fd4839

Please sign in to comment.