Created
November 25, 2013 15:20
-
-
Save justnom/7642872 to your computer and use it in GitHub Desktop.
Scan templates for old classes to upgrade to font-awesome v4.0.3.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Scrape FontAwesome latest icon names (4.0.3) | |
import re | |
import os | |
from bs4 import BeautifulSoup | |
import requests | |
def soup_from_url(url): | |
try: | |
r = requests.get(url) | |
return BeautifulSoup(r.text) | |
except requests.RequestException as err: | |
print 'There was an error scraping {}, {}'.format(url, err) | |
def extract_font_awesome_class_names_from_soup(soup): | |
names = [] | |
first_row_div = soup.find('div', class_='row') | |
fa_regex = re.compile(ur'fa-([a-z-]+)', re.UNICODE | re.MULTILINE) | |
for child in first_row_div.find_all('div', class_='col-md-4 col-sm-6 col-lg-3'): | |
text = child.get_text() | |
fa_icon_name = fa_regex.search(text).group(1) | |
names.append(unicode('icon-' + fa_icon_name)) | |
return names | |
def extract_glyphicons_class_names_from_soup(soup): | |
names = [] | |
first_row_div = soup.find('ul', class_='the-icons') | |
fa_regex = re.compile(ur'(icon-[a-z-]+)', re.UNICODE | re.MULTILINE) | |
for child in first_row_div.find_all('li'): | |
icon_name = fa_regex.search(child.get_text()).group(1) | |
names.append(unicode(icon_name)) | |
return names | |
def list_classes_in_cakephp_templates(start_directory, classes): | |
matches = {} | |
classes_regex = re.compile(ur'((?<=\s|"|\'){})(?=\s|"|\')'.format('|'.join(re.escape(k) for k in classes)), | |
re.UNICODE | re.MULTILINE) | |
for root, sub_folders, file_names in os.walk(start_directory): | |
for file_name in file_names: | |
if file_name[-3:] == 'ctp': | |
file_path = os.path.join(root, file_name) | |
with open(file_path, 'rb') as f: | |
data = unicode(f.read(), 'utf-8') | |
# Loop through the regular expression matches | |
for class_attr_match in classes_regex.finditer(data): | |
class_attr = class_attr_match.group(1) | |
if file_path in matches: | |
matches[file_path].append(class_attr) | |
else: | |
matches[file_path] = [class_attr] | |
return matches | |
def diff(a, b): | |
b = set(b) | |
return [aa for aa in a if aa not in b] | |
def main(): | |
fa_soup = soup_from_url('http://fontawesome.io/cheatsheet/') | |
gi_soup = soup_from_url('http://getbootstrap.com/2.3.2/base-css.html#icons') | |
fa_classes = extract_font_awesome_class_names_from_soup(fa_soup) | |
gi_classes = extract_glyphicons_class_names_from_soup(gi_soup) | |
# Diff the classes between them | |
diff_classes = diff(fa_classes, gi_classes) | |
matches = list_classes_in_cakephp_templates('/some/directory', diff_classes) | |
for file_path, icons in matches.items(): | |
print '"{}": {}\n'.format(file_path, icons) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment