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
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.
4
+
3
5
## Installation
4
6
5
7
1. Create a Python Virtual Environment
6
-
You can use `conda` or something else, this is the fastest for me.
8
+
You can use `conda` or something else, but this is the fastest for me.
7
9
```
8
10
python -m venv /path/to/new/virtual/environment
9
11
```
@@ -14,25 +16,82 @@ Make sure to activate your virtual environment before running!
14
16
pip3 install -r requirements.txt
15
17
```
16
18
19
+
## Usage
20
+
21
+
The extraction process is a 2-step process.
22
+
23
+
1. HTML Extraction
24
+
We first analyze the input HTML markup and create a map of old classes to new ones.
25
+
26
+
For example, if you have an HTML element like the following:
27
+
```html
28
+
<h1class="big red slant">This is a header</h1>
29
+
```
30
+
31
+
We would generate a mapping that is JSON-encoded like this:
32
+
```json
33
+
{
34
+
"newClass": "RANDOM_STRING_CLASS_NAME",
35
+
"oldClasses": "big, red, slant"
36
+
}
37
+
```
38
+
39
+
And produce an output HTML file like this:
40
+
```html
41
+
<h1class="RANDOM_STRING_CLASS_NAME">This is a header</h1>
42
+
```
43
+
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.
46
+
```css
47
+
.RANDOM_STRING_CLASS_NAME {
48
+
/* big */
49
+
font-size: 50px;
50
+
/* red */
51
+
color: red;
52
+
/* slant */
53
+
font-style: italic;
54
+
}
55
+
```
17
56
18
-
## Useage
57
+
> We keep this as a manual 2-step process to allow for debugging after whole process is completed. We may change this to just be a 1 step process in the future.
I like building websites with raw CSS but building UI's with it can be hard and annoying. I'm also not the most creative and it's a pain to write, so I end up copying elements or sections of content that I see on the internet.
75
+
I enjoy building websites with raw CSS, but creating UIs with it can be difficult and frustrating. I'm also not the most creative person, and writing CSS from scratch can be tedious, so I often end up copying elements or sections of content that I find on the internet.
27
76
28
-
After doing years of manual extraction of sites styles that I like, I decied to make a library that could do this for me. This library has saved me hours of manually reading through markup files and allows me to experiement much quicker.
77
+
After years of manually extracting styles from websites I liked, I decided to create a library to automate this process. This library has saved me hours of manually reading through markup files and allows me to experiment much faster.
29
78
30
79
## How does it work?
31
80
32
-
The first interation used regex to find and grab the css defintions from our desired input classes. This method actually turned out to be a bad approach because css is complicated expescially when you're dealing with @media queries and psudo selectors. There are some leftover functions that use regex matching, since I didn't bother to rewrite.
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.
33
82
34
-
I ended up picking up the `tinycss` library that handles parsing text into into a way that it can be programatically analyzed. This is way better to used with @mediahandlers and psudo selectors. This approach is a bit slow since we're doing this in a bunch of nested loops and the library is not built for speed. Once we iron out edge cases we can look at how we can improve the speed of the program.
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 @mediaqueries 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.
35
84
36
85
## Caveats
37
86
38
-
This is a very basic implementation. This has gotten me pretty far. There are cetain edge cases that the library does not catch. There are instances of classes that are nested (A child element within a parent element with a certain class). Not selectors are tricky and not caught. Feel free to add a issue for these bugs you find. If it's feasable we'll fix it.
87
+
- 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
+
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.
90
+
91
+
- 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.
92
+
93
+
- 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.
94
+
95
+
- 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.
96
+
97
+
- 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.
0 commit comments