exportManuscript.ps1
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 |
function Export-Manuscript { param( $markdownFile = "$pwd\README.md", $outputPath = $pwd, [ValidateSet('txt', 'md')] $chapterExtension = "txt" ) $manuscriptPath = "$outputPath\manuscript" Remove-Item -Recurse -Force $manuscriptPath -ErrorAction Ignore $null = mkdir $manuscriptPath $chapters = [ordered]@{} $chapterIndex = 0 if (!(Test-IsUri $markdownFile)) { $markdownContent = Get-Content $markdownFile } else { [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 $markdownContent = Invoke-RestMethod $markdownFile $markdownContent = $markdownContent -split "`n" } # switch -File ($markdownFile) { switch ($markdownContent) { "<!-- CHAPTER END -->" { $inChapter = $false $chapterIndex += 1 } {$inChapter} { $currentChapter = "chapter{0:0#}" -f $chapterIndex if (!$chapters.$currentChapter) { $chapters.$currentChapter = @() } $chapters.$currentChapter += $_ } "<!-- CHAPTER START -->" {$inChapter = $true} } foreach ($chapter in $chapters.Keys) { #$chapter $chapterName = "$($chapter).$($chapterExtension)" $chapterFile = "$($manuscriptPath)\$($chapterName)" $chapters.$chapter | Set-Content -Encoding Ascii $chapterFile $chapterName >> "$manuscriptPath\Book.txt" } } |