Exemple #1
0
    def run(self):

        try:
            language = self.arguments[0]
        except IndexError:
            language = self.options["language"]

        if self.content and "source-file" in self.options:
            error = self.state_machine.reporter.error(
                "You cannot both specify a source-file and include code directly.",
                nodes.literal_block(self.block_text, self.block_text),
                line=self.lineno)
            return [error]

        if not self.content:
            try:
                self.content = [
                    line.rstrip() for line in file(self.options["source-file"])
                ]
            except KeyError:
                # source-file was not specified
                pass
            except IOError:
                error = self.state_machine.reporter.error(
                    "Could not read file %s." % self.options["source-file"],
                    nodes.literal_block(self.block_text, self.block_text),
                    line=self.lineno)
                return [error]

        try:
            module = getattr(SilverCity, language)
            generator = getattr(module, language + "HTMLGenerator")
        except AttributeError:
            error = self.state_machine.reporter.error(
                "No SilverCity lexer found "
                "for language ''%s''." % language,
                nodes.literal_block(self.block_text, self.block_text),
                line=self.lineno)
            return [error]
        io = StringIO.StringIO()
        generator().generate_html(io, "\n".join(self.content))
        html = '<div class="code-block">\n%s\n</div>\n' % io.getvalue()
        raw = nodes.raw('', html, format='html')
        return [raw]
Exemple #2
0
   def run(self):
      
       try:
           language = self.arguments[0]
       except IndexError:
           language = self.options["language"]

       if self.content and "source-file" in self.options:
           error = self.state_machine.reporter.error( 
               "You cannot both specify a source-file and include code directly.",
               nodes.literal_block(self.block_text, self.block_text), line=self.lineno)
           return [error]
           
       if not self.content:
           try:
               self.content = [line.rstrip() for line in file(self.options["source-file"])]
           except KeyError:
               # source-file was not specified
               pass
           except IOError:
               error = self.state_machine.reporter.error( 
                   "Could not read file %s." %self.options["source-file"],
                   nodes.literal_block(self.block_text, self.block_text), line=self.lineno)
               return [error]
 
       try:
           module = getattr(SilverCity, language)
           generator = getattr(module, language+"HTMLGenerator")
       except AttributeError:
           error = self.state_machine.reporter.error( "No SilverCity lexer found "
                                                      "for language ''%s''." %language, 
							nodes.literal_block(self.block_text, self.block_text), line=self.lineno )
           return [error]
       io = StringIO.StringIO()
       generator().generate_html( io, "\n".join(self.content) )
       html = '<div class="code-block">\n%s\n</div>\n' % io.getvalue()
       raw = nodes.raw('',html, format = 'html')
       return [raw]
Exemple #3
0
def code_block(name, arguments, options, content, lineno,
               content_offset, block_text, state, state_machine):
    """
    The code-block directive provides syntax highlighting for blocks
    of code.  It is used with the the following syntax:

    .. code-block::
       :lang: CPP

       #include <iostream>

       int main( int argc, char* argv[] ) {
          std::cout << "Hello world" << std::endl;
       }

    There is also an option called 'url'. It can be used to specify an URL
    or a local file to read an fill the block in with its contents.

    The directive requires the name of a language supported by SilverCity.
    All code in the indented block following
    the directive will be colourized. Note that this directive is only
    supported for HTML writers.
    """

    # Python language by default when lang option not set
    if options.has_key('lang'):
        lang = options['lang']
    else:
        lang = 'Python'

    # get the right HTML Silvercity generator
    try:
        module = getattr(SilverCity, lang)
        generator = getattr(module, lang+"HTMLGenerator")
    except AttributeError:
        block = nodes.literal_block(block_text, block_text)
        error = state_machine.reporter.error("No SilverCity lexer found"
                                             "for language '%s'." % lang,
                                             block, line=lineno)
        return [error]

    # use url option for filling content in
    content2 = None
    if options.has_key('url'):
        url = options['url']
        if (urlparse.urlparse(url)[0] == 'http'):
            # Get the file via http
            content2 = urllib2.urlopen(url).readlines()
            content2 = [ s.replace('\n','') for s in content2 ]
        else:
            # load local file
            content2 = [ s.replace('\n','') for s in open(url).readlines() ]

    if not content2:
        #content = beautify(content)
        pass
    else:
        content = content2

    # add line numbers
    i, highest, data = 0, len(str(len(content))),[]
    while i < len(content):
        lineNb = string.rjust(str(i+1),highest)
        data.append(u'%s %s' % (lineNb,content[i]))
        i += 1

    io = StringIO.StringIO()
    generator().generate_html(io, '\n'.join(data))
    html = '<div class="code-block">\n%s\n</div>\n' % io.getvalue()
    return [nodes.raw('',html, format = 'html')]