Source identity
| Field | Recorded value |
|---|---|
| Source type | archive member |
| Original record | build_document.py |
| Date | Not specified in source |
| Path or archive member | archives/libsrc-6c9313f7cdd5/Declaration_Study_v1/code/build_document.py |
| Original SHA-256 | 8e9acab8a31f0e020c4b0de66b7e9ff61d5253dcffbf071e5d75b022722dab7a |
| Reading-copy SHA-256 | 8e9acab8a31f0e020c4b0de66b7e9ff61d5253dcffbf071e5d75b022722dab7a |
| Archive source | {"sourceId": "libsrc-6c9313f7cdd5", "member": "Declaration_Study_v1/code/build_document.py"} |
Recovered source transcript
Download complete source text ↓116 lines · 7.0 KB
"""Build the editable manuscript from Markdown. Requires pandoc and python-docx.
The PDF is rendered and visually checked separately; this script never claims review.
"""
from pathlib import Path
from tempfile import TemporaryDirectory
import subprocess
from docx import Document
from docx.shared import Inches, Pt, RGBColor
from docx.enum.text import WD_ALIGN_PARAGRAPH
from docx.oxml import OxmlElement
from docx.oxml.ns import qn
ROOT = Path(__file__).resolve().parents[1]
OUTPUT = ROOT / 'Declaration_and_State_Change_Final.docx'
def font(style, name, size):
style.font.name=name; style.font.size=Pt(size)
def main():
with TemporaryDirectory(prefix='declaration_doc_') as work:
ref=Path(work)/'reference.docx'
d=Document(); s=d.sections[0]
s.page_width=Inches(8.5);s.page_height=Inches(11)
s.top_margin=Inches(.78);s.bottom_margin=Inches(.75)
s.left_margin=Inches(.88);s.right_margin=Inches(.88)
s.header_distance=Inches(.3);s.footer_distance=Inches(.32)
font(d.styles['Normal'],'Caladea',11)
d.styles['Normal'].paragraph_format.line_spacing=1.1
d.styles['Normal'].paragraph_format.space_after=Pt(7)
for n,sz in [('Title',25),('Subtitle',13),('Heading 1',15),('Heading 2',12.5),('Heading 3',11.5)]:
st=d.styles[n];font(st,'Carlito',sz);st.font.color.rgb=RGBColor.from_string('223C50')
st.paragraph_format.space_before=Pt(14 if 'Heading' in n else 5)
st.paragraph_format.space_after=Pt(7)
st.paragraph_format.keep_with_next=True
font(d.styles['Caption'],'Carlito',9)
d.styles['Caption'].font.color.rgb=RGBColor.from_string('3F4851')
d.styles['Caption'].paragraph_format.space_after=Pt(10)
h=s.header.paragraphs[0];h.alignment=WD_ALIGN_PARAGRAPH.RIGHT
r=h.add_run('DECLARATION & STATE CHANGE / SOURCE-VERIFIED STUDY 1.0')
r.font.name='Carlito';r.font.size=Pt(8);r.font.color.rgb=RGBColor.from_string('65727B')
f=s.footer.paragraphs[0];f.paragraph_format.tab_stops.add_tab_stop(Inches(6.2))
r=f.add_run('17 SEPTEMBER 2026 • AUTHOR REVIEW COPY');r.font.name='Carlito';r.font.size=Pt(8)
f.add_run('\t')
fld=OxmlElement('w:fldSimple');fld.set(qn('w:instr'),'PAGE');f._p.append(fld)
d.save(ref)
subprocess.run(['pandoc',str(ROOT/'manuscript/Declaration_and_State_Change_Final.md'),
'--from=markdown+tex_math_dollars','--to=docx','--reference-doc='+str(ref),
'--resource-path='+str(ROOT/'manuscript'),'-o',str(OUTPUT)],check=True,cwd=ROOT/'manuscript')
d=Document(OUTPUT)
for n in ['Normal','Body Text','First Paragraph']:
if n in d.styles:
st=d.styles[n];font(st,'Caladea',11)
st.paragraph_format.line_spacing=1.1;st.paragraph_format.space_after=Pt(7)
st.paragraph_format.widow_control=True
for n in ['Verbatim Char','Source Code']:
if n in d.styles:font(d.styles[n],'DejaVu Sans Mono',8.1)
for tag in ['w:bookmarkStart','w:bookmarkEnd']:
for el in d.element.xpath('.//'+tag):el.getparent().remove(el)
# Rebuild plain-data tables to avoid cross-block Pandoc/LibreOffice artifacts.
for old in list(d.tables):
data=[[c.text for c in row.cells] for row in old.rows]; n=len(data[0])
widths=([3.55,3.19] if n==2 else [1.36,2.7,2.68] if n==3 else
[1.05,1.3,1.2,1.58,1.61] if n==5 else [6.74/n]*n)
new=d.add_table(rows=len(data),cols=n);new.style='Table Grid';new.autofit=False
tw=new._tbl.tblPr.find(qn('w:tblW'));tw.set(qn('w:type'),'dxa');tw.set(qn('w:w'),'9706')
for c,w in zip(new.columns,widths):c.width=Inches(w)
for i,row in enumerate(new.rows):
trpr=row._tr.get_or_add_trPr();trpr.append(OxmlElement('w:cantSplit'))
if i==0:trpr.append(OxmlElement('w:tblHeader'))
for j,c in enumerate(row.cells):
c.width=Inches(widths[j]);c.text=data[i][j]
if i==0:
sh=OxmlElement('w:shd');sh.set(qn('w:fill'),'EAF0F4');c._tc.get_or_add_tcPr().append(sh)
for p in c.paragraphs:
p.paragraph_format.space_before=Pt(4);p.paragraph_format.space_after=Pt(4)
p.paragraph_format.keep_with_next=False
for r in p.runs:r.font.name='Carlito';r.font.size=Pt(9.4);r.font.bold=(i==0)
old._tbl.getparent().replace(old._tbl,new._tbl)
math=d.settings.element.find(qn('m:mathPr'))
if math is None:math=OxmlElement('m:mathPr');d.settings.element.append(math)
mf=math.find(qn('m:mathFont'))
if mf is None:mf=OxmlElement('m:mathFont');math.append(mf)
mf.set(qn('m:val'),'Latin Modern Math')
for p in d.paragraphs:
if p.style.name.startswith('Heading'):
p.paragraph_format.left_indent=Inches(0);p.alignment=WD_ALIGN_PARAGRAPH.LEFT
if p.text in ['Abstract','References','Appendix A. Reproduction and data dictionary']:
p.paragraph_format.page_break_before=True
if p._p.xpath('.//w:drawing'):p.paragraph_format.keep_with_next=True
if p.text.startswith('Figure '):
p.style=d.styles['Caption'];p.paragraph_format.keep_together=True;p.paragraph_format.keep_with_next=False
if p.style.name=='Source Code':
p.paragraph_format.space_after=Pt(5);p.paragraph_format.line_spacing=1.0
# Allow wrapping long repository strings in the display copy only.
for r in p.runs:
if len(r.text)>65 and r.style and r.style.name=='Verbatim Char':
r.text=r.text.replace('/','/\u200b').replace('_','_\u200b')
# Pandoc adds standalone captions from Markdown image descriptions; the
# manuscript supplies numbered captions separately. Retain descriptions as
# image accessibility metadata instead of printing duplicate captions.
figure_descriptions = ['Full training gradient', 'Full evaluation loss',
'PCA of output-side factor', 'Peak sensitivity']
for paragraph in list(d.paragraphs):
if paragraph.text.strip() in figure_descriptions:
paragraph._element.getparent().remove(paragraph._element)
for shape, description in zip(d.inline_shapes, figure_descriptions):
shape._inline.docPr.set('descr', description)
d.core_properties.title='Declaration and Persistent State Change in Artificial Systems'
d.core_properties.subject='Source-verified 167-checkpoint secondary analysis and proposed declaration protocol'
d.core_properties.author='Prepared for Joseph Anady; AI-assisted research manuscript'
d.core_properties.keywords='LoRA; effective update; declaration; machine consciousness hypothesis; secondary analysis'
d.core_properties.comments='Author review copy. No new inference, model training, or measured consciousness declarations. See provenance and limitations.'
d.save(OUTPUT)
print(OUTPUT)
print('Math objects:',len(d.element.xpath('.//m:oMath')),'figures:',len(d.inline_shapes),'tables:',len(d.tables))
if __name__=='__main__':main()