#!/usr/bin/env python3
"""Measure the prose share of the /twitch-profile-picture-size study.

Counting rule: take the page <article>, drop <script>/<style>, article nav and
the article-meta template line, remove tags, unescape HTML entities, then split
on whitespace. Every remaining token counts, including numbers, table cells and
control labels. The numerator applies the same rule to the #study section only.
This is a local editorial count, not a semantic de-duplication rate.
"""

import html
import os
import re
import sys

HERE = os.path.dirname(os.path.abspath(__file__))
PAGE = os.path.join(HERE, "..", "twitch-profile-picture-size.html")


def tokens(markup, exclude_template=False):
    text = re.sub(r"(?is)<(script|style)\b[^>]*>.*?</\1>", " ", markup)
    if exclude_template:
        text = re.sub(r"(?is)<nav\b[^>]*>.*?</nav>", " ", text)
        text = re.sub(r'(?is)<p\b[^>]*class="[^"]*\barticle-meta\b[^"]*"[^>]*>.*?</p>', " ", text)
    text = html.unescape(re.sub(r"(?s)<[^>]+>", " ", text))
    return len(text.split())


def main():
    page = open(PAGE, encoding="utf-8").read()
    article = re.search(r"<article>([\s\S]*?)</article>", page)
    study = re.search(r'<section id="study">([\s\S]*?)</section>', page)
    if not article or not study:
        sys.exit('Expected <article> and <section id="study"> on the page')
    raw_body = tokens(article.group(1))
    body, module = tokens(article.group(1), exclude_template=True), tokens(study.group(1))
    excluded = raw_body - body
    share = module / body * 100
    print(f"study={module} article={body} excluded={excluded} share={share:.2f}%")
    if share < 30:
        sys.exit("Study share is below 30%")


if __name__ == "__main__":
    main()
