Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
## Function to generate a random word of a fixed or random length
function Get-RandomWord
{
[CmdLetBinding()]
param
(
# Include diacritical letters
[Parameter(Mandatory = $false)]
[switch] $IncludeDiacriticalLetters,
# Length of the random word (random length by default)
[Parameter(Mandatory = $false)]
[int] $Length = $(Get-Random -Minimum 1 -Maximum 16)
)
# Generate lowercase alphabets
$Letters = @(97..122 | ForEach-Object { [char]$_ })
# Add diacritical letters
if ($IncludeDiacriticalLetters)
{
# Add multiple times of lowercase letters to match with the count of diacritical letters
1..7 | ForEach-Object { $Letters += @(97..122 | ForEach-Object { [char]$_ }) }
# Add diacritical letters
$Letters += 192..383 | ForEach-Object { [char]$_ }
}
# Return a random word
return ( -join ($Letters | Get-Random -Count $Length)).ToLower()
}
## Function to generate a random sentence using the random words
function Get-RandomSentence
{
[CmdLetBinding()]
Param
(
# Include diacritical letters
[Parameter(Mandatory = $false)]
[switch] $IncludeDiacriticalLetters,
# Length of the random word (random length by default)
[Parameter(Mandatory = $false)]
[int] $WordLength = $(Get-Random -Minimum 1 -Maximum 16),
# Count of the random words (random count by default)
[Parameter(Mandatory = $false)]
[int] $WordCount = $(Get-Random -Minimum 2 -Maximum 50),
# Adds punctuations like capitalise the first letter and add period at the end, and add comma, semicolon or exclamatory.
[Parameter(Mandatory = $false)]
[switch] $AddPunctuation
)
# Genereate the random words with/out diacritical letters using `-IncludeDiacriticalLetters` parameter
$Words = switch ($IncludeDiacriticalLetters)
{
$true { 1..$WordCount | ForEach-Object { Get-RandomWord -Length $WordLength -IncludeDiacriticalLetters } }
$false { 1..$WordCount | ForEach-Object { Get-RandomWord -Length $WordLength } }
}
# Adding punctuation
if ($AddPunctuation)
{
# Capitalise the first letter
$Words[0] = (Get-Culture).TextInfo.ToTitleCase($Words[0])
# Add period at the end.
$Words[-1] += '.'
# Add comma, semicolon or exclamatory if the word count is greater than 20
if ($Words.Count -gt 20)
{ $Words[[int]($Words.Count / 2)] += (',', ';', '!' | Get-Random) }
}
# Join all the random words to generate a random sentence
$Sentence = ($Words -join ' ')
# Return a random sentence
return $Sentence
}
## Function to generate a random paragraph using the random words and sentences
function Get-RandomParagraph
{
[CmdLetBinding()]
Param
(
# Include diacritical letters
[Parameter(Mandatory = $false)]
[switch] $IncludeDiacriticalLetters,
# Count of the random sentences (random count by default)
[Parameter(Mandatory = $false)]
[int] $SentenceCount = $(Get-Random -Minimum 3 -Maximum 25),
# Adds punctuations like capitalise the first letter and add period at the end, and add comma, semicolon or exclamatory.
[Parameter(Mandatory = $false)]
[switch] $AddPunctuation
)
# Generate random paragraphs
$Sentences = 1..$SentenceCount | ForEach-Object {
$Params = @{
# Word Length and word count
WordLength = $(Get-Random -Minimum 1 -Maximum 16)
WordCount = $(Get-Random -Minimum 2 -Maximum 50)
}
# Includes diacritical letters with `-IncludeDiacriticalLetters` parameter
if ($IncludeDiacriticalLetters)
{ $Params.IncludeDiacriticalLetters = $true }
# Add punctuation with `-AddPunctuation` parameter
if ($AddPunctuation)
{ $Params.AddPunctuation = $true }
Get-RandomSentence @Params
}
# Return a random paragraph
return ($Sentences -join ' ')
}
# Function to generate the random document with random paragraph
function Get-RandomDocument
{
[CmdLetBinding()]
Param
(
# Include diacritical letters
[Parameter(Mandatory = $false)]
[switch] $IncludeDiacriticalLetters,
# Count of the random paragraphs (random count by default)
[Parameter(Mandatory = $false)]
[int] $ParagraphCount = $(Get-Random -Minimum 4 -Maximum 15),
# Adds punctuations like capitalise the first letter and add period at the end, and add comma, semicolon or exclamatory.
[Parameter(Mandatory = $false)]
[switch] $AddPunctuation
)
$Params = @{}
# Includes diacritical letters with `-IncludeDiacriticalLetters` parameter
if ($IncludeDiacriticalLetters)
{ $Params.IncludeDiacriticalLetters = $true }
# Add punctuation with `-AddPunctuation` parameter
if ($AddPunctuation)
{ $Params.AddPunctuation = $true }
# Generate random document
$Document += 1..$ParagraphCount | ForEach-Object {
if ($Params.Count -gt 0)
{ Get-RandomParagraph @Params }
else { Get-RandomParagraph }
}
# Return a random paragraph
return $Document
}
# Function to generate the random document with random paragraph and save them to disk
Function Get-RandomFile
{
[CmdLetBinding()]
Param
(
# Include diacritical letters
[Parameter(Mandatory = $false)]
[switch] $IncludeDiacriticalLetters,
# Adds punctuations like capitalise the first letter and add period at the end, and add comma, semicolon or exclamatory.
[Parameter(Mandatory = $false)]
[switch] $AddPunctuation,
# Files Count (10 by default)
[Parameter(Mandatory = $false)]
[int] $FileCount = 10,
# Save random documents to
[Parameter(Mandatory = $false)]
[ValidateScript( { (Test-Path -Path $_) -eq $true })]
[string] $FilePath = $env:TEMP
)
$Params = @{}
# Includes diacritical letters with `-IncludeDiacriticalLetters` parameter
if ($IncludeDiacriticalLetters)
{ $Params.IncludeDiacriticalLetters = $true }
# Add punctuation with `-AddPunctuation` parameter
if ($AddPunctuation)
{ $Params.AddPunctuation = $true }
# Generate the random files
1..$FileCount | ForEach-Object {
$Document = if ($Params.Count -gt 0)
{ Get-RandomDocument @Params }
else { Get-RandomDocument }
$FileName = Join-Path -Path $FilePath -ChildPath (Split-Path -Path ([System.IO.Path]::GetTempFileName()) -Leaf)
$Document | Out-File -FilePath $FileName
}
# Open the files path
Invoke-Item $FilePath
}
# Function to generate the random markdown document and save it to disk
function Get-RandomMarkdown
{
[CmdLetBinding()]
param
(
# Include diacritical letters
[Parameter(Mandatory = $false)]
[switch] $IncludeDiacriticalLetters,
# Adds punctuations like capitalise the first letter and add period at the end, and add comma, semicolon or exclamatory.
[Parameter(Mandatory = $false)]
[switch] $AddPunctuation,
# No of markdown elements in the markdown file (random number by default)
[Parameter(Mandatory = $false)]
[int] $NoOfElements = $(Get-Random -Minimum 4 -Maximum 15),
# Save random markdown file to
[Parameter(Mandatory = $false)]
[ValidateScript( { (Test-Path -Path $_) -eq $true })]
[string] $FilePath = $env:TEMP
)
# Paramaters splat based on the parameter values
$Params = @{
IncludeDiacriticalLetters = $($IncludeDiacriticalLetters -eq $true)
AddPunctuation = $($AddPunctuation -eq $true)
}
# Initializing the markdown content
$MDContent = @()
# Begin with h1 header
$MDContent += '{0} {1}' -f '#', $(Get-RandomSentence @Params)
# Looping through random times and selecting a random element each time and formating in the markdown systax
1..$NoOfElements | ForEach-Object {
$Element = @('h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'Quote', 'Highlight', 'Bold', 'Italic', 'BoldItalic', 'Table', 'li', 'checkbox') | Get-Random
$MDContent += switch ($Element)
{
# All headers
'h1' { '{0} {1}' -f '#', $(Get-RandomSentence @Params) }
'h2' { '{0} {1}' -f $('#' * 2), $(Get-RandomSentence @Params) }
'h3' { '{0} {1}' -f $('#' * 3), $(Get-RandomSentence @Params) }
'h4' { '{0} {1}' -f $('#' * 4), $(Get-RandomSentence @Params) }
'h5' { '{0} {1}' -f $('#' * 5), $(Get-RandomSentence @Params) }
'h6' { '{0} {1}' -f $('#' * 6), $(Get-RandomSentence @Params) }
# Quote
'Quote' { '{0}{1}' -f '>', $(Get-RandomSentence @Params) }
# Highlight
'Highlight'
{
$RandomWords = (Get-RandomSentence @Params) -split ' '
$Index = Get-Random -Minimum 1 -Maximum $($RandomWords.Count)
$RandomWords[$Index] = ('`', '`') -join "$($RandomWords[$Index])"
$RandomWords -join ' '
}
# Bold
'Bold'
{
$BoldTo = @('Word', 'Sentence', 'Paragraph') | Get-Random
switch ($BoldTo)
{
'Word'
{
$RandomWords = (Get-RandomSentence @Params) -split ' '
$Index = Get-Random -Minimum 1 -Maximum $($RandomWords.Count)
$RandomWords[$Index] = ('**', '**') -join "$($RandomWords[$Index])"
$RandomWords -join ' '
}
'Sentence'
{
'**{0}**' -f $(Get-RandomSentence @Params)
}
'Paragraph'
{
'**{0}**' -f $(Get-RandomParagraph @Params)
}
}
}
# Intalic
'Italic'
{
$ItalicTo = @('Word', 'Sentence', 'Paragraph') | Get-Random
switch ($ItalicTo)
{
'Word'
{
$RandomWords = (Get-RandomSentence @Params) -split ' '
$Index = Get-Random -Minimum 1 -Maximum $($RandomWords.Count)
$RandomWords[$Index] = ('*', '*') -join "$($RandomWords[$Index])"
$RandomWords -join ' '
}
'Sentence'
{
'*{0}*' -f $(Get-RandomSentence @Params)
}
'Paragraph'
{
'*{0}*' -f $(Get-RandomParagraph @Params)
}
}
}
# Bold and Italic
'BoldItalic'
{
$BoldItalicTo = @('Word', 'Sentence', 'Paragraph') | Get-Random
switch ($BoldItalicTo)
{
'Word'
{
$RandomWords = (Get-RandomSentence @Params) -split ' '
$Index = Get-Random -Minimum 1 -Maximum $($RandomWords.Count)
$RandomWords[$Index] = ('***', '***') -join "$($RandomWords[$Index])"
$RandomWords -join ' '
}
'Sentence'
{
'***{0}***' -f $(Get-RandomSentence @Params)
}
'Paragraph'
{
'***{0}***' -f $(Get-RandomParagraph @Params)
}
}
}
# Table
'Table'
{
$Rows = Get-Random -Minimum 2 -Maximum 5
$Cols = Get-Random -Minimum 2 -Maximum 5
$WordParams = @{IncludeDiacriticalLetters = $( $IncludeDiacriticalLetters -eq $true ) }
'|{0}|' -f $($(1..$Cols | ForEach-Object { '**{0}**' -f $(Get-RandomWord @WordParams) }) -join '|')
'|{0}|' -f $($(1..$Cols | ForEach-Object { '---' }) -join '|')
1..$Rows | ForEach-Object {
'|{0}|' -f $($(1..$Cols | ForEach-Object { Get-RandomWord @WordParams }) -join '|')
}
}
# List
'li'
{
1..$(Get-Random -Minimum 3 -Maximum 6) | ForEach-Object { '* {0}' -f $(Get-RandomSentence -WordCount $(Get-Random -Minimum 5 -Maximum 8) @Params) }
}
# Check box
'checkbox'
{
1..$(Get-Random -Minimum 3 -Maximum 6) | ForEach-Object { '[ ] {0}' -f $(Get-RandomSentence -WordCount $(Get-Random -Minimum 5 -Maximum 8) @Params) }
}
}
# Adding gap between element and content
$MDContent += [System.Environment]::NewLine
# Adding a random paragraph after an element
$MDContent += Get-RandomParagraph @Params
# Adding gap between content and next element
$MDContent += [System.Environment]::NewLine
}
# Create a random md file
$FileName = '{0}.md' -f $(Get-RandomWord)
$OutFile = Join-Path -Path $FilePath -ChildPath $FileName
$MDContent | Out-File -FilePath $OutFile
# Opens in browser in PS Core or shows in explrer in PS 5.1
if ($PSVersionTable.PSEdition -eq 'Core')
{ Show-Markdown -Path $OutFile -UseBrowser }
else { Start-Process -FilePath explorer -ArgumentList "/select,$OutFile" -Wait }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment