Skip to content

Commit

Permalink
plantuml-stdlib#383 fill undefined property columns and complete head…
Browse files Browse the repository at this point in the history
…er columns
  • Loading branch information
kirchsth committed Feb 9, 2025
1 parent 14dc19c commit 2deace0
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 0 deletions.
87 changes: 87 additions & 0 deletions C4.puml
Original file line number Diff line number Diff line change
Expand Up @@ -1161,49 +1161,136 @@ $elementSkin
!global $propTableCaption = ""
!global $propColCaption = "="

!global $isFirstProp = 1
!global $firstPropCol = 1
!global $lastPropCol = 1

!function $fillMissing($col, $colNext)
!if ($col == "" && $colNext != "")
!return " "
!endif
!return $col
!endfunction

!function $updatePropColumns($colIdx)
!if ($isFirstProp == 1 && $colIdx > $firstPropCol)
!$firstPropCol = $colIdx
!endif
!if ($isFirstProp == 0 && $colIdx > $lastPropCol)
!$lastPropCol = $colIdx
!endif
!return ""
!endfunction

' add missing header columns, if a following row has more columns
' (fixed in PlantUML v1.2025.1beta9; only required in older versions)
!function $fixHeaderColumns()
' the number of displayed columns considers only the first row
' if another row has more columns the first has to be filled with missing columns
!if ($lastPropCol > $firstPropCol)
!$delta = $lastPropCol - $firstPropCol
!$delta = $delta * 2
!$fix = %substr(" | | | |", 0, $delta)

' basically the line break \n should be the split
' but \n is not encoded (anymore?) therefore split only via
' \ and remove the last obsolete \ (changed order with add
' \ at the beginning is not working).
' "\n" would split \ and n ==> n would be an unwanted line break
!$lines = %splitstr($propTable, "\")
' !$lines = %splitstr_regex($propTable, "(?=[\x000A])")
!$first = 1
!$newTab = ""
!foreach $item in $lines
!if ($first == 1)
!$item = $item + $fix
!$first = 0
!endif
!$newTab = $newTab + $item + "\"
!endfor

!$fixLen = %strlen($newTab) - 1
!$newTab = %substr($newTab, 0, $fixLen)

!$propTable = $newTab
!endif

!$isFirstProp = 1
!$firstPropCol = 1
!$lastPropCol = 1

!return ""
!endfunction

!unquoted function SetPropertyHeader($col1Name, $col2Name = "", $col3Name = "", $col4Name = "")
!$col3Name = $fillMissing($col3Name, $col4Name)
!$col2Name = $fillMissing($col2Name, $col3Name)
!$col1Name = $fillMissing($col1Name, $col2Name)

!$propColCaption = ""
!$propTableCaption = "|= " + $col1Name + " |"
!if ($col2Name != "")
!$propTableCaption = $propTableCaption + "= " + $col2Name + " |"
$updatePropColumns(2)
!endif
!if ($col3Name != "")
!$propTableCaption = $propTableCaption + "= " + $col3Name + " |"
$updatePropColumns(3)
!endif
!if ($col4Name != "")
!$propTableCaption = $propTableCaption + "= " + $col4Name + " |"
$updatePropColumns(4)
!endif

!$isFirstProp = 0
!return ""
!endfunction

!unquoted function WithoutPropertyHeader()
!$propTableCaption = ""
!$propColCaption = "="

!$isFirstProp = 1
!$firstPropCol = 1
!$lastPropCol = 1

!return ""
!endfunction

!unquoted function AddProperty($col1, $col2 = "", $col3 = "", $col4 = "")
!$col3 = $fillMissing($col3, $col4)
!$col2 = $fillMissing($col2, $col3)
!$col1 = $fillMissing($col1, $col2)

!if ($propTable == "")
!if ($propTableCaption != "")
!$propTable = $propTableCaption + "\n"
!endif
!else
!$propTable = $propTable + "\n"
!endif

!$propTable = $propTable + "| " + $col1 + " |"
!if ($col2 != "")
!$propTable = $propTable + $propColCaption + " " + $col2 + " |"
$updatePropColumns(2)
!endif
!if ($col3 != "")
!$propTable = $propTable + " " + $col3 + " |"
$updatePropColumns(3)
!endif
!if ($col4 != "")
!$propTable = $propTable + " " + $col4 + " |"
$updatePropColumns(4)
!endif

!$isFirstProp = 0
!return ""
!endfunction

!unquoted function $getProps($alignedNL = "\n")
$fixHeaderColumns()

!if ($propTable != "")
!$retTable = $alignedNL + $propTable
!$propTable = ""
Expand Down
19 changes: 19 additions & 0 deletions percy/TestPropertyMissingColumns.puml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
@startuml
' convert it with additional command line argument -DRELATIVE_INCLUDE="./.." to use locally
!if %variable_exists("RELATIVE_INCLUDE")
!include %get_variable_value("RELATIVE_INCLUDE")/C4_Deployment.puml
!else
!include https://raw.githubusercontent.com/kirchsth/C4-PlantUML/extended/C4_Deployment.puml
!endif

' missing columns 3 and 4 are added that all columns are displayed
SetPropertyHeader("", $col2Name="2")
AddProperty($col1="col1")
AddProperty("", $col2="col2")
AddProperty(" ", " ", $col3="col3")
' missing columns 2 and 3 are inserted with empty values
AddProperty("", $col4="col4")

Container(c, "Container")

@enduml

0 comments on commit 2deace0

Please sign in to comment.