1- from bs4 import BeautifulSoup , Tag , NavigableString
1+ from bs4 import BeautifulSoup , Tag , NavigableString , Doctype
22import os
33from abc import ABC , abstractmethod
44from 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 ()
0 commit comments