From bf2c2544a220baf10f672d81182e703255a10c7e Mon Sep 17 00:00:00 2001 From: tnm Date: Sun, 15 Jul 2012 17:09:08 -0700 Subject: [PATCH] Make sure not to send any code as JSON --- lib/pygments/mentos.py | 14 +++++++++----- lib/pygments/popen.rb | 3 ++- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/lib/pygments/mentos.py b/lib/pygments/mentos.py index 430ebd87..18574338 100755 --- a/lib/pygments/mentos.py +++ b/lib/pygments/mentos.py @@ -213,17 +213,21 @@ def start(self): method = header["method"] # Default to empty array and empty dictionary if nothing is given. Default to - # no text and no further bytes to read. + # no further bytes to read. args = header.get("args", []) kwargs = header.get("kwargs", {}) lexer = kwargs.get("lexer", None) + _bytes = 0 if lexer: lexer = str(lexer) - if method == 'highlight': - text = args.encode('utf-8') - else: - text = None + # Read more bytes if necessary + _kwargs = header.get("kwargs", None) + if _kwargs: + _bytes = _kwargs.get("bytes", 0) + + # Read up to the given number bytes (possibly 0) + text = sys.stdin.read(_bytes) # And now get the actual data from pygments. try: diff --git a/lib/pygments/popen.rb b/lib/pygments/popen.rb index be1d1ae1..7f10383c 100644 --- a/lib/pygments/popen.rb +++ b/lib/pygments/popen.rb @@ -151,7 +151,7 @@ def highlight(code, opts={}) opts[:options][:outencoding] ||= 'utf-8' # Get back the string from mentos and force encoding if we can - str = mentos(:highlight, code, opts, code) + str = mentos(:highlight, nil, opts, code) str.force_encoding(opts[:options][:outencoding]) if str.respond_to?(:force_encoding) str end @@ -203,6 +203,7 @@ def mentos(method, args=[], kwargs={}, code=nil) # # If there's text/code to be highlighted, we send that after. @in.write(out_header) + @in.write(code) if code # Get the response header header = @out.gets