@@ -724,50 +724,61 @@ $Order = 2,
724
724
$Variable = @{}
725
725
)
726
726
727
+ # First, let us expand our axiom
727
728
$currentState = "$Axiom"
729
+ # (at least, as long as we're supposed to)
728
730
if ($Order -ge 1) {
729
731
$combinedPattern = "(?> $($Rule.Keys -join '|'))"
730
732
foreach ($iteration in 1..$Order) {
733
+ # To expand each iteration, we replace any matching characters
731
734
$currentState = $currentState -replace $combinedPattern, {
732
735
$match = $_
733
736
$matchingRule = $rule["$match"]
737
+ # a matching rule could be dynamically specified with a script block
734
738
if ($matchingRule -is [ScriptBlock]) {
735
739
return "$(. $matchingRule $match)"
736
740
} else {
741
+ # but is often statically expanded with a string.
737
742
return $matchingRule
738
743
}
739
744
}
740
745
}
741
746
}
742
747
743
- $localReplacement = [Ordered]@{}
744
- foreach ($key in $variable.Keys) {
745
- $localReplacement[$key] =
746
- if ($variable[$key] -is [ScriptBlock]) {
747
- [ScriptBlock]::Create($variable[$key])
748
- } else {
749
- $variable[$key]
750
- }
751
- }
752
-
748
+ # Now we know our final state
753
749
$finalState = $currentState
754
750
751
+ # and can add the appropriate data attributes.
755
752
$this.PathAttribute = [Ordered]@{
756
753
"data-l-order" = $Order
757
754
"data-l-axiom" = $Axiom
758
755
"data-l-rules" = ConvertTo-Json $Rule
759
756
"data-l-expanded" = $finalState
760
757
}
761
758
759
+ # Next, prepare our replacements.
760
+ # The provided script block will almost always be scoped differently
761
+ # so we need to recreate it.
762
+ $localReplacement = [Ordered]@{}
763
+ foreach ($key in $variable.Keys) {
764
+ $localReplacement[$key] =
765
+ if ($variable[$key] -is [ScriptBlock]) {
766
+ [ScriptBlock]::Create($variable[$key])
767
+ } else {
768
+ $variable[$key]
769
+ }
770
+ }
771
+
772
+ # Now we need to find all potential matches
762
773
$MatchesAny = "(?> $($variable.Keys -join '|'))"
763
774
$allMatches = @([Regex]::Matches($finalState, $MatchesAny, 'IgnoreCase,IgnorePatternWhitespace'))
775
+ # we want to minimize rematching, so create a temporary cache.
764
776
$matchCache = @{}
765
777
:nextMatch foreach ($match in $allMatches) {
766
778
$m = "$match"
767
779
if (-not $matchCache[$m]) {
768
780
foreach ($key in $Variable.Keys) {
769
- if (-not ($match -match $key)) { continue }
770
- # if ($variable[$key] -isnot [ScriptBlock]) { continue }
781
+ if (-not ($match -match $key)) { continue }
771
782
$matchCache[$m] = $localReplacement[$key]
772
783
break
773
784
}
0 commit comments