PowerShell Highlighting for GeSHi
9
Jun
A while back I switched to GeSHi for source code highlighting in my posts, and recently I started writing PowerShell scripts in my posts, and calmly sticking it in <code lang=“posh”> tags, half expecting it to just work, like all the other languages … but of course it didn’t. So after a few searches on Google and Ask, I concluded that a PowerShell syntax file doesn’t yet exist. So, I made one, feel free to grab it (it’s GPL, and I’ve even left intact the “any later version” clause from GeSHi’s license).
EDIT: 6/10/2007
I should mention that the way I do highlighting doesn’t care about “Nouns” at all: it just uses a list of Verbs, and matches anything that starts with a “Verb-” ... that means that for the sake of the highlighting, you can’t just say “Content” for Get-Content or (even though that actually works in a script). Also, I changed the version I was using so that it uses regular expression patters for command parameters, instead of a list. Because you can abbreviate parameters to the shortest distinguishable form, a list doesn’t really work.
Let’s just see in action, shall we?
# Get-Sql.ps1
###################################################################################################
# NOTICE that I have set default values for the server and database, but not for the query
# ALSO NOTICE that I'm not handling authentication (I just use Integrated Security)
#
# By default when you run this script it:
# * creates the functions
# * initializes the connection
# But we don't automatically do a query -- unless you pass one in!
#
# Thus, calling the script with no parameters results in an initialized connection,
# but it doesn't return anything, so it's basically silent if there are no errors.
#
# the default server and database
param( $Server = ".", $Database = "SOMR", $Query )
#
# change the SqlConnection (it's set to a default when the script it run)
#
function global:Set-SqlConnection( $Server = $(Read-Host "SQL Server Name"), $Database = $(Read-Host "Default Database") ) {
$SqlConnection.ConnectionString = "Server = $Server; Database = $Database; Integrated Security = True"
}
#
# A function to allow the user to
# perform a query that returns a table full of data
#
function global:Get-SqlDataTable( $Query = $(Read-Host "Enter SQL Query")) {
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = $Query
$SqlCmd.Connection = $SqlConnection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)
$SqlConnection.Close()
return $DataSet.Tables[0]
}
# Initialize the SqlConnection variable
Set-Variable SqlConnection (New-Object System.Data.SqlClient.SqlConnection) -Scope Global -Option AllScope -Desc "Personal variable for Sql Query functions"
# Initially create the SqlConnection
Set-SqlConnection $Server $Database
# go ahead and run the initial query if we have one...
if( $query -gt $null ) {
Get-SqlDataTable $Query
}
# Some aliases to let you use the functions with less typing
Set-Alias gsdt Get-SqlDataTable -Option AllScope -Description "Personal Function alias from Get-Sql.ps1"
Set-Alias ssc Set-SqlConnection -Option AllScope -Description "Personal Function alias from Get-Sql.ps1"
Now if only I could get it working that well in SciTE!
- By Joel 'Jaykul' Bennett
- Tagged as:
Add New Comment
Viewing 2 Comments
Thanks. Your comment is awaiting approval by a moderator.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Add New Comment
Trackbacks