# encoding: ascii
# api: powershell
# title: Repair-ScriptQuotes
# description: Script to replace special quote characters when copy – pasting from Word or from blogs.
# version: 0.1
# type: function
# author: P Sczepanski
# license: CC0
# function: Repair-ScriptQuotes
# x-poshcode-id: 3816
# x-archived: 2012-12-16T04:25:52
# x-published: 2012-12-11T13:16:00
#
# See also my blog entry “Quote or not quote” at http://redtoo.com/blog/?cat=13
#
function Repair-ScriptQuotes {
    param (
        [Parameter(Mandatory=$true,ValueFromPipeline=$true)]
        [string]$path
    )
    if ( !( Test-Path $path ) ) {
        Write-Warning "'$path' does not exist."
        Continue
    }
        

    (( Get-Content $path) |  
        ForEach-Object {
            # replace double quotes
            [Net.WebUtility]::HtmlDecode($_)
        } | 
            ForEach-Object {
                # replace double quotes
                $_ -replace "(\u201c|\u201d|\u201e)",[char]34
            } | 
                ForEach-Object {
                    # replace single quotes
                    $_ -replace "(\u2018|\u2019|\u201a)",[char]39
                } | 
                    ForEach-Object {
                        # replace dash
                        $_ -replace "(\u2013)",[char]45
                    } | 
                        ForEach-Object {
                            # replace tab with four spaces
                            $_ -replace "`t",(" " * 4)
                        }) | 
                            Set-Content $path
    Write-Host "Done"
}
