PSParseHTML.psm1

function Format-CSS {
    [CmdletBinding()]
    param([string] $File,
        [string] $OutputFile,
        [string] $Content)
    if ($File) {
        if (Test-Path -LiteralPath $File) { $Content = [IO.File]::ReadAllText($File) } else {
            Write-Warning 'Format-JavaScript - File doesnt exists'
            return
        }
    } elseif ($Content) { } else {
        Write-Warning 'Format-JavaScript - No choice file or Content. Termninated.'
        return
    }
    $Output = Format-InternalCSS -Content $Content
    if ($OutputFile) { [IO.File]::WriteAllText($OutputFile, $Output) } else { $Output }
}
function Format-HTML {
    [CmdletBinding()]
    param([string] $File,
        [string] $OutputFile,
        [string] $Content)
    if ($File) {
        if (Test-Path -LiteralPath $File) { $Content = [IO.File]::ReadAllText($File) } else {
            Write-Warning 'Format-HTML - File doesnt exists'
            return
        }
    } elseif ($Content) { } else {
        Write-Warning 'Format-HTML - No choice file or Content. Termninated.'
        return
    }
    $Output = Format-InternalHTML -Content $Content
    if ($OutputFile) { [IO.File]::WriteAllText($OutputFile, $Output) } else { $Output }
}
function Format-InternalCSS {
    [CmdletBinding()]
    param([string] $Content)
    $CssParser = [AngleSharp.Css.Parser.CssParser]::new()
    $ParsedDocument = $CssParser.ParseStyleSheet($Content)
    $StringWriter = [System.IO.StringWriter]::new()
    $PrettyMarkupFormatter = [AngleSharp.Css.CssStyleFormatter]::new()
    $ParsedDocument.ToCss($StringWriter, $PrettyMarkupFormatter)
    $StringWriter.ToString()
}
function Format-InternalHTML {
    [CmdletBinding()]
    param([string] $Content)
    $HTMLParser = [AngleSharp.Html.Parser.HtmlParser]::new()
    $ParsedDocument = $HTMLParser.ParseDocument($Content)
    $StringWriter = [System.IO.StringWriter]::new()
    $PrettyMarkupFormatter = [AngleSharp.Html.PrettyMarkupFormatter]::new()
    $ParsedDocument.ToHtml($StringWriter, $PrettyMarkupFormatter)
    $StringWriter.ToString()
}
function Format-InternalJS {
    [CmdletBinding()]
    param([string] $Content,
        [int] $IndentSize = 4,
        [string] $IndentChar = ' ',
        [bool] $IndentWithTabs = $false,
        [bool] $PreserveNewlines = $true,
        [double] $MaxPreserveNewlines = 10.0,
        [bool] $JslintHappy = $false,
        [Jsbeautifier.BraceStyle] $BraceStyle = [Jsbeautifier.BraceStyle]::Collapse,
        [bool] $KeepArrayIndentation = $false,
        [bool] $KeepFunctionIndentation = $false,
        [bool] $EvalCode = $false,
        [int] $WrapLineLength = 0,
        [bool] $BreakChainedMethods = $false)
    $Jsbeautifier = [Jsbeautifier.Beautifier]::new()
    $Jsbeautifier.Opts.IndentSize = $IndentSize
    $Jsbeautifier.Opts.IndentChar = $IndentChar
    $Jsbeautifier.Opts.IndentWithTabs = $IndentWithTabs
    $Jsbeautifier.Opts.PreserveNewlines = $PreserveNewlines
    $Jsbeautifier.Opts.MaxPreserveNewlines = $MaxPreserveNewlines
    $Jsbeautifier.Opts.JslintHappy = $JslintHappy
    $Jsbeautifier.Opts.BraceStyle = $BraceStyle
    $Jsbeautifier.Opts.KeepArrayIndentation = $KeepArrayIndentation
    $Jsbeautifier.Opts.KeepFunctionIndentation = $KeepFunctionIndentation
    $Jsbeautifier.Opts.EvalCode = $EvalCode
    $Jsbeautifier.Opts.WrapLineLength = $WrapLineLength
    $Jsbeautifier.Opts.BreakChainedMethods = $BreakChainedMethods
    $FormattedJS = $Jsbeautifier.Beautify($Content)
    $FormattedJS
}
function Format-JavaScript {
    [alias('Format-JS')]
    [CmdletBinding()]
    param([string] $File,
        [string] $OutputFile,
        [alias('FileContent')][string] $Content,
        [int] $IndentSize = 4,
        [string] $IndentChar = ' ',
        [bool] $IndentWithTabs = $false,
        [bool] $PreserveNewlines = $true,
        [double] $MaxPreserveNewlines = 10.0,
        [bool] $JslintHappy = $false,
        [Jsbeautifier.BraceStyle] $BraceStyle = [Jsbeautifier.BraceStyle]::Collapse,
        [bool] $KeepArrayIndentation = $false,
        [bool] $KeepFunctionIndentation = $false,
        [bool] $EvalCode = $false,
        [int] $WrapLineLength = 0,
        [bool] $BreakChainedMethods = $false)
    if ($File) {
        if (Test-Path -LiteralPath $File) { $Content = [IO.File]::ReadAllText($File) } else {
            Write-Warning 'Format-JavaScript - File doesnt exists'
            return
        }
    } elseif ($Content) { } else {
        Write-Warning 'Format-JavaScript - No choice file or Content. Termninated.'
        return
    }
    $SplatJS = @{IndentSize = $IndentSize
        IndentChar = $IndentChar
        IndentWithTabs = $IndentWithTabs
        PreserveNewlines = $PreserveNewlines
        MaxPreserveNewlines = $MaxPreserveNewlines
        JslintHappy = $JslintHappy
        BraceStyle = $BraceStyle
        KeepArrayIndentation = $KeepArrayIndentation
        KeepFunctionIndentation = $KeepFunctionIndentation
        EvalCode = $EvalCode
        WrapLineLength = $WrapLineLength
        BreakChainedMethods = $BreakChainedMethods
    }
    $Output = Format-InternalJS -Content $Content @SplatJS
    if ($OutputFile) { [IO.File]::WriteAllText($OutputFile, $Output) } else { $Output }
}
function Optimize-CSS {
    [CmdletBinding()]
    param([string] $File,
        [string] $OutputFile,
        [string] $Content,
        [string][ValidateSet('AngleSharp', 'YuiCompressorCssCompressor')] $Engine = 'AngleSharp')
    if ($File) {
        if (Test-Path -LiteralPath $File) { $Content = [IO.File]::ReadAllText($File) } else {
            Write-Warning 'Format-HTML - File doesnt exists'
            return
        }
    } elseif ($Content) { } else {
        Write-Warning 'Format-HTML - No choice file or Content. Termninated.'
        return
    }
    if ($Engine -eq 'AngleSharp') { $Output = Optimize-InternalCSS -Content $Content } else { $Output = Optimize-InternalYahoo -Content $Content }
    if ($OutputFile) { [IO.File]::WriteAllText($OutputFile, $Output) } else { $Output }
}
function Optimize-HTML {
    [CmdletBinding()]
    param([string] $File,
        [string] $OutputFile,
        [string] $Content,
        [bool] $ShouldKeepAttributeQuotes = $true,
        [bool] $ShouldKeepComments = $true,
        [bool] $ShouldKeepEmptyAttributes = $true,
        [bool] $ShouldKeepImpliedEndTag = $true,
        [bool] $ShouldKeepStandardElements = $true)
    if ($File) {
        if (Test-Path -LiteralPath $File) { $Content = [IO.File]::ReadAllText($File) } else {
            Write-Warning 'Optimize-JavaScript - File doesnt exists'
            return
        }
    } elseif ($Content) { } else {
        Write-Warning 'Optimize-JavaScript - No choice file or Content. Termninated.'
        return
    }
    $Output = Optimize-InternalHTML -Content $Content -ShouldKeepAttributeQuotes $ShouldKeepAttributeQuotes -ShouldKeepComments $ShouldKeepComments -ShouldKeepEmptyAttributes $ShouldKeepEmptyAttributes -ShouldKeepImpliedEndTag $ShouldKeepImpliedEndTag -ShouldKeepStandardElements $ShouldKeepStandardElements
    if ($OutputFile) { [IO.File]::WriteAllText($OutputFile, $Output) } else { $Output }
}
function Optimize-InternalCSS {
    [CmdletBinding()]
    param([string] $Content)
    $CSSParser = [AngleSharp.Css.Parser.CssParser]::new()
    $ParsedDocument = $CSSParser.ParseStyleSheet($Content)
    $StringWriter = [System.IO.StringWriter]::new()
    $PrettyMarkupFormatter = [AngleSharp.Css.MinifyStyleFormatter]::new()
    $ParsedDocument.ToCss($StringWriter, $PrettyMarkupFormatter)
    $StringWriter.ToString()
}
function Optimize-InternalHTML {
    [CmdletBinding()]
    param([string] $Content,
        [bool] $ShouldKeepAttributeQuotes,
        [bool] $ShouldKeepComments,
        [bool] $ShouldKeepEmptyAttributes,
        [bool] $ShouldKeepImpliedEndTag,
        [bool] $ShouldKeepStandardElements)
    $StringWriter = [System.IO.StringWriter]::new()
    $MinifyMarkupFormatter = [AngleSharp.Html.MinifyMarkupFormatter]::new()
    $MinifyMarkupFormatter.ShouldKeepAttributeQuotes = $ShouldKeepAttributeQuotes
    $MinifyMarkupFormatter.ShouldKeepComments = $ShouldKeepComments
    $MinifyMarkupFormatter.ShouldKeepEmptyAttributes = $ShouldKeepEmptyAttributes
    $MinifyMarkupFormatter.ShouldKeepImpliedEndTag = $ShouldKeepImpliedEndTag
    $MinifyMarkupFormatter.ShouldKeepStandardElements = $ShouldKeepStandardElements
    $HTMLParser = [AngleSharp.Html.Parser.HtmlParser]::new()
    $ParsedDocument = $HTMLParser.ParseDocument($Content)
    $ParsedDocument.ToHtml($StringWriter, $MinifyMarkupFormatter)
    $StringWriter.ToString()
}
function Optimize-InternalYahoo {
    [CmdletBinding()]
    param([string] $Content)
    $cssCompressor = [Yahoo.Yui.Compressor.CssCompressor]::new()
    $CompressedContent = $cssCompressor.Compress($Content)
    $CompressedContent
}
function Optimize-JavaScript {
    [CmdletBinding()]
    param([string] $File,
        [string] $OutputFile,
        [string] $Content)
    if ($File) {
        if (Test-Path -LiteralPath $File) { $Content = [IO.File]::ReadAllText($File) } else {
            Write-Warning 'Optimize-JavaScript - File doesnt exists'
            return
        }
    } elseif ($Content) { } else {
        Write-Warning 'Optimize-JavaScript - No choice file or Content. Termninated.'
        return
    }
    $Output = Optimize-InternalYahoo -Content $Content
    if ($OutputFile) { [IO.File]::WriteAllText($OutputFile, $Output) } else { $Output }
}
if ($PSEdition -eq 'Core') {
    Add-Type -Path $PSScriptRoot\Lib\Core\AngleSharp.Css.dll
    Add-Type -Path $PSScriptRoot\Lib\Core\AngleSharp.dll
    Add-Type -Path $PSScriptRoot\Lib\Core\AngleSharp.Js.dll
    Add-Type -Path $PSScriptRoot\Lib\Core\EcmaScript.NET.dll
    Add-Type -Path $PSScriptRoot\Lib\Core\jint.dll
    Add-Type -Path $PSScriptRoot\Lib\Core\Jsbeautifier.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\AngleSharp.Js.dll
    Add-Type -Path $PSScriptRoot\Lib\Default\EcmaScript.NET.dll
    Add-Type -Path $PSScriptRoot\Lib\Default\jint.dll
    Add-Type -Path $PSScriptRoot\Lib\Default\Jsbeautifier.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-CSS', 'Format-HTML', 'Format-JavaScript', 'Optimize-CSS', 'Optimize-HTML', 'Optimize-JavaScript') -Alias @('Format-JS')