-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompile-episode.ps1
More file actions
302 lines (249 loc) · 9.51 KB
/
compile-episode.ps1
File metadata and controls
302 lines (249 loc) · 9.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#!/usr/bin/env pwsh
<#!
.SYNOPSIS
Compiles episode scene files into EPUB and PDF-optimized markdown formats, then optionally generates EPUB/PDF via pandoc.
.DESCRIPTION
- Reads all scene files from an episode directory under content/episodes
- Produces two markdown outputs in output/
- *-epub.md (minimalist, page breaks via <div class="page"></div>)
- *-pdf.md (includes title page + cover art, page breaks via \\newpage)
- Normalizes per-scene headers to "## 1", "## 2", ...
- Copies cover art to output/coverart.jpg when available
.PARAMETER EpisodeNumber
The episode number to compile (e.g., 1, 2, 3)
#>
param(
[Parameter(Mandatory=$true, Position=0)]
[int]$EpisodeNumber
)
function Get-ProjectTitle {
$projectFile = "elements\project.md"
if (-not (Test-Path $projectFile)) {
return "Project"
}
$content = Get-Content $projectFile -Raw
$m = [regex]::Match($content, '(?m)^-\s*\*\*Title:\*\*\s*(.+)\s*$')
if ($m.Success) {
return $m.Groups[1].Value.Trim()
}
return "Project"
}
function Get-CoverPath {
$projectFile = "elements\project.md"
if (-not (Test-Path $projectFile)) {
return $null
}
$content = Get-Content $projectFile -Raw
$m = [regex]::Match($content, '(?m)^-\s*\*\*Cover Art Path:\*\*\s*`?([^`\r\n]+)`?\s*$')
if ($m.Success) {
return $m.Groups[1].Value.Trim()
}
return $null
}
function ConvertTo-SafeFileStem([string]$title) {
# Remove characters Windows disallows in filenames and collapse whitespace.
$safe = $title -replace '[<>:"/\\|?*]', ''
$safe = $safe.Trim()
if ([string]::IsNullOrWhiteSpace($safe)) {
return "Project"
}
# Prefer a compact stem (spaces removed) for filenames.
$safe = $safe -replace '\s+', ''
return $safe
}
function Get-EpisodeTitle([string]$episodeNumFormatted) {
$outlineFile = "elements\outlines\episode-$episodeNumFormatted.md"
if (-not (Test-Path $outlineFile)) {
return $null
}
$outline = Get-Content $outlineFile -Raw
# Preferred template: **Title:** ...
$m = [regex]::Match($outline, '(?m)^\*\*Title:\*\*\s+(.+)$')
if ($m.Success) {
return $m.Groups[1].Value.Trim()
}
# Fallback template: "## Episode 01: Title" or similar
$m2 = [regex]::Match($outline, '(?m)^##\s+Episode\s+\d+\s*:\s*(.+)$')
if ($m2.Success) {
return $m2.Groups[1].Value.Trim()
}
return $null
}
function ConvertFrom-SceneContent([string]$content) {
# Strip the scene header (e.g., "## Scene 1 - Scene Name")
# Supports both hyphen and em-dash.
$content = $content -replace '(?m)^##\s+Scene\s+\d+\s+(\u2014|-)\s*[^\r\n]*[\r\n]+', ''
return $content.Trim()
}
$episodeNumFormatted = $EpisodeNumber.ToString("00")
$episodeDirs = Get-ChildItem -Path "content\episodes" -Directory -Filter "episode-$episodeNumFormatted-*" -ErrorAction SilentlyContinue
if (-not $episodeDirs -or $episodeDirs.Count -eq 0) {
Write-Error "Could not find episode directory for episode $episodeNumFormatted under content\episodes"
exit 1
}
if ($episodeDirs.Count -gt 1) {
Write-Error "Found multiple episode directories for episode $episodeNumFormatted. Please keep only one episode-$episodeNumFormatted-* folder."
exit 1
}
$episodeDir = $episodeDirs[0]
$episodePath = $episodeDir.FullName
Write-Host "Compiling episode from: $($episodeDir.Name)" -ForegroundColor Cyan
$sceneFiles = Get-ChildItem -Path $episodePath -Filter "*.md" | Sort-Object Name
if ($sceneFiles.Count -eq 0) {
Write-Error "No scene files found in $episodePath"
exit 1
}
$projectTitle = Get-ProjectTitle
$projectStem = ConvertTo-SafeFileStem $projectTitle
$episodeTitle = Get-EpisodeTitle $episodeNumFormatted
if ($episodeTitle) {
Write-Host "Episode title: $episodeTitle" -ForegroundColor Green
} else {
Write-Warning "Could not extract episode title from elements/outlines/episode-$episodeNumFormatted.md (continuing)."
}
$outputDir = "output"
if (-not (Test-Path $outputDir)) {
New-Item -ItemType Directory -Path $outputDir | Out-Null
}
$epubMdFile = Join-Path $outputDir "$projectStem-Episode$episodeNumFormatted-epub.md"
$pdfMdFile = Join-Path $outputDir "$projectStem-Episode$episodeNumFormatted-pdf.md"
# Build EPUB markdown
Write-Host "Building EPUB markdown..." -ForegroundColor Cyan
$epubOutput = @()
$sceneNum = 1
foreach ($file in $sceneFiles) {
$epubOutput += "## $sceneNum"
$epubOutput += ""
$raw = Get-Content $file.FullName -Raw
$epubOutput += (ConvertFrom-SceneContent $raw)
if ($sceneNum -lt $sceneFiles.Count) {
$epubOutput += ""
$epubOutput += ""
$epubOutput += '<div class="page"></div>'
$epubOutput += ""
}
$sceneNum++
}
$epubOutput -join "`n" | Set-Content -Path $epubMdFile -NoNewline
Write-Host "Created: $epubMdFile" -ForegroundColor Green
# Copy cover art (optional)
$coverPath = Get-CoverPath
$resolvedCoverPath = $null
if ($coverPath) {
# Support values like: content/coverart.jpg or content\coverart.jpg
$resolvedCoverPath = $coverPath -replace '/', '\\'
}
$destCover = Join-Path $outputDir "coverart.jpg"
$hasCover = $false
if ($resolvedCoverPath -and (Test-Path $resolvedCoverPath)) {
Copy-Item -Path $resolvedCoverPath -Destination $destCover -Force
Write-Host "Copied cover art to $destCover" -ForegroundColor Green
$hasCover = $true
} else {
Write-Host "No cover art found (skipping)." -ForegroundColor DarkYellow
}
# Build PDF markdown
Write-Host "Building PDF markdown..." -ForegroundColor Cyan
$pdfOutput = @()
$pdfTitle = $projectTitle
$pdfSubtitle = if ($episodeTitle) { "Episode $episodeNumFormatted - $episodeTitle" } else { "Episode $episodeNumFormatted" }
$pdfOutput += "---"
$pdfOutput += "title: $pdfTitle"
$pdfOutput += "subtitle: $pdfSubtitle"
$pdfOutput += "---"
$pdfOutput += ""
if ($hasCover) {
$pdfOutput += ""
$pdfOutput += ""
}
$pdfOutput += '\\newpage'
$pdfOutput += ""
$sceneNum = 1
foreach ($file in $sceneFiles) {
$pdfOutput += "## $sceneNum"
$pdfOutput += ""
$raw = Get-Content $file.FullName -Raw
$pdfOutput += (ConvertFrom-SceneContent $raw)
if ($sceneNum -lt $sceneFiles.Count) {
$pdfOutput += ""
$pdfOutput += ""
$pdfOutput += '\\newpage'
$pdfOutput += ""
}
$sceneNum++
}
$pdfOutput -join "`n" | Set-Content -Path $pdfMdFile -NoNewline
Write-Host "Created: $pdfMdFile" -ForegroundColor Green
# Word count (from EPUB markdown content; exclude headers and page breaks)
$wordCount = ($epubOutput | Where-Object { $_ -notmatch '^##\s+\d+$' -and $_ -notmatch '<div class="page"></div>' } | Out-String | Measure-Object -Word).Words
Write-Host "Total word count: $wordCount" -ForegroundColor Green
# Optional pandoc generation
$pandoc = Get-Command pandoc -ErrorAction SilentlyContinue
if (-not $pandoc) {
Write-Host "Pandoc not found; skipping .epub/.pdf generation (markdown outputs still created)." -ForegroundColor DarkYellow
} else {
# EPUB
Write-Host "Generating EPUB via pandoc..." -ForegroundColor Cyan
Push-Location $outputDir
try {
$epubFileName = "$projectStem-Episode$episodeNumFormatted.epub"
$markdownFileName = "$projectStem-Episode$episodeNumFormatted-epub.md"
$pandocArgs = @(
$markdownFileName,
"-o", $epubFileName
)
if (Test-Path "..\epub-dark-mode.css") {
$pandocArgs += "--css=../epub-dark-mode.css"
}
if ($hasCover -and (Test-Path "coverart.jpg")) {
$pandocArgs += "--epub-cover-image=coverart.jpg"
}
$metaTitle = if ($episodeTitle) { "${projectTitle}: Episode $episodeNumFormatted - $episodeTitle" } else { "${projectTitle}: Episode $episodeNumFormatted" }
$pandocArgs += "--metadata"
$pandocArgs += "title=$metaTitle"
& pandoc @pandocArgs
if ($LASTEXITCODE -eq 0) {
Write-Host "Created: output\$epubFileName" -ForegroundColor Green
} else {
Write-Warning "Pandoc EPUB generation failed (exit code $LASTEXITCODE)."
}
}
finally {
Pop-Location
}
# PDF (requires LaTeX)
Write-Host "Generating PDF via pandoc (requires LaTeX)..." -ForegroundColor Cyan
Push-Location $outputDir
try {
$pdfFileName = "$projectStem-Episode$episodeNumFormatted.pdf"
$pdfMarkdownFileName = "$projectStem-Episode$episodeNumFormatted-pdf.md"
$pdfArgs = @(
$pdfMarkdownFileName,
"-o", $pdfFileName,
"--pdf-engine=xelatex",
"-V", "geometry:paperwidth=6in",
"-V", "geometry:paperheight=9in",
"-V", "geometry:margin=0.75in",
"-V", "fontsize=11pt",
"-V", "linestretch=1.15"
)
& pandoc @pdfArgs 2>&1 | Out-Null
if ($LASTEXITCODE -eq 0) {
Write-Host "Created: output\$pdfFileName" -ForegroundColor Green
} else {
Write-Warning "Pandoc PDF generation failed (exit code $LASTEXITCODE). This usually means LaTeX is missing."
}
}
finally {
Pop-Location
}
}
Write-Host ("=" * 60) -ForegroundColor Cyan
Write-Host "COMPILATION COMPLETE" -ForegroundColor Cyan
Write-Host ("=" * 60) -ForegroundColor Cyan
Write-Host "$projectTitle - Episode $episodeNumFormatted" -ForegroundColor White
Write-Host "Scenes: $($sceneFiles.Count)" -ForegroundColor White
Write-Host "Words: $wordCount" -ForegroundColor White
Write-Host "Output files:" -ForegroundColor White
Write-Host " -> $epubMdFile" -ForegroundColor Green
Write-Host " -> $pdfMdFile" -ForegroundColor Green