Skip to content

Commit

Permalink
Remove non-portable dependencies for JRuby
Browse files Browse the repository at this point in the history
  • Loading branch information
gfx committed Dec 24, 2016
1 parent fc7e79e commit b9ba68b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
26 changes: 15 additions & 11 deletions lib/pygments/popen.rb
@@ -1,6 +1,6 @@
# coding: utf-8
require 'posix/spawn'
require 'yajl'
require 'open3'
require 'multi_json'
require 'timeout'
require 'logger'
require 'time'
Expand All @@ -13,9 +13,13 @@ class MentosError < IOError
# Python process.
module Pygments
module Popen
include POSIX::Spawn
extend self

def popen4(cmd)
stdin, stdout, stderr, wait_thr = Open3.popen3(cmd)
[wait_thr[:pid], stdin, stdout, stderr]
end

# Get things started by opening a pipe to mentos (the freshmaker), a
# Python process that talks to the Pygments library. We'll talk back and
# forth across this pipe.
Expand Down Expand Up @@ -262,7 +266,7 @@ def mentos(method, args=[], kwargs={}, original_code=nil)

kwargs.freeze
kwargs = kwargs.merge("fd" => @out.to_i, "id" => id, "bytes" => bytesize)
out_header = Yajl.dump(:method => method, :args => args, :kwargs => kwargs)
out_header = MultiJson.dump(:method => method, :args => args, :kwargs => kwargs)

# Get the size of the header itself and write that.
bits = get_fixed_bits_from_header(out_header)
Expand Down Expand Up @@ -322,12 +326,12 @@ def check_for_error
def handle_header_and_return(header, id)
if header
header = header_to_json(header)
bytes = header["bytes"]
bytes = header[:bytes]

# Read more bytes (the actual response body)
res = @out.read(bytes.to_i)

if header["method"] == "highlight"
if header[:method] == "highlight"
# Make sure we have a result back; else consider this an error.
if res.nil?
@log.warn "No highlight result back from mentos."
Expand Down Expand Up @@ -423,7 +427,7 @@ def get_header
# want them, text otherwise.
def return_result(res, method)
unless method == :lexer_name_for || method == :highlight || method == :css
res = Yajl.load(res, :symbolize_keys => true)
res = MultiJson.load(res, :symbolize_keys => true)
end
res = res.rstrip if res.class == String
res
Expand All @@ -432,14 +436,14 @@ def return_result(res, method)
# Convert a text header into JSON for easy access.
def header_to_json(header)
@log.info "[In header: #{header} "
header = Yajl.load(header)
header = MultiJson.load(header, :symbolize_keys => true)

if header["error"]
if header[:error]
# Raise this as a Ruby exception of the MentosError class.
# Stop so we don't leave the pipe in an inconsistent state.
@log.error "Failed to convert header to JSON."
stop header["error"]
raise MentosError, header["error"]
stop header[:error]
raise MentosError, header[:error]
else
header
end
Expand Down
3 changes: 1 addition & 2 deletions pygments.rb.gemspec
Expand Up @@ -14,8 +14,7 @@ Gem::Specification.new do |s|
s.email = ['aman@tmm1.net']
s.license = 'MIT'

s.add_dependency 'yajl-ruby', '~> 1.2'
s.add_dependency 'posix-spawn', '~> 0.3.6'
s.add_dependency 'multi_json', '>= 1.0.0'
s.add_development_dependency 'rake-compiler', '~> 0.7.6'
s.add_development_dependency 'test-unit', '~> 3.0.0'

Expand Down

0 comments on commit b9ba68b

Please sign in to comment.