1515import collections
1616import datetime
1717import json
18+ import sys
1819import time
1920import textwrap
2021from typing import Callable , Deque , Dict , Iterable , List , Optional
@@ -328,14 +329,16 @@ def _print_live_output(self, output: str, *, stream: str, fmter: Optional[Callab
328329
329330
330331 def _print_code_output (self , code_collection : CodeCollection , * , chunk : CodeChunk , check_first_chunk = False ):
331- type = code_collection .type
332- lang = chunk .options .get ('placeholder_lang' , chunk .options ['lang' ] or '' )
333- name = code_collection .name or ''
334332 data = {
335- 'command' : 'output' ,
336- 'key' : f'{ type } >{ lang } >{ name } ' ,
333+ 'message_type' : 'output' ,
334+ 'origin' : chunk .origin_name or '' ,
335+ 'code_collection' : {
336+ 'type' : code_collection .type ,
337+ 'lang' : chunk .options .get ('placeholder_lang' , chunk .options ['lang' ] or '' ),
338+ 'name' : code_collection .name or '' ,
339+ },
337340 'inline' : chunk .inline ,
338- 'index ' : f'{ chunk .index + 1 } /{ len (code_collection .code_chunks )} ' ,
341+ 'number ' : f'{ chunk .index + 1 } /{ len (code_collection .code_chunks )} ' ,
339342 'attr_hash' : chunk .attr_hash ,
340343 'code_hash' : chunk .code_hash ,
341344 'output' : chunk .only_code_output (self ._only_code_output ),
@@ -346,7 +349,10 @@ def _print_code_output(self, code_collection: CodeCollection, *, chunk: CodeChun
346349 return
347350 elif chunk .index == 0 :
348351 self ._only_code_output_first_chunk_cache [code_collection .key ] = data
349- print (json .dumps (data ), flush = True )
352+ # All data I/O must be UTF-8, following Pandoc
353+ sys .stdout .buffer .write (json .dumps (data ).encode ('utf8' ))
354+ sys .stdout .buffer .write (b'\n ' )
355+ sys .stdout .buffer .flush ()
350356
351357
352358 def _update_message_count (self , code_collection : CodeCollection ):
0 commit comments