tsunami

log in
history

PowerShell: scripts useful for tsunami

Luke Breuer
2008-12-24 02:47 UTC

introduction
There is no built-in functionality to make writing tsunami syntax easier. It is easier to write "powershell functions"[1] for now, so that's what I'm doing. At some point, javascript versions with nice UIs will be required.
functions
# renumber (or add numbers to) all "hyperlinks"[2] and "references"[] within a section
#
# []  A
# [2] B
#
# in the example above, hyperlinks would now be linked to A, and references to B
#
# NOTE: does not support referencing the same hyperlink multiple times, and will
#       re-number out-of-orderness as exists a bit, which might break things!
function Quote-Tsunami
{
    # pull input from stdin if it's there, otherwise use the clipboard
    $s = Join-String $input -Newline
    
    if ($s) { $clip = $false; }
    else    { $clip = $true; $s = Get-Clipboard; }
    
    # parameterize Foreach-Object (%)
    $lambda = { $i = 1; $s = "" }, 
              { $s += "$_$i"; $i++; }, 
              { $s = $s.SubString(0, $s.Length - "$i".Length); $s; }
    
    # remove existing numbering
    $s = $s -replace     '(?<="[)[^\]]+', ''
    $s = $s -replace '(?m)(?<=^[)[^\]]+', ''
    
    # add new numbering
    $s = [Regex]::Split($s,     '(?<="[)(?=\])') | % $lambda[0] $lambda[1] $lambda[2]
    $s = [Regex]::Split($s, "(?m)(?<=^[)(?=\])") | % $lambda[0] $lambda[1] $lambda[2]
    
    # dump to clipboard if we pulled from there, otherwise dump to stdout
    if ($clip) { $s | Write-Clipboard }
    else       { $s }
}