-
-
Save csim/9481e179fc2f6b61d54e to your computer and use it in GitHub Desktop.
Jekyll powershell scripts
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 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