Hawkes Blog
Windows reboot | Linux be root
Hawkes Blog

Change Pelicans syntax highlighting Part II

Short How-To: SyntaxHighlighter with Pelican Improved



Update: 2013-08-12

Simple pelican plugin is available @github


Part1

I improved the rstdirectives.py a little bit. Some SyntaxHighlighter options are available now like:

  • Numbers On/Off
  • Highlight Line(s)
  • Start with Line

some other will be added later

the improved version:

class SyntaxHighlighter(Directive):
    """
        Source code syntax hightlighting with SyntaxHighlighter.
        http://alexgorbatchev.com/SyntaxHighlighter

    """
    required_arguments = 1
    optional_arguments = 3

    def shownumbers(argument):
        return directives.choice(argument, ('true', 'false'))


    """
        .. syntax:: (code type)
            :firstline: 1
            :highlightline: 1, 2, 5
            :numbers: true/false

            << code >>
    """
    option_spec = {
        'firstline': directives.positive_int,
        'highlightline': directives.positive_int_list,
        'numbers': shownumbers
    }
    final_argument_whitespace = True
    has_content = True


    def run(self):
        self.assert_has_content()
        # shell is default
        highlight = "shell"
        firstline = "first-line: 1;"
        """ to highlight a single line just use

            .. syntax:: bash
                :highlightline: LineNumber (e.g. 1)

            to highlight multiple lines use
            .. syntax:: bash
                :highlightline: 1, 2, 3, 4
        """
        highlightline = ""
        numbers = "gutter: true;"

        if "firstline" in self.options:
            firstline = "first-line: {0};".format(self.options["firstline"])
        if "highlightline" in self.options:
            highlightline = "highlight: {0};".format(self.options["highlightline"])
        if "numbers" in self.options:
            numbers = "gutter: {0};".format(self.options["numbers"])

        if self.arguments[0]:
            if self.arguments[0] in ("python", "py", "pyc"):
                highlight = "python"
            elif self.arguments[0] in ("javascript", "js", "jquery"):
                highlight = "js"
            elif self.arguments[0] in ("html", "htm", "web"):
                highlight = "html"

        parsed = '<pre class="brush: {0};{1}{2}{3}">{4}</pre>'.format(highlight, \
                                                                      firstline, \
                                                                      highlightline, \
                                                                      numbers, \
                                                                      self._get_escaped_content())
        return [nodes.raw('', parsed, format='html')]

    def _get_escaped_content(self):
            return u'\n'.join(map(escape, self.content))

directives.register_directive('syntax', SyntaxHighlighter)
directives.register_directive('sourcecode', SyntaxHighlighter)

How to use:

.. syntax:: bash
    :highlightline: 3, 5, 8
    :numbers: true
    :firstline: 3

    << code >>

MfG Hawkes


comments powered by Disqus