-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRemove-HTMLTags.ps1
65 lines (55 loc) · 1.68 KB
/
Remove-HTMLTags.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
function Remove-HTMLTags {
<#
.SYNOPSIS
Converts HTML into a semi-readable text block.
.DESCRIPTION
Converts HTML into a semi-readable text block.
.EXAMPLE
PS C:\> Remove-HTMLTags $email.Body.text
.EXAMPLE
PS C:\> $email.Body.text | Remove-HTMLTags
.EXAMPLE
PS C:\> $email.Body.text | Remove-HTMLTags -ReplaceTagsWithLineBreaks
#>
param(
[Parameter(
Position=0,
ValueFromPipeline,
ValueFromPipelineByPropertyName
)]
[Alias('string')]
[string]
$Html,
# Replace all tags with line breaks, instead of just removing the tags
[Parameter()]
[switch]
$ReplaceTagsWithLineBreaks
)
begin {}
process {
$stageOne = (
$Html -replace
'\s\s+',' ' -replace
'<\/li>',"`n" -replace
'<\/ul>',"`n" -replace
'<\/ol>',"`n" -replace
'<br>',"`n" -replace
'\\n',"`n" -replace
' '," " -replace
' '," "
)
# list items should start/end on a new line and each item should end on a new line
$stageTwo = $stageOne -replace
'<\/?[o|u]l>',"`n" -replace
'<li>' -replace
'<\/li>',"`n"
# All other tags get replaced by nothing, unless a switch is present
$stageThree = if ($ReplaceTagsWithLineBreaks.IsPresent) {
$stageTwo -replace '<[^>]+?>',"`n"
} else {
$stageTwo -replace '<[^>]+?>'
}
$stageThree.Trim()
}
end {}
}#END: function Remove-HTMLTags {}