Skip to content

Instantly share code, notes, and snippets.

@KimSarabia
Created August 31, 2023 22:02
Show Gist options
  • Save KimSarabia/1b43da141c72ad43ebf242807d98de17 to your computer and use it in GitHub Desktop.
Save KimSarabia/1b43da141c72ad43ebf242807d98de17 to your computer and use it in GitHub Desktop.
Quick tool for grabbing all font-size rules in a directory
import csv
import os
import re # Import the re module for regular expressions
# Define the directory path containing Less files
less_directory = 'YOUR_DIRECTORY_PATH'
output_csv_file = 'font_sizes.csv' # Define the output CSV file name
# Initialize a list to store font-size values along with filename, line number, class name/ID
font_sizes_info = []
# Define the regular expression pattern to match 'font-size' property
font_size_pattern = r'font-size:\s*([^;]+);'
# Iterate over all files in the directory and its subdirectories
for root, dirs, files in os.walk(less_directory):
for file in files:
if file.endswith(".less"):
file_path = os.path.join(root, file)
# Open and read each Less file
try:
with open(file_path, 'r') as less_file:
lines = less_file.readlines()
line_num = 0
while line_num < len(lines):
line = lines[line_num]
# Search for 'font-size' property in each line
match = re.search(font_size_pattern, line)
if match:
font_size = match.group(1).strip()
# Search for the nearest preceding class name or ID
class_name_id = "N/A" # Default value
for i in range(line_num, -1, -1):
prev_line = lines[i].strip()
if prev_line and (prev_line[0] == '.' or prev_line[0] == '#'):
class_name_id = prev_line.split('{')[0].strip() # Remove the '{'
break
# Append information to the list
font_sizes_info.append((file_path, line_num + 1, class_name_id, font_size))
line_num += 1
except Exception as e:
print(f"An error occurred while processing '{file_path}': {str(e)}")
# Check if any 'font-size' values were found
if font_sizes_info:
print(f"'font-size' values found in {less_directory}:")
# Write 'font-size' values along with filename, line number, class name/ID to a CSV file
with open(output_csv_file, 'w', newline='') as csv_file:
csv_writer = csv.writer(csv_file)
csv_writer.writerow(['File Name', 'Line Number', 'Class Name/ID', 'Font Sizes'])
csv_writer.writerows(font_sizes_info)
print(f"Font sizes, along with cleaned class names/IDs, have been written to '{output_csv_file}'")
else:
print(f'No "font-size" values found in any Less files in the directory: {less_directory}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment