|
| 1 | +# API Documentation for HtmlScssClassChecker Gem |
| 2 | + |
| 3 | +The `HtmlScssClassChecker` gem provides a Ruby API for identifying unused or unmatched HTML and SCSS classes in your web projects. This document outlines how to use the gem's API. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +Add this line to your application's Gemfile: |
| 8 | + |
| 9 | +```ruby |
| 10 | +gem 'html_scss_class_checker' |
| 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 html_scss_class_checker |
| 23 | +``` |
| 24 | + |
| 25 | +## Usage |
| 26 | + |
| 27 | +### Class Extraction |
| 28 | + |
| 29 | +The gem provides methods to extract class names from HTML and SCSS files. |
| 30 | + |
| 31 | +#### HTML Class Extraction |
| 32 | + |
| 33 | +```ruby |
| 34 | +require 'html_scss_class_checker' |
| 35 | + |
| 36 | +html_content = "<div class='test-class another-class'></div>" |
| 37 | +html_classes = HtmlScssClassChecker::ClassExtractor.extract_from_html(html_content) |
| 38 | + |
| 39 | +puts html_classes |
| 40 | +# Output: Set['test-class', 'another-class'] |
| 41 | +``` |
| 42 | + |
| 43 | +#### SCSS Class Extraction |
| 44 | + |
| 45 | +```ruby |
| 46 | +scss_content = ".test-class { color: red; } .another-class { color: blue; }" |
| 47 | +scss_classes = HtmlScssClassChecker::ClassExtractor.extract_from_scss(scss_content) |
| 48 | + |
| 49 | +puts scss_classes |
| 50 | +# Output: Set['test-class', 'another-class'] |
| 51 | +``` |
| 52 | + |
| 53 | +### File Processing |
| 54 | + |
| 55 | +You can process individual files to list the classes used in them. |
| 56 | + |
| 57 | +```ruby |
| 58 | +html_file_processor = HtmlScssClassChecker::FileProcessor.new('path/to/file.html', 'html') |
| 59 | +html_classes = html_file_processor.list_classes |
| 60 | + |
| 61 | +scss_file_processor = HtmlScssClassChecker::FileProcessor.new('path/to/style.scss', 'scss') |
| 62 | +scss_classes = scss_file_processor.list_classes |
| 63 | +``` |
| 64 | + |
| 65 | +### Class Checking |
| 66 | + |
| 67 | +The main functionality of the gem is to check for unmatched classes between HTML and SCSS files. |
| 68 | + |
| 69 | +```ruby |
| 70 | +config = { |
| 71 | + 'known_classes' => ['known-class1', 'known-class2'], |
| 72 | + 'html_directories' => ['path/to/html/files'], |
| 73 | + 'scss_directories' => ['path/to/scss/files'] |
| 74 | +} |
| 75 | + |
| 76 | +checker = HtmlScssClassChecker::ClassChecker.new(config) |
| 77 | +checker.check |
| 78 | + |
| 79 | +puts "Unmatched Classes: #{checker.unmatched_classes.to_a}" |
| 80 | +puts "Classes in each file: #{checker.file_class_mapping}" |
| 81 | +``` |
| 82 | + |
| 83 | +## Configuration |
| 84 | + |
| 85 | +The `ClassChecker` requires a configuration hash with the following keys: |
| 86 | + |
| 87 | +- `known_classes`: An array of class names that are known and should not be reported as unmatched. |
| 88 | +- `html_directories`: An array of directories to search for HTML files. |
| 89 | +- `scss_directories`: An array of directories to search for SCSS files. |
| 90 | + |
| 91 | +## Contributing |
| 92 | + |
| 93 | +Contributions are welcome, and they are greatly appreciated! Every little bit helps, and credit will always be given. Please see [CONTRIBUTING.md](/CONTRIBUTING.md) for details. |
| 94 | + |
| 95 | +## License |
| 96 | + |
| 97 | +The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT). |
0 commit comments