57 lines
3.1 KiB
Python
57 lines
3.1 KiB
Python
from pathlib import Path
|
|
from html.parser import HTMLParser
|
|
from urllib.parse import urlsplit,unquote
|
|
from collections import Counter
|
|
import json,re,subprocess
|
|
r=Path(__file__).resolve().parents[1];fail=[];assets=set();anchors=[]
|
|
class Page(HTMLParser):
|
|
def __init__(self):super().__init__();self.ids=[];self.refs=[];self.h1=0;self.scripts=[];self.ld=False;self.text=[];self.script=False;self.canonical=[]
|
|
def handle_starttag(self,t,attrs):
|
|
a=dict(attrs)
|
|
if 'id'in a:self.ids.append(a['id'])
|
|
self.h1+=t=='h1'
|
|
for k in ('src','href','poster','data-media-src'):
|
|
if a.get(k):self.refs.append(a[k])
|
|
if a.get('srcset'):self.refs.extend(x.strip().split(' ')[0] for x in a['srcset'].split(','))
|
|
if t=='script':self.script=True;self.ld=a.get('type')=='application/ld+json'
|
|
if t=='link'and a.get('rel')=='canonical':self.canonical.append(a.get('href'))
|
|
def handle_data(self,d):
|
|
if self.ld:self.scripts.append(d)
|
|
if not self.script:self.text.append(d)
|
|
def handle_endtag(self,t):
|
|
if t=='script':self.script=False;self.ld=False
|
|
for f in [r/'index.html',*sorted((r/'knowledge').rglob('index.html'))]:
|
|
s=f.read_text();p=Page();p.feed(s)
|
|
if '{{'in s:fail.append((str(f),'unrendered template'))
|
|
duplicates=[k for k,v in Counter(p.ids).items() if v>1]
|
|
if duplicates:fail.append((str(f),'duplicate ids',duplicates))
|
|
if 'http-equiv="refresh"'not in s and p.h1!=1:fail.append((str(f),'h1 count',p.h1))
|
|
if len(p.canonical)!=1 or not p.canonical[0].startswith('https://nodedc.ru/'):fail.append((str(f),'canonical',p.canonical))
|
|
for ld in p.scripts:json.loads(ld)
|
|
for url in p.refs:
|
|
parsed=urlsplit(url)
|
|
if parsed.scheme or parsed.netloc or url.startswith('data:'):continue
|
|
path=unquote(parsed.path)
|
|
target=(r/path.lstrip('/'))if path.startswith('/')else(f.parent/path)
|
|
if not path:target=f
|
|
if target.is_dir():target=target/'index.html'
|
|
if not target.exists():fail.append((str(f),'missing',url));continue
|
|
if target.suffix not in ('.html',):assets.add(str(target.resolve().relative_to(r.resolve())))
|
|
if parsed.fragment and target==f and parsed.fragment not in p.ids:fail.append((str(f),'missing anchor',url))
|
|
if re.search(r'1С|тендер|бухгалтер|юридическ', ' '.join(p.text),re.I):fail.append((str(f),'old business copy'))
|
|
old=json.loads(subprocess.check_output(['git','show','system-automation:content/pages/home.json'],cwd=r))
|
|
new=json.loads((r/'content/pages/home.json').read_text())
|
|
def videos(data):
|
|
result=set()
|
|
for reg in data['regions']:
|
|
for block in reg['blocks']:
|
|
content=block['content']
|
|
for item in [content,*content.get('sliderItems',[])]:
|
|
src=item.get('videoSrc','')
|
|
if src.endswith(('.mp4','.mov','.webm')):result.add(src)
|
|
return result
|
|
oldvideos=videos(old);newvideos=videos(new)
|
|
assert oldvideos<=newvideos,oldvideos-newvideos
|
|
report={'pages':len(list((r/'knowledge').rglob('index.html')))+1,'activeKnowledgeArticles':len(json.loads((r/'content/knowledge.json').read_text())['tree']),'legacyRedirects':len(json.loads((r/'content/redirects.json').read_text())),'preservedVideos':len(oldvideos),'referencedAssets':len(assets),'failures':fail}
|
|
print(json.dumps(report,ensure_ascii=False,indent=2));raise SystemExit(bool(fail))
|