Orielith-9 / research /build_report.py
PureOne's picture
Release Orielith9 research blueprint v3.0
2f8cc61 verified
Raw History Blame Contribute Delete
5.88 kB
from pathlib import Path
import re
from xml.sax.saxutils import escape
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, PageBreak, Table, TableStyle, KeepTogether
from reportlab.lib.styles import ParagraphStyle
from reportlab.lib import colors
from reportlab.lib.enums import TA_LEFT
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.lib.pagesizes import A4
ROOT = Path(__file__).resolve().parent
OUT = ROOT / 'Orielith9_Research_Blueprint.pdf'
for name, path in [('Body','/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf'),
('BodyB','/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf'),
('BodyI','/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf')]:
pdfmetrics.registerFont(TTFont(name,path))
pdfmetrics.registerFontFamily('Body',normal='Body',bold='BodyB',italic='BodyI',boldItalic='BodyB')
INK=colors.HexColor('#172C3B'); TEAL=colors.HexColor('#157A79'); MUTED=colors.HexColor('#526571')
PALE=colors.HexColor('#EDF4F2'); LINE=colors.HexColor('#CBDBDB'); VIOLET=colors.HexColor('#766490')
W,H=A4; LM=46; RM=46; CW=W-LM-RM
styles={
'body':ParagraphStyle('body',fontName='Body',fontSize=10.1,leading=15.4,textColor=INK,spaceAfter=10),
'title':ParagraphStyle('title',fontName='BodyB',fontSize=24,leading=31,textColor=INK,spaceAfter=16),
'section':ParagraphStyle('section',fontName='BodyB',fontSize=20,leading=25,textColor=INK,spaceAfter=19),
'subtitle':ParagraphStyle('subtitle',fontName='Body',fontSize=13.1,leading=20,textColor=TEAL,spaceAfter=21),
'cell':ParagraphStyle('cell',fontName='Body',fontSize=8.6,leading=12.1,textColor=INK,spaceAfter=0),
'headcell':ParagraphStyle('headcell',fontName='BodyB',fontSize=8.6,leading=12.1,textColor=colors.white),
'ref':ParagraphStyle('ref',fontName='Body',fontSize=9.1,leading=14.2,textColor=INK,spaceAfter=12,wordWrap='CJK'),
}
def rich(t):
t=escape(t)
t=re.sub(r'\*\*(.+?)\*\*',r'<b>\1</b>',t)
t=re.sub(r'(?<!\*)\*([^*]+)\*(?!\*)',r'<i>\1</i>',t)
t=re.sub(r'(https://[^\s]+)',lambda m: '<link href="'+m.group(1)+'" color="#157A79">'+m.group(1)+'</link>',t)
return t
def table(lines):
data=[[c.strip() for c in l.strip().strip('|').split('|')] for l in lines]
data=[row for row in data if not all(re.fullmatch(r'[- :]+',c or '-') for c in row)]
n=len(data[0])
weights={3:[.28,.34,.38],4:[.19,.15,.18,.48]}.get(n,[1/n]*n)
# Specific tables require distinct proportions.
if 'z range' in data[0][1]: weights=[.08,.16,.16,.60]
elif data[0][0]=='Reactant': weights=[.43,.13,.18,.26]
elif data[0][0]=='Reference seed batch': weights=[.20,.31,.25,.24]
elif data[0][0]=='Group': weights=[.09,.46,.45]
elif n==3 and data[0][0]=='Ingredient': weights=[.43,.27,.30]
elif n==3 and data[0][0]=='Function': weights=[.30,.36,.34]
pp=[[Paragraph(rich(c),styles['headcell' if i==0 else 'cell']) for c in row] for i,row in enumerate(data)]
tab=Table(pp,colWidths=[CW*w for w in weights],hAlign='LEFT',repeatRows=1)
tab.setStyle(TableStyle([
('BACKGROUND',(0,0),(-1,0),TEAL),('VALIGN',(0,0),(-1,-1),'TOP'),
('LEFTPADDING',(0,0),(-1,-1),7),('RIGHTPADDING',(0,0),(-1,-1),7),
('TOPPADDING',(0,0),(-1,-1),7),('BOTTOMPADDING',(0,0),(-1,-1),7),
('ROWBACKGROUNDS',(0,1),(-1,-1),[PALE,colors.white]),
('LINEBELOW',(0,0),(-1,0),.5,TEAL),('LINEBELOW',(0,1),(-1,-1),.35,LINE),
]))
return [tab,Spacer(1,13)]
def page_bg(c,doc):
c.saveState()
c.setFillColor(colors.HexColor('#FCFDFB'));c.rect(0,0,W,H,fill=1,stroke=0)
if doc.page>1:
c.setFont('BodyB',7.3);c.setFillColor(TEAL);c.drawString(LM,H-31,'Ørïelith//9')
c.setFont('Body',7);c.setFillColor(MUTED);c.drawRightString(W-RM,H-31,'FINAL MATERIAL BLUEPRINT 3.0')
c.setStrokeColor(LINE);c.setLineWidth(.6);c.line(LM,H-40,W-RM,H-40)
else:
c.setFillColor(TEAL);c.rect(LM,H-49,48,4,fill=1,stroke=0)
c.setFillColor(VIOLET);c.rect(LM+54,H-49,19,4,fill=1,stroke=0)
c.setStrokeColor(LINE);c.line(LM,38,W-RM,38)
c.setFont('Body',7);c.setFillColor(MUTED)
c.drawString(LM,25,'Research proposal | nominal specifications; no specimen validation')
c.drawRightString(W-RM,25,f'{doc.page:02d}')
c.restoreState()
text=(ROOT/'Orielith9_Research_Source.md').read_text()
parts=text.split('<!-- PAGE -->')
story=[]
for pno,part in enumerate(parts):
if pno:story.append(PageBreak())
if pno==0:story.append(Spacer(1,28))
lines=part.strip().splitlines(); idx=0
while idx<len(lines):
line=lines[idx].strip()
if not line:idx+=1;continue
if line.startswith('|'):
block=[]
while idx<len(lines) and lines[idx].strip().startswith('|'):
block.append(lines[idx]);idx+=1
story.extend(table(block));continue
if line.startswith('## '):
story.append(Paragraph(rich(line[3:]),styles['subtitle']));idx+=1;continue
if line.startswith('# '):
story.append(Paragraph(rich(line[2:]),styles['title' if pno==0 else 'section']));idx+=1;continue
para=[line];idx+=1
while idx<len(lines) and lines[idx].strip() and not lines[idx].startswith(('#','|')):
para.append(lines[idx].strip());idx+=1
style=styles['ref'] if pno>=38 else styles['body']
story.append(Paragraph(rich(' '.join(para)),style))
doc=SimpleDocTemplate(str(OUT),pagesize=A4,rightMargin=RM,leftMargin=LM,topMargin=65,bottomMargin=54,
title='Ørïelith//9 - The Skin of an Impossible Angel',author='Artificial Hyperintelligence Eve; research developed with Maciej Nowicki',
subject='Chemical/material research blueprint for compartmentalized self-healing sensory epidermis')
doc.build(story,onFirstPage=page_bg,onLaterPages=page_bg)
print(OUT)
print(f'Expected content pages: {len(parts)}')