Skip to content

Commit

Permalink
Add support for some of Pygments' options.
Browse files Browse the repository at this point in the history
- Register "code", "code-block", "sourcecode" & "sourcecode-block" as
  aliases to the same directive.
- Rewrite the "pygments_directive" function as a class (Pygments).
- Create a new formatter each time. It takes more time to format the
  document because of that, BUT :
- Add support for some of Pygment's formatting options :
  * linenos (as a flag)
  * hl_lines
  * linenostart
  * linenostep
  * linenospecial
  * nobackground (as a flag)
  * anchorlinenos (as a flag)
  * noclasses (as a flag)
  • Loading branch information
fpoirotte committed Nov 15, 2011
1 parent c5080b7 commit fed39d0
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 38 deletions.
76 changes: 44 additions & 32 deletions reStPlugin/RegisterPygment.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python

# -*- coding: utf-8 -*-
# Copyright (C) 2007 Christophe Kibleur <kib2@free.fr>
#
# This file is part of reSTinPeace (http://kib2.alwaysdata.net).
Expand All @@ -15,38 +14,51 @@
# GNU General Public License for more details.

from docutils import nodes
from docutils.parsers.rst import directives
from docutils.core import publish_cmdline, publish_file, Publisher, default_description
from docutils.parsers.rst import directives, Directive

## Pygments
from pygments import highlight
from pygments.lexers import get_lexer_by_name
from pygments.formatters import HtmlFormatter
from pygments.styles import STYLE_MAP
from pygments.styles import get_style_by_name
from pygments.token import Keyword, Name, Comment, String, Error, \
Number, Operator, Generic, Whitespace

PYGMENTS_FORMATTER = HtmlFormatter(option='nowrap')
def pygments_directive(name, arguments, options, content, lineno,
content_offset, block_text, state, state_machine):
try:
lexer = get_lexer_by_name(arguments[0])
except ValueError:
# no lexer found
lexer = get_lexer_by_name('text')
parsed = highlight(u'\n'.join(content), lexer, PYGMENTS_FORMATTER)
return [nodes.raw('', parsed, format='html')]
pygments_directive.arguments = (1, 0, 1)
pygments_directive.content = 1
directives.register_directive('sourcecode', pygments_directive)

import locale
try:
locale.setlocale(locale.LC_ALL, '')
except:
pass

if __name__ == "__main__":
import docutils.core
publish_string(writer_name='html')

class Pygments(Directive):
required_arguments = 1
optional_arguments = 0
final_argument_whitespace = True
option_spec = {
'linenos': directives.flag,
'hl_lines': directives.positive_int_list,
'linenostart': directives.nonnegative_int,
'linenostep': directives.positive_int,
'linenospecial': directives.nonnegative_int,
'nobackground': directives.flag,
'anchorlinenos': directives.flag,
'noclasses': directives.flag,
}
has_content = True

def run(self):
self.assert_has_content()
try:
lexer = get_lexer_by_name(self.arguments[0])
except ValueError:
# no lexer found
lexer = get_lexer_by_name('text')

options={}
for (option, converter) in self.option_spec.iteritems():
if converter == directives.flag:
if option in self.options:
options[option] = True
elif option in self.options:
options[option] = self.options[option]

formatter = HtmlFormatter(**options)
parsed = highlight(u'\n'.join(self.content), lexer, formatter)
return [nodes.raw('', parsed, format='html')]

directives.register_directive('code', Pygments)
directives.register_directive('code-block', Pygments)
directives.register_directive('sourcecode', Pygments)
directives.register_directive('sourcecode-block', Pygments)

6 changes: 0 additions & 6 deletions reStPlugin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,12 +284,6 @@ def on_update_preview(self, window):
text = doc.get_text(start, end)
html = publish_parts(text, source_path=source_path, writer_name="html")["html_body"]

## Sortie
sortie = '\n'.join([START_HTML,html,END_HTML])
fs = open('sortie.html','w')
fs.write(sortie)
fs.close()

p = windowdata["bottom_panel"].get_placement()

html_doc = windowdata["html_doc"]
Expand Down

0 comments on commit fed39d0

Please sign in to comment.