public/content-helpers/Get-ConfluenceContentMap.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
function Get-ConfluenceContentMap($TemplateContent,$UserContentMap = $global:PowerConfluence.Templates.Layout.UserSection.DefaultMap) {
    #start an array to track the content we find
    $userContent = @()

    # get the sections
    $sections = Get-ConfluenceSections -StorageFormat $TemplateContent

    # if the page doesn't have sections, wrap the content in a section and go again
    if ($sections -eq $null) { $sections = Get-ConfluenceSections -StorageFormat (Format-ConfluenceSection -Contents $TemplateContent) }

    # make sure the map matches the content we have, count-wise
    if ($sections.Count -ne $UserContentMap.Count) { Throw "There is a mis-match between number of sections in the supplied UserContentMap and the content"}

    # loop through the map and grab the content we need
    for($i=0;$i -lt $UserContentMap.Count;$i++) {
        switch ($UserContentMap[$i].GetType().Name) {
            Boolean {
                $userContent += (&{if($UserContentMap[$i]) {@{Content=$sections[$i];Generated=$false}} else {@{Content=$null;Generated=$true}}})
            }
            "Object[]" {
                # get the cells
                $cells = Get-ConfluenceCells -StorageFormat $sections[$i]
                $cellMap = $UserContentMap[$i]
                # make sure the map matches the content we have, count-wise
                if ($cells.Count -ne $cellMap.Count) { Throw "There is a mis-match between the number of cells in a section in supplied UserContentMap and the content"}
                # loop through the map and grab the content we need
                $cellContent = @()
                for($j=0;$j -lt $cellMap.Count;$j++) {
                    $cellContent += (&{if($cellMap[$j]) {@{Content=$cells[$j];Generated=$false}} else {@{Content=$null;Generated=$true}}})                    
                }
                # add the cells to the user contents array
                $userContent += ,$cellContent
            }
            Default {
                Throw "Unrecognized value in UserContentMap"
            }
        }
    }

    # return
    $userContent
}