Skip to content

Commit

Permalink
refactor(utils): improve CSSProcessor class
Browse files Browse the repository at this point in the history
- Added a method to remove comments from CSS content
- Refactored the process_imports method to handle multiple import patterns
- Improved the process_variables method to handle multiple :root blocks
  • Loading branch information
amnweb committed Oct 20, 2024
1 parent d41d3a7 commit 0e822d1
Showing 1 changed file with 30 additions and 22 deletions.
52 changes: 30 additions & 22 deletions src/core/utils/css_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,37 +19,45 @@ def read_css_file(self, file_path: str) -> str:
except (FileNotFoundError, OSError) as e:
logging.error(f"CSSProcessor Error '{file_path}': {e}")
return ''


def remove_comments(self):
self.css_content = re.sub(r'/\*.*?\*/|//.*', '', self.css_content, flags=re.DOTALL)

def process_imports(self):
import_patterns = [
re.compile(r'@import\s+url\(([^)]+)\);'),
re.compile(r'@import\s+"([^"]+)";')
]

for import_pattern in import_patterns:
while True:
match = import_pattern.search(self.css_content)
if not match:
break
import_path = match.group(1).strip('\'"')
full_import_path = os.path.join(self.base_path, import_path)
imported_css = self.read_css_file(full_import_path)
if imported_css:
self.css_content = self.css_content.replace(match.group(0), imported_css)
self.imported_files.append(full_import_path)
else:
self.css_content = self.css_content.replace(match.group(0), '')
while True:
initial_content = self.css_content
for import_pattern in import_patterns:
matches = import_pattern.findall(self.css_content)
for match in matches:
import_path = match.strip('\'"')
full_import_path = os.path.normpath(os.path.join(self.base_path, import_path))
imported_css = self.read_css_file(full_import_path)
if imported_css:
self.css_content = self.css_content.replace(f'@import url({match});', imported_css)
self.css_content = self.css_content.replace(f'@import "{match}";', imported_css)
self.imported_files.append(full_import_path)
else:
self.css_content = self.css_content.replace(f'@import url({match});', '')
self.css_content = self.css_content.replace(f'@import "{match}";', '')
if self.css_content == initial_content:
self.remove_comments()
break

def process_variables(self):
root_vars = {}
root_match = re.search(r':root\s*{([^}]*)}', self.css_content)
if root_match:
root_content = root_match.group(1)
root_matches = re.findall(r':root\s*{([^}]*)}', self.css_content)
for root_content in root_matches:
var_matches = re.findall(r'--([^:]+):\s*([^;]+);', root_content)
root_vars = {f'--{name.strip()}': value.strip() for name, value in var_matches}
self.css_content = re.sub(r':root\s*{[^}]*}', '', self.css_content)
for var_name, var_value in root_vars.items():
self.css_content = self.css_content.replace(f'var({var_name})', var_value)
root_vars.update({f'--{name.strip()}': value.strip() for name, value in var_matches})

self.css_content = re.sub(r':root\s*{[^}]*}', '', self.css_content)
for var_name, var_value in root_vars.items():
self.css_content = self.css_content.replace(f'var({var_name})', var_value)
self.remove_comments()

def process(self) -> str:
if not self.css_content:
Expand Down

0 comments on commit 0e822d1

Please sign in to comment.