webcode-tk 1.2.17__tar.gz → 1.2.18__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {webcode_tk-1.2.17 → webcode_tk-1.2.18}/PKG-INFO +1 -1
- {webcode_tk-1.2.17 → webcode_tk-1.2.18}/pyproject.toml +1 -1
- {webcode_tk-1.2.17 → webcode_tk-1.2.18}/webcode_tk/__init__.py +1 -1
- {webcode_tk-1.2.17 → webcode_tk-1.2.18}/webcode_tk/css_tools.py +2 -0
- {webcode_tk-1.2.17 → webcode_tk-1.2.18}/webcode_tk/font_tools.py +100 -0
- {webcode_tk-1.2.17 → webcode_tk-1.2.18}/LICENSE.rst +0 -0
- {webcode_tk-1.2.17 → webcode_tk-1.2.18}/README.rst +0 -0
- {webcode_tk-1.2.17 → webcode_tk-1.2.18}/webcode_tk/_style_inheritance.py +0 -0
- {webcode_tk-1.2.17 → webcode_tk-1.2.18}/webcode_tk/animation_tools.py +0 -0
- {webcode_tk-1.2.17 → webcode_tk-1.2.18}/webcode_tk/cascade_tools.py +0 -0
- {webcode_tk-1.2.17 → webcode_tk-1.2.18}/webcode_tk/color_keywords.py +0 -0
- {webcode_tk-1.2.17 → webcode_tk-1.2.18}/webcode_tk/color_tools.py +0 -0
- {webcode_tk-1.2.17 → webcode_tk-1.2.18}/webcode_tk/contrast_tools.py +0 -0
- {webcode_tk-1.2.17 → webcode_tk-1.2.18}/webcode_tk/html_tools.py +0 -0
- {webcode_tk-1.2.17 → webcode_tk-1.2.18}/webcode_tk/sample_report.json +0 -0
- {webcode_tk-1.2.17 → webcode_tk-1.2.18}/webcode_tk/utils.py +0 -0
- {webcode_tk-1.2.17 → webcode_tk-1.2.18}/webcode_tk/ux_tools.py +0 -0
- {webcode_tk-1.2.17 → webcode_tk-1.2.18}/webcode_tk/validator_tools.py +0 -0
|
@@ -1362,6 +1362,8 @@ def get_font_families(sheet: Stylesheet) -> list:
|
|
|
1362
1362
|
# always take the last family as it would be an override in CSS
|
|
1363
1363
|
family = families[-1]
|
|
1364
1364
|
font_families.append({"selector": selector, "family": family})
|
|
1365
|
+
else:
|
|
1366
|
+
print()
|
|
1365
1367
|
return font_families
|
|
1366
1368
|
|
|
1367
1369
|
|
|
@@ -25,8 +25,11 @@ font-file or some other hidden calculation from the browser.
|
|
|
25
25
|
import re
|
|
26
26
|
from typing import Any
|
|
27
27
|
from typing import Union
|
|
28
|
+
from file_clerk import clerk
|
|
28
29
|
|
|
29
30
|
from webcode_tk import css_tools
|
|
31
|
+
from webcode_tk import html_tools as html
|
|
32
|
+
|
|
30
33
|
|
|
31
34
|
absolute_keyword_regex = (
|
|
32
35
|
r"\b(?:xx-small|x-small|small(?!-caps)|medium|large|x-large|"
|
|
@@ -357,6 +360,103 @@ def is_valid_shorthand(value: str) -> bool:
|
|
|
357
360
|
return is_valid
|
|
358
361
|
|
|
359
362
|
|
|
363
|
+
def get_google_font_report(project_dir: str, min=1, max=2) -> str:
|
|
364
|
+
"""determines if the page is using the min number of google fonts
|
|
365
|
+
|
|
366
|
+
We're looking for three things in each web page:
|
|
367
|
+
1) a link in the head to the googlefonts API,
|
|
368
|
+
2) each of those fonts being applied, and
|
|
369
|
+
3) at least the minimum number of unique Google fonts and no more than the
|
|
370
|
+
max #.
|
|
371
|
+
|
|
372
|
+
Args:
|
|
373
|
+
project_dir: the folder where the files reside
|
|
374
|
+
min: minimum number of Google font families in use
|
|
375
|
+
max: maximum number of Google font families in use
|
|
376
|
+
|
|
377
|
+
Returns:
|
|
378
|
+
report: a pass/fail report with accompanying data
|
|
379
|
+
"""
|
|
380
|
+
|
|
381
|
+
# Loop through all HTML docs and their styles
|
|
382
|
+
report = []
|
|
383
|
+
styles = css_tools.get_styles_by_html_files(project_dir)
|
|
384
|
+
for data in styles:
|
|
385
|
+
|
|
386
|
+
# Step 1: get all linked Google fonts
|
|
387
|
+
file = data.get("file")
|
|
388
|
+
filename = clerk.get_file_name(file)
|
|
389
|
+
links = html.get_elements("link", file)
|
|
390
|
+
google_fonts_linked = []
|
|
391
|
+
|
|
392
|
+
for link in links:
|
|
393
|
+
href = link.attrs.get("href")
|
|
394
|
+
if (
|
|
395
|
+
"fonts.googleapis.com" in href
|
|
396
|
+
and href != "https://fonts.googleapis.com"
|
|
397
|
+
):
|
|
398
|
+
fonts = get_google_font_data(href)
|
|
399
|
+
if isinstance(fonts, list):
|
|
400
|
+
google_fonts_linked += fonts
|
|
401
|
+
else:
|
|
402
|
+
google_fonts_linked.append(fonts)
|
|
403
|
+
|
|
404
|
+
# Check to see the linked fonts match required number.
|
|
405
|
+
num_fonts = len(google_fonts_linked)
|
|
406
|
+
if num_fonts < min:
|
|
407
|
+
data = f"fail: {filename} does not link to min number of Google "
|
|
408
|
+
data += "fonts."
|
|
409
|
+
report.append(data)
|
|
410
|
+
continue
|
|
411
|
+
|
|
412
|
+
# if we have any google fonts, are they being used?
|
|
413
|
+
stylesheets = data.get("stylesheets")
|
|
414
|
+
fonts_used = []
|
|
415
|
+
for sheet in stylesheets:
|
|
416
|
+
families = []
|
|
417
|
+
font_data = css_tools.get_font_families(sheet)
|
|
418
|
+
if font_data:
|
|
419
|
+
families = extract_families(font_data)
|
|
420
|
+
if families:
|
|
421
|
+
for family in google_fonts_linked:
|
|
422
|
+
if family in families:
|
|
423
|
+
if family not in fonts_used:
|
|
424
|
+
fonts_used.append(family)
|
|
425
|
+
|
|
426
|
+
num_used = len(fonts_used)
|
|
427
|
+
if num_used >= min and num_used <= max:
|
|
428
|
+
data = f"pass: {filename} uses the correct number of Google fonts"
|
|
429
|
+
report.append(data)
|
|
430
|
+
else:
|
|
431
|
+
data = f"fail: {filename} with {num_used} font families does not "
|
|
432
|
+
data += "have enough applied Google fonts"
|
|
433
|
+
report.append(data)
|
|
434
|
+
return report
|
|
435
|
+
|
|
436
|
+
|
|
437
|
+
def extract_families(family_data: list) -> list:
|
|
438
|
+
families = []
|
|
439
|
+
for datum in family_data:
|
|
440
|
+
family_value = datum.get("family")
|
|
441
|
+
family = family_value.split(",")[0]
|
|
442
|
+
family = family.strip()
|
|
443
|
+
family = family.replace('"', "")
|
|
444
|
+
family = family.replace("'", "")
|
|
445
|
+
families.append(family)
|
|
446
|
+
return families
|
|
447
|
+
|
|
448
|
+
|
|
449
|
+
def get_google_font_data(href: str) -> list:
|
|
450
|
+
font_families = []
|
|
451
|
+
font_data = href.split("family=")
|
|
452
|
+
font_data.pop(0)
|
|
453
|
+
for item in font_data:
|
|
454
|
+
family = item.split(":")[0]
|
|
455
|
+
family = family.replace("+", " ")
|
|
456
|
+
font_families.append(family)
|
|
457
|
+
return font_families
|
|
458
|
+
|
|
459
|
+
|
|
360
460
|
if __name__ == "__main__":
|
|
361
461
|
sample_styles = """
|
|
362
462
|
/* <absolute-size> values */
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|