nirvdrum-jekyll 0.5.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +7 -0
- data/History.txt +143 -0
- data/README.textile +42 -0
- data/Rakefile +92 -0
- data/VERSION.yml +4 -0
- data/bin/jekyll +150 -0
- data/features/create_sites.feature +60 -0
- data/features/embed_filters.feature +60 -0
- data/features/pagination.feature +40 -0
- data/features/permalinks.feature +63 -0
- data/features/post_data.feature +153 -0
- data/features/site_configuration.feature +63 -0
- data/features/site_data.feature +82 -0
- data/features/step_definitions/jekyll_steps.rb +136 -0
- data/features/support/env.rb +16 -0
- data/jekyll.gemspec +134 -0
- data/lib/jekyll.rb +86 -0
- data/lib/jekyll/albino.rb +122 -0
- data/lib/jekyll/converters/csv.rb +26 -0
- data/lib/jekyll/converters/mephisto.rb +79 -0
- data/lib/jekyll/converters/mt.rb +59 -0
- data/lib/jekyll/converters/textpattern.rb +50 -0
- data/lib/jekyll/converters/typo.rb +49 -0
- data/lib/jekyll/converters/wordpress.rb +54 -0
- data/lib/jekyll/convertible.rb +89 -0
- data/lib/jekyll/core_ext.rb +30 -0
- data/lib/jekyll/filters.rb +47 -0
- data/lib/jekyll/layout.rb +36 -0
- data/lib/jekyll/page.rb +112 -0
- data/lib/jekyll/pager.rb +45 -0
- data/lib/jekyll/post.rb +251 -0
- data/lib/jekyll/site.rb +295 -0
- data/lib/jekyll/stylesheet.rb +88 -0
- data/lib/jekyll/tags/highlight.rb +56 -0
- data/lib/jekyll/tags/include.rb +31 -0
- data/test/helper.rb +27 -0
- data/test/source/_includes/sig.markdown +3 -0
- data/test/source/_layouts/default.html +27 -0
- data/test/source/_layouts/simple.html +1 -0
- data/test/source/_posts/2008-02-02-not-published.textile +8 -0
- data/test/source/_posts/2008-02-02-published.textile +8 -0
- data/test/source/_posts/2008-10-18-foo-bar.textile +8 -0
- data/test/source/_posts/2008-11-21-complex.textile +8 -0
- data/test/source/_posts/2008-12-03-permalinked-post.textile +9 -0
- data/test/source/_posts/2008-12-13-include.markdown +8 -0
- data/test/source/_posts/2009-01-27-array-categories.textile +10 -0
- data/test/source/_posts/2009-01-27-categories.textile +7 -0
- data/test/source/_posts/2009-01-27-category.textile +7 -0
- data/test/source/_posts/2009-03-12-hash-#1.markdown +6 -0
- data/test/source/_posts/2009-05-18-tag.textile +6 -0
- data/test/source/_posts/2009-05-18-tags.textile +9 -0
- data/test/source/_posts/2009-06-22-empty-yaml.textile +3 -0
- data/test/source/_posts/2009-06-22-no-yaml.textile +1 -0
- data/test/source/_stylesheets/nested/override.less +1 -0
- data/test/source/_stylesheets/simple.less +3 -0
- data/test/source/about.html +6 -0
- data/test/source/category/_posts/2008-9-23-categories.textile +6 -0
- data/test/source/contacts.html +5 -0
- data/test/source/css/screen.css +76 -0
- data/test/source/foo/_posts/bar/2008-12-12-topical-post.textile +8 -0
- data/test/source/index.html +22 -0
- data/test/source/win/_posts/2009-05-24-yaml-linebreak.markdown +7 -0
- data/test/source/z_category/_posts/2008-9-23-categories.textile +6 -0
- data/test/suite.rb +9 -0
- data/test/test_configuration.rb +29 -0
- data/test/test_filters.rb +49 -0
- data/test/test_generated_site.rb +53 -0
- data/test/test_page.rb +87 -0
- data/test/test_pager.rb +47 -0
- data/test/test_post.rb +302 -0
- data/test/test_site.rb +85 -0
- data/test/test_stylesheet.rb +67 -0
- data/test/test_tags.rb +116 -0
- metadata +196 -0
@@ -0,0 +1,136 @@
|
|
1
|
+
Before do
|
2
|
+
FileUtils.mkdir(TEST_DIR)
|
3
|
+
Dir.chdir(TEST_DIR)
|
4
|
+
end
|
5
|
+
|
6
|
+
After do
|
7
|
+
Dir.chdir(TEST_DIR)
|
8
|
+
FileUtils.rm_rf(TEST_DIR)
|
9
|
+
end
|
10
|
+
|
11
|
+
Given /^I have a blank site in "(.*)"$/ do |path|
|
12
|
+
FileUtils.mkdir(path)
|
13
|
+
end
|
14
|
+
|
15
|
+
# Like "I have a foo file" but gives a yaml front matter so jekyll actually processes it
|
16
|
+
Given /^I have an "(.*)" page(?: with (.*) "(.*)")? that contains "(.*)"$/ do |file, key, value, text|
|
17
|
+
File.open(file, 'w') do |f|
|
18
|
+
f.write <<EOF
|
19
|
+
---
|
20
|
+
#{key || 'layout'}: #{value || 'nil'}
|
21
|
+
---
|
22
|
+
#{text}
|
23
|
+
EOF
|
24
|
+
f.close
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
Given /^I have an "(.*)" file that contains "(.*)"$/ do |file, text|
|
29
|
+
File.open(file, 'w') do |f|
|
30
|
+
f.write(text)
|
31
|
+
f.close
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
Given /^I have a (.*) layout that contains "(.*)"$/ do |layout, text|
|
36
|
+
File.open(File.join('_layouts', layout + '.html'), 'w') do |f|
|
37
|
+
f.write(text)
|
38
|
+
f.close
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
Given /^I have a (.*) directory$/ do |dir|
|
43
|
+
FileUtils.mkdir(dir)
|
44
|
+
end
|
45
|
+
|
46
|
+
Given /^I have the following posts?(?: (.*) "(.*)")?:$/ do |direction, folder, table|
|
47
|
+
table.hashes.each do |post|
|
48
|
+
date = Date.parse(post['date']).strftime('%Y-%m-%d')
|
49
|
+
title = post['title'].downcase.gsub(/[^\w]/, " ").strip.gsub(/\s+/, '-')
|
50
|
+
|
51
|
+
if direction && direction == "in"
|
52
|
+
before = folder || '.'
|
53
|
+
elsif direction && direction == "under"
|
54
|
+
after = folder || '.'
|
55
|
+
end
|
56
|
+
|
57
|
+
path = File.join(before || '.', '_posts', after || '.', "#{date}-#{title}.#{post['type'] || 'textile'}")
|
58
|
+
|
59
|
+
matter_hash = {}
|
60
|
+
%w(title layout tag tags category categories published author).each do |key|
|
61
|
+
matter_hash[key] = post[key] if post[key]
|
62
|
+
end
|
63
|
+
matter = matter_hash.map { |k, v| "#{k}: #{v}\n" }.join.chomp
|
64
|
+
|
65
|
+
content = post['content']
|
66
|
+
if post['input'] && post['filter']
|
67
|
+
content = "{{ #{post['input']} | #{post['filter']} }}"
|
68
|
+
end
|
69
|
+
|
70
|
+
File.open(path, 'w') do |f|
|
71
|
+
f.write <<EOF
|
72
|
+
---
|
73
|
+
#{matter}
|
74
|
+
---
|
75
|
+
#{content}
|
76
|
+
EOF
|
77
|
+
f.close
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
Given /^I have a configuration file with "(.*)" set to "(.*)"$/ do |key, value|
|
83
|
+
File.open('_config.yml', 'w') do |f|
|
84
|
+
f.write("#{key}: #{value}")
|
85
|
+
f.close
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
Given /^I have a configuration file with "([^\"]*)" set to:$/ do |key, table|
|
90
|
+
File.open('_config.yml', 'w') do |f|
|
91
|
+
f.write("#{key}:\n")
|
92
|
+
table.hashes.each do |row|
|
93
|
+
f.write("- #{row["Value"]}\n")
|
94
|
+
end
|
95
|
+
f.close
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
|
100
|
+
When /^I run jekyll$/ do
|
101
|
+
run_jekyll
|
102
|
+
end
|
103
|
+
|
104
|
+
When /^I debug jekyll$/ do
|
105
|
+
run_jekyll(:debug => true)
|
106
|
+
end
|
107
|
+
|
108
|
+
When /^I change "(.*)" to contain "(.*)"$/ do |file, text|
|
109
|
+
File.open(file, 'a') do |f|
|
110
|
+
f.write(text)
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
Then /^the (.*) directory should exist$/ do |dir|
|
115
|
+
assert File.directory?(dir)
|
116
|
+
end
|
117
|
+
|
118
|
+
Then /^the (.*) file should exist$/ do |file|
|
119
|
+
assert File.file?(file)
|
120
|
+
end
|
121
|
+
|
122
|
+
Then /^I should see "(.*)" in "(.*)"$/ do |text, file|
|
123
|
+
assert_match Regexp.new(Regexp.escape(text)), File.open(file).readlines.join
|
124
|
+
end
|
125
|
+
|
126
|
+
Then /^the "(.*)" file should not exist$/ do |file|
|
127
|
+
assert !File.exists?(file)
|
128
|
+
end
|
129
|
+
|
130
|
+
Then /^I should see today's time in "(.*)"$/ do |file|
|
131
|
+
assert_match Regexp.new(Regexp.escape(Time.now.to_s)), File.open(file).readlines.join
|
132
|
+
end
|
133
|
+
|
134
|
+
Then /^I should see today's date in "(.*)"$/ do |file|
|
135
|
+
assert_match Regexp.new(Date.today.to_s), File.open(file).readlines.join
|
136
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
require 'rr'
|
3
|
+
require 'test/unit'
|
4
|
+
|
5
|
+
World do
|
6
|
+
include Test::Unit::Assertions
|
7
|
+
end
|
8
|
+
|
9
|
+
TEST_DIR = File.join('/', 'tmp', 'jekyll')
|
10
|
+
JEKYLL_PATH = File.join(ENV['PWD'], 'bin', 'jekyll')
|
11
|
+
|
12
|
+
def run_jekyll(opts = {})
|
13
|
+
command = JEKYLL_PATH
|
14
|
+
command << " >> /dev/null 2>&1" if opts[:debug].nil?
|
15
|
+
system command
|
16
|
+
end
|
data/jekyll.gemspec
ADDED
@@ -0,0 +1,134 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{jekyll}
|
5
|
+
s.version = "0.5.2"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Tom Preston-Werner"]
|
9
|
+
s.date = %q{2009-06-24}
|
10
|
+
s.default_executable = %q{jekyll}
|
11
|
+
s.description = %q{Jekyll is a simple, blog aware, static site generator.}
|
12
|
+
s.email = %q{tom@mojombo.com}
|
13
|
+
s.executables = ["jekyll"]
|
14
|
+
s.extra_rdoc_files = [
|
15
|
+
"README.textile"
|
16
|
+
]
|
17
|
+
s.files = [
|
18
|
+
".gitignore",
|
19
|
+
"History.txt",
|
20
|
+
"README.textile",
|
21
|
+
"Rakefile",
|
22
|
+
"VERSION.yml",
|
23
|
+
"bin/jekyll",
|
24
|
+
"features/create_sites.feature",
|
25
|
+
"features/embed_filters.feature",
|
26
|
+
"features/pagination.feature",
|
27
|
+
"features/permalinks.feature",
|
28
|
+
"features/post_data.feature",
|
29
|
+
"features/site_configuration.feature",
|
30
|
+
"features/site_data.feature",
|
31
|
+
"features/step_definitions/jekyll_steps.rb",
|
32
|
+
"features/support/env.rb",
|
33
|
+
"jekyll.gemspec",
|
34
|
+
"lib/jekyll.rb",
|
35
|
+
"lib/jekyll/albino.rb",
|
36
|
+
"lib/jekyll/converters/csv.rb",
|
37
|
+
"lib/jekyll/converters/mephisto.rb",
|
38
|
+
"lib/jekyll/converters/mt.rb",
|
39
|
+
"lib/jekyll/converters/textpattern.rb",
|
40
|
+
"lib/jekyll/converters/typo.rb",
|
41
|
+
"lib/jekyll/converters/wordpress.rb",
|
42
|
+
"lib/jekyll/convertible.rb",
|
43
|
+
"lib/jekyll/core_ext.rb",
|
44
|
+
"lib/jekyll/filters.rb",
|
45
|
+
"lib/jekyll/layout.rb",
|
46
|
+
"lib/jekyll/page.rb",
|
47
|
+
"lib/jekyll/pager.rb",
|
48
|
+
"lib/jekyll/post.rb",
|
49
|
+
"lib/jekyll/site.rb",
|
50
|
+
"lib/jekyll/tags/highlight.rb",
|
51
|
+
"lib/jekyll/tags/include.rb",
|
52
|
+
"test/helper.rb",
|
53
|
+
"test/source/_includes/sig.markdown",
|
54
|
+
"test/source/_layouts/default.html",
|
55
|
+
"test/source/_layouts/simple.html",
|
56
|
+
"test/source/_posts/2008-02-02-not-published.textile",
|
57
|
+
"test/source/_posts/2008-02-02-published.textile",
|
58
|
+
"test/source/_posts/2008-10-18-foo-bar.textile",
|
59
|
+
"test/source/_posts/2008-11-21-complex.textile",
|
60
|
+
"test/source/_posts/2008-12-03-permalinked-post.textile",
|
61
|
+
"test/source/_posts/2008-12-13-include.markdown",
|
62
|
+
"test/source/_posts/2009-01-27-array-categories.textile",
|
63
|
+
"test/source/_posts/2009-01-27-categories.textile",
|
64
|
+
"test/source/_posts/2009-01-27-category.textile",
|
65
|
+
"test/source/_posts/2009-03-12-hash-#1.markdown",
|
66
|
+
"test/source/_posts/2009-05-18-tag.textile",
|
67
|
+
"test/source/_posts/2009-05-18-tags.textile",
|
68
|
+
"test/source/_posts/2009-06-22-empty-yaml.textile",
|
69
|
+
"test/source/_posts/2009-06-22-no-yaml.textile",
|
70
|
+
"test/source/about.html",
|
71
|
+
"test/source/category/_posts/2008-9-23-categories.textile",
|
72
|
+
"test/source/contacts.html",
|
73
|
+
"test/source/css/screen.css",
|
74
|
+
"test/source/foo/_posts/bar/2008-12-12-topical-post.textile",
|
75
|
+
"test/source/index.html",
|
76
|
+
"test/source/win/_posts/2009-05-24-yaml-linebreak.markdown",
|
77
|
+
"test/source/z_category/_posts/2008-9-23-categories.textile",
|
78
|
+
"test/suite.rb",
|
79
|
+
"test/test_configuration.rb",
|
80
|
+
"test/test_filters.rb",
|
81
|
+
"test/test_generated_site.rb",
|
82
|
+
"test/test_page.rb",
|
83
|
+
"test/test_pager.rb",
|
84
|
+
"test/test_post.rb",
|
85
|
+
"test/test_site.rb",
|
86
|
+
"test/test_tags.rb"
|
87
|
+
]
|
88
|
+
s.homepage = %q{http://github.com/mojombo/jekyll}
|
89
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
90
|
+
s.require_paths = ["lib"]
|
91
|
+
s.rubyforge_project = %q{jekyll}
|
92
|
+
s.rubygems_version = %q{1.3.4}
|
93
|
+
s.summary = %q{Jekyll is a simple, blog aware, static site generator.}
|
94
|
+
s.test_files = [
|
95
|
+
"test/helper.rb",
|
96
|
+
"test/suite.rb",
|
97
|
+
"test/test_configuration.rb",
|
98
|
+
"test/test_filters.rb",
|
99
|
+
"test/test_generated_site.rb",
|
100
|
+
"test/test_page.rb",
|
101
|
+
"test/test_pager.rb",
|
102
|
+
"test/test_post.rb",
|
103
|
+
"test/test_site.rb",
|
104
|
+
"test/test_tags.rb"
|
105
|
+
]
|
106
|
+
|
107
|
+
if s.respond_to? :specification_version then
|
108
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
109
|
+
s.specification_version = 3
|
110
|
+
|
111
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
112
|
+
s.add_runtime_dependency(%q<RedCloth>, [">= 4.2.1"])
|
113
|
+
s.add_runtime_dependency(%q<liquid>, [">= 1.9.0"])
|
114
|
+
s.add_runtime_dependency(%q<classifier>, [">= 1.3.1"])
|
115
|
+
s.add_runtime_dependency(%q<maruku>, [">= 0.5.9"])
|
116
|
+
s.add_runtime_dependency(%q<directory_watcher>, [">= 1.1.1"])
|
117
|
+
s.add_runtime_dependency(%q<open4>, [">= 0.9.6"])
|
118
|
+
else
|
119
|
+
s.add_dependency(%q<RedCloth>, [">= 4.2.1"])
|
120
|
+
s.add_dependency(%q<liquid>, [">= 1.9.0"])
|
121
|
+
s.add_dependency(%q<classifier>, [">= 1.3.1"])
|
122
|
+
s.add_dependency(%q<maruku>, [">= 0.5.9"])
|
123
|
+
s.add_dependency(%q<directory_watcher>, [">= 1.1.1"])
|
124
|
+
s.add_dependency(%q<open4>, [">= 0.9.6"])
|
125
|
+
end
|
126
|
+
else
|
127
|
+
s.add_dependency(%q<RedCloth>, [">= 4.2.1"])
|
128
|
+
s.add_dependency(%q<liquid>, [">= 1.9.0"])
|
129
|
+
s.add_dependency(%q<classifier>, [">= 1.3.1"])
|
130
|
+
s.add_dependency(%q<maruku>, [">= 0.5.9"])
|
131
|
+
s.add_dependency(%q<directory_watcher>, [">= 1.1.1"])
|
132
|
+
s.add_dependency(%q<open4>, [">= 0.9.6"])
|
133
|
+
end
|
134
|
+
end
|
data/lib/jekyll.rb
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
$:.unshift File.dirname(__FILE__) # For use/testing when no gem is installed
|
2
|
+
|
3
|
+
# rubygems
|
4
|
+
require 'rubygems'
|
5
|
+
|
6
|
+
# core
|
7
|
+
require 'fileutils'
|
8
|
+
require 'time'
|
9
|
+
require 'yaml'
|
10
|
+
|
11
|
+
# stdlib
|
12
|
+
|
13
|
+
# 3rd party
|
14
|
+
require 'liquid'
|
15
|
+
require 'redcloth'
|
16
|
+
|
17
|
+
# internal requires
|
18
|
+
require 'jekyll/core_ext'
|
19
|
+
require 'jekyll/pager'
|
20
|
+
require 'jekyll/site'
|
21
|
+
require 'jekyll/convertible'
|
22
|
+
require 'jekyll/layout'
|
23
|
+
require 'jekyll/stylesheet'
|
24
|
+
require 'jekyll/page'
|
25
|
+
require 'jekyll/post'
|
26
|
+
require 'jekyll/filters'
|
27
|
+
require 'jekyll/tags/highlight'
|
28
|
+
require 'jekyll/tags/include'
|
29
|
+
require 'jekyll/albino'
|
30
|
+
|
31
|
+
module Jekyll
|
32
|
+
# Default options. Overriden by values in _config.yml or command-line opts.
|
33
|
+
# (Strings rather symbols used for compatability with YAML)
|
34
|
+
DEFAULTS = {
|
35
|
+
'auto' => false,
|
36
|
+
'server' => false,
|
37
|
+
'server_port' => 4000,
|
38
|
+
|
39
|
+
'source' => '.',
|
40
|
+
'destination' => File.join('.', '_site'),
|
41
|
+
|
42
|
+
'lsi' => false,
|
43
|
+
'pygments' => false,
|
44
|
+
'markdown' => 'maruku',
|
45
|
+
'permalink' => 'date',
|
46
|
+
|
47
|
+
'maruku' => {
|
48
|
+
'use_tex' => false,
|
49
|
+
'use_divs' => false,
|
50
|
+
'png_engine' => 'blahtex',
|
51
|
+
'png_dir' => 'images/latex',
|
52
|
+
'png_url' => '/images/latex'
|
53
|
+
}
|
54
|
+
}
|
55
|
+
|
56
|
+
# Generate a Jekyll configuration Hash by merging the default options
|
57
|
+
# with anything in _config.yml, and adding the given options on top
|
58
|
+
# +override+ is a Hash of config directives
|
59
|
+
#
|
60
|
+
# Returns Hash
|
61
|
+
def self.configuration(override)
|
62
|
+
# _config.yml may override default source location, but until
|
63
|
+
# then, we need to know where to look for _config.yml
|
64
|
+
source = override['source'] || Jekyll::DEFAULTS['source']
|
65
|
+
|
66
|
+
# Get configuration from <source>/_config.yml
|
67
|
+
config_file = File.join(source, '_config.yml')
|
68
|
+
begin
|
69
|
+
config = YAML.load_file(config_file)
|
70
|
+
raise "Invalid configuration - #{config_file}" if !config.is_a?(Hash)
|
71
|
+
STDOUT.puts "Configuration from #{config_file}"
|
72
|
+
rescue => err
|
73
|
+
STDERR.puts "WARNING: Could not read configuration. Using defaults (and options)."
|
74
|
+
STDERR.puts "\t" + err.to_s
|
75
|
+
config = {}
|
76
|
+
end
|
77
|
+
|
78
|
+
# Merge DEFAULTS < _config.yml < override
|
79
|
+
Jekyll::DEFAULTS.deep_merge(config).deep_merge(override)
|
80
|
+
end
|
81
|
+
|
82
|
+
def self.version
|
83
|
+
yml = YAML.load(File.read(File.join(File.dirname(__FILE__), *%w[.. VERSION.yml])))
|
84
|
+
"#{yml[:major]}.#{yml[:minor]}.#{yml[:patch]}"
|
85
|
+
end
|
86
|
+
end
|
@@ -0,0 +1,122 @@
|
|
1
|
+
##
|
2
|
+
# Wrapper for the Pygments command line tool, pygmentize.
|
3
|
+
#
|
4
|
+
# Pygments: http://pygments.org/
|
5
|
+
#
|
6
|
+
# Assumes pygmentize is in the path. If not, set its location
|
7
|
+
# with Albino.bin = '/path/to/pygmentize'
|
8
|
+
#
|
9
|
+
# Use like so:
|
10
|
+
#
|
11
|
+
# @syntaxer = Albino.new('/some/file.rb', :ruby)
|
12
|
+
# puts @syntaxer.colorize
|
13
|
+
#
|
14
|
+
# This'll print out an HTMLized, Ruby-highlighted version
|
15
|
+
# of '/some/file.rb'.
|
16
|
+
#
|
17
|
+
# To use another formatter, pass it as the third argument:
|
18
|
+
#
|
19
|
+
# @syntaxer = Albino.new('/some/file.rb', :ruby, :bbcode)
|
20
|
+
# puts @syntaxer.colorize
|
21
|
+
#
|
22
|
+
# You can also use the #colorize class method:
|
23
|
+
#
|
24
|
+
# puts Albino.colorize('/some/file.rb', :ruby)
|
25
|
+
#
|
26
|
+
# Another also: you get a #to_s, for somewhat nicer use in Rails views.
|
27
|
+
#
|
28
|
+
# ... helper file ...
|
29
|
+
# def highlight(text)
|
30
|
+
# Albino.new(text, :ruby)
|
31
|
+
# end
|
32
|
+
#
|
33
|
+
# ... view file ...
|
34
|
+
# <%= highlight text %>
|
35
|
+
#
|
36
|
+
# The default lexer is 'text'. You need to specify a lexer yourself;
|
37
|
+
# because we are using STDIN there is no auto-detect.
|
38
|
+
#
|
39
|
+
# To see all lexers and formatters available, run `pygmentize -L`.
|
40
|
+
#
|
41
|
+
# Chris Wanstrath // chris@ozmm.org
|
42
|
+
# GitHub // http://github.com
|
43
|
+
#
|
44
|
+
require 'open4'
|
45
|
+
|
46
|
+
class Albino
|
47
|
+
@@bin = Rails.development? ? 'pygmentize' : '/usr/bin/pygmentize' rescue 'pygmentize'
|
48
|
+
|
49
|
+
def self.bin=(path)
|
50
|
+
@@bin = path
|
51
|
+
end
|
52
|
+
|
53
|
+
def self.colorize(*args)
|
54
|
+
new(*args).colorize
|
55
|
+
end
|
56
|
+
|
57
|
+
def initialize(target, lexer = :text, format = :html)
|
58
|
+
@target = File.exists?(target) ? File.read(target) : target rescue target
|
59
|
+
@options = { :l => lexer, :f => format, :O => 'encoding=utf-8' }
|
60
|
+
end
|
61
|
+
|
62
|
+
def execute(command)
|
63
|
+
output = ''
|
64
|
+
Open4.popen4(command) do |pid, stdin, stdout, stderr|
|
65
|
+
stdin.puts @target
|
66
|
+
stdin.close
|
67
|
+
output = stdout.read.strip
|
68
|
+
[stdout, stderr].each { |io| io.close }
|
69
|
+
end
|
70
|
+
output
|
71
|
+
end
|
72
|
+
|
73
|
+
def colorize(options = {})
|
74
|
+
html = execute(@@bin + convert_options(options))
|
75
|
+
# Work around an RDiscount bug: http://gist.github.com/97682
|
76
|
+
html.to_s.sub(%r{</pre></div>\Z}, "</pre>\n</div>")
|
77
|
+
end
|
78
|
+
alias_method :to_s, :colorize
|
79
|
+
|
80
|
+
def convert_options(options = {})
|
81
|
+
@options.merge(options).inject('') do |string, (flag, value)|
|
82
|
+
string + " -#{flag} #{value}"
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
if $0 == __FILE__
|
88
|
+
require 'rubygems'
|
89
|
+
require 'test/spec'
|
90
|
+
require 'mocha'
|
91
|
+
begin require 'redgreen'; rescue LoadError; end
|
92
|
+
|
93
|
+
context "Albino" do
|
94
|
+
setup do
|
95
|
+
@syntaxer = Albino.new(__FILE__, :ruby)
|
96
|
+
end
|
97
|
+
|
98
|
+
specify "defaults to text" do
|
99
|
+
syntaxer = Albino.new(__FILE__)
|
100
|
+
syntaxer.expects(:execute).with('pygmentize -f html -l text').returns(true)
|
101
|
+
syntaxer.colorize
|
102
|
+
end
|
103
|
+
|
104
|
+
specify "accepts options" do
|
105
|
+
@syntaxer.expects(:execute).with('pygmentize -f html -l ruby').returns(true)
|
106
|
+
@syntaxer.colorize
|
107
|
+
end
|
108
|
+
|
109
|
+
specify "works with strings" do
|
110
|
+
syntaxer = Albino.new('class New; end', :ruby)
|
111
|
+
assert_match %r(highlight), syntaxer.colorize
|
112
|
+
end
|
113
|
+
|
114
|
+
specify "aliases to_s" do
|
115
|
+
assert_equal @syntaxer.colorize, @syntaxer.to_s
|
116
|
+
end
|
117
|
+
|
118
|
+
specify "class method colorize" do
|
119
|
+
assert_equal @syntaxer.colorize, Albino.colorize(__FILE__, :ruby)
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|