47 lines
5.1 KiB
PowerShell

# Pure OOXML fixture generation: does not start Word, connect to PlotDirector or alter existing files.
param([Parameter(Mandatory=$true)][string]$OutputDirectory)
$ErrorActionPreference='Stop'
Add-Type -AssemblyName System.IO.Compression.FileSystem
New-Item -ItemType Directory -Force -Path $OutputDirectory | Out-Null
function New-Fixture([string]$Name,[int]$Words,[int]$WordsPerParagraph,[int]$Book) {
$target=Join-Path $OutputDirectory "$Name.docx"
if(Test-Path $target){throw "Refusing to overwrite $target"}
$guid=[Guid]::NewGuid().ToString('D')
$builder=New-Object System.Text.StringBuilder
[void]$builder.Append('<?xml version="1.0" encoding="utf-8"?><w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"><w:body>')
$remaining=$Words;$chapter=0;$scene=0;$paragraph=0
while($remaining -gt 0){
if($paragraph % 100 -eq 0){
$chapter++
[void]$builder.Append("<w:sdt><w:sdtPr><w:tag w:val=`"PD-CHAPTER-$($Book*1000+$chapter)`"/></w:sdtPr><w:sdtContent><w:p><w:pPr><w:pStyle w:val=`"Heading1`"/></w:pPr><w:r><w:t>Chapter $chapter</w:t></w:r></w:p></w:sdtContent></w:sdt>")
}
if($paragraph % 20 -eq 0){
$scene++
[void]$builder.Append("<w:sdt><w:sdtPr><w:tag w:val=`"PD-SCENE-$($Book*10000+$scene)`"/></w:sdtPr><w:sdtContent><w:p><w:pPr><w:pStyle w:val=`"Heading2`"/></w:pPr><w:r><w:t>Scene $scene</w:t></w:r></w:p></w:sdtContent></w:sdt>")
}
$take=[Math]::Min($WordsPerParagraph,$remaining)
$text=(1..$take | ForEach-Object {'word'}) -join ' '
[void]$builder.Append("<w:p><w:r><w:t>$text</w:t></w:r></w:p>")
$remaining-=$take;$paragraph++
}
[void]$builder.Append('<w:sectPr/></w:body></w:document>')
$entries=@{
'[Content_Types].xml'='<?xml version="1.0"?><Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types"><Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/><Default Extension="xml" ContentType="application/xml"/><Override PartName="/word/document.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"/><Override PartName="/word/styles.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml"/><Override PartName="/docProps/custom.xml" ContentType="application/vnd.openxmlformats-officedocument.custom-properties+xml"/></Types>'
'_rels/.rels'='<?xml version="1.0"?><Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rDoc" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="word/document.xml"/><Relationship Id="rProps" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/custom-properties" Target="docProps/custom.xml"/></Relationships>'
'word/_rels/document.xml.rels'='<?xml version="1.0"?><Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rStyles" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/></Relationships>'
'word/styles.xml'='<?xml version="1.0"?><w:styles xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"><w:style w:type="paragraph" w:styleId="Normal"><w:name w:val="Normal"/></w:style><w:style w:type="paragraph" w:styleId="Heading1"><w:name w:val="heading 1"/><w:pPr><w:outlineLvl w:val="0"/></w:pPr></w:style><w:style w:type="paragraph" w:styleId="Heading2"><w:name w:val="heading 2"/><w:pPr><w:outlineLvl w:val="1"/></w:pPr></w:style></w:styles>'
'word/document.xml'=$builder.ToString()
'docProps/custom.xml'="<?xml version=`"1.0`"?><Properties xmlns=`"http://schemas.openxmlformats.org/officeDocument/2006/custom-properties`" xmlns:vt=`"http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes`"><property fmtid=`"{D5CDD505-2E9C-101B-9397-08002B2CF9AE}`" pid=`"2`" name=`"PlotDirector.Native.DocumentGuid`"><vt:lpwstr>$guid</vt:lpwstr></property><property fmtid=`"{D5CDD505-2E9C-101B-9397-08002B2CF9AE}`" pid=`"3`" name=`"PlotDirector.Native.ProjectID`"><vt:lpwstr>1</vt:lpwstr></property><property fmtid=`"{D5CDD505-2E9C-101B-9397-08002B2CF9AE}`" pid=`"4`" name=`"PlotDirector.Native.BookID`"><vt:lpwstr>$Book</vt:lpwstr></property></Properties>"
}
$stream=[System.IO.File]::Open($target,[System.IO.FileMode]::CreateNew)
$zip=New-Object System.IO.Compression.ZipArchive($stream,[System.IO.Compression.ZipArchiveMode]::Create)
try{foreach($item in $entries.GetEnumerator()){$entry=$zip.CreateEntry($item.Key);$writer=New-Object System.IO.StreamWriter($entry.Open(),(New-Object System.Text.UTF8Encoding($false)));try{$writer.Write($item.Value)}finally{$writer.Dispose()}}}finally{$zip.Dispose();$stream.Dispose()}
[pscustomobject]@{File=$target;ProseWords=$Words;Paragraphs=$paragraph;Book=$Book;ManuscriptGuid=$guid}
}
@(
New-Fixture 'Discovery-190k' 190000 60 101
New-Fixture 'Reckoning-250k' 250000 60 102
New-Fixture 'Redemption-many-short' 250000 10 103
New-Fixture 'Long-paragraph-250k' 250000 250000 104
) | ConvertTo-Json | Set-Content (Join-Path $OutputDirectory 'fixtures.json')