Skip to content

Instantly share code, notes, and snippets.

@bpatra
Created August 24, 2014 13:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bpatra/79d8f54b50ea6a2c484c to your computer and use it in GitHub Desktop.
Save bpatra/79d8f54b50ea6a2c484c to your computer and use it in GitHub Desktop.
Powershell script for extracting YAMLS out of Jekyll files
#Script for extractingn YAML of .html files.
#The extracted content is put in a directory YAMLS <websiteroot>/ignored/YAMLS
if(-Not (Test-Path (Join-Path $PSScriptRoot -ChildPath "ignored"))){
throw "It is assumed that a directory ignored exists"
}
function ContainsYAML([string] $htmlFile){
$lines = Get-Content $htmlFile
$lineCount = $lines.Length - 1
$indices = (0 .. $lineCount)
$indices | Where-Object{
$lines[$_].StartsWith("---")
}
}
$yamlFolder = Join-Path $PSScriptRoot -ChildPath "ignored/YAMLS"
if(Test-Path $yamlFolder){
Write-Output "Wipe out content in" $yamlFolder
Remove-Item $yamlFolder -Recurse
}
New-Item $yamlFolder -ItemType "directory"
Get-ChildItem -Path $PSScriptRoot -Include "*.html" -R | Where-Object {
-Not ($_.FullName.Contains("ignored"))
} | Where-Object {
$indices = ContainsYAML $_.FullName
$indices.Length -eq 2
} | ForEach-Object{
Write-Host "Extracting YAML in " $_.FullName
$indices = ContainsYAML $_
$name = (Join-Path $yamlFolder -ChildPath ($_.FullName.Substring($PSScriptRoot.Length))).ToString()
$dirName = [System.IO.Path]::GetDirectoryName($name)
If(-Not (Test-Path($dirName))){
Write-Host "Create directory " $dirName
New-Item $dirName -ItemType "directory"
}
Write-Host "Create file " $name
New-Item -Path $name -ItemType "file"
$lines = Get-Content $_
Write-Host "Write " ($indices[1] - $indices[0] + 1) " lines"
for($i = $indices[0]; $i -le $indices[1];$i++){
Add-Content $name -Value $lines[$i]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment