Last active
August 29, 2015 14:19
-
-
Save ChaseFlorell/f0bd9d0b342e2540a510 to your computer and use it in GitHub Desktop.
This file contains 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
function Add-Post { | |
param ( | |
[parameter(Mandatory=$true, Position=0)][string]$post, | |
[parameter(Mandatory=$false, Position=1)][switch]$draft | |
) | |
if(!(Assert-IsGitRepo)) { | |
throw 'you are currently not in a git repository.' | |
} | |
push-location "$(git rev-parse --show-toplevel)" | |
$current_datetime = get-date | |
$current_date = $current_datetime.ToString("yyyy-MM-dd") | |
$current_time = $current_datetime.ToString("HH:mm:ss") | |
$url_title = parameterize($post) | |
$filename = "{0}-{1}.md" -f $current_date, $url_title | |
# the target dir is either _drafts or _posts based on the $draft flag set by the user. | |
$dir = if($draft.IsPresent){ "_drafts" } else { "_posts" } | |
# Jekyll and Pretzel stores posts in a _posts directory, | |
# therefore we have to make sure this directory exists. | |
if (-not (test-path $dir)) { | |
new-item -itemtype directory $dir | |
} | |
$path = "$dir/{0}" -f $filename | |
# Add the default YAML Front Matter at the top of the file. | |
@" | |
--- | |
layout: post | |
title: $post | |
date: $current_date $current_time | |
categories: [] | |
tags: [] | |
--- | |
"@ > $path | |
# Force UTF-8 Encoding | |
$MyFile = Get-Content $path | |
$Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding($False) | |
[System.IO.File]::WriteAllLines($path, $MyFile, $Utf8NoBomEncoding) | |
pop-location | |
} | |
Set-Alias post Add-Post | |
function parameterize($title) { | |
$parameterized_title = $title.ToLower() | |
$words = $parameterized_title.Split() | |
$normalized_words = @() | |
foreach ($word in $words) { | |
# Convert a Unicode string into its ASCII counterpart, e.g. māja -> maja. | |
$normalized_word = $word.Normalize([Text.NormalizationForm]::FormD) | |
# Normalize method returns ASCII letters together with a symbol that "matches" | |
# the diacritical mark used in the Unicode. These symbols have to be removed | |
# in order to get a valid string. | |
$normalized_words += $normalized_word -replace "[^a-z0-9]", [String]::Empty | |
} | |
$normalized_words -join "-" | |
} | |
function Assert-IsGitRepo { | |
try { | |
git status | |
return $true | |
} catch { | |
return $false | |
} | |
} |
This file contains 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
if (Get-Module personal) { return } | |
Push-Location $psScriptRoot | |
. .\jekyll.ps1 | |
Pop-Location | |
Export-ModuleMember ` | |
-Alias @( | |
'*') ` | |
-Function @('Add-Post') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment