nakajima-makers-mark 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/bin/mark ADDED
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require File.join(File.dirname(__FILE__), *%w[.. lib makers-mark])
4
+
5
+ content = ARGV.first ? File.read(ARGV.first) : $stdin.read
6
+
7
+ $VERBOSE = true
8
+
9
+ puts MakersMark.generate(content)
@@ -0,0 +1,60 @@
1
+ module MakersMark
2
+ class Generator
3
+ def initialize(markdown)
4
+ @markdown = markdown
5
+ end
6
+
7
+ def to_html
8
+ highlight!
9
+ doc.search('body > *').to_html
10
+ end
11
+
12
+ private
13
+
14
+ def doc
15
+ @doc ||= Nokogiri::HTML(markup)
16
+ end
17
+
18
+ def highlight!
19
+ doc.search('div.code').each do |div|
20
+ lexer = div['rel'] || :ruby
21
+
22
+ lexted_text = Albino.new(div.text, lexer).to_s
23
+
24
+ highlighted = Nokogiri::HTML(lexted_text).at('div')
25
+
26
+ klasses = highlighted['class'].split(/\s+/)
27
+ klasses << lexer
28
+ klasses << 'code'
29
+ klasses << 'highlight'
30
+ highlighted['class'] = klasses.join(' ')
31
+
32
+ div.replace(highlighted)
33
+ end
34
+ end
35
+
36
+ def markup
37
+ @markup ||= begin
38
+ logger.info "WRITING!"
39
+ t = RDiscount.new(@markdown.dup).to_html
40
+ t.gsub!(/^(?:<p>)?@@@(?:<\/p>)?$/, '</div>')
41
+ t.gsub!(/^(?:<p>)?@@@\s*(\w+)(?:<\/p>)?$/, '<div class="code" rel="\1">')
42
+ t
43
+ end
44
+ end
45
+
46
+ def logger
47
+ @logger ||= Class.new {
48
+ def info(msg)
49
+ say msg
50
+ end
51
+
52
+ private
53
+
54
+ def say(msg)
55
+ $stdout.puts msg if $VERBOSE
56
+ end
57
+ }.new
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,13 @@
1
+ $LOAD_PATH << File.dirname(__FILE__)
2
+
3
+ require 'rubygems'
4
+ require 'rdiscount'
5
+ require 'nokogiri'
6
+ require File.join(File.dirname(__FILE__), *%w[.. vendor albino])
7
+ require 'makers-mark/generator'
8
+
9
+ module MakersMark
10
+ def self.generate(markdown)
11
+ Generator.new(markdown).to_html
12
+ end
13
+ end
data/vendor/albino.rb ADDED
@@ -0,0 +1,80 @@
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 = '/usr/local/bin/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 = :ruby, format = :html)
58
+ @target = File.exists?(target) ? File.read(target) : target rescue target.chomp
59
+ @options = { :l => lexer, :f => format }
60
+ end
61
+
62
+ def execute(command)
63
+ pid, stdin, stdout, stderr = Open4.popen4(command)
64
+ stdin.puts @target
65
+ stdin.close
66
+ stdout.read.strip
67
+ end
68
+
69
+ def colorize(options = {})
70
+ execute @@bin + convert_options(options)
71
+ end
72
+ alias_method :to_s, :colorize
73
+
74
+ def convert_options(options = {})
75
+ @options.merge(options).inject('') do |string, (flag, value)|
76
+ string += " -#{flag} #{value}"
77
+ string
78
+ end
79
+ end
80
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nakajima-makers-mark
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.4
5
+ platform: ruby
6
+ authors:
7
+ - Pat Nakajima
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-04-05 00:00:00 -07:00
13
+ default_executable: mark
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rtomayko-rdiscount
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: nokogiri
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: patnakajima@gmail.com
37
+ executables:
38
+ - mark
39
+ extensions: []
40
+
41
+ extra_rdoc_files: []
42
+
43
+ files:
44
+ - bin/mark
45
+ - lib/makers-mark
46
+ - lib/makers-mark/generator.rb
47
+ - lib/makers-mark.rb
48
+ - vendor/albino.rb
49
+ has_rdoc: false
50
+ homepage:
51
+ post_install_message:
52
+ rdoc_options: []
53
+
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: "0"
61
+ version:
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: "0"
67
+ version:
68
+ requirements: []
69
+
70
+ rubyforge_project:
71
+ rubygems_version: 1.2.0
72
+ signing_key:
73
+ specification_version: 2
74
+ summary: Generate syntax highlighted HTML using Markdown/Lighthouse conventions.
75
+ test_files: []
76
+