-
Notifications
You must be signed in to change notification settings - Fork 14
/
Get-WebTemplateEditableRegion.ps1
87 lines (72 loc) · 6.09 KB
/
Get-WebTemplateEditableRegion.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
function Get-WebTemplateEditableRegion
{
<#
.Synopsis
Extracts out the editable regions from a dynamic web template
.Description
Determines what portions of a document are editable
.Example
Get-ChildItem -Filter *.dwt | Get-WebTemplateEditableRegion
.Link
Get-Web
#>
[CmdletBinding(DefaultParameterSetName='FilePath')]
[OutputType([PSObject])]
param(
# The path to a document
[Parameter(Mandatory=$true,
ParameterSetName='FilePath',
ValueFromPipelineByPropertyName=$true)]
[Alias('Fullname')]
[String]
$FilePath,
# The content of the document
[Parameter(Mandatory=$true,
Position=0,
ParameterSetName='DynamicWebTemplate',
ValueFromPipelineByPropertyName=$true)]
[Alias('DWT')]
[String]
$DynamicWebTemplate
)
process {
if ($PSCmdlet.ParameterSetName -eq 'FilePath') {
$resolvedPath = $ExecutionContext.SessionState.Path.GetResolvedPSPathFromPSPath($FilePath)
if (-not $resolvedPath) { return }
if ([IO.file]::Exists($resolvedPath)) {
$text = [IO.File]::ReadAllText($resolvedPath)
$rInf = Get-WebTemplateEditableRegion -DynamicWebTemplate $text -ErrorAction SilentlyContinue
$regionInfo =
New-Object PSObject -Property @{
FilePath = "$resolvedPath"
Region = $rInf | Select-Object -ExpandProperty Region
MatchInfo = $rInf | Select-Object -ExpandProperty MatchInfo
}
$regionInfo.pstypenames.clear()
$regionInfo.pstypenames.add('DWTInfo')
$regionInfo
}
} elseif ($PSCmdlet.ParameterSetName -eq 'DynamicWebTemplate') {
# Find the start of the HTML tag, and try to convert to XHTML.
# If this fails, present an error to the user. Be sure to ignore the case, because we
# really don't care
$htmlStart = $DynamicWebTemplate.IndexOf("<html", [StringComparison]::OrdinalIgnoreCase)
if (-not $htmlStart) {
Write-Error "This content does not appear to be an HTML document. It does not have a <html> tag."
return
}
$r = New-Object Text.RegularExpressions.Regex ("<!--\s#BeginEditable\s`"(?<region>\w+)`""), ("Singleline", "IgnoreCase")
$results= @($r.Matches($DynamicWebTemplate))
foreach ($r in $results) {
$regionInfo =
New-Object PSObject -Property @{
Region = $r.Groups[1].Value
MatchInfo = $r
}
$regionInfo.pstypenames.clear()
$regionInfo.pstypenames.add('DWTInfo')
$regionInfo
}
}
}
}