PSParseHTML.psm1

function Format-HTML {
    [CmdletBinding()]
    param([string] $File,
        [string] $OutputFile)
    if ($File -and (Test-Path -LiteralPath $File)) {
        $FileContent = [IO.File]::ReadAllText($file)
        $HTMLParser = New-Object -TypeName AngleSharp.Html.Parser.HtmlParser
        $ParsedDocument = $HTMLParser.ParseDocument($FileContent)
        $StringWriter = [System.IO.StringWriter]::new()
        $PrettyMarkupFormatter = New-Object -TypeName AngleSharp.Html.PrettyMarkupFormatter
        $ParsedDocument.ToHtml($StringWriter, $PrettyMarkupFormatter)
        $FormattedHTML = $StringWriter.ToString()
        if ($FormattedHTML) { [IO.File]::WriteAllText($OutputFile, $FormattedHTML) }
    }
}
function Optimize-HTML {
    [CmdletBinding()]
    param([string] $File,
        [string] $OutputFile,
        [switch] $ShouldKeepAttributeQuotes,
        [switch] $ShouldKeepComments,
        [switch] $ShouldKeepEmptyAttributes,
        [switch] $ShouldKeepImpliedEndTag,
        [switch] $ShouldKeepStandardElements)
    if ($File -and (Test-Path -LiteralPath $File)) {
        $FileContent = [IO.File]::ReadAllText($file)
        $HTMLParser = New-Object -TypeName AngleSharp.Html.Parser.HtmlParser
        $ParsedDocument = $HTMLParser.ParseDocument($FileContent)
        $StringWriter = [System.IO.StringWriter]::new()
        $MinifyMarkupFormatter = New-Object -TypeName AngleSharp.Html.MinifyMarkupFormatter
        $ParsedDocument.ToHtml($StringWriter, $MinifyMarkupFormatter)
        $MinifyMarkupFormatter.ShouldKeepAttributeQuotes = $ShouldKeepAttributeQuotes
        $MinifyMarkupFormatter.ShouldKeepComments = $ShouldKeepComments
        $MinifyMarkupFormatter.ShouldKeepEmptyAttributes = $ShouldKeepEmptyAttributes
        $MinifyMarkupFormatter.ShouldKeepImpliedEndTag = $ShouldKeepImpliedEndTag
        $MinifyMarkupFormatter.ShouldKeepStandardElements = $ShouldKeepStandardElements
        $FormattedHTML = $StringWriter.ToString()
        if ($FormattedHTML) { [IO.File]::WriteAllText($OutputFile, $FormattedHTML) }
    }
}
function Optimize-ResourceFile {
    [alias('New-MinifiFile', 'New-MinifyFile')]
    [CmdletBinding()]
    param([string] $File,
        [string] $OutputFile)
    if ($File -and (Test-Path -LiteralPath $File)) {
        $FileContent = [IO.File]::ReadAllText($file)
        try {
            $cssCompressor = New-Object -TypeName Yahoo.Yui.Compressor.CssCompressor
            $CompressedContent = $cssCompressor.Compress($FileContent)
        } catch { }
        if ($CompressedContent) { [IO.File]::WriteAllText($OutputFile, $CompressedContent) }
    }
}
function Optimize-ResourceFolder {
    [alias('New-MinifiFolder', 'New-MinifyFolder')]
    [CmdletBinding()]
    Param([Parameter(Mandatory = $true)][string]$Folder,
        [string[]] $Include = @('*.js', '.css'))
    $Files = Get-ChildItem -LiteralPath $Folder -Recurse -Force -Include $Include
    foreach ($File in $Files) { New-MinifyFile -File $File.FullName -OutputFile $File.FullName }
}
if ($PSEdition -eq 'Core') {
    Add-Type -Path $PSScriptRoot\Lib\Core\AngleSharp.dll
    Add-Type -Path $PSScriptRoot\Lib\Core\EcmaScript.NET.dll
    Add-Type -Path $PSScriptRoot\Lib\Core\Yahoo.Yui.Compressor.dll
} else {
    Add-Type -Path $PSScriptRoot\Lib\Default\AngleSharp.Css.dll
    Add-Type -Path $PSScriptRoot\Lib\Default\AngleSharp.dll
    Add-Type -Path $PSScriptRoot\Lib\Default\EcmaScript.NET.dll
    Add-Type -Path $PSScriptRoot\Lib\Default\System.Text.Encoding.CodePages.dll
    Add-Type -Path $PSScriptRoot\Lib\Default\System.XAngleSharp.dll
    Add-Type -Path $PSScriptRoot\Lib\Default\Yahoo.Yui.Compressor.dll
}
Export-ModuleMember -Function @('Format-HTML', 'Optimize-HTML', 'Optimize-ResourceFile', 'Optimize-ResourceFolder') -Alias @('New-MinifiFile', 'New-MinifiFolder', 'New-MinifyFile', 'New-MinifyFolder')