diff options
author | gentoo90 <gentoo90@gmail.com> | 2013-04-19 21:50:38 +0300 |
---|---|---|
committer | gentoo90 <gentoo90@gmail.com> | 2013-04-19 21:50:38 +0300 |
commit | d0166615eef115dbea294d9f9442e0f78334167a (patch) | |
tree | 683713b07423e332b0a4979031e96f09a106cb3c | |
parent | 7d58dcb21438f9231dd9cbf58cca50141ef59214 (diff) | |
download | pygments-d0166615eef115dbea294d9f9442e0f78334167a.tar.gz |
PowerShellLexer: more fixes
* '$(...)' blocks inside double quoted strings and here-strings now highlighted as code
* Here-strings can't be nested directly. Only inside '$(...)' block
* Add new example with here-strings
* Add 'throw' keyword
* Remove redundant code
-rw-r--r-- | pygments/lexers/shell.py | 27 | ||||
-rw-r--r-- | tests/examplefiles/Get-CommandDefinitionHtml.ps1 | 66 |
2 files changed, 76 insertions, 17 deletions
diff --git a/pygments/lexers/shell.py b/pygments/lexers/shell.py index bc084b4c..d117fd86 100644 --- a/pygments/lexers/shell.py +++ b/pygments/lexers/shell.py @@ -342,7 +342,7 @@ class PowerShellLexer(RegexLexer): 'dynamicparam do default continue cmdletbinding break begin alias \\? ' '% #script #private #local #global mandatory parametersetname position ' 'valuefrompipeline valuefrompipelinebypropertyname ' - 'valuefromremainingarguments helpmessage try catch').split() + 'valuefromremainingarguments helpmessage try catch throw').split() operators = ( 'and as band bnot bor bxor casesensitive ccontains ceq cge cgt cle ' @@ -368,13 +368,17 @@ class PowerShellLexer(RegexLexer): tokens = { 'root': [ + # we need to count pairs of parentheses for correct highlight + # of '$(...)' blocks in strings + (r'\(', Punctuation, '#push'), + (r'\)', Punctuation, '#pop'), (r'\s+', Text), (r'^(\s*#[#\s]*)(\.(?:%s))([^\n]*$)' % '|'.join(commenthelp), bygroups(Comment, String.Doc, Comment)), (r'#[^\n]*?$', Comment), (r'(<|<)#', Comment.Multiline, 'multline'), (r'@"\n', String.Heredoc, 'heredoc-double'), - (r"@'\n", String.Heredoc, 'heredoc-single'), + (r"@'\n.*?\n'@", String.Heredoc), # escaped syntax (r'`[\'"$@-]', Punctuation), (r'"', String.Double, 'string'), @@ -387,7 +391,7 @@ class PowerShellLexer(RegexLexer): (r'\[[a-z_\[][a-z0-9_. `,\[\]]*\]', Name.Constant), # .net [type]s (r'-[a-z_][a-z0-9_]*', Name), (r'\w+', Name), - (r'([.,;@{}\[\]$()=+*/\\&%!~?^`|<>-]|::)', Punctuation), + (r'[.,;@{}\[\]$()=+*/\\&%!~?^`|<>-]|::', Punctuation), ], 'multline': [ (r'[^#&.]+', Comment.Multiline), @@ -398,26 +402,15 @@ class PowerShellLexer(RegexLexer): 'string': [ (r"`[0abfnrtv'\"\$]", String.Escape), (r'[^$`"]+', String.Double), - (r'\$\(', String.Interpol, 'interpol'), + (r'\$\(', Punctuation, 'root'), (r'""', String.Double), (r'[`$]', String.Double), (r'"', String.Double, '#pop'), ], 'heredoc-double': [ - (r'@"\n', String.Heredoc, '#push'), - (r"@'\n", String.Heredoc, 'heredoc-single'), (r'\n"@', String.Heredoc, '#pop'), + (r'\$\(', Punctuation, 'root'), + (r'[^@\n]+"]', String.Heredoc), (r".", String.Heredoc), - ], - 'heredoc-single': [ - (r'@"\n', String.Heredoc, 'heredoc-double'), - (r"@'\n", String.Heredoc, '#push'), - (r"\n'@", String.Heredoc, '#pop'), - (r".", String.Heredoc), - ], - 'interpol': [ - (r'[^$)]+', String.Interpol), - (r'\$\(', String.Interpol, '#push'), - (r'\)', String.Interpol, '#pop'), ] } diff --git a/tests/examplefiles/Get-CommandDefinitionHtml.ps1 b/tests/examplefiles/Get-CommandDefinitionHtml.ps1 new file mode 100644 index 00000000..b181955f --- /dev/null +++ b/tests/examplefiles/Get-CommandDefinitionHtml.ps1 @@ -0,0 +1,66 @@ +
+function Get-CommandDefinitionHtml {
+
+ # this tells powershell to allow advanced features,
+ # like the [validatenotnullorempty()] attribute below.
+ [CmdletBinding()]
+ param(
+ [ValidateNotNullOrEmpty()]
+ [string]$name
+ )
+
+ $command = get-command $name
+
+ # Look mom! I'm a cmdlet!
+ $PSCmdlet.WriteVerbose("Dumping HTML for " + $command)
+
+@"
+ <html>
+ <head>
+ <title>$($command.name)</title>
+ </head>
+ <body>
+ <table border="1">
+$(
+ $command.parametersets | % {
+@"
+
+ <tr>
+ <td>$($_.name)</td>
+ <td>
+ <table border="1">
+ <tr>
+ <th colspan="8">Parameters</th>
+
+$(
+ $count = 0
+ $_.parameters | % {
+ if (0 -eq ($count % 8)) {
+@'
+ </tr>
+ <tr>
+'@
+ }
+@"
+ <td>$($_.name)</td>
+"@
+ $count++
+ }
+)
+ </tr>
+ </table>
+ </td>
+ </tr>
+"@
+ }
+)
+ </table>
+ </body>
+ </html>
+"@
+}
+
+Get-CommandDefinitionHtml get-item > out.html
+
+# show in browser
+invoke-item out.html
|