Create a gist now

Instantly share code, notes, and snippets.

@aigarsdz /create.ps1
Last active Dec 20, 2015

A simple PowerShell script to automate post file creation using Pretzel or Jekyll static site generators.
param (
# A user has to provide the post title as a -Post parameter in order
# for script to work.
[string]$post = $(throw "-post is required")
)
# Convert any text to a URL friendly string.
#
# $title - String to convert.
#
# Examples:
#
# parameterize("Šis ir gadījuma teksts!")
# #=> sis-ir-gadijuma-teksts
#
# Returns a String.
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 "-"
}
$current_datetime = get-date
$current_date = $current_datetime.ToString("yyyy-MM-dd")
$url_title = parameterize($post)
$filename = "{0}-{1}.md" -f $current_date, $url_title
# Jekyll and Pretzel stores posts in a _posts directory,
# therefore we have to make sure this directory exists.
if (-not (test-path _posts)) {
new-item -itemtype directory _posts
}
$path = "_posts/{0}" -f $filename
new-item -itemtype file $path
# Add the default YAML Front Matter at the top of the file.
"---" >> $path
"layout: post" >> $path
"title: " + $post >> $path
"---" >> $path
@csim
csim commented Apr 15, 2014

Thanks, this is helpful. I took this an enhanced it for my own purposes.

https://gist.github.com/csim/9481e179fc2f6b61d54e

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment