Skip to content

Commit

Permalink
add a diff lexer
Browse files Browse the repository at this point in the history
  • Loading branch information
jneen committed Sep 3, 2012
1 parent 03c9d26 commit 87f2370
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/rouge.rb
Expand Up @@ -16,6 +16,7 @@ def highlight(text, lexer, formatter)
load load_dir.join('rouge/token.rb')
load load_dir.join('rouge/lexer.rb')
load load_dir.join('rouge/lexers/text.rb')
load load_dir.join('rouge/lexers/diff.rb')
load load_dir.join('rouge/lexers/shell.rb')
load load_dir.join('rouge/lexers/javascript.rb')

Expand Down
27 changes: 27 additions & 0 deletions lib/rouge/lexers/diff.rb
@@ -0,0 +1,27 @@
module Rouge
module Lexers
class Diff < RegexLexer
tag 'diff'
aliases 'patch'

state :header do
rule /^diff .*?\n(?=---|\+\+\+)/m, 'Generic.Heading'
rule /^--- .*?\n/, 'Generic.Deleted'
rule /^\+\+\+ .*?\n/, 'Generic.Inserted'
end

state :diff do
rule /@@ -\d+,\d+ \+\d+,\d+ @@.*?\n/, 'Generic.Heading'
rule /^\+.*?\n/, 'Generic.Inserted'
rule /^-.*?\n/, 'Generic.Deleted'
rule /^ .*?\n/, 'Text'
rule /^.*?\n/, 'Error'
end

state :root do
mixin :header
mixin :diff
end
end
end
end
55 changes: 55 additions & 0 deletions spec/visual/samples/diff
@@ -0,0 +1,55 @@
diff --git a/README.md b/README.md
index 87f10f4..09ba8c0 100644
--- a/README.md
+++ b/README.md
@@ -1,10 +1,23 @@
# Rouge

-This project is not yet finished, but this works:
+This project needs your help! There are lots of lexers to be implemented / ported / fixed, and features that need to be added or implemented. If you'd like to help out, send me a pull request (even if it's not done yet!). Bonus points for feature branches.
+
+## Usage

``` ruby
-formatter = Rouge::Formatters::HTML.new
+# make some nice lexed html, compatible with pygments stylesheets
+formatter = Rouge::Formatters::HTML.new(:css_class => '.highlight')
Rouge.highlight(File.read('/etc/bash.bashrc'), 'shell', formatter)
+
+# apply a theme
+Rouge::Themes::ThankfulEyes.new(:scope => '.highlight').render
```

-More features, documentation, lexers, and formatters to come. Help is appreciated, too, if you think this is awesome :)
+Rouge aims to be simple to extend, and to be a drop-in replacement pygments, with the same quality of output.
+
+### Advantages to pygments.rb
+* No python bridge is necessary - you can deploy it on Heroku effortlessly, without the need for [epic hacks][].
+
+### Advantages to CodeRay
+
+[epic hacks]: https://github.com/rumblelabs/pygments-heroku
diff --git a/lib/rouge.rb b/lib/rouge.rb
index e86da00..c947a8b 100644
--- a/lib/rouge.rb
+++ b/lib/rouge.rb
@@ -16,6 +16,7 @@ load_dir = Pathname.new(__FILE__).dirname
load load_dir.join('rouge/token.rb')
load load_dir.join('rouge/lexer.rb')
load load_dir.join('rouge/lexers/text.rb')
+load load_dir.join('rouge/lexers/diff.rb')
load load_dir.join('rouge/lexers/shell.rb')
load load_dir.join('rouge/lexers/javascript.rb')

diff --git a/lib/rouge/token.rb b/lib/rouge/token.rb
index ab1701b..155fa52 100644
--- a/lib/rouge/token.rb
+++ b/lib/rouge/token.rb
@@ -112,6 +112,7 @@ module Rouge
token 'Keyword.Type', 'kt'

token 'Name', 'n'
+
token 'Name.Attribute', 'na'
token 'Name.Builtin', 'nb'
token 'Name.Builtin.Pseudo', 'bp'

0 comments on commit 87f2370

Please sign in to comment.