Skip to content

Commit

Permalink
Use the newly-forked rb-pygments rather than bundling it ourself.
Browse files Browse the repository at this point in the history
  • Loading branch information
nex3 committed Jan 25, 2010
1 parent 6ad0009 commit 3173a4e
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 81 deletions.
7 changes: 3 additions & 4 deletions README.md
Expand Up @@ -17,10 +17,9 @@ The names that will be recognized for these languages are listed under "short na

## Requirements

yard-pygments requires, unsurprisingly, YARD and Pygments.
YARD will be installed along with the gem,
but since Pygments is written in Python,
it needs to be installed manually.
yard-pygments requires [rb-pygments](http://github.com/nex3/rb-pygments),
which in turn requires Pygments itself.
Since Pygments is written in Python, it needs to be installed manually.
If you've got [`easy_install`](http://peak.telecommunity.com/DevCenter/EasyInstall), you can do

!!!sh
Expand Down
2 changes: 1 addition & 1 deletion Rakefile
Expand Up @@ -14,7 +14,7 @@ DESC
gem.homepage = "http://github.com/nex3/yard-pygments"
gem.authors = ["Nathan Weizenbaum"]
gem.add_dependency('yard', '~> 0.5.3')
gem.requirements << 'pygments, 1.2.2 or greater'
gem.add_dependency('rb-pygments', '= 0.1.0')
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
end
Jeweler::GemcutterTasks.new
Expand Down
77 changes: 1 addition & 76 deletions lib/yard-pygments.rb
@@ -1,4 +1,4 @@
require 'shellwords'
require 'rb-pygments'

YARD::Templates::Engine.register_template_path File.dirname(__FILE__) + '/../templates'

Expand All @@ -18,79 +18,4 @@ def respond_to?(name)
Pygments.languages.include?($1.downcase))
end
end

# A module wrapping the [Pygments](http://pygments.org) command-line interface.
module Pygments
extend self

# Returns a list of all langauges supported by Pygments.
# This list contains the short names of the languages
# as documented [on the Pygments site](http://pygments.org/docs/lexers/).
#
# @return [Array<String>]
def languages
@languages ||= languages!
end

# Returns the style definitions for a given style and formatter.
# For example, returns the CSS definitions for the HTML formatter
# and the LaTeX style definitions for the LaTeX formatter.
#
# @param style [#to_s] The name of the color theme to use, e.g. `:default` or `:colorful`.
# The full list of color themes can be found by running `pygmentize -L styles`.
# @param formatter [#to_s] The name of the formatter to use, e.g. `:html` or `:latex`.
# The full list of formatters can be found by running `pygmentize -L formatters`,
# or [online](http://pygments.org/docs/formatters/).
# At time of writing only `:html` and `:latex` support styles.
# @param options [{String => String}] Options passed to the formatter.
# These are formatter-specific;
# available options for a formatter can be found by running
# `pygmentize -H formatter #{formatter}`.
# @return [String]
def style(style, formatter, options = {})
execute(["-S", style, "-f", formatter] + convert_options(options))
end

# Returns the source code, highlighted using the given lexer and formatter.
# For example, returns HTML markup surrounding the source code for the `:html` formatter,
# or LaTeX markup for the `:latex` formatter.
#
# @param source [String] The source code to be highlighted
# @param lexer [#to_s] The name of the lexer (that is, language) to use, e.g. `:ruby` or `:python`.
# The full list of lexers can be found by running `pygmentize -L lexers`,
# or [online](http://pygments.org/docs/lexers/).
# @param formatter [#to_s] The name of the formatter to use, e.g. `:html` or `:latex`.
# The full list of formatters can be found by running `pygmentize -L formatters`,
# or [online](http://pygments.org/docs/formatters/).
# @param options [{String => String}] Options passed to the formatter and lexer.
# These are specific to the formatter and lexer being used;
# available options can be found by running `pygmentize -H formatter #{formatter}`
# or `pygmentize -H lexer #{lexer}`.
# @return [String]
def highlight(source, lexer, formatter, options = {})
execute(["-l", lexer, "-f", formatter] + convert_options(options), source)
end

private

def convert_options(options)
options.inject([]) {|a, (n, v)| a + ["-P", "#{n}=#{v}"]}
end

def languages!
execute(%w[-L lexers]).split("\n").grep(/^\* (.*):$/) {$1.split(",").map {|s| s.strip}}.flatten
end

def execute(flags, stdin = nil)
flags = flags.flatten.map {|f| Shellwords.shellescape(f.to_s)}.join(" ")
IO.popen("pygmentize #{flags}", stdin ? "r+" : "r") do |io|
if stdin
io.puts stdin
io.close_write
end

io.read
end
end
end
end

0 comments on commit 3173a4e

Please sign in to comment.