Skip to content

Commit 2832dbc

Browse files
committed
Updated README
1 parent c6f927b commit 2832dbc

13 files changed

Lines changed: 229 additions & 122 deletions

README.md

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,38 @@
1-
# _CSSKRT_
2-
3-
Csskrt is a python module for automatically adding css styles for popular frameworks
1+
# _CSSKRT CSSKRT_
2+
Csskrt-csskrt is a python module for automatically adding css styles for popular frameworks such as Bootstrap and Bulma
43
to make them more visually appealing and make it faster to get started with them.
54

5+
## Supported Frameworks
6+
<a href="https://getbootstrap.com/docs/4.1/getting-started/introduction/" target="_blank">
7+
<img alt="Bootstrap" src="resources/bootstrap_brand.svg" height="150" width="150"
8+
style="margin: 0px 50px">
9+
</a>
10+
11+
<a href="https://bulma.io/documentation/" target="_blank">
12+
<img alt="Bulma" src="resources/bulma_brand.png" height="150" width="150"
13+
style="margin: 0px 50px">
14+
</a>
15+
16+
## Quick install
17+
```pip install csskrt```
18+
19+
## Usage
20+
To run the program, type:
21+
22+
```$ csskrt --help```
23+
24+
## Contributing
25+
All feedback and suggestions are welcome, just post an issue!
26+
27+
## License
28+
This project is distributed under the [MIT](LICENSE.txt) license.
29+
30+
## Demo
31+
#### Original
32+
<img alt="before" align="center" src="resources/before.png" border="1" height="100%" width="100%">
633

7-
## Usage
34+
#### Bootstrap
35+
<img alt="bootstrap" align="center" src="resources/after_bootstrap.png" border="1" height="100%" width="100%">
36+
37+
#### Bulma
38+
<img alt="bulma" align="center" src="resources/after_bulma.png" border="1" height="100%" width="100%">

csskrt/bootstrapCsskrt.py

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ def __init__(self, fileName):
99
'select': 'custom-select',
1010
'button': 'btn btn-primary',
1111
'checkbox': 'form-check-input',
12+
'radio': 'form-check-input',
1213
}
1314
super().__init__(fileName, tag_styles)
1415

@@ -62,33 +63,51 @@ def add_form_classes(self, tag_dict: dict):
6263
:return:
6364
"""
6465
for form in self.soup.find_all('form'):
65-
spotted_label = None
66+
input_ = None
67+
label = None
68+
6669
for elem in form.children:
6770
if elem.name == 'label':
68-
spotted_label = elem
69-
if 'label' in tag_dict:
70-
self.add_class_to_element(elem, tag_dict['label'])
71-
elif elem.name == 'input':
72-
self.add_class_to_element(elem, tag_dict['input'])
71+
label = elem
72+
# See if there is a input within the label (sometimes done for radio/cb)
73+
input_within_label = elem.find_all('input', recursive=False)
74+
if input_within_label:
75+
# todo handle this case
76+
raise Warning("No support for inputs nested within labels for Bootstrap"
77+
"for now.")
78+
79+
elif elem.name == 'input' or elem.name == 'select':
80+
if input_:
81+
raise Exception("Found input without adjacent label")
82+
else:
83+
input_ = elem
84+
7385
if elem.get('type') == 'radio':
7486
if 'radio' in tag_dict:
7587
self.add_class_to_element(elem, tag_dict['radio'])
7688
elif elem.get('type') == 'checkbox':
7789
if 'checkbox' in tag_dict:
7890
self.add_class_to_element(elem, tag_dict['checkbox'])
91+
else:
92+
self.add_class_to_element(elem, tag_dict['input'])
93+
94+
elif elem.name == 'div':
95+
# todo handle the case we have a prexisting div in form
96+
raise Warning("No support yet for input elements in divs within a form")
97+
98+
# Add overall wrapper
99+
if input_ and label:
100+
if input_.get('type') == 'radio' or input_.get('type') == 'checkbox':
101+
self.add_class_to_element(label, 'form-check-label')
102+
field_div = self.soup.new_tag('div', **{'class': 'form-check'})
103+
# Add label then input
104+
input_.wrap(field_div)
105+
field_div.append(label)
79106

80-
# Add overall wrapper
81-
if not spotted_label:
82-
# add wrapper
83-
field_div = self.soup.new_tag('div', **{'class': 'form-group'})
84-
elem.wrap(field_div)
85107
else:
86-
# the input has a preceding label
87108
field_div = self.soup.new_tag('div', **{'class': 'form-group'})
88-
spotted_label.wrap(field_div)
89-
field_div.append(elem)
90-
spotted_label = None
109+
# Add label then input
110+
label.wrap(field_div)
111+
field_div.append(input_)
91112

92-
# Add input wrapper
93-
control_div = self.soup.new_tag('div', **{'class': 'control'})
94-
elem.wrap(control_div)
113+
input_, label = None, None

csskrt/bulmaCsskrt.py

Lines changed: 57 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ def __init__(self, fileName):
88
'input': 'input',
99
'label': 'label',
1010
'textarea': 'textarea',
11-
'select': 'select',
1211
'button': 'button',
1312
'checkbox': 'checkbox',
1413
'radio': 'radio',
@@ -51,9 +50,6 @@ def get_table_styles(self):
5150
'td': 'td'
5251
}
5352

54-
def get_list_styles(self):
55-
return {} # no list styles
56-
5753
def add_form_classes(self, tag_dict: dict):
5854
"""
5955
The only difference between this and parent implementation is the addition of adding
@@ -62,33 +58,70 @@ def add_form_classes(self, tag_dict: dict):
6258
:return:
6359
"""
6460
for form in self.soup.find_all('form'):
65-
spotted_label = None
61+
label, input_ = None, None
6662
for elem in form.children:
6763
if elem.name == 'label':
68-
spotted_label = elem
64+
label = elem
6965
if 'label' in tag_dict:
7066
self.add_class_to_element(elem, tag_dict['label'])
67+
68+
# See if there is a input within the label (sometimes done for radio/cb)
69+
input_within_label = elem.find_all('input', recursive=False)
70+
if input_within_label:
71+
input_type = input_within_label.get('type')
72+
if input_type == 'checkbox':
73+
self.add_class_to_element(label, 'checkbox')
74+
elif input_type == 'radio':
75+
self.add_class_to_element(label, 'radio')
76+
label = None
77+
input = None
78+
7179
elif elem.name == 'input':
72-
self.add_class_to_element(elem, tag_dict['input'])
73-
if elem.get('type') == 'radio':
74-
if 'radio' in tag_dict:
75-
self.add_class_to_element(elem, tag_dict['radio'])
76-
elif elem.get('type') == 'checkbox':
77-
if 'checkbox' in tag_dict:
78-
self.add_class_to_element(elem, tag_dict['checkbox'])
80+
if input_:
81+
raise Exception("Found input without adjacent label")
82+
else:
83+
input_ = elem
84+
85+
if elem.get('type') != 'radio' and elem.get('type') != 'checkbox':
86+
# Radio/CB don't take a class but their label does
87+
self.add_class_to_element(elem, tag_dict['input'])
88+
89+
elif elem.name == 'select':
90+
# Add select div
91+
select_div = self.soup.new_tag('div', **{'class': 'select'})
92+
elem.wrap(select_div)
93+
94+
# Add control div
95+
control_div = self.soup.new_tag('div', **{'class': 'control'})
96+
select_div.wrap(control_div)
7997

8098
# Add overall wrapper
81-
if not spotted_label:
82-
# add wrapper
83-
field_div = self.soup.new_tag('div', **{'class': 'field'})
84-
elem.wrap(field_div)
99+
field_div = self.soup.new_tag('div', **{'class': 'field'})
100+
control_div.wrap(field_div)
101+
102+
if input_ and label:
103+
if input_.get('type') == 'radio' or input_.get('type') == 'checkbox':
104+
if input_.get('type') == 'radio':
105+
self.add_class_to_element(label, 'radio')
106+
else:
107+
self.add_class_to_element(label, 'checkbox')
108+
109+
# Add input then label
110+
# todo wrap all consecutive inputs with wrapper
111+
# control_div = self.soup.new_tag('div', **{'class': 'control'})
112+
# label.wrap(control_div)
113+
label.insert(0, input_)
85114
else:
86-
# the input has a preceding label
115+
# Add overall wrapper
87116
field_div = self.soup.new_tag('div', **{'class': 'field'})
88-
spotted_label.wrap(field_div)
89-
field_div.append(elem)
90-
spotted_label = None
117+
label.wrap(field_div)
118+
field_div.append(input_)
91119

92-
# Add input wrapper
93-
control_div = self.soup.new_tag('div', **{'class': 'control'})
94-
elem.wrap(control_div)
120+
# Add input wrapper
121+
control_div = self.soup.new_tag('div', **{'class': 'control'})
122+
input_.wrap(control_div)
123+
124+
label, input_ = None, None
125+
126+
def get_list_styles(self):
127+
return {} # no list styles

csskrt/main.py renamed to main.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
#!/usr/bin/env python
21
import click
3-
from .bulmaCsskrt import BulmaCsskrt
4-
from .bootstrapCsskrt import BootstrapCsskrt
2+
from csskrt.bulmaCsskrt import BulmaCsskrt
3+
from csskrt.bootstrapCsskrt import BootstrapCsskrt
54

65

76
@click.command()
87
@click.argument('filename', type=click.Path(exists=True))
9-
@click.option('--framework', '-f', default='bulma',
8+
@click.option('--framework', '-f', default='bootstrap',
109
type=click.Choice(['bulma', 'bootstrap']),
1110
help='Name of the framework. De')
1211
@click.option('--pretty-print', '-p', is_flag=True)

resources/after_bootstrap.png

66.1 KB
Loading

resources/after_bulma.png

69.6 KB
Loading

resources/before.png

58.4 KB
Loading

resources/bootstrap_brand.svg

Lines changed: 4 additions & 0 deletions
Loading

resources/bulma_brand.png

10.1 KB
Loading

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ def long_description():
1717
url='https://github.com/4d11/csskrt',
1818
entry_points='''
1919
[console_scripts]
20-
csskrt=csskrt.main:freshify
20+
csskrt=main:freshify
2121
''',
2222
)

0 commit comments

Comments
 (0)