Miniparser 1.0.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: f0f1628ed2308d38a4f6770e20fefafed643600019cfc6d120d791dafbd1e9b2
4
+ data.tar.gz: 615e5495cab6b7b543fbeaeb1f43481461fedbcbda2d5017e15f905d2a9ad737
5
+ SHA512:
6
+ metadata.gz: 4111f751778605a9dfcc54f4176c5561ce20564743e35117e8746675c9cb9547ffea9dea5acff78ded5266014f7bad86e1acb5c99c286794435ba0a11350e718
7
+ data.tar.gz: 5e4a8fd2377205e39cfcab82ecbbf5fafb44b9d4d55b77596ced731f5b902e15103b6fc116174c75a9022e41f4e3fc780fc01dccf9d90802595b4073cd357bd5
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2023-08-23
4
+
5
+ - Initial release
@@ -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 ceo@pixelatedstudios.net. 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/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in Miniparse.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
12
+ gem "rubocop", "~> 1.21"
data/LICENSE ADDED
@@ -0,0 +1,9 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 PixelRidge-Softworks
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/Miniparser/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'Miniparser'
7
+ spec.version = Miniparser::VERSION
8
+ spec.authors = ['VetheonGames']
9
+ spec.email = ['ceo@pixelatedstudios.net']
10
+
11
+ spec.summary = 'A Gem for Validating and Minifying HTML, CSS, and JS smartly'
12
+ spec.description = 'Miniparser works pretty simply. You pass the Gem some input (HTML, JS, or CSS), and it will
13
+ validate it, return the validated status, then minify it, and return the minified version as either
14
+ a file (path) and a text return, respective to what input it got'
15
+ spec.homepage = 'https://git.pixelridgesoftworks.com/PixelRidge-Softworks/Miniparser'
16
+ spec.license = 'MIT'
17
+ spec.required_ruby_version = '>= 3.2.2'
18
+
19
+ spec.metadata['homepage_uri'] = spec.homepage
20
+ spec.metadata['source_code_uri'] = 'https://git.pixelridgesoftworks.com/PixelRidge-Softworks/Miniparser'
21
+ spec.metadata['changelog_uri'] = 'https://git.pixelridgesoftworks.com/PixelRidge-Softworks/Miniparser/src/branch/main/CHANGELOG.md'
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ spec.files = Dir.glob('{bin,lib,sig}/**/*') + Dir.glob('*').reject do |f|
25
+ f.start_with?('spec', '.rspec', 'Miniparser.gemspec')
26
+ end
27
+ spec.files << 'LICENSE'
28
+ spec.files << 'README.md'
29
+ spec.files << 'Miniparser.gemspec'
30
+
31
+ spec.require_paths = ['lib']
32
+ end
data/README.md ADDED
@@ -0,0 +1,61 @@
1
+ # Miniparse
2
+
3
+ Miniparse is a Ruby gem that takes code as input (HTML, JavaScript, or CSS), determines the code language, validates it, and then minifies it. It returns whether the code is valid or not, and if valid, provides the minified code.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'Miniparse'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ ```bash
16
+ $ bundle install
17
+ ```
18
+
19
+ Or install it yourself as:
20
+
21
+ ```bash
22
+ $ gem install Miniparse
23
+ ```
24
+
25
+ ## Usage
26
+
27
+ Miniparse provides two main methods to process either a file or a string containing HTML, JavaScript, or CSS code.
28
+
29
+ ### Processing a File
30
+
31
+ You can process a file by calling the `type_file` method and passing the file path as an argument:
32
+
33
+ ```ruby
34
+ result = Miniparse::Processor.type_file('path/to/yourfile.html')
35
+ ```
36
+
37
+ ### Processing a String
38
+
39
+ You can process a string containing code by calling the `type_string` method:
40
+
41
+ ```ruby
42
+ result = Miniparse::Processor.type_string('<html><head></head><body></body></html>')
43
+ ```
44
+
45
+ Both methods return a hash containing the validation status, any errors, and the minified code if the input was valid:
46
+
47
+ ```ruby
48
+ {
49
+ valid: true or false,
50
+ errors: "Error message if any",
51
+ minified_code: "Minified code if valid"
52
+ }
53
+ ```
54
+
55
+ ## Contributing
56
+
57
+ Bug reports and pull requests are welcome. Please adhere to the code of conduct.
58
+
59
+ ## License
60
+
61
+ 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 "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]
data/bin/console ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "Miniparser"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ require "irb"
11
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Miniparser
4
+ VERSION = "1.0.0"
5
+ end
data/lib/Miniparser.rb ADDED
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'Miniparser/version'
4
+ require_relative 'lang_detect'
5
+ require_relative 'lang_minify'
6
+ require_relative 'lang_validate'
7
+
8
+ module Miniparser
9
+ class Error < StandardError; end
10
+
11
+ class Processor
12
+ def self.type_file(file_path)
13
+ code = File.read(file_path)
14
+ result = process_code(code)
15
+ if result[:valid]
16
+ output_file_path = file_path.gsub(/(\.\w+)$/, '_minified\1')
17
+ File.write(output_file_path, result[:minified_code])
18
+ result[:minified_code] = output_file_path
19
+ end
20
+ result
21
+ end
22
+
23
+ def self.type_string(code_string)
24
+ process_code(code_string)
25
+ end
26
+
27
+ def self.process_code(code)
28
+ language_detector = LanguageDetector.new
29
+ language = language_detector.detect(code)
30
+ validator = Validator.new(language)
31
+ valid, errors = validator.validate(code)
32
+ minifier = Minifier.new(language)
33
+ minified_code = minifier.minify(code) if valid
34
+
35
+ { valid:, errors:, minified_code: }
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Miniparser
4
+ class LanguageDetector
5
+ def detect(code)
6
+ return :html if code.match?(/<\s*html.*>|<\s*head.*>|<\s*body.*>/i)
7
+ return :css if code.match?(/^\s*(\.\w+|#\w+|body)\s*\{/)
8
+ return :js if code.match?(/function\s+\w+\s*\(|var\s+\w+\s*=|let\s+\w+\s*=|const\s+\w+\s*=/)
9
+
10
+ :unknown
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Miniparser
4
+ class Minifier
5
+ def initialize(language)
6
+ @language = language
7
+ end
8
+
9
+ def minify(code)
10
+ case @language
11
+ when :html
12
+ minify_html(code)
13
+ when :js
14
+ minify_js(code)
15
+ when :css
16
+ minify_css(code)
17
+ else
18
+ raise "Unknown language: #{@language}"
19
+ end
20
+ end
21
+
22
+ private
23
+
24
+ def minify_html(code)
25
+ code.gsub(%r{>\s*</}, '> </').gsub(/>\s+</, '><').strip
26
+ end
27
+
28
+ def minify_js(code)
29
+ # Remove single-line and multi-line comments, and all unnecessary whitespace
30
+ code.gsub(%r{//.*$}, '').gsub(%r{/\*.*?\*/}m, '').gsub(/\s+/, ' ').strip
31
+ end
32
+
33
+ def minify_css(code)
34
+ # Remove comments and all unnecessary whitespace, including newlines
35
+ code.gsub(%r{/\*.*?\*/}m, '').gsub(/\s+/, '').strip
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,78 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'English'
4
+ module Miniparser
5
+ class Validator
6
+ def initialize(language)
7
+ @language = language
8
+ end
9
+
10
+ def validate(code)
11
+ case @language
12
+ when :html
13
+ validate_html(code)
14
+ when :css
15
+ validate_css(code)
16
+ when :js
17
+ validate_js(code)
18
+ else
19
+ raise "Unknown language: #{@language}"
20
+ end
21
+ end
22
+
23
+ private
24
+
25
+ def validate_html(code)
26
+ # List of self-closing singleton tags in HTML5
27
+ singleton_tags = %w[area base br col embed hr img input link meta param source track wbr]
28
+
29
+ # Stack to keep track of opening tags
30
+ stack = []
31
+
32
+ # Iterate through all the tags in the code
33
+ code.scan(%r{</?\w+[^>]*>}) do |tag|
34
+ tag_name = tag.match(%r{</?(\w+)})[1]
35
+
36
+ # Ignore singleton tags and declarations like <!DOCTYPE html>
37
+ next if singleton_tags.include?(tag_name) || tag.start_with?('<!')
38
+
39
+ if tag.start_with?('</') # Closing tag
40
+ # Check if the closing tag matches the last opening tag
41
+ if stack.last == tag_name
42
+ stack.pop
43
+ else
44
+ return [false, "Mismatched HTML tags: expected </#{stack.last}> but found #{tag}"]
45
+ end
46
+ else # Opening tag
47
+ stack.push(tag_name)
48
+ end
49
+ end
50
+
51
+ # Check if there are any unmatched opening tags
52
+ if stack.empty?
53
+ [true, nil]
54
+ else
55
+ [false, "Mismatched HTML tags: unmatched <#{stack.last}>"]
56
+ end
57
+ end
58
+
59
+ def validate_css(code)
60
+ # Basic validation: check for balanced curly braces
61
+ valid = code.count('{') == code.count('}')
62
+ [valid, valid ? nil : 'Mismatched CSS curly braces']
63
+ end
64
+
65
+ require 'tempfile'
66
+ require 'shellwords'
67
+
68
+ def validate_js(code)
69
+ Tempfile.create(['temp', '.js']) do |temp_file|
70
+ temp_file.write(code)
71
+ temp_file.close
72
+ output = `node -c #{Shellwords.escape(temp_file.path)} 2>&1`
73
+ success = $CHILD_STATUS.success?
74
+ [success, success ? nil : output]
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,4 @@
1
+ module Miniparser
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: Miniparser
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - VetheonGames
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-08-24 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: |-
14
+ Miniparser works pretty simply. You pass the Gem some input (HTML, JS, or CSS), and it will
15
+ validate it, return the validated status, then minify it, and return the minified version as either
16
+ a file (path) and a text return, respective to what input it got
17
+ email:
18
+ - ceo@pixelatedstudios.net
19
+ executables: []
20
+ extensions: []
21
+ extra_rdoc_files: []
22
+ files:
23
+ - CHANGELOG.md
24
+ - CODE_OF_CONDUCT.md
25
+ - Gemfile
26
+ - LICENSE
27
+ - Miniparser.gemspec
28
+ - README.md
29
+ - Rakefile
30
+ - bin/console
31
+ - bin/setup
32
+ - lib/Miniparser.rb
33
+ - lib/Miniparser/version.rb
34
+ - lib/lang_detect.rb
35
+ - lib/lang_minify.rb
36
+ - lib/lang_validate.rb
37
+ - sig/Miniparser.rbs
38
+ homepage: https://git.pixelridgesoftworks.com/PixelRidge-Softworks/Miniparser
39
+ licenses:
40
+ - MIT
41
+ metadata:
42
+ homepage_uri: https://git.pixelridgesoftworks.com/PixelRidge-Softworks/Miniparser
43
+ source_code_uri: https://git.pixelridgesoftworks.com/PixelRidge-Softworks/Miniparser
44
+ changelog_uri: https://git.pixelridgesoftworks.com/PixelRidge-Softworks/Miniparser/src/branch/main/CHANGELOG.md
45
+ post_install_message:
46
+ rdoc_options: []
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: 3.2.2
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ requirements: []
60
+ rubygems_version: 3.5.0.dev
61
+ signing_key:
62
+ specification_version: 4
63
+ summary: A Gem for Validating and Minifying HTML, CSS, and JS smartly
64
+ test_files: []