Last active
February 23, 2021 12:03
-
-
Save benjamincorcoran/c0091159610c27d5833d330afd61302e to your computer and use it in GitHub Desktop.
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
import os | |
import bs4 | |
COVERAGE_REPORT_DIR = './htmlcov/' | |
def embed_css_in_html_file(html_file, css_dir): | |
with open(html_file, 'r') as f: | |
soup = bs4.BeautifulSoup(f.read(), "html.parser") | |
stylesheets = soup.findAll("link", {"rel": "stylesheet"}) | |
for s in stylesheets: | |
t = soup.new_tag('style') | |
css_file = s["href"] | |
print(f"found link to {css_file}") | |
with open(os.path.join(css_dir, css_file), 'r') as f: | |
c = bs4.element.NavigableString(f.read()) | |
t.insert(0, c) | |
t['type'] = 'text/css' | |
s.replaceWith(t) | |
with open(html_file, 'w', encoding="UTF8") as f: | |
f.write(str(soup)) | |
for dp, dn, fn in os.walk(os.path.expanduser(COVERAGE_REPORT_DIR)): | |
for file in fn: | |
if file.endswith(".html"): | |
print(f"Embedding CSS in {file}") | |
embed_css_in_html_file(os.path.join( | |
dp, file), dp) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment