Using PowerShell to make quick changes to MadCap Flare source files
In a previous post, I explained how to use PowerShell and Html Agility Pack with MadCap Flare’s build events feature to make changes to HTML outputs.
You can also use PowerShell with MadCap Flare for other purposes. Specifically, you can use PowerShell to modify multiple source files in multiple projects in a single step. Here are some examples.
Change class names
Get-ChildItem -Path .\ -Include *.htm, *.flsnp -Recurse -File| ForEach-Object {
$file = $_.FullName
$output = Get-Content -Path $file -Raw
$hashTable = @{}
$hashTable.'class="OldClass1"' = 'class="NewClass1"'
$hashTable.'class="OldClass2"' = 'class="NewClass2"'
$hashTable.'class="OldClass3"' = 'class="NewClass3"'
Foreach ($key in $hashTable.Keys) {
$output = $output.Replace($key, $hashTable.$key)
}
Write-Output $output | Set-Content -Path $file -Encoding UTF8
}
Change one type of element to another type of element
Get-ChildItem -Path .\ -Include *.htm, *.flsnp -Recurse -File| ForEach-Object {
$file = $_.FullName
$xml = New-Object -TypeName XML
$xml.Load($file)
$bTags = $xml.SelectNodes('//b')
if ($bTags) {
$bTags | ForEach-Object {
$node = $_
$newNode = $xml.CreateElement('strong')
$newNode.InnerText = $node.InnerText
$node.ParentNode.InsertAfter($newNode,$node)
$node.ParentNode.RemoveChild($node)
}
}
$xml.Save($file)
}
Assign a class to paragraphs in the first column of select tables
Recommended by LinkedIn
Get-ChildItem -Path .\ -Include *.htm, *.flsnp -Recurse -File| ForEach-Object {
$file = $_.FullName
$xml = New-Object -TypeName XML
$xml.Load($file)
$headingColumnParas = $xml.SelectNodes('//table[@class="TableStyle-CustomTableStyle"]/tbody/tr/td[1]/p')
if ($headingColumnParas) {
$headingColumnParas | ForEach-Object {
$node = $_
$node.SetAttribute("class","FirstColumnStyle")
}
}
$xml.Save($file)
}
Change the text in a heading or other element
Get-ChildItem -Path .\ -Include *.htm, *.flsnp -Recurse -File| ForEach-Object {
$file = $_.FullName
$xml = New-Object -TypeName XML
$xml.Load($file)
$productRows = $xml.SelectNodes('//p[@class="Sidehead" and contains(text(),"Products")]')
if ($productRows) {
$productRows | ForEach-Object {
$node = $_
$node.InnerText = "Models"
}
}
$xml.Save($file)
}
Move an element to the bottom of a topic
Get-ChildItem -Path .\ -Include *.htm, *.flsnp -Recurse -File| ForEach-Object {
$file = $_.FullName
$xml = New-Object -TypeName XML
$xml.Load($file)
$body = $xml.SelectSingleNode('//body')
$topicLinks = $xml.SelectNodes('//div[@class="TopicLinks"]')
if ($topicLinks) {
$topicLinks | ForEach-Object {
$node = $_
$body.AppendChild($node)
}
}
$xml.Save($file)
}
There are a couple of key things to keep in mind if you use PowerShell to make changes to your source files:
Get-ChildItem -Path .\ -Include *.htm, *.flsnp -Recurse -File| ForEach-Object {
$file = $_.FullName
$xml = New-Object -TypeName XML
$xml.Load($file)
$ns = New-Object Xml.XmlNameSpaceManager $xml.NameTable
$ns.AddNamespace("MadCap",$xml.DocumentElement.NameSpaceURI)
…
$xml.Save($file)
}
This is another example of how you can take advantage of Flare’s XML-based architecture to automate repetitive or complex tasks.