Skip to content

Commit 2deace0

Browse files
committed
plantuml-stdlib#383 fill undefined property columns and complete header columns
1 parent 14dc19c commit 2deace0

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed

C4.puml

+87
Original file line numberDiff line numberDiff line change
@@ -1161,49 +1161,136 @@ $elementSkin
11611161
!global $propTableCaption = ""
11621162
!global $propColCaption = "="
11631163

1164+
!global $isFirstProp = 1
1165+
!global $firstPropCol = 1
1166+
!global $lastPropCol = 1
1167+
1168+
!function $fillMissing($col, $colNext)
1169+
!if ($col == "" && $colNext != "")
1170+
!return " "
1171+
!endif
1172+
!return $col
1173+
!endfunction
1174+
1175+
!function $updatePropColumns($colIdx)
1176+
!if ($isFirstProp == 1 && $colIdx > $firstPropCol)
1177+
!$firstPropCol = $colIdx
1178+
!endif
1179+
!if ($isFirstProp == 0 && $colIdx > $lastPropCol)
1180+
!$lastPropCol = $colIdx
1181+
!endif
1182+
!return ""
1183+
!endfunction
1184+
1185+
' add missing header columns, if a following row has more columns
1186+
' (fixed in PlantUML v1.2025.1beta9; only required in older versions)
1187+
!function $fixHeaderColumns()
1188+
' the number of displayed columns considers only the first row
1189+
' if another row has more columns the first has to be filled with missing columns
1190+
!if ($lastPropCol > $firstPropCol)
1191+
!$delta = $lastPropCol - $firstPropCol
1192+
!$delta = $delta * 2
1193+
!$fix = %substr(" | | | |", 0, $delta)
1194+
1195+
' basically the line break \n should be the split
1196+
' but \n is not encoded (anymore?) therefore split only via
1197+
' \ and remove the last obsolete \ (changed order with add
1198+
' \ at the beginning is not working).
1199+
' "\n" would split \ and n ==> n would be an unwanted line break
1200+
!$lines = %splitstr($propTable, "\")
1201+
' !$lines = %splitstr_regex($propTable, "(?=[\x000A])")
1202+
!$first = 1
1203+
!$newTab = ""
1204+
!foreach $item in $lines
1205+
!if ($first == 1)
1206+
!$item = $item + $fix
1207+
!$first = 0
1208+
!endif
1209+
!$newTab = $newTab + $item + "\"
1210+
!endfor
1211+
1212+
!$fixLen = %strlen($newTab) - 1
1213+
!$newTab = %substr($newTab, 0, $fixLen)
1214+
1215+
!$propTable = $newTab
1216+
!endif
1217+
1218+
!$isFirstProp = 1
1219+
!$firstPropCol = 1
1220+
!$lastPropCol = 1
1221+
1222+
!return ""
1223+
!endfunction
1224+
11641225
!unquoted function SetPropertyHeader($col1Name, $col2Name = "", $col3Name = "", $col4Name = "")
1226+
!$col3Name = $fillMissing($col3Name, $col4Name)
1227+
!$col2Name = $fillMissing($col2Name, $col3Name)
1228+
!$col1Name = $fillMissing($col1Name, $col2Name)
1229+
11651230
!$propColCaption = ""
11661231
!$propTableCaption = "|= " + $col1Name + " |"
11671232
!if ($col2Name != "")
11681233
!$propTableCaption = $propTableCaption + "= " + $col2Name + " |"
1234+
$updatePropColumns(2)
11691235
!endif
11701236
!if ($col3Name != "")
11711237
!$propTableCaption = $propTableCaption + "= " + $col3Name + " |"
1238+
$updatePropColumns(3)
11721239
!endif
11731240
!if ($col4Name != "")
11741241
!$propTableCaption = $propTableCaption + "= " + $col4Name + " |"
1242+
$updatePropColumns(4)
11751243
!endif
1244+
1245+
!$isFirstProp = 0
11761246
!return ""
11771247
!endfunction
11781248

11791249
!unquoted function WithoutPropertyHeader()
11801250
!$propTableCaption = ""
11811251
!$propColCaption = "="
1252+
1253+
!$isFirstProp = 1
1254+
!$firstPropCol = 1
1255+
!$lastPropCol = 1
1256+
11821257
!return ""
11831258
!endfunction
11841259

11851260
!unquoted function AddProperty($col1, $col2 = "", $col3 = "", $col4 = "")
1261+
!$col3 = $fillMissing($col3, $col4)
1262+
!$col2 = $fillMissing($col2, $col3)
1263+
!$col1 = $fillMissing($col1, $col2)
1264+
11861265
!if ($propTable == "")
11871266
!if ($propTableCaption != "")
11881267
!$propTable = $propTableCaption + "\n"
11891268
!endif
11901269
!else
11911270
!$propTable = $propTable + "\n"
11921271
!endif
1272+
11931273
!$propTable = $propTable + "| " + $col1 + " |"
11941274
!if ($col2 != "")
11951275
!$propTable = $propTable + $propColCaption + " " + $col2 + " |"
1276+
$updatePropColumns(2)
11961277
!endif
11971278
!if ($col3 != "")
11981279
!$propTable = $propTable + " " + $col3 + " |"
1280+
$updatePropColumns(3)
11991281
!endif
12001282
!if ($col4 != "")
12011283
!$propTable = $propTable + " " + $col4 + " |"
1284+
$updatePropColumns(4)
12021285
!endif
1286+
1287+
!$isFirstProp = 0
12031288
!return ""
12041289
!endfunction
12051290

12061291
!unquoted function $getProps($alignedNL = "\n")
1292+
$fixHeaderColumns()
1293+
12071294
!if ($propTable != "")
12081295
!$retTable = $alignedNL + $propTable
12091296
!$propTable = ""

percy/TestPropertyMissingColumns.puml

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
@startuml
2+
' convert it with additional command line argument -DRELATIVE_INCLUDE="./.." to use locally
3+
!if %variable_exists("RELATIVE_INCLUDE")
4+
!include %get_variable_value("RELATIVE_INCLUDE")/C4_Deployment.puml
5+
!else
6+
!include https://raw.githubusercontent.com/kirchsth/C4-PlantUML/extended/C4_Deployment.puml
7+
!endif
8+
9+
' missing columns 3 and 4 are added that all columns are displayed
10+
SetPropertyHeader("", $col2Name="2")
11+
AddProperty($col1="col1")
12+
AddProperty("", $col2="col2")
13+
AddProperty(" ", " ", $col3="col3")
14+
' missing columns 2 and 3 are inserted with empty values
15+
AddProperty("", $col4="col4")
16+
17+
Container(c, "Container")
18+
19+
@enduml

0 commit comments

Comments
 (0)