webcode-tk 1.2.17__tar.gz → 1.2.19__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: webcode-tk
3
- Version: 1.2.17
3
+ Version: 1.2.19
4
4
  Summary: A set of tools for inspecting HTML and CSS code
5
5
  License: MIT
6
6
  License-File: LICENSE.rst
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "webcode-tk"
3
- version = "1.2.17"
3
+ version = "1.2.19"
4
4
  description = "A set of tools for inspecting HTML and CSS code"
5
5
  authors = ["hundredvisionsguy <cwinikka@gmail.com>"]
6
6
  keywords = ["html", "validation", "css", ]
@@ -40,4 +40,4 @@
40
40
  - **`validator_tools`**: sends HTML or CSS code to the W3C Validator to check
41
41
  for errors.
42
42
  """
43
- __version__ = "1.2.17"
43
+ __version__ = "1.2.19"
@@ -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,113 @@ 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
+ result = f"fail: {filename} does not link to min number of Google "
408
+ result += "fonts."
409
+ report.append(result)
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
+ result = (
429
+ f"pass: {filename} uses the correct number of Google fonts"
430
+ )
431
+ report.append(result)
432
+ elif num_used > max:
433
+ result = f"fail: {filename} has too many font families - max is "
434
+ result += f"{max}, and you have {num_used} families."
435
+ report.append(result)
436
+ else:
437
+ result = (
438
+ f"fail: {filename} with {num_used} font families does not "
439
+ )
440
+ result += "have enough applied Google fonts"
441
+ report.append(result)
442
+ return report
443
+
444
+
445
+ def extract_families(family_data: list) -> list:
446
+ families = []
447
+ for datum in family_data:
448
+ family_value = datum.get("family")
449
+ family = family_value.split(",")[0]
450
+ family = family.strip()
451
+ family = family.replace('"', "")
452
+ family = family.replace("'", "")
453
+ family = family.replace("&", "")
454
+ families.append(family)
455
+ return families
456
+
457
+
458
+ def get_google_font_data(href: str) -> list:
459
+ font_families = []
460
+ font_data = href.split("family=")
461
+ font_data.pop(0)
462
+ for item in font_data:
463
+ family = item.split(":")[0]
464
+ family = family.replace("+", " ")
465
+ family = family.replace("&", "")
466
+ font_families.append(family)
467
+ return font_families
468
+
469
+
360
470
  if __name__ == "__main__":
361
471
  sample_styles = """
362
472
  /* <absolute-size> values */
File without changes
File without changes