jackb 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +2 -0
- data/Gemfile +13 -0
- data/README.md +57 -0
- data/Rakefile +34 -0
- data/VERSION +1 -0
- data/lib/jack.rb +13 -0
- data/lib/jack/highlight.rb +33 -0
- data/lib/jack/html.rb +19 -0
- data/lib/jack/markdown.rb +17 -0
- data/spec/lib/highlight_spec.rb +36 -0
- data/spec/lib/html_spec.rb +23 -0
- data/spec/lib/jack_spec.rb +8 -0
- data/spec/lib/markdown_spec.rb +14 -0
- data/spec/spec_helper.rb +1 -0
- metadata +84 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
# Jack
|
2
|
+
|
3
|
+
In the loving memory of Jack Bauer. Wherever you are now, if you need help hiding, you can count on us !
|
4
|
+
|
5
|
+
## What is it ?
|
6
|
+
|
7
|
+
When you're displaying content from a database, after some time, you might end up with some contents in HTML, other in markdown and maybe even worst ...
|
8
|
+
Jack takes your content and only the format it should be in and returns you the proper output.
|
9
|
+
|
10
|
+
The formats supported are based on my own needs. So for now, it's only HTML and Markdown.
|
11
|
+
Feel free to fork the project and add new formats. I'd be glad to merge it.
|
12
|
+
|
13
|
+
## Basic Usage
|
14
|
+
|
15
|
+
require 'jack'
|
16
|
+
@content = Jack.render "my page content in HTML", "html"
|
17
|
+
@content = Jack.render "my page content in Markdown", "markdown"
|
18
|
+
|
19
|
+
That's all !
|
20
|
+
|
21
|
+
|
22
|
+
## Syntax Highlighting
|
23
|
+
|
24
|
+
We use [albino](http://github.com/github/albino) (and so [pygments](http://pygments.org/)) to do some syntax highlighting on the code in the page.
|
25
|
+
You can specify the language in which the code is.
|
26
|
+
|
27
|
+
In HTML :
|
28
|
+
|
29
|
+
[code lang="ruby"]This is ruby code[/code]
|
30
|
+
[code language="html"]This is HTML code[/code]
|
31
|
+
|
32
|
+
|
33
|
+
In Markdown :
|
34
|
+
|
35
|
+
I'm still trying to figure out the best way to specify the language with the markdown code syntax.
|
36
|
+
The idea would be something like this format :
|
37
|
+
|
38
|
+
"ruby"
|
39
|
+
This is ruby code
|
40
|
+
|
41
|
+
## Contributing
|
42
|
+
|
43
|
+
Once you've made your great commits:
|
44
|
+
|
45
|
+
1. [Fork][0] Jack
|
46
|
+
2. Create a topic branch - `git checkout -b my_branch`
|
47
|
+
3. Push to your branch - `git push origin my_branch`
|
48
|
+
4. Create an [Issue][1] with a link to your branch
|
49
|
+
5. That's it!
|
50
|
+
|
51
|
+
|
52
|
+
## Author
|
53
|
+
|
54
|
+
Damien MATHIEU :: 42@dmathieu.com :: @dmathieu
|
55
|
+
|
56
|
+
[0]: http://help.github.com/forking/
|
57
|
+
[1]: http://github.com/dmathieu/jack/issues
|
data/Rakefile
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
begin
|
2
|
+
require File.expand_path('../.bundle/environment', __FILE__)
|
3
|
+
rescue LoadError
|
4
|
+
require "rubygems"
|
5
|
+
require "bundler"
|
6
|
+
Bundler.setup
|
7
|
+
end
|
8
|
+
require 'spec/rake/spectask'
|
9
|
+
|
10
|
+
#
|
11
|
+
# The rspec tasks
|
12
|
+
#
|
13
|
+
task :default => :spec
|
14
|
+
|
15
|
+
desc "Run all specs"
|
16
|
+
Spec::Rake::SpecTask.new('spec') do |t|
|
17
|
+
t.spec_files = FileList['spec/**/*.rb']
|
18
|
+
t.spec_opts = ['-cfs']
|
19
|
+
end
|
20
|
+
|
21
|
+
begin
|
22
|
+
require 'jeweler'
|
23
|
+
|
24
|
+
Jeweler::Tasks.new do |gemspec|
|
25
|
+
gemspec.name = "jackb"
|
26
|
+
gemspec.summary = "Is your content in HTML or Markdown ?"
|
27
|
+
gemspec.description = "Takes some content and parses it depending of the format your specify (HTML or Markdown)"
|
28
|
+
gemspec.email = "42@dmathieu.com"
|
29
|
+
gemspec.homepage = "http://github.com/dmathieu/jack"
|
30
|
+
gemspec.authors = ["Damien MATHIEU"]
|
31
|
+
end
|
32
|
+
rescue LoadError
|
33
|
+
puts "Jeweler not available. Install it with: gem install jeweler"
|
34
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
data/lib/jack.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'albino'
|
2
|
+
|
3
|
+
module Jack
|
4
|
+
|
5
|
+
class Highlight
|
6
|
+
def self.render(content)
|
7
|
+
self.new.render(content)
|
8
|
+
end
|
9
|
+
|
10
|
+
def render(content)
|
11
|
+
content.gsub(highlight_regex) do |s|
|
12
|
+
Albino.new(unescape($2), extract_lang($1))
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
def unescape(content)
|
18
|
+
CGI::unescapeHTML content
|
19
|
+
end
|
20
|
+
|
21
|
+
def extract_lang(attributes)
|
22
|
+
result = :ruby
|
23
|
+
attributes.gsub(/(language|lang)=('|")([0-z]+)('|")/i) do |s|
|
24
|
+
result = $3.to_sym
|
25
|
+
end
|
26
|
+
result
|
27
|
+
end
|
28
|
+
|
29
|
+
def highlight_regex
|
30
|
+
/<pre><code([^<>]*)>(.*?)<\/code><\/pre>/im
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
data/lib/jack/html.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
module Jack
|
2
|
+
class Html < Jack::Highlight
|
3
|
+
attr_reader :content
|
4
|
+
|
5
|
+
def initialize(content)
|
6
|
+
@content = content
|
7
|
+
end
|
8
|
+
|
9
|
+
|
10
|
+
def render
|
11
|
+
super(@content)
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
def highlight_regex
|
16
|
+
/\[code([^\[\]]*)\](.*?)\[\/code\]/im
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'rdiscount'
|
2
|
+
|
3
|
+
module Jack
|
4
|
+
class Markdown < Jack::Highlight
|
5
|
+
attr_reader :content, :formatted_content
|
6
|
+
|
7
|
+
def initialize(content)
|
8
|
+
@content = content
|
9
|
+
@formatted_content = RDiscount.new(content)
|
10
|
+
end
|
11
|
+
|
12
|
+
|
13
|
+
def render
|
14
|
+
super(formatted_content.to_html)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Jack::Highlight do
|
4
|
+
describe 'code highlighting' do
|
5
|
+
it 'should highlight the code' do
|
6
|
+
Jack::Highlight.render('<pre><code><%= "test" %></code></pre>').should eql(Albino.new('<%= "test" %>', :ruby).to_s)
|
7
|
+
end
|
8
|
+
|
9
|
+
it 'should highlight the code in an other language' do
|
10
|
+
Jack::Highlight.render('<pre><code language="python"><%= "test" %></code></pre>').should eql(Albino.new('<%= "test" %>', :python).to_s)
|
11
|
+
end
|
12
|
+
|
13
|
+
describe 'unescape' do
|
14
|
+
it 'should unescape the <' do
|
15
|
+
Jack::Highlight.new.send(:unescape, 'hey<').should eql('hey<')
|
16
|
+
end
|
17
|
+
it 'should unescape the >' do
|
18
|
+
Jack::Highlight.new.send(:unescape, 'hey>').should eql('hey>')
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe 'extract_lang' do
|
23
|
+
it 'should extract a language' do
|
24
|
+
Jack::Highlight.new.send(:extract_lang, ' language="python"').should eql(:python)
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'should extract a language with short lang attribute' do
|
28
|
+
Jack::Highlight.new.send(:extract_lang, ' lang="python"').should eql(:python)
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'should get the default language' do
|
32
|
+
Jack::Highlight.new.send(:extract_lang, '').should eql(:ruby)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Jack::Html do
|
4
|
+
|
5
|
+
it 'should render the content from html to html' do
|
6
|
+
format = Jack::Html.new('My <strong>test</strong>')
|
7
|
+
format.render.should eql('My <strong>test</strong>')
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'should highlight the code' do
|
11
|
+
Jack::Html.new('[code language="ruby"]<%= "test" %>[/code]').render.should eql(Albino.new('<%= "test" %>', :ruby).to_s)
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'should highlight multiple codes' do
|
15
|
+
Jack::Html.new('Some Text. [code language="ruby"]<%= "test" %>[/code]; some other text. [code language="python"]<%= "testing" %>[/code]').
|
16
|
+
render.should eql("Some Text. #{Albino.new('<%= "test" %>', :ruby).to_s}; some other text. #{Albino.new('<%= "testing" %>', :python).to_s}")
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'should highlight multiple codes with new lines' do
|
20
|
+
Jack::Html.new("Some Text. [code language='ruby']<%= 'test' %>\n\n<%= 'second' %>[/code]; some other text. [code language='python']<%= 'testing' %>[/code]").
|
21
|
+
render.should eql("Some Text. #{Albino.new("<%= 'test' %>\n\n<%= 'second' %>", :ruby).to_s}; some other text. #{Albino.new("<%= 'testing' %>", :python).to_s}")
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Jack::Html do
|
4
|
+
|
5
|
+
it 'should render the content from markdown to html' do
|
6
|
+
format = Jack::Markdown.new('My **test**')
|
7
|
+
format.render.should eql("<p>My <strong>test</strong></p>\n")
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'should highlight the code' do
|
11
|
+
Jack::Markdown.new("Hey!\n\n <%= \"test\" %>").render.
|
12
|
+
should eql("<p>Hey!</p>\n\n#{Albino.new('<%= "test" %>', :ruby).to_s}\n")
|
13
|
+
end
|
14
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'lib/jack'
|
metadata
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jackb
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Damien MATHIEU
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-06-11 00:00:00 +02:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: Takes some content and parses it depending of the format your specify (HTML or Markdown)
|
23
|
+
email: 42@dmathieu.com
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files:
|
29
|
+
- README.md
|
30
|
+
files:
|
31
|
+
- .gitignore
|
32
|
+
- Gemfile
|
33
|
+
- README.md
|
34
|
+
- Rakefile
|
35
|
+
- VERSION
|
36
|
+
- lib/jack.rb
|
37
|
+
- lib/jack/highlight.rb
|
38
|
+
- lib/jack/html.rb
|
39
|
+
- lib/jack/markdown.rb
|
40
|
+
- spec/lib/highlight_spec.rb
|
41
|
+
- spec/lib/html_spec.rb
|
42
|
+
- spec/lib/jack_spec.rb
|
43
|
+
- spec/lib/markdown_spec.rb
|
44
|
+
- spec/spec_helper.rb
|
45
|
+
has_rdoc: true
|
46
|
+
homepage: http://github.com/dmathieu/jack
|
47
|
+
licenses: []
|
48
|
+
|
49
|
+
post_install_message:
|
50
|
+
rdoc_options:
|
51
|
+
- --charset=UTF-8
|
52
|
+
require_paths:
|
53
|
+
- lib
|
54
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
hash: 3
|
60
|
+
segments:
|
61
|
+
- 0
|
62
|
+
version: "0"
|
63
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
64
|
+
none: false
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
hash: 3
|
69
|
+
segments:
|
70
|
+
- 0
|
71
|
+
version: "0"
|
72
|
+
requirements: []
|
73
|
+
|
74
|
+
rubyforge_project:
|
75
|
+
rubygems_version: 1.3.7
|
76
|
+
signing_key:
|
77
|
+
specification_version: 3
|
78
|
+
summary: Is your content in HTML or Markdown ?
|
79
|
+
test_files:
|
80
|
+
- spec/lib/markdown_spec.rb
|
81
|
+
- spec/lib/html_spec.rb
|
82
|
+
- spec/lib/highlight_spec.rb
|
83
|
+
- spec/lib/jack_spec.rb
|
84
|
+
- spec/spec_helper.rb
|