Skip to content

Commit bf87e22

Browse files
committed
Updaded parsing methodology
1 parent e2465b0 commit bf87e22

4 files changed

Lines changed: 39 additions & 17 deletions

File tree

bootstrapCsskrt.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,3 @@ def add_form_classes(self, tag_dict: dict):
9292
# Add input wrapper
9393
control_div = self.soup.new_tag('div', **{'class': 'control'})
9494
elem.wrap(control_div)
95-
elif type(elem) == Tag: # ignore NavigableStrings like /n
96-
spotted_label = None
97-
if (tag_dict.get(elem.name)):
98-
self.add_class_to_element(elem, tag_dict[elem.name])

bulmaCsskrt.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ def __init__(self, fileName):
1212
'button': 'button',
1313
'checkbox': 'checkbox',
1414
'radio': 'radio',
15+
'h1': 'title is-1',
16+
'h2': 'title is-2',
17+
'h3': 'title is-3',
18+
'h4': 'title is-4',
19+
'h5': 'title is-5',
20+
'h6': 'title is-6'
1521
}
1622
super().__init__(fileName, tag_styles)
1723

@@ -85,9 +91,4 @@ def add_form_classes(self, tag_dict: dict):
8591

8692
# Add input wrapper
8793
control_div = self.soup.new_tag('div', **{'class': 'control'})
88-
elem.wrap(control_div)
89-
90-
elif type(elem) == Tag: # ignore NavigableStrings like /n
91-
spotted_label = None
92-
if (tag_dict.get(elem.name)):
93-
self.add_class_to_element(elem, tag_dict[elem.name])
94+
elem.wrap(control_div)

csskrt.py

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from bs4 import BeautifulSoup, Tag, NavigableString
1+
from bs4 import BeautifulSoup, Tag, NavigableString, Doctype
22
import os
33
from abc import ABC, abstractmethod
44
from typing import List, Dict, NoReturn
@@ -105,9 +105,9 @@ def add_form_classes(self, tag_dict: dict) -> NoReturn:
105105
if 'checkbox' in tag_dict:
106106
self.add_class_to_element(elem, tag_dict['checkbox'])
107107

108-
elif type(elem) == Tag: # ignore NavigableStrings like /n
109-
if tag_dict.get(elem.name):
110-
self.add_class_to_element(elem, tag_dict[elem.name])
108+
# elif type(elem) == Tag: # ignore NavigableStrings like /n
109+
# if tag_dict.get(elem.name):
110+
# self.add_class_to_element(elem, tag_dict[elem.name])
111111

112112
def add_table_classes(self, table_tag_dict: dict) -> NoReturn:
113113
"""
@@ -148,6 +148,20 @@ def add_list_classes(self, list_tags: dict) -> NoReturn:
148148
# recursive=False to prevent double modifying for nested lists
149149
self.add_class_to_element(li, list_tags['li'])
150150

151+
152+
def add_general_classes(self):
153+
"""
154+
Adds styles to single elements
155+
:return:
156+
"""
157+
supported_classes = (
158+
'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'button', 'a', 'nav'
159+
)
160+
for tag in self.tag_styles:
161+
if tag in supported_classes:
162+
for elem in self.soup.find_all(tag):
163+
self.add_class_to_element(elem, self.tag_styles[tag])
164+
151165
def output(self) -> NoReturn:
152166
"""
153167
Outputs a new file.
@@ -161,22 +175,33 @@ def output(self) -> NoReturn:
161175
with open(new_file_name, 'w') as out_file:
162176
out_file.write(self.soup.prettify())
163177

178+
164179
def freshify(self) -> NoReturn:
165180
"""
166181
Main function that applies all the necessary styles
182+
167183
:return:
168184
"""
169185
starter_tags = self.get_starter_tags()
170186
wrapper_tag = self.get_wrapper_tag()
171187
table_styles = self.get_table_styles()
172188
list_styles = self.get_list_styles()
173189

190+
# Modify the head
174191
self.initialize_framework(self.soup.head, starter_tags)
192+
193+
# Add the "wrapper" tag
175194
if wrapper_tag:
176195
self.add_wrapper_tag(wrapper_tag)
196+
197+
# Elements that have children eg. tables, lists, forms have their own
198+
# dedicated function to support complex operations if necessary.
177199
self.add_form_classes(self.tag_styles)
178200
self.add_list_classes(list_styles)
179201
self.add_table_classes(table_styles)
180-
self.output()
181202

182-
# print(btn.get('class', []))
203+
# Add styles for the rest of the elements
204+
self.add_general_classes()
205+
206+
# Output the modified html file
207+
self.output()

main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def freshify(filename, framework):
1717
raise Exception("Invalid framework")
1818

1919
csskrter.freshify()
20-
print("Done !")
20+
print("Done!")
2121

2222

2323
if __name__ == '__main__':

0 commit comments

Comments
 (0)