ShowHTML.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
Function Out-HTML
{
<#PSScriptInfo
 
.VERSION
1.0
 
.GUID
a279a77a-7c5b-4e9e-b85a-48604b8d93e1
 
.AUTHOR
edgenl
 
.Description
Convert a HTML string to an HTML file
 
.COMPANYNAME
 
.COPYRIGHT
 
.TAGS Show HTML
 
.LICENSEURI
 
.PROJECTURI
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
#>

   #.Description
   # Convert a HTML string to an HTML file
   #
   # If -HTMLFile is empty, a temporary file wil be created
   #
   #.Example
   # $Webpage = Invoke-WebRequest -Uri 'http://www.nu.nl'
   #
   # Out-HTML -HTML $Webpage.Content -URL 'http://www.nu.nl/' -HTMLFile "c:\temp\test.htm"
   #
   param(
      # The HTML content
      [Parameter(Mandatory=$True, ValueFromPipeline=$True)]
      [string]$HTML,

      # The URL to use as base for relative links
      [string]$URL,

      # The file to write to
      [string]$HTMLFile
   )
 
  If ($URL)
  {
    $UrlParts = $URL -split "/"
    $UrlRoot = $UrlParts[0] + "//" + $UrlParts[2] + "/"

    $HTML = $HTML -ireplace '<head>', ('<head><base href="' + $UrlRoot + '" />')
    $HTML = $HTML -ireplace '</head>', '</head>'
  }

  If (-not $HTMLFile)
  {
    $HTMLFile = ([System.IO.Path]::GetTempFileName()).Replace(".tmp",".htm")
  }

  $HTML | Out-File $HTMLFile

  Invoke-Item $HtmlFile
}