Last active
May 11, 2020 20:09
-
-
Save jaredh159/c80e70d059c8a781275a7584a5adc5ba to your computer and use it in GitHub Desktop.
Piety Promoted Chapter Splitter (a `fl` cli command)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import fs from 'fs'; | |
const edition = 'original'; | |
export default function handler(): void { | |
const path = `/Users/jared/fl/en/compilations/youthful-piety/${edition}/02-youthful-piety.adoc`; | |
const adoc = fs.readFileSync(path).toString(); | |
const parts = adoc | |
.replace(/\n\n([A-Z]{3,}( [A-Z]+\.?)? [A-Z]{3,})/g, '~~~$1') | |
.trim() | |
.split('~~~') | |
.slice(1); | |
const chapters: [string, string[], number][] = [['', [], 0]]; | |
let chapterIdx = 0; | |
const namePattern = /([A-Z]{3,}( [A-Z]+\.?)? [A-Z]{3,})/; | |
parts.forEach((part, idx) => { | |
const match = part.match(namePattern); | |
if (!match) { | |
console.log('Part has no name!', idx, part); | |
return; | |
} | |
const name = match[0] | |
.toLowerCase() | |
.split(' ') | |
.map(s => s.charAt(0).toUpperCase() + s.slice(1)) | |
.join(' '); | |
let chapter = chapters[chapterIdx]; | |
let [, , linesInChapter] = chapter; | |
if (linesInChapter > MAX_CHAPTER_LINES) { | |
chapterIdx++; | |
chapter = ['', [], 0]; | |
chapters[chapterIdx] = chapter; | |
} | |
chapter[0] = `${chapter[0]}\n${part}`; | |
chapter[1].push(name); | |
chapter[2] += part.split('\n').length; | |
}); | |
chapters.forEach(([mainText, names], idx) => { | |
const title = `== Chapter ${idx + 1}`; | |
const cs = ['[.chapter-synopsis]'].concat(names.map(name => `* ${name}`)).join('\n'); | |
let chapterText = `${title}\n\n${cs}\n\n${mainText}`; | |
const lines = chapterText.split('\n\n'); | |
if (lines[lines.length - 1].trim() === "[.asterism]\n'''") { | |
chapterText = lines.slice(0, lines.length - 1).join('\n\n'); | |
} | |
const fileNum = String(idx + 2).padStart(2, '0'); | |
const chNum = String(idx + 1).padStart(2, '0'); | |
const path = `/Users/jared/fl/en/compilations/youthful-piety/${edition}/${fileNum}-chapter-${chNum}.adoc`; | |
fs.writeFileSync(path, chapterText); | |
}); | |
} | |
const MAX_CHAPTER_LINES = 800; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment