wbzyl-codehighlighter-middleware 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (36) hide show
  1. data/.gitignore +4 -0
  2. data/LICENSE +0 -0
  3. data/README.markdown +290 -0
  4. data/Rakefile +39 -0
  5. data/TODO +22 -0
  6. data/VERSION.yml +4 -0
  7. data/codehighlighter-middleware.gemspec +74 -0
  8. data/examples/app.rb +15 -0
  9. data/examples/config.ru +14 -0
  10. data/examples/public/javascripts/lang-css.js +2 -0
  11. data/examples/public/javascripts/lang-hs.js +2 -0
  12. data/examples/public/javascripts/lang-lisp.js +3 -0
  13. data/examples/public/javascripts/lang-lua.js +2 -0
  14. data/examples/public/javascripts/lang-ml.js +2 -0
  15. data/examples/public/javascripts/lang-proto.js +1 -0
  16. data/examples/public/javascripts/lang-sql.js +2 -0
  17. data/examples/public/javascripts/lang-vb.js +2 -0
  18. data/examples/public/javascripts/lang-wiki.js +1 -0
  19. data/examples/public/javascripts/prettify.js +31 -0
  20. data/examples/public/stylesheets/application.css +29 -0
  21. data/examples/public/stylesheets/coderay.css +126 -0
  22. data/examples/public/stylesheets/prettify.css +44 -0
  23. data/examples/public/stylesheets/syntax.css +79 -0
  24. data/examples/public/stylesheets/uv/amy.css +147 -0
  25. data/examples/public/stylesheets/uv/blackboard.css +88 -0
  26. data/examples/public/stylesheets/uv/cobalt.css +149 -0
  27. data/examples/public/stylesheets/uv/dawn.css +121 -0
  28. data/examples/public/stylesheets/uv/espresso_libre.css +109 -0
  29. data/examples/public/stylesheets/uv/sunburst.css +180 -0
  30. data/examples/public/stylesheets/uv/twilight.css +137 -0
  31. data/examples/public/stylesheets/uv/zenburnesque.css +91 -0
  32. data/examples/public/stylesheets/uv.css +1022 -0
  33. data/examples/views/index.rdiscount +75 -0
  34. data/examples/views/layout.rdiscount +36 -0
  35. data/lib/codehighlighter-middleware.rb +99 -0
  36. metadata +97 -0
@@ -0,0 +1,75 @@
1
+ ### C
2
+
3
+ :::c
4
+ int a, b;
5
+ int main(int argc, char *argv[]) {
6
+ hello();
7
+ return 0;
8
+ }
9
+ void hello(void) {
10
+ printf("hello world\n");
11
+ }
12
+
13
+ ### html
14
+
15
+ :::html
16
+ <html>
17
+ <head>
18
+ <title>hello world</title>
19
+ </head>
20
+ <body>
21
+ <h1>hello world</h1>
22
+ </body>
23
+ </html>
24
+
25
+ ### ruby
26
+
27
+ :::ruby
28
+ def hello
29
+ puts 'hello world'
30
+ end
31
+
32
+ ### css21
33
+
34
+ :::css
35
+ html {
36
+ margin: 0;
37
+ padding: 0;
38
+ background-color: #0C7D85;
39
+ line-height: 1.6;
40
+ }
41
+ body {
42
+ margin: 1em auto 1em auto;
43
+ padding: 1em 2em 2em 1em;
44
+ width: 760px;
45
+ border: 1px solid black;
46
+ background-color: #E8DDCB;
47
+ }
48
+
49
+ ### sqlite
50
+
51
+ :::sql
52
+ drop table if exists exams;
53
+ create table exams (
54
+ id integer primary key autoincrement,
55
+ student_id integer not null, -- foreign key
56
+ result integer default 0 collate nocase,
57
+ when datetime
58
+ );
59
+
60
+ ### javascript
61
+
62
+ :::javascript
63
+ for (var i = 0, length = properties.length; i < length; i++) {
64
+ var property = properties[i], value = source[property];
65
+ if (ancestor && Object.isFunction(value) &&
66
+ value.argumentNames().first() == "$super") {
67
+ var method = value, value = Object.extend((function(m) {
68
+ return function() { return ancestor[m].apply(this, arguments) };
69
+ })(property).wrap(method), {
70
+ valueOf: function() { return method },
71
+ toString: function() { return method.toString() }
72
+ });
73
+ }
74
+ this.prototype[property] = value;
75
+ }
@@ -0,0 +1,36 @@
1
+ <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" http://www.w3.org/TR/html4/strict.dtd">
2
+ <html>
3
+ <head>
4
+ <meta http-equiv="Content-type" content="text/html; charset=utf-8">
5
+ <link rel="stylesheet" href="/stylesheets/application.css"
6
+ type="text/css" media="screen" charset="utf-8">
7
+ <link rel="stylesheet" href="/stylesheets/syntax.css"
8
+ type="text/css" media="screen" charset="utf-8">
9
+ <link rel="stylesheet" href="/stylesheets/coderay.css"
10
+ type="text/css" media="screen" charset="utf-8">
11
+ <link rel="stylesheet" href="/stylesheets/uv.css"
12
+ type="text/css" media="screen" charset="utf-8">
13
+
14
+ <link rel="stylesheet" href="/stylesheets/prettify.css"
15
+ type="text/css" media="screen" charset="utf-8"/>
16
+
17
+ <script src="/javascripts/prettify.js"
18
+ type="text/javascript" charset="utf-8"></script>
19
+
20
+ <script src="http://www.google.com/jsapi"></script>
21
+ <script>
22
+ // on page load complete, fire off a prettyPrint
23
+ google.setOnLoadCallback(function() {
24
+ prettyPrint();
25
+ });
26
+ </script>
27
+
28
+ <title><%= "Code Highlighting Middleware" %></title>
29
+ </head>
30
+
31
+ <body>
32
+
33
+ <%= yield %>
34
+
35
+ </body>
36
+ </html>
@@ -0,0 +1,99 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require 'rack'
4
+ require 'hpricot'
5
+
6
+ module Rack
7
+ class Codehighlighter
8
+ def initialize(app, highlighter = :syntax, opts = {})
9
+ @app = app
10
+ @highlighter = highlighter
11
+ @opts = opts
12
+ end
13
+ def call(env)
14
+ status, headers, response = @app.call(env)
15
+ if headers['Content-Type'] != nil && headers['Content-Type'].include?("text/html")
16
+ content = ""
17
+ response.each { |part| content += part }
18
+ doc = Hpricot(content)
19
+ nodes = doc.search("//pre/code")
20
+ nodes.each do |node|
21
+ s = node.inner_html || "[++where is the code?++]"
22
+ node.parent.swap(send(@highlighter, s))
23
+ end
24
+ STDERR.puts "Highlighting code with: #{@highlighter}"
25
+ body = doc.to_html
26
+ size = body.respond_to?(:bytesize) ? body.bytesize : body.size
27
+ headers['Content-Length'] = size.to_s
28
+ [status, headers, [body]]
29
+ else
30
+ [status, headers, response]
31
+ end
32
+ end
33
+
34
+ private
35
+
36
+ def syntax(string)
37
+ translate = {
38
+ 'html' => 'xml',
39
+ 'c' => 'ansic',
40
+ 'css' => 'css21',
41
+ 'sql' => 'sqlite'
42
+ }
43
+ lang = "unknown"
44
+ if /\A:::(\w+)\s*\n/ =~ string # extract language name
45
+ lang = $1
46
+ convertor = ::Syntax::Convertors::HTML.for_syntax translate[lang]
47
+ convertor.convert(unescape_html(string.sub(/\A.*\n/, "")) || "[=this can'n happen=]")
48
+ else
49
+ "<pre>#{string}</pre>"
50
+ end
51
+ end
52
+
53
+ def coderay(string)
54
+ lang = "unknown"
55
+ if /\A:::(\w+)\s*\n/ =~ string # extract language name
56
+ lang = $1
57
+ str = unescape_html(string.sub(/\A.*\n/, ""))
58
+ "<pre class='CodeRay'>#{::CodeRay.encoder(:html).encode str, lang}</pre>"
59
+ else
60
+ "<pre class='CodeRay'>#{string}</pre>"
61
+ end
62
+ end
63
+
64
+ def prettify(string)
65
+ translate = {
66
+ 'ruby' => 'rb',
67
+ 'bash' => 'bsh',
68
+ 'javascript' => 'js',
69
+ 'python' => 'py'
70
+ }
71
+ lang = "unknown"
72
+ if /\A:::(\w+)\s*\n/ =~ string # extract language name
73
+ lang = $1
74
+ str = string.sub(/\A.*\n/, "")
75
+ "<pre class='prettyprint lang-#{translate[lang] || lang}'>#{str}</pre>"
76
+ else
77
+ "<pre>#{string}</pre>"
78
+ end
79
+ end
80
+
81
+ def ultraviolet(string)
82
+ opts = { :theme => 'espresso_libre', :lines => false }
83
+ opts.merge! @opts
84
+ lang = 'text'
85
+ if /\A:::(\w+)\s*\n/ =~ string # extract language name
86
+ lang = $1
87
+ str = unescape_html(string.sub(/\A.*\n/, ""))
88
+ "<pre class='#{opts[:theme]}'>#{::Uv.parse(str, 'xhtml', lang, opts[:lines], opts[:theme])}</pre>"
89
+ else
90
+ "<pre class='#{opts[:theme]}'>#{string}</pre>"
91
+ end
92
+ end
93
+
94
+ def unescape_html(string)
95
+ string.gsub("&lt;", '<').gsub("&gt;", '>').gsub("&amp;", '&')
96
+ end
97
+
98
+ end
99
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wbzyl-codehighlighter-middleware
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.7
5
+ platform: ruby
6
+ authors:
7
+ - Wlodek Bzyl
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-05-22 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: hpricot
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 0.8.1
24
+ version:
25
+ description: Rack Middleware for Code Highlighting.
26
+ email: matwb@univ.gda.pl
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - LICENSE
33
+ - README.markdown
34
+ files:
35
+ - .gitignore
36
+ - LICENSE
37
+ - README.markdown
38
+ - Rakefile
39
+ - TODO
40
+ - VERSION.yml
41
+ - codehighlighter-middleware.gemspec
42
+ - examples/app.rb
43
+ - examples/config.ru
44
+ - examples/public/javascripts/lang-css.js
45
+ - examples/public/javascripts/lang-hs.js
46
+ - examples/public/javascripts/lang-lisp.js
47
+ - examples/public/javascripts/lang-lua.js
48
+ - examples/public/javascripts/lang-ml.js
49
+ - examples/public/javascripts/lang-proto.js
50
+ - examples/public/javascripts/lang-sql.js
51
+ - examples/public/javascripts/lang-vb.js
52
+ - examples/public/javascripts/lang-wiki.js
53
+ - examples/public/javascripts/prettify.js
54
+ - examples/public/stylesheets/application.css
55
+ - examples/public/stylesheets/coderay.css
56
+ - examples/public/stylesheets/prettify.css
57
+ - examples/public/stylesheets/syntax.css
58
+ - examples/public/stylesheets/uv.css
59
+ - examples/public/stylesheets/uv/amy.css
60
+ - examples/public/stylesheets/uv/blackboard.css
61
+ - examples/public/stylesheets/uv/cobalt.css
62
+ - examples/public/stylesheets/uv/dawn.css
63
+ - examples/public/stylesheets/uv/espresso_libre.css
64
+ - examples/public/stylesheets/uv/sunburst.css
65
+ - examples/public/stylesheets/uv/twilight.css
66
+ - examples/public/stylesheets/uv/zenburnesque.css
67
+ - examples/views/index.rdiscount
68
+ - examples/views/layout.rdiscount
69
+ - lib/codehighlighter-middleware.rb
70
+ has_rdoc: false
71
+ homepage: http://github.com/wbzyl/codehighlighter-middleware
72
+ post_install_message:
73
+ rdoc_options:
74
+ - --charset=UTF-8
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: "0"
82
+ version:
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: "0"
88
+ version:
89
+ requirements: []
90
+
91
+ rubyforge_project:
92
+ rubygems_version: 1.2.0
93
+ signing_key:
94
+ specification_version: 3
95
+ summary: Rack Middleware for Code Highlighting.
96
+ test_files:
97
+ - examples/app.rb