Specifically, it looks for <code> blocks inside <div class="codeSnippetBody"> blocks and removes any instances of   or
Disclaimers:
- This is combination of my remedial PS knowledge and some ChatGPT heavy lifting. It works for me but it might not work for you.
- You will obviously lose all indentation from your code block, you could fix this with some very specific CSS padding but this is out of scope for this thread.
1) Save the following content into a .ps1 file in the root of your Project (same place as your .flprj file). Call it anything you like but remember what you call it. I went with remove_160.ps1
Code: Select all
# Define the directory path as an argument or hard-code it if needed
param (
[string]$directoryPath = "C:\path\to\your\directory" # Replace with your directory path
)
# Check if directory path exists
if (!(Test-Path $directoryPath)) {
Write-Host "Directory does not exist: $directoryPath"
exit
}
# Recursively iterate through all .html and .htm files in the directory and subdirectories
Get-ChildItem -Path $directoryPath -Recurse -Include *.html, *.htm -File | ForEach-Object {
$fileContent = Get-Content $_.FullName -Raw
# Use regex to find <code> elements inside <div class="codeSnippetBody"> and remove   and
$updatedContent = [regex]::Replace($fileContent, '(?is)(<div[^>]*class="codeSnippetBody"[^>]*>.*?<code>)(.*?)(</code>.*?</div>)', {
param($match)
# Add logging to check if this section is triggered
Write-Host "Match Found in File: $($_.FullName)"
Write-Host "Group 1 (Opening Tags): $($match.Groups[1].Value)"
Write-Host "Group 2 (Code Content): $($match.Groups[2].Value)"
Write-Host "Group 3 (Closing Tags): $($match.Groups[3].Value)"
# Remove all occurrences of   and within the <code> content
$codeContent = $match.Groups[2].Value -replace ' ', '' -replace ' ', ''
# Optionally, trim extra spaces
$codeContent = $codeContent -replace '\s+', ' ' # Replace multiple spaces with a single space
$codeContent = $codeContent.Trim() # Trim leading and trailing whitespace
# Return the updated content to replace the original match
return $match.Groups[1].Value + $codeContent + $match.Groups[3].Value
})
# Add "<p>Processed by post-build task</p>" before the closing </body> tag
if ($updatedContent -notlike '*<p>Processed by post-build task</p>*') {
$updatedContent = $updatedContent -replace '(</body>)', '<p>Processed by post-build task</p>$1'
}
# Write the updated content back to the file
Set-Content $_.FullName -Value $updatedContent
Write-Host "Updated file: $($_.FullName)"
}
Code: Select all
powershell -ExecutionPolicy Bypass -File "$(ProjectDirectory)remove_160.ps1" -directoryPath "$(OutputDirectory)\content\topics\"