Repository navigation
SEO checks: one file per check, so PRs adding checks stop conflicting #1222
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Large diffs are not rendered by default.
Oops, something went wrong.
Empty file.
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,315 @@ | ||
| """The parsed build that every check in this folder receives. | ||
|
|
||
| `load_site(public_dir)` reads each index.html under the build once. A check's | ||
| `check(site)` uses `site.pages` (one `Page` per index.html, sorted by path), | ||
| `site.home` (the homepage's `Page`, or None) and `site.public_dir`. | ||
| """ | ||
|
|
||
| import os | ||
| import re | ||
| from html.parser import HTMLParser | ||
| from urllib.parse import unquote, urlparse | ||
|
|
||
| SOURCE_URL_TOKEN = re.compile(r'https?://[^\s<>"]+') | ||
|
|
||
|
|
||
| class PageParser(HTMLParser): | ||
| def __init__(self): | ||
| super().__init__(convert_charrefs=True) | ||
| self.h1_texts = [] | ||
| self.urls = [] | ||
| self.imgs = [] | ||
| self.card_pictures = [] | ||
| self._whole_card_picture = None | ||
| self.meta = {} | ||
| self.code_classes = [] | ||
| self.metas = [] | ||
| self._in_pre = False | ||
| self._h1_depth = 0 | ||
| self._h1_buffer = [] | ||
| self.jsonld = [] | ||
| self.canonical = None | ||
| self.title = "" | ||
| self.resource_links = [] | ||
| self._in_title = False | ||
| self._in_resource_heading = False | ||
| self.gloss_terms = [] | ||
| self._jsonld_buffer = None | ||
| self.largest_inline_script = 0 | ||
| self._inline_script = None | ||
| self.ids = set() | ||
| self.references = set() | ||
| self.reference_sources = [] | ||
| self._ref_source = None | ||
| self.citations = [] | ||
| self.crumb_links = [] | ||
| self._in_crumbs = False | ||
| self.alternates = [] | ||
| self.related_links = [] | ||
| self._related_tag = None # the element holding a term page's related terms (section.term-related) | ||
| self.takeaway_links = [] | ||
| self._in_takeaways = False | ||
| self.anchors = [] | ||
| self.h1_english_texts = [] # text of the elements marked lang="en" inside an <h1> | ||
| self._h1_english_tag = None | ||
| self.empty_network_icons = False | ||
| self._network_icon_items = None | ||
|
|
||
| def _finish_ref_source(self): | ||
| if self._ref_source is not None: | ||
| self.reference_sources.append(self._ref_source) | ||
| self._ref_source = None | ||
|
|
||
| def handle_starttag(self, tag, attrs): | ||
| attrs = dict(attrs) | ||
| if tag == "li": | ||
| self._finish_ref_source() | ||
| if self._network_icon_items is not None: | ||
| self._network_icon_items += 1 | ||
| if tag == "ul": | ||
| ul_classes = (attrs.get("class") or "").split() | ||
| if "summary-takeaways" in ul_classes: | ||
| self._in_takeaways = True | ||
| if "network-icon" in ul_classes: | ||
| self._network_icon_items = 0 | ||
| if tag == "a" and self._in_takeaways: | ||
| self.takeaway_links.append(attrs.get("href") or "") | ||
| if tag == "div": | ||
| classes = (attrs.get("class") or "").split() | ||
| self._whole_card_picture = "ec-thumb-whole" in classes if "ac-thumb" in classes else None | ||
| if tag == "script" and not attrs.get("src"): | ||
| self._inline_script = 0 | ||
| if attrs.get("id"): | ||
| self.ids.add(attrs["id"]) | ||
| if attrs.get("data-reference"): | ||
| self.references.add(attrs["data-reference"]) | ||
| if attrs.get("data-source"): | ||
| if tag == "li": | ||
| self._ref_source = (attrs["data-source"], []) | ||
| else: | ||
| self.reference_sources.append((attrs["data-source"], [])) | ||
| if tag == "a" and self._ref_source is not None and attrs.get("href"): | ||
| self._ref_source[1].append(attrs["href"]) | ||
| if tag == "nav" and "crumbs" in (attrs.get("class") or "").split(): | ||
| self._in_crumbs = True | ||
| if tag == "link" and attrs.get("rel") == "alternate" and attrs.get("hreflang") and attrs.get("href"): | ||
| self.alternates.append(attrs["href"]) | ||
| if self._related_tag is None and "term-related" in (attrs.get("class") or "").split(): | ||
| self._related_tag = tag | ||
| if tag == "a" and self._related_tag: | ||
| self.related_links.append(attrs.get("href") or "") | ||
| if tag == "a" and self._in_crumbs: | ||
| self.crumb_links.append(attrs.get("href") or "") | ||
| if tag == "a" and "cite" in (attrs.get("class") or "").split(): | ||
| self.citations.append((attrs.get("href") or "", attrs.get("data-cite") or "")) | ||
| if tag == "script" and attrs.get("type") == "application/ld+json": | ||
| self._jsonld_buffer = [] | ||
| elif tag == "link" and attrs.get("rel") == "canonical": | ||
| self.canonical = attrs.get("href") | ||
| if tag == "title": | ||
| self._in_title = True | ||
| if tag == "h2" and "res-title" in (attrs.get("class") or "").split(): | ||
| self._in_resource_heading = True | ||
| if tag == "a" and self._in_resource_heading and attrs.get("href"): | ||
| self.resource_links.append(attrs["href"]) | ||
| # The English part of a translated term's heading: <bdi lang="en"> in Doors, <span lang="en"> on main. | ||
| if self._h1_depth and self._h1_english_tag is None and attrs.get("lang") == "en": | ||
| self._h1_english_tag = tag | ||
| self.h1_english_texts.append("") | ||
| if tag == "a" and attrs.get("href"): | ||
| self.anchors.append(attrs["href"]) | ||
| if tag == "h1": | ||
| self._h1_depth += 1 | ||
| elif tag in ("script", "link"): | ||
| url = attrs.get("src") or attrs.get("href") | ||
| if url: | ||
| self.urls.append(url) | ||
| elif tag == "img": | ||
| self.imgs.append(attrs) | ||
| if self._whole_card_picture is not None and attrs.get("src"): | ||
| folder = urlparse(attrs["src"]).path.rpartition("/")[0] | ||
| self.card_pictures.append((folder, self._whole_card_picture)) | ||
| elif tag == "a" and "gloss-term" in (attrs.get("class") or "").split(): | ||
| self.gloss_terms.append(attrs.get("href") or "") | ||
| elif tag == "meta": | ||
| self.metas.append(attrs) | ||
| key = attrs.get("property") or attrs.get("name") | ||
| if key and key not in self.meta: | ||
| self.meta[key] = attrs.get("content") or "" | ||
| elif tag == "pre": | ||
| self._in_pre = True | ||
| elif tag == "code" and self._in_pre: | ||
| self.code_classes.append(attrs.get("class") or "") | ||
|
|
||
| def handle_endtag(self, tag): | ||
| if tag == self._h1_english_tag: | ||
| self.h1_english_texts[-1] = self.h1_english_texts[-1].strip() | ||
| self._h1_english_tag = None | ||
| if tag in ("li", "ul"): | ||
| self._finish_ref_source() | ||
| if tag == "div": | ||
| self._whole_card_picture = None | ||
| if tag == "ul": | ||
| self._in_takeaways = False | ||
| if self._network_icon_items is not None: | ||
| if self._network_icon_items == 0: | ||
| self.empty_network_icons = True | ||
| self._network_icon_items = None | ||
| if tag == "nav": | ||
| self._in_crumbs = False | ||
| if tag == self._related_tag: | ||
| self._related_tag = None | ||
| if tag == "script" and self._inline_script is not None: | ||
| self.largest_inline_script = max(self.largest_inline_script, self._inline_script) | ||
| self._inline_script = None | ||
| if tag == "script" and self._jsonld_buffer is not None: | ||
| self.jsonld.append("".join(self._jsonld_buffer)) | ||
| self._jsonld_buffer = None | ||
| if tag == "title": | ||
| self._in_title = False | ||
| if tag == "h2": | ||
| self._in_resource_heading = False | ||
| if tag == "pre": | ||
| self._in_pre = False | ||
| if tag == "h1" and self._h1_depth > 0: | ||
| self._h1_depth -= 1 | ||
| if self._h1_depth == 0: | ||
| self.h1_texts.append("".join(self._h1_buffer).strip()) | ||
| self._h1_buffer = [] | ||
|
|
||
| def close(self): | ||
| super().close() | ||
| self._finish_ref_source() | ||
| # A script left open at the end of the file still counts. | ||
| if self._inline_script is not None: | ||
| self.largest_inline_script = max(self.largest_inline_script, self._inline_script) | ||
| self._inline_script = None | ||
|
|
||
| def handle_data(self, data): | ||
| if self._ref_source is not None: | ||
| self._ref_source[1].extend(m.group(0) for m in SOURCE_URL_TOKEN.finditer(data)) | ||
| if self._in_title: | ||
| self.title += data | ||
| if self._inline_script is not None: | ||
| self._inline_script += len(data.encode("utf-8")) | ||
| if self._jsonld_buffer is not None: | ||
| self._jsonld_buffer.append(data) | ||
| if self._h1_depth > 0: | ||
| self._h1_buffer.append(data) | ||
| if self._h1_english_tag is not None: | ||
| self.h1_english_texts[-1] += data | ||
|
|
||
|
|
||
| class Page: | ||
| def __init__(self, path, parser): | ||
| self.path = path | ||
| self.h1_texts = parser.h1_texts | ||
| self.title = parser.title.strip() | ||
| self.resource_links = parser.resource_links | ||
| self.urls = parser.urls | ||
| self.imgs = parser.imgs | ||
| self.card_pictures = parser.card_pictures | ||
| self.meta = parser.meta | ||
| self.code_blocks = [cls.split() for cls in parser.code_classes] | ||
| self.descriptions = [m.get("content") or "" for m in parser.metas | ||
| if (m.get("name") or "").lower() == "description"] | ||
| robots = " ".join((m.get("content") or "").lower() for m in parser.metas | ||
| if (m.get("name") or "").lower() == "robots") | ||
| self.refreshes = [m.get("content") or "" for m in parser.metas | ||
| if (m.get("http-equiv") or "").lower() == "refresh"] | ||
| url_path = "/" + os.path.dirname(path).replace(os.sep, "/") | ||
| self.url_path = url_path.rstrip("/") + "/" | ||
| elsewhere = (parser.canonical is not None and | ||
| unquote(urlparse(parser.canonical).path) != unquote(self.url_path)) | ||
| # Redirect stubs, noindexed pages and pages canonicalised to another URL | ||
| # never reach search results as such. | ||
| self.noindex = "noindex" in robots | ||
| self.indexable = not self.noindex and not self.refreshes and not elsewhere | ||
| self.jsonld = parser.jsonld | ||
| self.canonical = parser.canonical | ||
| self.largest_inline_script = parser.largest_inline_script | ||
| self.ids = parser.ids | ||
| self.references = parser.references | ||
| self.reference_sources = parser.reference_sources | ||
| self.citations = parser.citations | ||
| self.gloss_terms = parser.gloss_terms | ||
| self.crumb_links = parser.crumb_links | ||
| self.alternates = parser.alternates | ||
| self.related_links = parser.related_links | ||
| self.takeaway_links = parser.takeaway_links | ||
| self.anchors = parser.anchors | ||
| self.h1_english_texts = parser.h1_english_texts | ||
| self.empty_network_icons = parser.empty_network_icons | ||
|
|
||
| def loads(self, needle): | ||
| return any(needle in url for url in self.urls) | ||
|
|
||
| @property | ||
| def has_highlight_code(self): | ||
| return any("hljs" not in cls and "language-mermaid" not in cls | ||
| for cls in self.code_blocks) | ||
|
|
||
| @property | ||
| def has_mermaid_code(self): | ||
| return any("language-mermaid" in cls for cls in self.code_blocks) | ||
|
|
||
| @property | ||
| def imgs_without_alt(self): | ||
| return [img for img in self.imgs if "alt" not in img] | ||
|
|
||
| @property | ||
| def imgs_without_dims(self): | ||
| return [img for img in self.imgs | ||
| if "width" not in img or "height" not in img] | ||
|
|
||
|
|
||
| def load_pages(public_dir): | ||
| pages = [] | ||
| for root, _dirs, files in os.walk(public_dir): | ||
| for name in files: | ||
| if name != "index.html": | ||
| continue | ||
| full = os.path.join(root, name) | ||
| parser = PageParser() | ||
| with open(full, encoding="utf-8", errors="replace") as handle: | ||
| parser.feed(handle.read()) | ||
| parser.close() | ||
| pages.append(Page(os.path.relpath(full, public_dir), parser)) | ||
| pages.sort(key=lambda page: page.path) | ||
| return pages | ||
|
|
||
|
|
||
| class Site: | ||
| def __init__(self, public_dir, pages): | ||
| self.public_dir = public_dir | ||
| self.pages = pages | ||
| self.home = next((p for p in pages if p.path == "index.html"), None) | ||
|
|
||
|
|
||
| def load_site(public_dir): | ||
| return Site(public_dir, load_pages(public_dir)) | ||
|
|
||
|
|
||
| def page_from_html(text, path="index.html"): | ||
| """One parsed page, for tests.""" | ||
| parser = PageParser() | ||
| parser.feed(text) | ||
| parser.close() | ||
| return Page(path, parser) | ||
|
|
||
|
|
||
| SHARE_IMAGE_EXTS = (".png", ".jpg", ".jpeg") | ||
|
|
||
|
|
||
| def is_share_format(url): | ||
| """X and LinkedIn show previews for PNG and JPEG share images, not WebP (#870).""" | ||
| return url.split("?")[0].lower().endswith(SHARE_IMAGE_EXTS) | ||
|
|
||
|
|
||
| # A preview build serves the site under a prefix (e.g. /rebrand-doors/); glossary URLs are compared from /glossary/ on. | ||
| GLOSSARY_URL_PATH = re.compile(r"/glossary/.*$") | ||
|
|
||
|
|
||
| def glossary_url_key(href): | ||
| match = GLOSSARY_URL_PATH.search(unquote(urlparse(href).path)) | ||
| return match.group(0) if match else None |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.