"""Two original CC0 metadata controls sharing one body corpus; browser repeats are not originals."""
from pathlib import Path
import html,json,hashlib
out=Path('benchmarks/fixtures/rtf-metadata1');out.mkdir(exist_ok=False)
values={
 'literals':dict(title=r'Literal {title} \author Ω 🐢 界 é العربية & <tag>',creator=r'Writer {author} \title Ж 🦉 界 é & <name>'),
 'boundary':dict(title=('Ω🐢{\\}界 é '*400)[:4096],creator=('Ж🦉{\\}界 á '*400)[:4096]),
}
# The HTML input limit is UTF16 code units, not Python code points.
for meta in values.values():
 for key,value in list(meta.items()):
  b=value.encode('utf-16-le')[:4096*2]
  while True:
   try:meta[key]=b.decode('utf-16-le').strip();break
   except UnicodeDecodeError:b=b[:-2]
body='<h1>Independent metadata escape control</h1><p>Original body Ω Ж café. <strong>Bold control.</strong> <em>Italic control.</em></p><pre>  literal\ttext\n\nlast  </pre><table><tr><td>Cell one</td><td>Cell two</td></tr></table><p><a href="https://example.invalid/manual">Manual link</a></p>'
rows=[]
for name,meta in values.items():
 source=('<!doctype html><html lang="fr-CA"><head><meta charset="utf-8"><title>'+html.escape(meta['title'])+'</title><meta name="author" content="'+html.escape(meta['creator'],quote=True)+'"></head><body>'+body+'</body></html>').encode()
 (out/(name+'.html')).write_bytes(source);rows.append(dict(file=name+'.html',bytes=len(source),sha256=hashlib.sha256(source).hexdigest(),**meta,titleUtf16Units=len(meta['title'].encode('utf-16-le'))//2,authorUtf16Units=len(meta['creator'].encode('utf-16-le'))//2))
(out/'manifest.json').write_text(json.dumps(dict(license='CC0-1.0',originalMetadataControls=2,sharedOriginalBodyCorpora=1,rows=rows),indent=2,ensure_ascii=False)+'\n')
(out/'LICENSE.txt').write_text('These original synthetic HTML fixtures are dedicated to the public domain under CC0 1.0: https://creativecommons.org/publicdomain/zero/1.0/\n')
print([(r['file'],r['titleUtf16Units'],r['authorUtf16Units'])for r in rows])
