rb-pygments 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (8) hide show
  1. data/.gitignore +3 -0
  2. data/.yardopts +3 -0
  3. data/MIT-LICENSE +20 -0
  4. data/README.md +31 -0
  5. data/Rakefile +29 -0
  6. data/VERSION +1 -0
  7. data/lib/rb-pygments.rb +76 -0
  8. metadata +70 -0
@@ -0,0 +1,3 @@
1
+ /pkg
2
+ /doc
3
+ /.yardoc
@@ -0,0 +1,3 @@
1
+ --markup markdown
2
+ --markup-provider maruku
3
+ --no-private
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Nathan Weizenbaum
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,31 @@
1
+ # rb-pygments
2
+
3
+ This is a wrapper for the [Pygments](http://pygments.org) syntax highlighter.
4
+ It can be used to highlight a wide variety of languages in a wide variety of formats.
5
+
6
+ ## Usage
7
+
8
+ All functionality is available through the {Pygments} module.
9
+ Highlighting is done via the {Pygments.highlight highlight} method,
10
+ like so (reformatted for readability:
11
+
12
+ Pygments.highlight("Some.ruby(:code)", :ruby, :html, :nowrap => true)
13
+ #=> <span class="no">Some</span>
14
+ <span class="o">.</span>
15
+ <span class="n">ruby</span>
16
+ <span class="p">(</span>
17
+ <span class="ss">:code</span>
18
+ <span class="p">)</span>
19
+
20
+ Stylesheets and such can be retrieved via the {Pygments.style style} method.
21
+
22
+ ## Requirements
23
+
24
+ yard-pygments requires that Pygments be installed.
25
+ Since Pygments is written in Python, it needs to be installed manually.
26
+ If you've got [`easy_install`](http://peak.telecommunity.com/DevCenter/EasyInstall), you can do
27
+
28
+ !!!sh
29
+ easy_install Pygments
30
+
31
+ Otherwise, it can be downloaded [here](http://pypi.python.org/pypi/Pygments).
@@ -0,0 +1,29 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "rb-pygments"
8
+ gem.summary = %Q{A Ruby wrapper for the Pygments syntax highlighter.}
9
+ gem.description = gem.summary
10
+ gem.email = "nex342@gmail.com"
11
+ gem.homepage = "http://github.com/nex3/rb-pygments"
12
+ gem.authors = ["Nathan Weizenbaum"]
13
+ gem.add_development_dependency('yard', '~> 0.5.3')
14
+ gem.requirements << 'pygments, 1.2.2 or greater'
15
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
16
+ end
17
+ Jeweler::GemcutterTasks.new
18
+ rescue LoadError
19
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
20
+ end
21
+
22
+ begin
23
+ require 'yard'
24
+ YARD::Rake::YardocTask.new
25
+ rescue LoadError
26
+ task :yardoc do
27
+ abort "YARD is not available. In order to run yardoc, you must: sudo gem install yard"
28
+ end
29
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,76 @@
1
+ require 'shellwords'
2
+
3
+ # A wrapper for the [Pygments](http://pygments.org) command-line interface.
4
+ module Pygments
5
+ class << self
6
+ # Returns a list of all langauges supported by Pygments.
7
+ # This list contains the short names of the languages
8
+ # as documented [on the Pygments site](http://pygments.org/docs/lexers/).
9
+ #
10
+ # @return [Array<String>]
11
+ def languages
12
+ @languages ||= languages!
13
+ end
14
+
15
+ # Returns the style definitions for a given style and formatter.
16
+ # For example, returns the CSS definitions for the HTML formatter
17
+ # and the LaTeX style definitions for the LaTeX formatter.
18
+ #
19
+ # @param style [#to_s] The name of the color theme to use, e.g. `:default` or `:colorful`.
20
+ # The full list of color themes can be found by running `pygmentize -L styles`.
21
+ # @param formatter [#to_s] The name of the formatter to use, e.g. `:html` or `:latex`.
22
+ # The full list of formatters can be found by running `pygmentize -L formatters`,
23
+ # or [online](http://pygments.org/docs/formatters/).
24
+ # At time of writing only `:html` and `:latex` support styles.
25
+ # @param options [{String => String}] Options passed to the formatter.
26
+ # These are formatter-specific;
27
+ # available options for a formatter can be found by running
28
+ # `pygmentize -H formatter #{formatter}`.
29
+ # @return [String]
30
+ def style(style, formatter, options = {})
31
+ execute(["-S", style, "-f", formatter] + convert_options(options))
32
+ end
33
+
34
+ # Returns the source code, highlighted using the given lexer and formatter.
35
+ # For example, returns HTML markup surrounding the source code for the `:html` formatter,
36
+ # or LaTeX markup for the `:latex` formatter.
37
+ #
38
+ # @param source [String] The source code to be highlighted
39
+ # @param lexer [#to_s] The name of the lexer (that is, language) to use, e.g. `:ruby` or `:python`.
40
+ # The full list of lexers can be found by running `pygmentize -L lexers`,
41
+ # or [online](http://pygments.org/docs/lexers/).
42
+ # @param formatter [#to_s] The name of the formatter to use, e.g. `:html` or `:latex`.
43
+ # The full list of formatters can be found by running `pygmentize -L formatters`,
44
+ # or [online](http://pygments.org/docs/formatters/).
45
+ # @param options [{String => String}] Options passed to the formatter and lexer.
46
+ # These are specific to the formatter and lexer being used;
47
+ # available options can be found by running `pygmentize -H formatter #{formatter}`
48
+ # or `pygmentize -H lexer #{lexer}`.
49
+ # @return [String]
50
+ def highlight(source, lexer, formatter, options = {})
51
+ execute(["-l", lexer, "-f", formatter] + convert_options(options), source)
52
+ end
53
+
54
+ private
55
+
56
+ def convert_options(options)
57
+ options.inject([]) {|a, (n, v)| a + ["-P", "#{n}=#{v}"]}
58
+ end
59
+
60
+ def languages!
61
+ execute(%w[-L lexers]).split("\n").grep(/^\* (.*):$/) {$1.split(",").map {|s| s.strip}}.flatten
62
+ end
63
+
64
+ def execute(flags, stdin = nil)
65
+ flags = flags.flatten.map {|f| Shellwords.shellescape(f.to_s)}.join(" ")
66
+ IO.popen("pygmentize #{flags}", stdin ? "r+" : "r") do |io|
67
+ if stdin
68
+ io.puts stdin
69
+ io.close_write
70
+ end
71
+
72
+ io.read
73
+ end
74
+ end
75
+ end
76
+ end
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rb-pygments
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nathan Weizenbaum
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-01-24 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: yard
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ~>
22
+ - !ruby/object:Gem::Version
23
+ version: 0.5.3
24
+ version:
25
+ description: A Ruby wrapper for the Pygments syntax highlighter.
26
+ email: nex342@gmail.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - README.md
33
+ files:
34
+ - .gitignore
35
+ - .yardopts
36
+ - MIT-LICENSE
37
+ - README.md
38
+ - Rakefile
39
+ - VERSION
40
+ - lib/rb-pygments.rb
41
+ has_rdoc: true
42
+ homepage: http://github.com/nex3/rb-pygments
43
+ licenses: []
44
+
45
+ post_install_message:
46
+ rdoc_options:
47
+ - --charset=UTF-8
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: "0"
55
+ version:
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: "0"
61
+ version:
62
+ requirements:
63
+ - pygments, 1.2.2 or greater
64
+ rubyforge_project:
65
+ rubygems_version: 1.3.5
66
+ signing_key:
67
+ specification_version: 3
68
+ summary: A Ruby wrapper for the Pygments syntax highlighter.
69
+ test_files: []
70
+