Class: Mint::CssParser
- Inherits:
-
Object
- Object
- Mint::CssParser
- Defined in:
- lib/mint/css_parser.rb
Overview
Parses CSS files to extract @import statements and calculate relative paths
Class Method Summary collapse
-
.extract_imports(css_content) ⇒ Array<String>
Extracts @import statements from CSS content.
-
.generate_link_tags(css_file_paths) ⇒ String
Generates HTML link tags for CSS files.
-
.resolve_css_files(main_css_path, html_output_path) ⇒ Array<String>
Resolves all CSS files (main + imports) and calculates their paths relative to HTML output.
Class Method Details
.extract_imports(css_content) ⇒ Array<String>
Extracts @import statements from CSS content
11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/mint/css_parser.rb', line 11 def self.extract_imports(css_content) imports = [] # Match @import statements with various formats: # @import "file.css"; # @import 'file.css'; # @import url("file.css"); # @import url('file.css'); css_content.scan(/@import\s+(?:url\()?['"]([^'"]+)['"](?:\))?;?/i) do |match| imports << match[0] end imports end |
.generate_link_tags(css_file_paths) ⇒ String
Generates HTML link tags for CSS files
70 71 72 73 74 |
# File 'lib/mint/css_parser.rb', line 70 def self.(css_file_paths) css_file_paths.map do |css_path| %Q{<link rel="stylesheet" href="#{css_path}">} end.join("\n ") end |
.resolve_css_files(main_css_path, html_output_path) ⇒ Array<String>
Resolves all CSS files (main + imports) and calculates their paths relative to HTML output
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/mint/css_parser.rb', line 31 def self.resolve_css_files(main_css_path, html_output_path) css_files = [] main_css_file = Pathname.new(main_css_path) html_file = Pathname.new(html_output_path) # Add the main CSS file css_files << main_css_file.relative_path_from(html_file.dirname).to_s # Only process if main CSS file exists and is a .css file return css_files unless main_css_file.exist? && main_css_file.extname == '.css' begin css_content = File.read(main_css_path) imports = extract_imports(css_content) imports.each do |import_path| # Resolve import path relative to the main CSS file's directory import_file = main_css_file.dirname + import_path # Only include if it's a .css file and exists if import_file.exist? && import_file.extname == '.css' # Calculate path relative to HTML output relative_import = import_file.relative_path_from(html_file.dirname).to_s css_files << relative_import end end rescue => e # If we can't read the CSS file, just return the main file # This allows the system to gracefully handle missing or unreadable files end css_files end |