From d300bd034bc4882e445fbfeb1ba83cae6ea8ffa9 Mon Sep 17 00:00:00 2001 From: Markus Fleschutz Date: Tue, 11 Apr 2023 22:28:51 +0200 Subject: [PATCH] Add convert-md2html.ps1 --- Scripts/convert-docx2md.ps1 | 40 ++++++++++++++++++++++++++++++++++++- Scripts/convert-md2docx.ps1 | 4 +++- Scripts/convert-md2html.ps1 | 38 +++++++++++++++++++++++++++++++++++ Scripts/convert-ps2bat.ps1 | 4 ++-- 4 files changed, 82 insertions(+), 4 deletions(-) create mode 100644 Scripts/convert-md2html.ps1 diff --git a/Scripts/convert-docx2md.ps1 b/Scripts/convert-docx2md.ps1 index 33483322..7442f330 100644 --- a/Scripts/convert-docx2md.ps1 +++ b/Scripts/convert-docx2md.ps1 @@ -1 +1,39 @@ -gci -r -i *.docx |foreach{$md=$_.directoryname+"\"+$_.basename+".md";pandoc -f docx -s $_.name -o $md} \ No newline at end of file +<# +.SYNOPSIS + Converts .DOCX file(s) into Markdown +.DESCRIPTION + This PowerShell script converts .DOCX file(s) into Markdown. +.PARAMETER FilePattern + Specifies the file pattern to the .DOCX file(s) +.EXAMPLE + PS> ./convert-docx2md *.docx +.LINK + https://github.com/fleschutz/PowerShell +.NOTES + Author: Markus Fleschutz | License: CC0 +#> + +param([string]$FilePattern = "") + +try { + $StopWatch = [system.diagnostics.stopwatch]::startNew() + + if ($FilePattern -eq "" ) { $FilePattern = Read-Host "Enter the file pattern to the .DOCX file(s)" } + + Write-Host "⏳ Searching for pandoc..." + $null = (pandoc --version) + if ($lastExitCode -ne "0") { throw "Can't execute 'pandoc' - make sure it's installed and available" } + + Write-Host "⏳ Converting..." + gci -r -i $FilePattern | foreach { + $TargetPath = $_.directoryname + "\" + $_.basename + ".md" + pandoc -f docx -s $_.name -o $TargetPath + } + + [int]$Elapsed = $StopWatch.Elapsed.TotalSeconds + "✔️ converted in $Elapsed sec" + exit 0 # success +} catch { + "⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" + exit 1 +} diff --git a/Scripts/convert-md2docx.ps1 b/Scripts/convert-md2docx.ps1 index ea7c4e42..021c26b1 100644 --- a/Scripts/convert-md2docx.ps1 +++ b/Scripts/convert-md2docx.ps1 @@ -1 +1,3 @@ -gci -r -i *.md |foreach{$docx=$_.directoryname+"\"+$_.basename+".docx";pandoc -f markdown -s --citeproc $_.name -o $docx} \ No newline at end of file + + +gci -r -i *.md |foreach{$docx=$_.directoryname+"\"+$_.basename+".docx";pandoc -f markdown -s --citeproc $_.name -o $docx} diff --git a/Scripts/convert-md2html.ps1 b/Scripts/convert-md2html.ps1 new file mode 100644 index 00000000..cf38245d --- /dev/null +++ b/Scripts/convert-md2html.ps1 @@ -0,0 +1,38 @@ +<# +.SYNOPSIS + Converts Markdown file(s) into HTML +.DESCRIPTION + This PowerShell script converts Markdown file(s) into HTML. +.PARAMETER FilePattern + Specifies the file pattern to the Markdown file(s) +.EXAMPLE + PS> ./convert-md2html *.md +.LINK + https://github.com/fleschutz/PowerShell +.NOTES + Author: Markus Fleschutz | License: CC0 +#> + +param([string]$FilePattern = "") + +try { + $StopWatch = [system.diagnostics.stopwatch]::startNew() + + if ($FilePattern -eq "" ) { $FilePattern = Read-Host "Enter the file pattern to the Markdown file(s)" } + + Write-Host "⏳ Searching for pandoc..." + $null = (pandoc --version) + if ($lastExitCode -ne "0") { throw "Can't execute 'pandoc' - make sure it's installed and available" } + + Write-Host "⏳ Converting..." + gci -r -i $FilePattern | foreach { + $TargetPath = $_.directoryname + "\" + $_.basename + ".html" + pandoc -f md -s $_.name -o $TargetPath + } + [int]$Elapsed = $StopWatch.Elapsed.TotalSeconds + "✔️ converted in $Elapsed sec" + exit 0 # success +} catch { + "⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" + exit 1 +} diff --git a/Scripts/convert-ps2bat.ps1 b/Scripts/convert-ps2bat.ps1 index 8273f43f..871940d5 100755 --- a/Scripts/convert-ps2bat.ps1 +++ b/Scripts/convert-ps2bat.ps1 @@ -34,9 +34,9 @@ function Convert-PowerShellToBatch } try { - if ($Filepattern -eq "") { $Filepattern = read-host "Enter path to the PowerShell script(s)" } + if ($Filepattern -eq "") { $Filepattern = Read-Host "Enter path to the PowerShell script(s)" } - $Files = get-childItem -path "$Filepattern" + $Files = Get-ChildItem -path "$Filepattern" foreach ($File in $Files) { Convert-PowerShellToBatch "$File" }