/
ConvertMarkdown2Html.psm1
137 lines (120 loc) · 4.87 KB
/
ConvertMarkdown2Html.psm1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
Set-StrictMode -Version latest
$ErrorActionPreference = "Stop"
[String]$ScriptDir = Split-Path $script:MyInvocation.MyCommand.Path
Import-Module "$ScriptDir\Helper.psm1"
Import-Module "$ScriptDir\Configuration.psm1"
# -------------------------------------------------------------------------------------------------------------------------------------
function ConvertMarkdown2Html
{
Param
(
[Parameter()][string] $InputFilePath,
[Parameter()][string] $OutputFilePath,
[Parameter()][string] $StyleSheetPath = "$ScriptDir\GithubStyle.css",
[Parameter()][string] $DocumentTitle = "",
[Parameter()][switch] $GenerateTableOfContents,
[Parameter()][switch] $SkipIfNotInstalled,
[Parameter()][switch] $PauseOnError
)
Try
{
Write-Host -ForegroundColor "Green" "Converting Markdown to HTML..."
# check whether the conversion tool is installed
if (!(Test-Path "$PandocPath" -PathType Leaf))
{
if ($SkipIfNotInstalled)
{
Write-Host -ForegroundColor "Yellow" "The converting tool 'Pandoc' is not installed. Skipping conversion..."
return
}
else
{
Write-Host -ForegroundColor "Red" "The converting tool 'Pandoc' is not installed!"
throw "The converting tool 'Pandoc' is not installed!"
}
}
$InputFilePath = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath("$InputFilePath")
$OutputFilePath = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath("$OutputFilePath")
$StyleSheetPath = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath("$StyleSheetPath")
# check whether the given parameters are valid
# InputFilePath
if ([string]::IsNullOrWhiteSpace($InputFilePath))
{
throw "The parameter 'InputFilePath' contains null, only whitespaces or is empty! Define a path to the markdown file to convert."
}
if (!(Test-Path "$InputFilePath" -PathType Leaf))
{
throw "The file for parameter 'InputFilePath' does not exists!"
}
if ([IO.Path]::GetExtension($InputFilePath) -ne ".md")
{
throw "The file given for parameter 'InputFilePath' is not a Markdown file!"
}
Write-Host "Conversion: Input file '$InputFilePath' is valid."
# OutputFilePath
if ([string]::IsNullOrWhiteSpace($OutputFilePath))
{
throw "The parameter 'OutputFilePath' contains null, only whitespaces or is empty! Define a path where to store the HTML file."
}
if ([IO.Path]::GetExtension($OutputFilePath) -ne ".html")
{
throw "The file given for parameter 'OutputFilePath' is not a HTML file!"
}
# create folder, if it does not exist, yet
$OutputFileDir = Split-Path $OutputFilePath
if (!(Test-Path $OutputFileDir -PathType Container)) {
New-Item -Path $OutputFileDir -ItemType Directory -Force | Out-Null
}
Write-Host "Conversion: Output file '$OutputFilePath' is valid."
$IsStyleSheetIncluded = $true
# StyleSheetPath
if ([string]::IsNullOrWhiteSpace($StyleSheetPath))
{
$IsStyleSheetIncluded = $false
Write-Host -ForegroundColor "Yellow" "Conversion: Style sheet is not included."
}
if ($IsStyleSheetIncluded)
{
if (!(Test-Path "$StyleSheetPath" -PathType Leaf))
{
throw "The file for parameter 'StyleSheetPath' does not exists!"
}
if ([IO.Path]::GetExtension($StyleSheetPath) -ne ".css")
{
throw "The file given for parameter 'StyleSheetPath' is not a CSS file!"
}
Write-Host "Conversion: Style sheet '$StyleSheetPath' is valid."
}
# create command for pandoc.exe with given parameters
$cmd = @($PandocPath, "-f", "gfm", "-t", "html5", "--self-contained")
if ($GenerateTableOfContents)
{
$cmd += "--toc"
Write-Host "Conversion: Generate table of contents."
}
if (![string]::IsNullOrWhiteSpace($DocumentTitle))
{
$cmd += "--metadata"
$cmd += "title=""$DocumentTitle"""
Write-Host "Conversion: Add title '$DocumentTitle' to document metadata."
}
if ($IsStyleSheetIncluded)
{
$cmd += "--css"
$cmd += "$StyleSheetPath"
}
$cmd += "$InputFilePath"
$cmd += "-o"
$cmd += "$OutputFilePath"
EnvRunExec ($cmd)
}
Catch [Exception]
{
# tell the caller it has all gone wrong
echo $_.Exception|format-list -force
if ($PauseOnError) {
Read-Host "Press ANY key..."
}
exit(-1)
}
}