Advertisement
Guest User

Untitled

a guest
Jun 30th, 2015
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.77 KB | None | 0 0
  1. param(
  2. [Parameter(Mandatory=$true)]$File
  3. )
  4.  
  5. $lines = foreach($ln in Get-Content $File) {
  6. if($ln -match '^(\t*)(.+)$') {
  7. @{
  8. originalText = $ln
  9. text = $Matches[2]
  10. indentLevel = $Matches[1].Length
  11. }
  12. }
  13. }
  14. $lines = $lines | ? { $_.text -and $_.text.Trim() }
  15.  
  16. function create-Indents($count) {
  17. return ([System.Linq.Enumerable]::Range(0, $count) | % { " " }) -join ''
  18. }
  19. function parse-Tree($currentLine, $lines) {
  20. function writein($text) {
  21. # Write-Host "$(create-Indents $currentLine.indentLevel)$text" -ForegroundColor Cyan
  22. }
  23. writein $currentLine.text
  24. writein "- $($lines.Count) remaining"
  25. $nextLine, $otherLines = $lines
  26. if(-not $currentLine) {
  27. return @{
  28. nodes = @()
  29. remaining = @()
  30. }
  31. }
  32. $node = @{
  33. line = $currentLine
  34. indentLevel = $currentLine.indentLevel
  35. text = $currentLine.text
  36. }
  37. if(-not $nextLine -or $nextLine.indentLevel -lt $currentLine.indentLevel) {
  38. $node.children = @()
  39. writein "- done"
  40. return @{
  41. nodes = @($node)
  42. remaining = $lines
  43. }
  44. }
  45. if($nextLine.indentLevel -eq $currentLine.indentLevel) {
  46. writein "- going over"
  47. $siblingsTree = parse-Tree $nextLine $otherLines
  48. writein "- adding $($siblingsTree.nodes.Count) siblings to $($node.text)"
  49. return @{
  50. nodes = @($node) + $siblingsTree.nodes
  51. remaining = $siblingsTree.remaining
  52. }
  53. }
  54. if($nextLine.indentLevel -gt $currentLine.indentLevel) {
  55. writein "- going down"
  56. $nextTree = parse-Tree $nextLine $otherLines
  57. writein "- adding $($nextTree.nodes.Count) children to $($node.text)"
  58. $node.children = $nextTree.nodes
  59.  
  60. #todo - this can be slicker as it basically just repeats the above
  61. $nextLine, $otherLines = $nextTree.remaining
  62. if($nextLine.indentLevel -eq $currentLine.indentLevel){
  63. writein "- going over"
  64. $siblingsTree = parse-Tree $nextLine $otherLines
  65. writein "- adding $($siblingsTree.nodes.Count) siblings to $($node.text)"
  66. return @{
  67. nodes = @($node) + $siblingsTree.nodes
  68. remaining = $siblingsTree.remaining
  69. }
  70. }
  71. if(-not $nextLine -or $nextLine.indentLevel -lt $currentLine.indentLevel) {
  72. writein "- done"
  73. return @{
  74. nodes = @($node)
  75. remaining = @($nextLine) + $otherLines
  76. }
  77. }
  78. }
  79. }
  80. function visitLast($node, [scriptblock]$fn) {
  81. if(-not $node) {
  82. return
  83. }
  84. foreach($child in $node.children){
  85. visitLast $child $fn
  86. }
  87. &$fn $node
  88. }
  89. function visitFirst($node, [scriptblock]$fn, $acc) {
  90. if(-not $node) {
  91. return
  92. }
  93. &$fn $node $acc
  94. foreach($child in $node.children){
  95. visitFirst $child $fn $acc
  96. }
  97. }
  98.  
  99. $head, $tail = $lines
  100. $parsed = parse-Tree $head $tail
  101.  
  102. function parse-nodeText($node) {
  103. $parts = @( $node.text -split ',' | % { $_.Trim() } )
  104. if($parts[0] -match '^(\d+)\s*(-\s*(\d+))?') {
  105. $node.item = $parts[1]
  106. $node.description = $parts[2]
  107. if($node.children) {
  108. Write-Warning "Line has an estimate and children - ignoring children sum. '$($node.line.text)'"
  109. }
  110. $node.estimate = [int]$matches[1],[int]$matches[1]
  111. if($matches.Count -eq 4) {
  112. $node.estimate[1] = [int]$matches[3]
  113. }
  114. } else {
  115. $node.item = $parts[0]
  116. $node.description = $parts[1]
  117. $node.estimate = $null,$null
  118. if(-not $node.children) {
  119. Write-Warning "no estimate on leaf '$($node.text)'"
  120. }
  121. foreach($child in $node.children) {
  122. $node.estimate[0] += $child.estimate[0]
  123. $node.estimate[1] += $child.estimate[1]
  124. }
  125. }
  126. }
  127.  
  128. foreach($node in $parsed.nodes) {
  129. visitLast $node ${function:parse-nodeText}
  130. }
  131.  
  132. function create-OutputItem($node, $acc) {
  133. $hash = [ordered]@{
  134. min = $node.estimate[0]
  135. max = $node.estimate[1]
  136. item = "$(create-Indents $node.indentLevel)$($node.item)"
  137. description = $node.description
  138. }
  139. $acc.Add( (New-Object PsObject -property $hash) ) | out-null
  140. }
  141. $outgoingItems = new-object System.Collections.ArrayList
  142. foreach($node in $parsed.nodes) {
  143. visitFirst $node ${function:create-OutputItem} $outgoingItems
  144. }
  145. return $outgoingItems
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement