forked from LieberLieber/LemonTree.Automation.Workflows
-
Notifications
You must be signed in to change notification settings - Fork 1
/
extractImages_saveLocal.ps1
100 lines (83 loc) · 3.29 KB
/
extractImages_saveLocal.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
88
89
90
91
92
93
94
95
96
97
98
99
100
param (
[string]$xmlFilePath = $args[0],
[string]$outputDirectory = 'output_images'
)
# Check if an XML file path is provided as an argument
if (-not $xmlFilePath) {
$xmlFiles = Get-ChildItem -Path . -Filter *.xml
if ($xmlFiles -eq $null -or $xmlFiles.Count -eq 0) {
Write-Host "No XML files found in the current directory."
Exit
} else {
$xmlFilePath = $xmlFiles[0].FullName
}
}
Write-Host "Using XML file: $xmlFilePath"
try {
[xml]$xml = Get-Content -Path $xmlFilePath
} catch {
Write-Host "Failed to load the XML file: $_"
Exit
}
# Ensure the output directory exists
if (-not (Test-Path -Path $outputDirectory)) {
New-Item -ItemType Directory -Path $outputDirectory -Force > $null
}
# Define the XML namespaces
$ns = New-Object Xml.XmlNamespaceManager $xml.NameTable
$ns.AddNamespace("ns", "http://www.lieberlieber.com")
# Select all diagramPicture elements using the defined namespace
$diagramPictures = $xml.SelectNodes('//ns:diagramPictures/ns:diagramPicture', $ns)
# Output the count of found elements
Write-Host "Found $($diagramPictures.Count) Diagrams in $xmlFilePath."
# Create an HTML header
$html = '<html><head><title>Diagram Pictures</title></head><body>'
# Iterate through each diagram picture
foreach ($diagramPicture in $diagramPictures) {
$guid = $diagramPicture.guid
$diagramType = $diagramPicture.diagramType
$name = $diagramPicture.diagramPictureA.name
$nameB = $diagramPicture.diagramPictureB.name
# Extract the CDATA content for diagramPictureA and diagramPictureB
$cdata_a = $diagramPicture.diagramPictureA.InnerText
$cdata_b = $diagramPicture.diagramPictureB.InnerText
# Check if the CDATA sections are empty
if (![string]::IsNullOrEmpty($cdata_a) -or ![string]::IsNullOrEmpty($cdata_b)) {
# Generate unique filenames for the images
$filename_a = Join-Path -Path $outputDirectory -ChildPath "$name-DiagramA.svg"
$filename_b = Join-Path -Path $outputDirectory -ChildPath "$nameB-DiagramB.svg"
# Save the images as files
if (![string]::IsNullOrEmpty($cdata_a)) {
[System.Convert]::FromBase64String($cdata_a) | Set-Content -Path $filename_a -Encoding Byte
}
if (![string]::IsNullOrEmpty($cdata_b)) {
[System.Convert]::FromBase64String($cdata_b) | Set-Content -Path $filename_b -Encoding Byte
}
# Create an HTML element for the diagram pictures
$html += @"
<div>
<h2>Name: $name</h2>
<p>GUID: $guid</p>
<p>Type: $diagramType</p>
<div style="display: flex;">
<div style="flex: 1;">
<p>Diagram A:</p>
<img src='data:image/svg+xml;base64,$cdata_a' />
</div>
<div style="flex: 1;">
<p>Diagram B:</p>
<img src='data:image/svg+xml;base64,$cdata_b' />
</div>
</div>
</div>
"@
} else {
Write-Host "Empty CDATA section found for diagram with name: $name"
}
}
# Close the HTML page
$html += '</body></html>'
# Save the HTML page to a file
$html | Set-Content -Path 'output.html' -Force
Write-Host "HTML page has been generated and saved as 'output.html'."
Write-Host "Images saved in $outputDirectory."