You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+18-11
Original file line number
Diff line number
Diff line change
@@ -1,16 +1,18 @@
1
-
# CSS Extractor
1
+

2
2
3
-
The handiest script for UI development. Copy your favorite elements from the internet and get back `HTML` and `CSS` files that you can use and modify right away. No more manually copying and pasting elements from stylesheets.
3
+
The handiest script for UI development. Copy your favorite elements from the internet and get back `HTML` and `CSS` files that you can use and modify right away. No more manually copying and pasting definitions from stylesheets.
4
4
5
5
## Installation
6
6
7
-
1. Create a Python Virtual Environment
7
+
1.**Create a Python Virtual Environment**
8
+
8
9
You can use `conda` or something else, but this is the fastest for me.
9
10
```
10
11
python -m venv /path/to/new/virtual/environment
11
12
```
12
13
13
-
2. Download Dependencies
14
+
2.**Download Dependencies**
15
+
14
16
Make sure to activate your virtual environment before running!
We first analyze the input HTML markup and create a map of old classes to new ones.
25
+
1.**HTML Extraction**
26
+
27
+
We first analyze the input HTML markup and create a map of old classes to new ones.
25
28
26
29
For example, if you have an HTML element like the following:
27
30
```html
@@ -30,19 +33,23 @@ For example, if you have an HTML element like the following:
30
33
31
34
We would generate a mapping that is JSON-encoded like this:
32
35
```json
36
+
[
33
37
{
34
38
"newClass": "RANDOM_STRING_CLASS_NAME",
35
39
"oldClasses": "big, red, slant"
36
40
}
41
+
]
37
42
```
38
43
39
44
And produce an output HTML file like this:
40
45
```html
41
46
<h1class="RANDOM_STRING_CLASS_NAME">This is a header</h1>
42
47
```
43
48
44
-
2. CSS Extraction
45
-
The JSON file generated in the previous step is then fed into the CSS extraction process, which builds our new class with all the styles from the old classes. The output class will have the old class names annotated in the CSS as comments for debugging purposes.
49
+
2.**CSS Extraction**
50
+
51
+
The JSON file generated in the previous step is then fed into the CSS extraction process, which builds our new class with all the styles from the old classes. The output class will have the old class names annotated in the CSS as comments for debugging purposes.
52
+
46
53
```css
47
54
.RANDOM_STRING_CLASS_NAME {
48
55
/* big */
@@ -78,15 +85,15 @@ After years of manually extracting styles from websites I liked, I decided to cr
78
85
79
86
## How does it work?
80
87
81
-
The first iteration used regex to find and grab the CSS definitions from the desired input classes. However, this approach turned out to be problematic because CSS is complex, especially when you're dealing with @media queries and pseudo-selectors. There are still some leftover functions that use regex matching, as I didn't bother to rewrite them.
88
+
The first iteration used regex to find and grab the CSS definitions from the desired input classes. However, this approach turned out to be problematic because CSS is complex, especially when you're dealing with `@media` queries and `pseudo-selectors`. There are still some leftover functions that use regex matching, as I didn't bother to rewrite them.
82
89
83
-
I eventually started using the `tinycss2` library, which parses CSS text into a format that can be programmatically analyzed. This is much better for handling @media queries and pseudo-selectors. This approach is a bit slow, as it involves many nested loops, and the library is not optimized for speed. Once we resolve the edge cases, we can focus on improving the program's performance.
90
+
I eventually started using the `tinycss2` library, which parses CSS text into a format that can be programmatically analyzed. This is much better for handling `@media` queries and `pseudo-selectors`. This approach is a bit slow, as it involves many nested loops, and the library is not optimized for speed. Once we resolve the edge cases, we can focus on improving the program's performance.
84
91
85
92
## Caveats
86
93
87
94
- 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.
88
95
89
-
-"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
+
-`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.
90
97
91
98
- 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.
0 commit comments