|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import math |
| 4 | +import re |
| 5 | + |
| 6 | +from docutils import nodes |
| 7 | + |
| 8 | +SKIP_CONTAINER_CLASSES = { |
| 9 | + "sphx-glr-script-out", |
| 10 | + "sphx-glr-single-img", |
| 11 | + "sphx-glr-thumbnail", |
| 12 | + "sphx-glr-horizontal", |
| 13 | +} |
| 14 | + |
| 15 | + |
| 16 | +class TextExtractor(nodes.NodeVisitor): |
| 17 | + def __init__(self, document): |
| 18 | + super().__init__(document) |
| 19 | + self.text = [] |
| 20 | + |
| 21 | + def visit_Text(self, node): |
| 22 | + self.text.append(node.astext()) |
| 23 | + |
| 24 | + def visit_literal_block(self, node): |
| 25 | + # Don't visit the children of literal blocks (i.e., code blocks) |
| 26 | + raise nodes.SkipNode |
| 27 | + |
| 28 | + def visit_figure(self, node): |
| 29 | + raise nodes.SkipNode |
| 30 | + |
| 31 | + def visit_image(self, node): |
| 32 | + raise nodes.SkipNode |
| 33 | + |
| 34 | + def visit_container(self, node): |
| 35 | + classes = set(node.get("classes", ())) |
| 36 | + if classes & SKIP_CONTAINER_CLASSES: |
| 37 | + raise nodes.SkipNode |
| 38 | + |
| 39 | + def unknown_visit(self, node): |
| 40 | + """Pass for all other nodes.""" |
| 41 | + pass |
| 42 | + |
| 43 | + |
| 44 | +EXAMPLE_PREFIX = "generated/auto_examples/" |
| 45 | + |
| 46 | + |
| 47 | +def _should_calculate(pagename: str) -> bool: |
| 48 | + if not pagename: |
| 49 | + return False |
| 50 | + if not pagename.startswith(EXAMPLE_PREFIX): |
| 51 | + return False |
| 52 | + if pagename.endswith("/sg_execution_times"): |
| 53 | + return False |
| 54 | + if pagename == "generated/auto_examples/index": |
| 55 | + return False |
| 56 | + return True |
| 57 | + |
| 58 | + |
| 59 | +def html_page_context(app, pagename, templatename, context, doctree): |
| 60 | + """Add estimated reading time directly under tutorial titles.""" |
| 61 | + if not doctree or not _should_calculate(pagename): |
| 62 | + context.pop("reading_time", None) |
| 63 | + return |
| 64 | + |
| 65 | + visitor = TextExtractor(doctree) |
| 66 | + doctree.walk(visitor) |
| 67 | + |
| 68 | + full_text = " ".join(visitor.text) |
| 69 | + word_count = len(re.findall(r"\w+", full_text)) |
| 70 | + |
| 71 | + wpm = 200 # Median reading speed |
| 72 | + reading_time = math.ceil(word_count / wpm) if wpm > 0 else 0 |
| 73 | + |
| 74 | + if reading_time <= 0: |
| 75 | + context.pop("reading_time", None) |
| 76 | + return |
| 77 | + |
| 78 | + context["reading_time"] = reading_time |
| 79 | + |
| 80 | + body = context.get("body") |
| 81 | + if not isinstance(body, str) or "</h1>" not in body: |
| 82 | + return |
| 83 | + |
| 84 | + minutes_label = "minute" if reading_time == 1 else "minutes" |
| 85 | + badge_html = ( |
| 86 | + '<div class="eegdash-reading-time" role="note">' |
| 87 | + '<span class="eegdash-reading-time__label">Estimated reading time:</span>' |
| 88 | + f'<span class="eegdash-reading-time__value">{reading_time} {minutes_label}</span>' |
| 89 | + "</div>" |
| 90 | + ) |
| 91 | + |
| 92 | + insert_at = body.find("</h1>") |
| 93 | + if insert_at == -1: |
| 94 | + return |
| 95 | + |
| 96 | + context["body"] = body[: insert_at + 5] + badge_html + body[insert_at + 5 :] |
| 97 | + |
| 98 | + |
| 99 | +def setup(app): |
| 100 | + """Setup the Sphinx extension.""" |
| 101 | + app.connect("html-page-context", html_page_context) |
| 102 | + return { |
| 103 | + "version": "0.1", |
| 104 | + "parallel_read_safe": True, |
| 105 | + "parallel_write_safe": True, |
| 106 | + } |
0 commit comments