Skip to content

Instantly share code, notes, and snippets.

@csim

csim/jekyll.ps1 Secret

Created April 15, 2014 19:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save csim/9481e179fc2f6b61d54e to your computer and use it in GitHub Desktop.
Save csim/9481e179fc2f6b61d54e to your computer and use it in GitHub Desktop.
Jekyll powershell scripts
function Publish-Post {
param (
# A user has to provide the post title as a -Post parameter in order
# for script to work.
[string]$Title = "*"
)
$path = "c:\source\csim.github.io\_drafts\"
$posts = (Get-ChildItem -Path $path $Title | ? { $_.Name -ne "_draft.md" })
$posts
}
function Create-Post {
param (
# A user has to provide the post title as a -Post parameter in order
# for script to work.
[string]$Title = $(throw "-title is required")
, [string]$SitePath = "c:\source\csim.github.io\" # Change this to your defualt directory
)
# 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($Title)
$filename = "{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 = "c:\source\csim.github.io\_drafts\{0}" -f $filename
$template = "{0}_drafts\_draft.md" -f $SitePath
$content = (Get-Content -Path $template)
$content = $content -replace "{{ title }}", $Title
# Copy-Item -Path $template -Destination $Path
# new-item -itemtype file $path
# Add the default YAML Front Matter at the top of the file.
# $content = @()
# $content += "---"
# $content += "title: " + $Title
# $content += "layout: post"
# $content += "comments: false"
# #$content += "published: false"
# $content += "---"
# $content += ""
# $content = $content -join "`n"
Add-Content -Path $path -Value $content -Encoding ASCII
Edit $path # change this to your favorite editor
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment