augustl-redclothcoderay 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README ADDED
@@ -0,0 +1,64 @@
1
+ == Redcloth with CodeRay
2
+
3
+ Adds CodeRay syntax highlighting support to RedCloth, with a 'source' tag. See the examples below.
4
+
5
+ A short summary of what you can do:
6
+
7
+ * <source>foo</source> - Use this tag to produce CodeRay highlighted HTML for the contents within that tag. The language defaults to Ruby.
8
+ * <source:css>foo</source> - Highlight as usual, but highlight as CSS. Supports everything CodeRay supports. Refer to the CodeRay documentation for a list of supported languages.
9
+
10
+ == Installing
11
+
12
+ Installation as usual:
13
+
14
+ sudo gem install redclothcoderay
15
+
16
+
17
+ == Using
18
+
19
+ You have to specify that you want to use refs_syntax_highlighter when calling RedCloth#to_html.
20
+
21
+ require 'rubygems'
22
+ require 'redcloth'
23
+ require 'coderay'
24
+ require 'redclothcoderay'
25
+
26
+ RedCloth.new('I am *bold* and <source>@hi_tech</source>').to_html(:textile, :refs_syntax_highlighter)
27
+
28
+ You can also use the always_on method to always use the refs_syntax_highlighter parser when calling to_html
29
+
30
+ RedclothCoderay.always_on
31
+
32
+ You can specify the CodeRay options, too (defaults to RedclothCoderay::CODERAY_OPTIONS).
33
+
34
+ RedclothCoderay.coderay_options :line_numbers => :table
35
+
36
+ == In Ruby on Rails
37
+
38
+ In an initializer (config/initializers/[anything].rb) or at the bottom of environment.rb, add this line:
39
+
40
+ Redclothcoderay.always_on
41
+
42
+ == Example
43
+
44
+ This input:
45
+
46
+ Hello, this is *textilized*. It also has <source>@inline_code_examples</source>!
47
+
48
+ What about a multi-line code sample?
49
+
50
+ <source:html>
51
+ <h1>Hello, world!</h1>
52
+ </source>
53
+
54
+ produces this output (indented for clarity):
55
+
56
+ <p>Hello, this is <strong>textilized</strong>. It also has <code class="inline_code">
57
+ <span class="iv">@inline_code_examples</span>
58
+ </code>!</p>
59
+
60
+ <p>What about a multi-line code sample?</p>
61
+
62
+ <pre><code class=\"multiline_code\">
63
+ <span class=\"ta\">&amp;lt;h1&amp;gt;</span>Hello, world!<span class=\"ta\">&amp;lt;/h1&amp;gt;</span>
64
+ </code></pre>
@@ -0,0 +1,9 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+
4
+ task :default => :test
5
+
6
+ task :test do
7
+ $LOAD_PATH << File.join(File.dirname(__FILE__), 'test')
8
+ load "test/redclothcoderay_test.rb"
9
+ end
@@ -0,0 +1,44 @@
1
+ module RedclothCoderay
2
+ SINGLE_LINE = '<code class="inline_code">%s</code>'
3
+ MULTI_LINE = '<pre><code class="multiline_code">%s</code></pre>'
4
+ SOURCE_TAG_REGEXP = /(([\t\n])?<source(?:\:([a-z]+))?>(.+?)<\/source>[\t\n]?)/m
5
+ CODERAY_OPTIONS = {:wrap => nil, :css => :class}
6
+
7
+ # The RedCloth extension that performs the syntax highlighting.
8
+ def refs_syntax_highlighter(text)
9
+ text.gsub!(SOURCE_TAG_REGEXP) do |m|
10
+ all_of_it = $~[1]
11
+ whitespace_before = $~[2]
12
+ lang = ($~[3] || :ruby).to_sym
13
+ code = $~[4].strip
14
+
15
+ wrap_in = all_of_it =~ /\n/ ? MULTI_LINE : SINGLE_LINE
16
+ highlighted = wrap_in % CodeRay.scan(code, lang).div(CODERAY_OPTIONS)
17
+
18
+ "#{whitespace_before}<notextile>#{highlighted}</notextile>"
19
+ end
20
+ end
21
+
22
+ # Adds the syntax highlighter to all RedCloth#to_htmls, so that you don't have to
23
+ # do that to_html(:textile, :refs_syntax_highlighter) thin.
24
+ def self.always_on
25
+ if RedCloth::VERSION.to_s < "4"
26
+ RedCloth::DEFAULT_RULES << :refs_syntax_highlighter
27
+ else
28
+ RedCloth::TextileDoc.class_eval {
29
+ alias :_to_html :to_html
30
+
31
+ def to_html(*rules)
32
+ rules << :refs_syntax_highlighter
33
+ _to_html(*rules)
34
+ end
35
+ }
36
+ end
37
+ end
38
+
39
+ def self.coderay_options(options)
40
+ CODERAY_OPTIONS.replace(options)
41
+ end
42
+ end
43
+
44
+ RedCloth.class_eval { include RedclothCoderay }
@@ -0,0 +1,24 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "redclothcoderay"
3
+ s.version = "0.2.0"
4
+ s.date = "2009-04-23"
5
+ s.authors = ["August Lilleaas"]
6
+ s.email = "augustlilleaas@gmail.com"
7
+ s.rubyforge_project = "redclothcoderay"
8
+ s.has_rdoc = true
9
+ s.add_dependency('RedCloth')
10
+ s.add_dependency('coderay')
11
+ s.summary = "Integrates CodeRay with RedCloth by adding a <source> tag."
12
+ s.homepage = "http://redclothcoderay.rubyforge.org"
13
+ s.extensions = ["Rakefile"]
14
+ s.files =
15
+ ["redclothcoderay.gemspec",
16
+ "README",
17
+ "Rakefile",
18
+ "lib/redclothcoderay.rb",
19
+ "test/redclothcoderay_test.rb",
20
+ "test/always_on_3_test.rb",
21
+ "test/always_on_4_test.rb",
22
+ "test/coderay_options_test.rb",
23
+ "test/test_helper.rb"]
24
+ end
@@ -0,0 +1,14 @@
1
+ require 'test_helper'
2
+ gem 'RedCloth', "< 4.0.0"
3
+ require 'RedCloth'
4
+ require 'redclothcoderay'
5
+
6
+ # Is in isolation, because it meddles with states and shit. CBA to mock that.
7
+ class AlwaysOn3Test < Test::Unit::TestCase
8
+ def test_always_on
9
+ RedclothCoderay.always_on
10
+ result = RedCloth.new(%{I thar. <source>inline_code</source> and *textile*!}).to_html
11
+ assert result.include?("<strong>textile</strong>")
12
+ assert result.include?('<code class="inline_code">')
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ require 'test_helper'
2
+ gem 'RedCloth', ">= 4.0.0"
3
+ require 'RedCloth'
4
+ require 'redclothcoderay'
5
+
6
+ # Is in isolation, because it meddles with states and shit. CBA to mock that.
7
+ class AlwaysOn3Test < Test::Unit::TestCase
8
+ def test_always_on
9
+ RedclothCoderay.always_on
10
+ result = RedCloth.new(%{I thar. <source>inline_code</source> and *textile*!}).to_html
11
+ assert result.include?("<strong>textile</strong>")
12
+ assert result.include?('<code class="inline_code">')
13
+ end
14
+ end
@@ -0,0 +1,11 @@
1
+ require 'test_helper'
2
+ require 'RedCloth'
3
+ require 'redclothcoderay'
4
+
5
+ class CoderayOptionsTest < Test::Unit::TestCase
6
+ def test_it
7
+ RedclothCoderay.coderay_options :line_numbers => :table
8
+ assert_equal :table, RedclothCoderay::CODERAY_OPTIONS[:line_numbers]
9
+ assert RedCloth.new("<source>ugly_inline :stuff</source>").to_html(:refs_syntax_highlighter).include?(%{<table class="CodeRay"})
10
+ end
11
+ end
@@ -0,0 +1,56 @@
1
+ require 'test_helper'
2
+ require 'redcloth'
3
+ require 'redclothcoderay'
4
+
5
+ class PluginTest < Test::Unit::TestCase
6
+ def test_parsing_inline_code
7
+ text = "Hello, this is *textilized*. It also has <source>@inline_code_examples</source>!"
8
+ result = RedCloth.new(text).to_html(:textile, :refs_syntax_highlighter)
9
+
10
+ assert_equal(%Q{<p>Hello, this is <strong>textilized</strong>. It also has <code class="inline_code"><span class="iv">@inline_code_examples</span></code>!</p>}, result)
11
+ end
12
+
13
+ def test_parsing_multiline_code
14
+ text = %Q{Hello, this is *textilized*.
15
+ <source>
16
+ def hello_world
17
+ puts "Allright"
18
+ end
19
+ </source>}
20
+ result = RedCloth.new(text).to_html(:textile, :refs_syntax_highlighter)
21
+
22
+ assert result.include?(%Q{<strong>textilized</strong>})
23
+ assert result.include?(%Q{<pre><code class="multiline_code">})
24
+ end
25
+
26
+ def test_one_liner_multiline_code
27
+ text = %Q{Hello, this is *textilized*.
28
+
29
+ <source>
30
+ hello_to_you
31
+ </source>
32
+ }
33
+ result = RedCloth.new(text).to_html(:textile, :refs_syntax_highlighter)
34
+
35
+ assert result.include?(%Q{<strong>textilized</strong>})
36
+ assert result.include?(%Q{<pre><code class=\"multiline_code\">hello_to_you</code></pre>})
37
+ end
38
+
39
+ def test_parsing_other_languages
40
+ text = %Q{Hello, this is *textilized* with some <source:html><p>HTML code</p></source>!}
41
+ result = RedCloth.new(text).to_html(:textile, :refs_syntax_highlighter)
42
+
43
+ assert result.include?(%Q{<strong>textilized</strong>})
44
+ assert result.include?(%Q{<code class="inline_code"><span class="ta">&lt;p&gt;</span>HTML code<span class="ta">&lt;/p&gt;</span></code>})
45
+ end
46
+
47
+ def test_after_a_header
48
+ text = %Q{h2. Hello, world.
49
+
50
+ <source>
51
+ hello_world
52
+ </source>}
53
+ result = RedCloth.new(text).to_html(:textile, :refs_syntax_highlighter)
54
+ assert result.include?(%Q{<h2>Hello, world.</h2>})
55
+ end
56
+ end
@@ -0,0 +1,6 @@
1
+ require 'rubygems'
2
+ require 'coderay'
3
+ require 'test/unit'
4
+
5
+ path = File.join(File.dirname(__FILE__), '..', 'lib')
6
+ $LOAD_PATH << path unless $LOAD_PATH.include?(path)
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: augustl-redclothcoderay
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - August Lilleaas
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-04-23 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: RedCloth
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: coderay
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
35
+ description:
36
+ email: augustlilleaas@gmail.com
37
+ executables: []
38
+
39
+ extensions:
40
+ - Rakefile
41
+ extra_rdoc_files: []
42
+
43
+ files:
44
+ - redclothcoderay.gemspec
45
+ - README
46
+ - Rakefile
47
+ - lib/redclothcoderay.rb
48
+ - test/redclothcoderay_test.rb
49
+ - test/always_on_3_test.rb
50
+ - test/always_on_4_test.rb
51
+ - test/coderay_options_test.rb
52
+ - test/test_helper.rb
53
+ has_rdoc: true
54
+ homepage: http://redclothcoderay.rubyforge.org
55
+ post_install_message:
56
+ rdoc_options: []
57
+
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: "0"
65
+ version:
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: "0"
71
+ version:
72
+ requirements: []
73
+
74
+ rubyforge_project: redclothcoderay
75
+ rubygems_version: 1.2.0
76
+ signing_key:
77
+ specification_version: 2
78
+ summary: Integrates CodeRay with RedCloth by adding a <source> tag.
79
+ test_files: []
80
+