本帖最后由 flashercs 于 2022-6-2 21:06 编辑
- function Get-AngleNode {
- [CmdletBinding()]
- [OutputType('System.Text.RegularExpressions.Match')]
- param(
- [Parameter(Mandatory = $true)]
- [string]$Html,
- # MUST like "<(?<openTag>[\w.:-]+)[^>]*>"
- [Parameter(mandatory = $true)]
- [regex]$ReAngle,
- # To find all nodes
- [switch]$FindAll
- )
- $startPosition = 0
- $reTag = [regex]'</(?<closeTag>[\w.:-]+)>|<(?<openTag>[\w.:-]+)[^>]*>'
- $stackTag = New-Object 'System.Collections.Generic.Stack[string]'
- do {
- $m = $ReAngle.Match($Html, $startPosition)
- if ($m.Success) {
- $m #openTag
- $stackTag.Push($m.Groups['openTag'].Value)
- $startPosition = $m.Index + $m.Length
- while ($true) {
- $m = $reTag.Match($Html, $startPosition)
- if (-not $m.Success) { break }
- $startPosition = $m.Index + $m.Length
- if ($m.Groups['openTag'].Success) {
- $stackTag.Push($m.Groups['openTag'].Value)
- } elseif ($stackTag.Peek() -eq $m.Groups['closeTag'].Value) {
- $null = $stackTag.Pop()
- if ($stackTag.Count -eq 0) {
- $m #clostTag
- break
- }
- } else { $PSCmdlet.WriteWarning("html parsing error: Index=$($m.Groups['closeTag'].Index),Value=$m") }
- }
- } else { break }
- }while ($FindAll)
- }
- function Edit-HtmlNode {
- [CmdletBinding()]
- [OutputType('string')]
- param(
- [Parameter(Mandatory = $true)]
- [string]$Html,
- [Parameter(Mandatory = $true)]
- [System.Collections.IDictionary]$DicNode
- )
- $strb = New-Object System.Text.StringBuilder
- $newhtml = $Html
- foreach ($key in $DicNode.Keys) {
- $null = $strb.Clear()
- $arrM = Get-AngleNode -Html $newhtml -ReAngle $key -FindAll
- $index = 0
- foreach ($m in $arrM) {
- if ($m.Groups['openTag'].Success) {
- $null = $strb.Append($newhtml, $index, $m.Index + $m.Length - $index)
- } else {
- $null = $strb.Append($DicNode[$key]).Append($m.Value)
- }
- $index = $m.Index + $m.Length
- }
- $null = $strb.Append($newhtml.Substring($index))
- $newhtml = $strb.ToString()
- }
- $newhtml
- }
- # region config
- $html = @"
- <div>
- <div id="topic_panel" style="display: none">
- <div class="p_div"><p>Insert</p></div>
- <ul>
- <li id="sub-topic" class="submenu_btn"><span>A</label></span></li>
- </ul>
- </div>
- </div>
- <h1 id="to_cd">-------------------------------------</h1>
- <div id="transform_panel" style="display: none">
- <ul>
- <li id="clear_transform" class="submenu_btn" data-action="reset"><span>Reset Rotation/Skew</span></li>
- <li id="rotate_left" class="submenu_btn" data-action="rotate-left"><span>Rotate Left</span></li>
- <li id="rotate_right" class="submenu_btn" data-action="rotate-right"><span>Rotate Right</span></li>
- <li id="skew_left" class="submenu_btn" data-action="skew-left"><span>Skew Left</span></li>
- <li id="skew_right" class="submenu_btn" data-action="skew-right"><span>Skew Right</span></li>
- </ul>
- <div id="top_ab" class="p_div"><p>Insert</p></div>
- </div>
- "@
-
- $htNode = @{
- '(?i)<(?<openTag>div) id="topic_panel"[^>]*>' = '<abc>text</abc>'
- '(?i)<(?<openTag>div) id="top_ab"[^>]*>' = '<a>tet</a>'
- '(?i)<(?<openTag>H1) id="to_cd"[^>]*>' = '<b>xt</b>'
- }
- $newhtml = Edit-HtmlNode -Html $html -DicNode $htNode
- $newhtml
复制代码
|