Skip to content

Example on how to format CSS? #41

@PrzemyslawKlys

Description

@PrzemyslawKlys

I'm sorry if it's a dumb question, but I've been trying to format CSS (prettify) the same way I do for HTML and for some reason things fail for me. I know the code is in PowerShell (which is loading your NET library) but I don't have a way to build this in C#.

This is the output I'm getting. Is it like I'm missing small detail or a bug (unlikely)?

image

function Format-CSS {
    [CmdletBinding()]
    param(
        [string] $File,
        [string] $OutputFile
    )
    if ($File -and (Test-Path -LiteralPath $File)) {
        $FileContent = [IO.File]::ReadAllText($file)

        $CssParser = New-Object -TypeName AngleSharp.Css.Parser.CssParser
        $ParsedDocument = $CssParser.ParseStyleSheet($FileContent)
        $StringWriter = [System.IO.StringWriter]::new()
        $PrettyMarkupFormatter = New-Object -TypeName AngleSharp.Css.PrettyStyleFormatter
        $ParsedDocument.ToCss($StringWriter, $PrettyMarkupFormatter)

        $FormattedCSS = $StringWriter.ToString()

        if ($FormattedCSS) {
            [IO.File]::WriteAllText($OutputFile, $FormattedCSS)
        }
    }
}

HTML is 99% the same, and it works just fine.

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)
        #$ParsedDocument.StyleSheets

        $FormattedHTML = $StringWriter.ToString()

        if ($FormattedHTML) {
            [IO.File]::WriteAllText($OutputFile, $FormattedHTML)
        }
    }
}

What am I missing? Would you be able to provide simple C# based example how you would format CSS natively?

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions