|
1 # -*- coding: utf-8 -*- |
|
2 """ |
|
3 sphinx.directives.code |
|
4 ~~~~~~~~~~~~~~~~~~~~~~ |
|
5 |
|
6 :copyright: 2007-2008 by Georg Brandl. |
|
7 :license: BSD. |
|
8 """ |
|
9 |
|
10 import sys |
|
11 import codecs |
|
12 from os import path |
|
13 |
|
14 from docutils import nodes |
|
15 from docutils.parsers.rst import directives |
|
16 |
|
17 from sphinx import addnodes |
|
18 |
|
19 |
|
20 # ------ highlight directive -------------------------------------------------------- |
|
21 |
|
22 def highlightlang_directive(name, arguments, options, content, lineno, |
|
23 content_offset, block_text, state, state_machine): |
|
24 if 'linenothreshold' in options: |
|
25 try: |
|
26 linenothreshold = int(options['linenothreshold']) |
|
27 except Exception: |
|
28 linenothreshold = 10 |
|
29 else: |
|
30 linenothreshold = sys.maxint |
|
31 return [addnodes.highlightlang(lang=arguments[0].strip(), |
|
32 linenothreshold=linenothreshold)] |
|
33 |
|
34 highlightlang_directive.content = 0 |
|
35 highlightlang_directive.arguments = (1, 0, 0) |
|
36 highlightlang_directive.options = {'linenothreshold': directives.unchanged} |
|
37 directives.register_directive('highlight', highlightlang_directive) |
|
38 directives.register_directive('highlightlang', highlightlang_directive) # old name |
|
39 |
|
40 |
|
41 # ------ code-block directive ------------------------------------------------------- |
|
42 |
|
43 def codeblock_directive(name, arguments, options, content, lineno, |
|
44 content_offset, block_text, state, state_machine): |
|
45 code = u'\n'.join(content) |
|
46 literal = nodes.literal_block(code, code) |
|
47 literal['language'] = arguments[0] |
|
48 literal['linenos'] = 'linenos' in options |
|
49 return [literal] |
|
50 |
|
51 codeblock_directive.content = 1 |
|
52 codeblock_directive.arguments = (1, 0, 0) |
|
53 codeblock_directive.options = {'linenos': directives.flag} |
|
54 directives.register_directive('code-block', codeblock_directive) |
|
55 directives.register_directive('sourcecode', codeblock_directive) |
|
56 |
|
57 |
|
58 # ------ literalinclude directive --------------------------------------------------- |
|
59 |
|
60 def literalinclude_directive(name, arguments, options, content, lineno, |
|
61 content_offset, block_text, state, state_machine): |
|
62 """Like .. include:: :literal:, but only warns if the include file is not found.""" |
|
63 if not state.document.settings.file_insertion_enabled: |
|
64 return [state.document.reporter.warning('File insertion disabled', line=lineno)] |
|
65 env = state.document.settings.env |
|
66 rel_fn = arguments[0] |
|
67 source_dir = path.dirname(path.abspath(state_machine.input_lines.source( |
|
68 lineno - state_machine.input_offset - 1))) |
|
69 fn = path.normpath(path.join(source_dir, rel_fn)) |
|
70 |
|
71 encoding = options.get('encoding', env.config.source_encoding) |
|
72 try: |
|
73 f = codecs.open(fn, 'r', encoding) |
|
74 text = f.read() |
|
75 f.close() |
|
76 except (IOError, OSError): |
|
77 retnode = state.document.reporter.warning( |
|
78 'Include file %r not found or reading it failed' % arguments[0], line=lineno) |
|
79 except UnicodeError: |
|
80 retnode = state.document.reporter.warning( |
|
81 'Encoding %r used for reading included file %r seems to ' |
|
82 'be wrong, try giving an :encoding: option' % |
|
83 (encoding, arguments[0])) |
|
84 else: |
|
85 retnode = nodes.literal_block(text, text, source=fn) |
|
86 retnode.line = 1 |
|
87 if options.get('language', ''): |
|
88 retnode['language'] = options['language'] |
|
89 if 'linenos' in options: |
|
90 retnode['linenos'] = True |
|
91 state.document.settings.env.note_dependency(rel_fn) |
|
92 return [retnode] |
|
93 |
|
94 literalinclude_directive.options = {'linenos': directives.flag, |
|
95 'language': directives.unchanged, |
|
96 'encoding': directives.encoding} |
|
97 literalinclude_directive.content = 0 |
|
98 literalinclude_directive.arguments = (1, 0, 0) |
|
99 directives.register_directive('literalinclude', literalinclude_directive) |