Skip to content

Instantly share code, notes, and snippets.

@Injac
Created September 11, 2013 23:34
Show Gist options
  • Save Injac/6531253 to your computer and use it in GitHub Desktop.
Save Injac/6531253 to your computer and use it in GitHub Desktop.
Little PowerShell script to update your bootstrap 2 Project to Bootstrap 3. Please use carefully and get a copy of your Project files and run it on the copy! No warranty that it will work or not destroy your work! Replace [FULL PATH TO YOUR HTML FILE FOLDER] with the full path to your Project files, and [FULL PATH TO YOUR LOGFILE] with the full …
Get-ChildItem '[FULL PATH TO YOUR HTML FILE FOLDER]' *.html -recurse |
Foreach-Object {
$c = ($_ | Get-Content)
$logFile = "[FULL PATH TO YOUR LOGFILE]";
$output = "Modifiying file: " + $_.FullName
$output | out-file $LogFile -Append
$c = $c -replace 'span([0-9]+)','col-md-${1}'
$c = $c -replace 'row-fluid','row'
$c = $c -replace 'container-fluid','container'
$c = $c -replace 'form-search','form-inline'
$c = $c -replace 'input-block-level',''
$c = $c -replace 'help-inline','help-block'
$c = $c -replace 'control-group','form-group'
$c = $c -replace 'controls',''
$c = $c -replace 'navbar-search','navbar-form'
$c = $c -replace 'navbar-inner','container'
$c = $c -replace 'brand','navbar-brand'
$c = $c -replace '.brand','.navbar-brand'
#Replace them 2 times, please check, because i don't know if you use both of them or only one
#$c = $c -replace 'navbar-toggle','navbar-header'
$c = $c -replace 'btn-inverse','btn-default'
$c = $c -replace 'hero-unit','jumbotron'
$c = $c -replace 'bar','progress-bar'
$c = $c -replace 'phone','sm'
$c = $c -replace 'tablet','md'
$c = $c -replace 'desktop','lg'
$c = $c -replace 'alert-block','alert'
#$c = $c -replace 'label','label-default'
#replace input element within a form if it contains a class element, and add the form-control class
$regEx = "((<input.+?)(>|/>))"
$x = new-object System.Text.RegularExpressions.Regex ($regEx,[System.Text.RegularExpressions.RegexOptions]::IgnoreCase)
$matches = $x.Matches($c)
foreach($m in $matches)
{
if($m.ToString().ToUpper().Contains("CLASS"))
{
$repl = $m.ToString() -replace '(.*)(class=)([''\"])(.*)([''\"])(.*)', '${1}${2}${3} form-control ${4}${5}${6}'
$output = "Added form-control class to input in file:" + $_.FullName
$output | out-file $LogFile -Append
$c = $c.Replace($m.ToString(), $repl)
}
else
{
$output = "Added form-control class to input in file:" + $_.FullName
$output | out-file $LogFile -Append
$repl = $m.ToString() -replace '(>|/>)', "class='form-control'>"
$c = $c.Replace($m.ToString(), $repl)
}
}
#replace select element within a form if it contains a class element, and add the form-control class
$regEx = "((<select.+?)(>))"
$x = new-object System.Text.RegularExpressions.Regex ($regEx,[System.Text.RegularExpressions.RegexOptions]::IgnoreCase)
$matches = $x.Matches($c)
foreach($m in $matches)
{
if($m.ToString().ToUpper().Contains("CLASS"))
{
$repl = $m.ToString() -replace '(.*)(class=)([''\"])(.*)([''\"])(.*)', '${1}${2}${3} form-control ${4}${5}${6}'
$output = "Added form-control class to select in file:" + $_.FullName
$output | out-file $LogFile -Append
$c = $c.Replace($m.ToString(), $repl)
}
else
{
$repl = $m.ToString() -replace '(>|/>)', " class='form-control'>"
$c = $c.Replace($m.ToString(), $repl)
}
}
#Wrap checkboxes into separate divs
$regEx = "((<input.+?)(>))"
$x = new-object System.Text.RegularExpressions.Regex ($regEx,[System.Text.RegularExpressions.RegexOptions]::IgnoreCase)
$matches = $x.Matches($c)
foreach($m in $matches)
{
if($m.ToString().ToUpper().Contains("CHECKBOX"))
{
$c = $c.Replace($m.ToString(), "<div>" + $m.ToString() + "</div>")
$output = "Wrapped checkbox into div in file:" + $_.FullName
$output | out-file $LogFile -Append
}
}
#replace ol and ul classes
$regEx = "(((<ul.+?)(>))|((<ol.+?)(>)))"
$x = new-object System.Text.RegularExpressions.Regex ($regEx,[System.Text.RegularExpressions.RegexOptions]::IgnoreCase)
$matches = $x.Matches($c)
foreach($m in $matches)
{
$output = "Replaced ul or ol classes in file:" + $_.FullName
$output | out-file $LogFile -Append
$text = $m.ToString().Replace("muted","text-muted")
$text = $text.Replace("unstyled","list-unstyled")
$text = $text.Replace("inline","list-inline")
$c = $c.Replace($m.ToString(), $text)
}
#$c = $c -replace 'class=(''|\")(.*)(''|\")','class=${1}${2} from-control ${3}'
[IO.File]::WriteAllText($_.FullName, ($c -join "`r`n"))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment