예제 #1
0
 def highlight(self, code):
     '''
     This should return a *list* of docutils nodes representing the
     highlighted tokens.
     
     Usually the easient way to do this is to implement
     self.highlight_lang(), and let the default work.
     
     Parameters:
         Code - the code to highlight.
     
     Required for:
         All
     '''
     return highlight_as(code, self.highlight_lang())
예제 #2
0
    def handle(self, args, options, content):
        file_like_args    = [arg for arg in args if arg.find('.') != -1]
        command_like_args = [arg for arg in args if arg.find('.') == -1]
        
        def cmd(s):
            return directives.choice(s, [
                'exec', 'done', 'restart', 'noeval', 'redo',
                'join', 'noecho', 'new', 'recall'
            ])
        commands = map(cmd, command_like_args)

        if len(file_like_args) > 0: source_name = file_like_args[0]
        elif 'new' in commands:
            source_name = 'main' + str(unique_block_id()) + self.language.extension()
        else: source_name = 'main' + self.language.extension()
        
        was_empty = self.context.is_empty(source_name)
        was_lines = self.context.count_lines(source_name)
 
        if 'recall' in commands:
            input_display  = self.do_recall(source_name, commands, options, content)
            output_display = None
        else:
            input_display  = self.do_mods(source_name, commands, options, content)
            output_display = self.do_run(source_name, commands, options, content)
            
        if 'join' in commands: header_node = nodes.inline()
        else:
            cont = '(cont)' if not was_empty else ''
            header_text = '     %s %s\n\n' % (source_name, cont)
            header_node = nodes.inline(
                header_text, header_text, classes=['file-header']
            )
        
        if input_display != None:
            source_nodes = []
            for sblock in input_display:
                if sblock.name == None:
                    btext = sblock.text()
                    if 'highlight' in options:
                        hlbtext = highlight_as(btext, options['highlight'])
                    else:
                        hlbtext = self.language.highlight(sblock.text())
                    source_nodes +=  hlbtext
                else:
                    source_nodes += [nodes.inline('',
                        '[... %s ...]' % sblock.name,
                        classes = ['omission']
                    ), nodes.inline('\n', '\n')]
                    
            if self.language.number_lines():
                source_nodes = add_line_numbers(source_nodes, was_lines+1)
            
            source_node = nodes.literal_block(
                classes=['code', 'code-' + self.directive_name])
            source_node += header_node
            for n in source_nodes:
                source_node += n
        else:
            source_node = None
        
        if output_display != None:
            if isinstance(output_display, str):
                output = strip_blank_lines(output_display)
                try:
                    output = output.decode('utf-8')
                except:
                    output = filter(lambda c: ord(c)<128, output)
                output_node = nodes.literal_block(output, output,
                    classes=['run-output', 'run-output-' + self.directive_name]
                )
            else:
                output_node = output_display
        else:
            output_node = None
        
        if 'noecho' in commands: return [ ]
        else:
            result = []
            if source_node != None: result.append(source_node)
            if output_node != None: result.append(output_node)
        
            return result