Functions/Data/ConvertTo-HTML5.ps1
function ConvertTo-HTML5 { [CmdletBinding()] Param ( # Array to feed into table [Parameter(Mandatory=$true,ValueFromPipeline=$true)] [psobject] $Array, # ID to apply to table [Parameter(Mandatory=$false)] [string] $TableID, # Table Classes to Inject [Parameter(Mandatory=$false)] [ValidateSet("rounded","neutral","alert","warn","good","bordered")] [string[]] $TableClasses = ("rounded","neutral"), # Table Data to Inject [Parameter(Mandatory=$false)] [string[]] $TableAttributes = 'data-page-length="100"', # CSS Class Logic to apply to cells [Parameter(Mandatory=$false)] [psobject[]] $CellLogic ) Begin { $InnerArray = @() } Process { # Enumerate Table Attributes $TableAttributes = $TableAttributes -join " " # Get Column Members $Properties = $array[0].psobject.properties.name $ClassStr = $TableClasses -join ' ' # Construct Static Table Elements $TableStart = "<table id=`"$TableID`" class=`"$ClassStr`" $TableAttributes>" $HeadStart = "<thead><tr>" $Head = $(foreach ($Property in $Properties){"<th>$Property</th>"}) -join '' $HeadEnd = "</tr></thead>" $BodyStart = "<tbody>" $BodyEnd = "</tbody>" $TableEnd = "</table>" # Build Inner Array from Pipeline Array $InnerArray += $Array } End { # Construct Table Body $Body = foreach ($line in $InnerArray) { $CellStr = foreach ($Property in $Properties) { $CellClasses = [System.Collections.ArrayList]@() $CellStyles = [System.Collections.ArrayList]@() $CellVal = $line.$property $CellValFinal = $Cellval # Test Supplies Logic and Append Classes/Styles if ($CellLogic.fieldname -eq $Property) { $Logics = $CellLogic | where fieldname -eq $Property foreach ($logic in $logics) { $Test = Test-Logic -Value $Cellval -ReferenceValue $logic.value -Type $logic.Type if ($test) { if ($Logic.Classes){foreach ($LogicClass in $logic.Classes){$ADDClass = $CellClasses.add($LogicClass)}} if ($Logic.Style){foreach ($LogicStyle in $logic.Style){$ADDStyle = $CellStyles.Add($LogicStyle)}} } if ($Logic.stripval){$cellValfinal = ""} } } $Classes=if($CellClasses){" class=`"$($CellClasses -join " ")`""}else{""} $Styles=if($CellStyles){" style=`"$($CellStyles -join ";")`""}else{""} $TDPrefix = "<td"+$classes+$styles+">" $TDPrefix+$CellValFinal+"</td>" } -join '' "<tr>"+$Cellstr+"</tr>" } $BodyStr = ($Body -join '').trim() # Complete table and output $Table = @($TableStart,$HeadStart,$Head,$HeadEnd,$BodyStart,$Bodystr,$BodyEnd,$TableEnd) $Table } } |