almost-happy 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/almost-happy.gemspec +2 -1
- data/lib/almost_happy/active_record_mixin.rb +1 -1
- data/lib/almost_happy/convertor.rb +7 -1
- data/lib/almost_happy.rb +1 -1
- data/lib/vendor/albino.rb +98 -0
- metadata +3 -2
data/almost-happy.gemspec
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{almost-happy}
|
8
|
-
s.version = "0.1.
|
8
|
+
s.version = "0.1.1"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Darcy Laycock"]
|
@@ -31,6 +31,7 @@ Gem::Specification.new do |s|
|
|
31
31
|
"lib/almost_happy/format_validator.rb",
|
32
32
|
"lib/almost_happy/publishable.rb",
|
33
33
|
"lib/almost_happy/scopeable.rb",
|
34
|
+
"lib/vendor/albino.rb",
|
34
35
|
"test/helper.rb",
|
35
36
|
"test/test_almost-happy.rb"
|
36
37
|
]
|
@@ -11,7 +11,7 @@ module AlmostHappy
|
|
11
11
|
self.convertable_fields += fields
|
12
12
|
# Define accessors for each field.
|
13
13
|
fields.each do |field|
|
14
|
-
define_method(:"#{field}_as_html") { send(field).to_s.html_safe }
|
14
|
+
define_method(:"#{field}_as_html") { send(:"rendered_#{field}").to_s.html_safe }
|
15
15
|
end
|
16
16
|
validate_convertable_format if options.fetch(:validate, true)
|
17
17
|
end
|
@@ -1,3 +1,9 @@
|
|
1
|
+
require 'nokogiri'
|
2
|
+
require 'RedCloth'
|
3
|
+
require 'rdiscount'
|
4
|
+
|
5
|
+
require File.expand_path('../vendor/albino', File.dirname(__FILE__))
|
6
|
+
|
1
7
|
module AlmostHappy
|
2
8
|
class Convertor
|
3
9
|
|
@@ -79,7 +85,7 @@ module AlmostHappy
|
|
79
85
|
end
|
80
86
|
|
81
87
|
add_renderer :markdown, :filters => [:smart, :autolink] do |raw|
|
82
|
-
html = RDiscount.new(raw, *
|
88
|
+
html = RDiscount.new(raw, *Convertor[:markdown].fetch(:filters, [])).to_html
|
83
89
|
html.gsub!(/^(?:<p>)?@@@(?:<\/p>)?$/, '</div>')
|
84
90
|
html.gsub!(/^(?:<p>)?@@@\s*([\w\+]+)(?:<\/p>)?$/, '<div class="code" rel="\1">')
|
85
91
|
Albino.highlight_code(html)
|
data/lib/almost_happy.rb
CHANGED
@@ -0,0 +1,98 @@
|
|
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.env.development? ? 'pygmentize' : '/usr/bin/pygmentize'
|
48
|
+
|
49
|
+
def self.highlight_code(html)
|
50
|
+
doc = Nokogiri::HTML(html)
|
51
|
+
doc.search('div.code').each do |div|
|
52
|
+
lexer = div['rel'] || :ruby
|
53
|
+
lexted_text = Albino.new(div.text, lexer).to_s
|
54
|
+
highlighted = Nokogiri::HTML(lexted_text).at('div')
|
55
|
+
klasses = highlighted['class'].split(/\s+/)
|
56
|
+
klasses << lexer
|
57
|
+
klasses << 'code'
|
58
|
+
klasses << 'highlight'
|
59
|
+
highlighted['class'] = klasses.join(' ')
|
60
|
+
div.replace highlighted
|
61
|
+
end
|
62
|
+
doc.search('body > *').to_html
|
63
|
+
end
|
64
|
+
|
65
|
+
def self.bin=(path)
|
66
|
+
@@bin = path
|
67
|
+
end
|
68
|
+
|
69
|
+
def self.colorize(*args)
|
70
|
+
new(*args).colorize
|
71
|
+
end
|
72
|
+
|
73
|
+
def initialize(target, lexer = :text, format = :html)
|
74
|
+
@target = File.exists?(target) ? File.read(target) : target rescue target
|
75
|
+
@options = { :l => lexer, :f => format }
|
76
|
+
end
|
77
|
+
|
78
|
+
def execute(command)
|
79
|
+
tmp = Tempfile.new("albino")
|
80
|
+
tmp.write @target
|
81
|
+
tmp.close
|
82
|
+
command = [command, tmp.path].join(" ")
|
83
|
+
%x(#{command}).strip
|
84
|
+
ensure
|
85
|
+
tmp.unlink if tmp
|
86
|
+
end
|
87
|
+
|
88
|
+
def colorize(options = {})
|
89
|
+
execute @@bin + convert_options(options)
|
90
|
+
end
|
91
|
+
alias_method :to_s, :colorize
|
92
|
+
|
93
|
+
def convert_options(options = {})
|
94
|
+
@options.merge(options).inject('') do |string, (flag, value)|
|
95
|
+
string + " -#{flag} #{value}"
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 1
|
8
|
-
-
|
9
|
-
version: 0.1.
|
8
|
+
- 1
|
9
|
+
version: 0.1.1
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Darcy Laycock
|
@@ -104,6 +104,7 @@ files:
|
|
104
104
|
- lib/almost_happy/format_validator.rb
|
105
105
|
- lib/almost_happy/publishable.rb
|
106
106
|
- lib/almost_happy/scopeable.rb
|
107
|
+
- lib/vendor/albino.rb
|
107
108
|
- test/helper.rb
|
108
109
|
- test/test_almost-happy.rb
|
109
110
|
has_rdoc: true
|