#!/usr/bin/env python3
"""Check HTML files for Surreal CMS integration problems.

Usage: python3 check.py page.html [more.html ...]

Flags the same problems the Surreal CMS source editor underlines, plus a few
more that break publishing. Exits with status 1 if any errors are found.
Standard library only.
"""

import re
import sys
from html.parser import HTMLParser

BLOCK = set(
    'address article aside blockquote dd div dt fieldset figure footer header hgroup li main nav ol section '
    'td th ul'.split()
)
INLINE = set(
    'abbr acronym b big caption cite code del dfn em figcaption h1 h2 h3 h4 h5 h6 i ins kbd label mark p q s '
    'samp small span strong sub sup tt var'.split()
)
VOID = set('area base br col embed hr img input link meta param source track wbr'.split())
LEGACY = {
    'editable': 'cms-editable',
    'cushycms': 'cms-editable',
    'cushycms-wysiwyg': 'cms-editable',
    'clienteditor': 'cms-editable',
    'clienteditor-wysiwyg': 'cms-editable',
    'editable-text': 'cms-editable + data-type="text"',
    'cushycms-text': 'cms-editable + data-type="text"',
    'clienteditor-text': 'cms-editable + data-type="text"',
    'cms-editable-text': 'cms-editable + data-type="text"',
    'editable-gallery': 'cms-editable + data-type="gallery"',
    'repeatable': 'cms-repeatable',
    'uneditable': 'cms-lock',
}
TEMPLATE_TAG = re.compile(r'\{\{.*?\}\}|\{%.*?%\}|<%.*?%>', re.S)


class Frame:
    def __init__(self, tag, attrs, line):
        self.tag = tag
        self.attrs = attrs
        self.line = line
        self.classes = set((attrs.get('class') or '').split())

    @property
    def is_region(self):
        return 'cms-editable' in self.classes

    @property
    def is_background(self):
        return self.is_region and self.attrs.get('data-type') == 'background'


class Checker(HTMLParser):
    def __init__(self, path):
        super().__init__(convert_charrefs=False)
        self.path = path
        self.stack = []
        self.ids = {}
        self.problems = []
        self.ignore_open = None

    # Helpers

    def report(self, level, message, line=None):
        self.problems.append((line or self.getpos()[0], level, message))

    def ancestors(self):
        return reversed(self.stack)

    def region(self):
        """The innermost editable region that holds content (not a background region)."""
        for frame in self.ancestors():
            if frame.is_region and not frame.is_background:
                return frame
        return None

    def inside(self, predicate):
        return any(predicate(frame) for frame in self.stack)

    # Parser callbacks

    def handle_starttag(self, tag, attr_list):
        attrs = {k: (v if v is not None else '') for k, v in attr_list}
        frame = Frame(tag, attrs, self.getpos()[0])
        classes = frame.classes
        region = self.region()
        el_id = attrs.get('id')

        if el_id:
            if el_id in self.ids:
                self.report('error', f'Duplicate id "{el_id}" (first used on line {self.ids[el_id]}).')
            else:
                self.ids[el_id] = frame.line

        for cls in classes & LEGACY.keys():
            self.report('warning', f'Legacy class "{cls}". Use {LEGACY[cls]} instead.')

        if frame.is_region:
            self.check_region(frame, region)

        if 'cms-include' in classes and not el_id:
            self.report('error', f'<{tag}> has cms-include but no id.')

        if 'cms-repeatable' in classes:
            if not region:
                self.report('error', 'cms-repeatable must be inside a block or inline content region.')
            if el_id:
                self.report('error', f'Repeatable has id "{el_id}". Duplicating it will duplicate the id.')

        if 'cms-lock' in classes and not region:
            self.report('error', 'cms-lock must be inside a block or inline content region.')

        if 'cms-unlock' in classes and not self.inside(lambda f: 'cms-lock' in f.classes) and 'cms-lock' not in classes:
            self.report('error', 'cms-unlock must be inside a cms-lock element.')

        if el_id and not frame.is_region and self.inside(lambda f: 'cms-repeatable' in f.classes):
            self.report('error', f'id "{el_id}" is inside a repeatable. Duplicating it will duplicate the id.')

        if region and not frame.is_region:
            if tag == 'svg':
                self.report('error', f'Inline <svg> inside region "{region.attrs.get("id")}" will be removed. Use <img>.')
            elif '-' in tag:
                self.report('error', f'Custom element <{tag}> inside region "{region.attrs.get("id")}" will be removed.')
            elif tag == 'script':
                self.report('warning', f'<script> inside region "{region.attrs.get("id")}" runs in the editor. Move it out.')
            for name in attrs:
                if name.startswith('on'):
                    self.report('warning', f'Inline {name} handler inside region "{region.attrs.get("id")}" will be removed.')
            if (attrs.get('href') or '').strip().lower().startswith('javascript:'):
                self.report('warning', f'javascript: link inside region "{region.attrs.get("id")}" will be removed.')

        if tag not in VOID:
            self.stack.append(frame)

    def handle_startendtag(self, tag, attrs):
        self.handle_starttag(tag, attrs)
        if tag not in VOID and self.stack and self.stack[-1].tag == tag:
            self.stack.pop()

    def handle_endtag(self, tag):
        for i in range(len(self.stack) - 1, -1, -1):
            if self.stack[i].tag == tag:
                del self.stack[i:]
                return

    def handle_comment(self, data):
        text = data.strip()
        if text in ('cms-disable', '/cms-disable'):
            self.report('warning', 'cms-disable comments are deprecated. Use <!--cms-ignore--> and <!--/cms-ignore-->.')
        if data in ('cms-ignore', 'cms-disable'):
            if self.ignore_open:
                self.report('error', f'cms-ignore opened again before the one on line {self.ignore_open} was closed.')
            self.ignore_open = self.getpos()[0]
            self.check_ignore_placement()
        elif data in ('/cms-ignore', '/cms-disable'):
            if not self.ignore_open:
                self.report('error', 'Closing cms-ignore comment without an opening one.')
            self.ignore_open = None
        elif text in ('cms-ignore', '/cms-ignore'):
            self.report('error', 'cms-ignore comments must be written with no spaces: <!--cms-ignore-->.')

    def handle_pi(self, data):
        region = self.region()
        if region:
            self.report('error', f'Server-side code inside region "{region.attrs.get("id")}" will be lost on publish.')

    def handle_data(self, data):
        region = self.region()
        if region and TEMPLATE_TAG.search(data) and not self.inside(lambda f: f.tag in ('code', 'pre')):
            self.report('warning', f'Template tag inside region "{region.attrs.get("id")}". If a template engine renders this page, it will be lost on publish.')

    # Checks

    def check_region(self, frame, outer):
        tag, attrs = frame.tag, frame.attrs
        data_type = attrs.get('data-type')
        el_id = attrs.get('id')

        if not el_id:
            self.report('error', f'<{tag}> has cms-editable but no id.')

        if outer:
            self.report('error', f'Region "{el_id}" is nested inside region "{outer.attrs.get("id")}" (line {outer.line}).')
        elif frame.is_background and self.inside(lambda f: f.is_region):
            self.report('error', f'Background region "{el_id}" can\'t be inside another region.')

        if data_type == 'gallery':
            if tag not in ('div', 'ul', 'ol'):
                self.report('error', f'Gallery region "{el_id}" must be a <div>, <ul>, or <ol>.')
        elif data_type in ('background', 'text'):
            pass
        elif tag not in BLOCK and tag not in INLINE and tag not in ('img', 'a'):
            self.report('error', f'<{tag}> can\'t be a content region. Wrap it in a <div> and make that the region.')

        if tag == 'img':
            bad = [a for a in ('srcset', 'sizes', 'width', 'height') if a in attrs]
            if bad:
                self.report('warning', f'Image region "{el_id}" has {", ".join(bad)}. Only src and alt are updated on publish.')
            if self.stack and self.stack[-1].tag == 'picture':
                self.report('error', f'Image region "{el_id}" is inside <picture>. The browser will keep showing the old image.')

        if self.ignore_open:
            self.report('error', f'Region "{el_id}" is inside a cms-ignore block and will be hidden from the editor.')

    def check_ignore_placement(self):
        region = self.region()
        if region:
            self.report('error', f'cms-ignore inside region "{region.attrs.get("id")}". The ignored content is lost on publish.')
        elif self.inside(lambda f: 'cms-include' in f.classes):
            self.report('error', 'cms-ignore inside a static include. The ignored content is lost on publish.')

    def finish(self):
        self.close()
        if self.ignore_open:
            self.report('error', 'cms-ignore comment is never closed.', self.ignore_open)
        return sorted(self.problems)


def check_file(path):
    with open(path, 'rb') as f:
        raw = f.read()
    try:
        text = raw.decode('utf-8')
        problems = []
    except UnicodeDecodeError:
        text = raw.decode('latin-1')
        problems = [(1, 'error', 'File isn\'t UTF-8. Special characters will be damaged on publish.')]

    checker = Checker(path)
    checker.feed(text)
    problems += checker.finish()

    if 'cms-editable' not in text and 'cms-include' not in text:
        problems.append((1, 'warning', 'No content regions or includes found. Nothing on this page is editable.'))
    for match in re.finditer(r'<title>\s*(<\?|\{\{|\{%|<%)', text):
        problems.append((text.count('\n', 0, match.start()) + 1, 'warning', '<title> is generated by code. It will be overwritten on publish.'))

    return sorted(problems)


def main(paths):
    if not paths:
        print(__doc__.strip())
        return 2
    errors = 0
    for path in paths:
        problems = check_file(path)
        for line, level, message in problems:
            print(f'{path}:{line}: {level}: {message}')
            errors += level == 'error'
        if not problems:
            print(f'{path}: ok')
    return 1 if errors else 0


if __name__ == '__main__':
    sys.exit(main(sys.argv[1:]))
