Skip to content

Commit 843a8ff

Browse files
committed
Added typing to help with readability. Also rewroe the readme.
1 parent 1fd022f commit 843a8ff

File tree

3 files changed

+12
-10
lines changed

3 files changed

+12
-10
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,12 @@ I eventually started using the `tinycss2` library, which parses CSS text into a
9393

9494
- This is a very basic implementation, but it has served me well so far. There are certain edge cases that the library does not handle. For example, it struggles with nested classes (a child element within a parent element with a specific class). This nested class example will break the script.
9595

96-
- `Not` selectors and complex `pseudo-selectors` are also tricky and not detected. Feel free to open an issue for any bugs you encounter. If it's feasible, we'll fix them.
96+
- CSS Combinators and complex `pseudo-selectors` are also tricky and not detected. Feel free to open an issue for any bugs you encounter. If it's feasible, we'll fix them.
9797

9898
- The formatting of the output files is a bit janky. In the future, we can clean this up, but for now, it's easy enough to reformat CSS and HTML in a code editor or IDE.
9999

100100
- The tool currently only works with single files. If the CSS is split across multiple files on a website, you will have to manually combine them for the `input.css`. Similarly, if CSS is embedded in a style tag within the HTML, you'll need to extract that and place it in a separate file.
101101

102102
- Styling and look is heavily affected by the css reset's applied. Make sure that you're using a similar reset file. For example, if your cloning tailwind css, make sure to include the tailwind css reset. Other styling frameworks should work the same.
103103

104-
- If a class is not found in the input stylesheet, the library makes a best note to mark which class could not be found. this appears right after the css class defination as a comment.
104+
- If a class is not found in the input stylesheet, the library makes a best note to mark which class could not be found. this appears right after the css class defination as a comment.

main.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import re
22
import json
33
import tinycss2
4+
from typing import List
45
from at_media_handling import extract_class_from_media_queries, combine_matching_media_queries, combine_nested_classes_in_media
56
from psudo_selector import split_css_classes_and_pseudo, split_css_by_pseudo_selector, combine_css_classes_with_pseudo
67
import logging
@@ -12,7 +13,7 @@
1213
# This formatting is used across all the scripts
1314
LOGGING_FORMATING = "%(asctime)s - %(filename)s:%(funcName)s:%(lineno)s - %(levelname)s - %(message)s"
1415

15-
def extract_regular_css_class(css_content, wanted_class_name):
16+
def extract_regular_css_class(css_content, wanted_class_name) -> str:
1617
rules = tinycss2.parse_stylesheet_bytes(css_content, skip_whitespace=True, skip_comments=True)
1718
extracted_css_nodes = []
1819
for rule in rules[0]:
@@ -27,7 +28,7 @@ def extract_regular_css_class(css_content, wanted_class_name):
2728
extracted_css = tinycss2.serialize(extracted_css_nodes)
2829
return extracted_css
2930

30-
def combine_regular_css_classes(css_contents, combined_class_name):
31+
def combine_regular_css_classes(css_contents: str, combined_class_name: str) -> str:
3132
# Updated regex pattern to match class selectors with dashes and underscores
3233
class_pattern = re.compile(r'\.([a-zA-Z0-9-_]+)\s*{([^}]*)}', re.DOTALL)
3334

@@ -89,7 +90,7 @@ def tailwind_keepout(input_class_name: str):
8990
return True
9091
return False
9192

92-
def css_class_converter(css_content: str, new_class_name: str, class_name_array: [str]):
93+
def css_class_converter(css_content: str, new_class_name: str, class_name_array: List[str]):
9394
regular_css_buffer = ''
9495
at_media_css_buffer = ''
9596
not_found_regular_css_class = []

psudo_selector.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
import tinycss2
33
import logging
44
from collections import defaultdict
5+
from typing import List, Tuple
56

67
logger = logging.getLogger(__name__)
78

8-
def does_prelude_contain_psuodo_selector(prelude):
9+
def does_prelude_contain_psuodo_selector(prelude) -> bool:
910
for item in prelude:
1011
if isinstance(item, tinycss2.ast.LiteralToken):
1112
if item.value == ":":
@@ -23,7 +24,7 @@ def get_psudo_selector_from_prelude(prelude):
2324
return my_psudo
2425
return None
2526

26-
def split_css_classes_and_pseudo(css_content):
27+
def split_css_classes_and_pseudo(css_content: str) -> Tuple[str, str]:
2728
byte_thing = str.encode(css_content)
2829
rules = tinycss2.parse_stylesheet_bytes(byte_thing, skip_whitespace=True, skip_comments=True)
2930
regular_classes = []
@@ -41,7 +42,7 @@ def split_css_classes_and_pseudo(css_content):
4142
psudo_class_string = tinycss2.serialize(psedo_selector_classes)
4243
return regular_class_string, psudo_class_string
4344

44-
def split_css_by_pseudo_selector(css_content):
45+
def split_css_by_pseudo_selector(css_content: str) -> dict:
4546
pseudo_styles_string_dict = {}
4647
if css_content == "":
4748
logger.error("no css content for psudo selector")
@@ -66,8 +67,8 @@ def split_css_by_pseudo_selector(css_content):
6667

6768
return pseudo_styles_string_dict
6869

69-
def combine_css_classes_with_pseudo(pseudo_selector: str, css_content:str, combine_class_name: str):
70-
combined_styles = []
70+
def combine_css_classes_with_pseudo(pseudo_selector: str, css_content:str, combine_class_name: str) -> str:
71+
combined_styles: List[str] = []
7172
byte_thing = str.encode(css_content)
7273
rules = tinycss2.parse_stylesheet_bytes(byte_thing, skip_whitespace=True, skip_comments=True)
7374
for rule in rules[0]:

0 commit comments

Comments
 (0)