/ doc / _sphinxext / md_include.py
md_include.py
 1  import sys
 2  import os.path
 3  import time
 4  from docutils import io, nodes, statemachine, utils
 5  from docutils.utils.error_reporting import SafeString, ErrorString
 6  from docutils.parsers.rst import Directive
 7  from docutils.parsers.rst import directives, states
 8  
 9  
10  from sphinx.directives.code import LiteralIncludeReader
11  from recommonmark.parser import CommonMarkParser
12  
13  
14  class MdInclude(Directive):
15  
16      """ Include a markdown file in an rst file.  """
17  
18      required_arguments = 1
19      optional_arguments = 0
20      final_argument_whitespace = True
21      option_spec = {'start-line': int,
22                     'end-line': int,
23                     'start-after': directives.unchanged_required,
24                     'end-before': directives.unchanged_required,
25                     'encoding': directives.encoding,}
26  
27      standard_include_path = os.path.join(os.path.dirname(states.__file__),
28                                           'include')
29  
30      def run(self):
31          """Include a file as part of the content of this reST file."""
32  
33          # copied from docutils.parsers.rst.directives.misc.Include
34          if not self.state.document.settings.file_insertion_enabled:
35              raise self.warning('"%s" directive disabled.' % self.name)
36          source = self.state_machine.input_lines.source(
37              self.lineno - self.state_machine.input_offset - 1)
38          source_dir = os.path.dirname(os.path.abspath(source))
39          path = directives.path(self.arguments[0])
40          if path.startswith('<') and path.endswith('>'):
41              path = os.path.join(self.standard_include_path, path[1:-1])
42          path = os.path.normpath(os.path.join(source_dir, path))
43          path = utils.relative_path(None, path)
44          path = nodes.reprunicode(path)
45          encoding = self.options.get(
46              'encoding', self.state.document.settings.input_encoding)
47          e_handler=self.state.document.settings.input_encoding_error_handler
48          try:
49              self.state.document.settings.record_dependencies.add(path)
50              include_file = io.FileInput(source_path=path,
51                                          encoding=encoding,
52                                          error_handler=e_handler)
53          except UnicodeEncodeError as error:
54              raise self.severe(u'Problems with "%s" directive path:\n'
55                                'Cannot encode input file path "%s" '
56                                '(wrong locale?).' %
57                                (self.name, SafeString(path)))
58          except IOError as error:
59              raise self.severe(u'Problems with "%s" directive path:\n%s.' %
60                        (self.name, ErrorString(error)))
61          startline = self.options.get('start-line', None)
62          endline = self.options.get('end-line', None)
63          try:
64              if startline or (endline is not None):
65                  lines = include_file.readlines()
66                  rawtext = ''.join(lines[startline:endline])
67              else:
68                  rawtext = include_file.read()
69          except UnicodeError as error:
70              raise self.severe(u'Problem with "%s" directive:\n%s' %
71                                (self.name, ErrorString(error)))
72          # start-after/end-before: no restrictions on newlines in match-text,
73          # and no restrictions on matching inside lines vs. line boundaries
74          after_text = self.options.get('start-after', None)
75          if after_text:
76              # skip content in rawtext before *and incl.* a matching text
77              after_index = rawtext.find(after_text)
78              if after_index < 0:
79                  raise self.severe('Problem with "start-after" option of "%s" '
80                                    'directive:\nText not found.' % self.name)
81              rawtext = rawtext[after_index + len(after_text):]
82          before_text = self.options.get('end-before', None)
83          if before_text:
84              # skip content in rawtext after *and incl.* a matching text
85              before_index = rawtext.find(before_text)
86              if before_index < 0:
87                  raise self.severe('Problem with "end-before" option of "%s" '
88                                    'directive:\nText not found.' % self.name)
89              rawtext = rawtext[:before_index]
90      
91          # copied code ends
92          parser = CommonMarkParser()
93          md_document = utils.new_document(path, self.state.document.settings)
94          parser.parse(rawtext, md_document)
95          return md_document.children
96  
97  
98  def setup(app):
99      directives.register_directive('mdinclude', MdInclude)