[PATCH 5 of 7 sherpa] [doc] Implement an 'include-url' ReST directive

Sylvain Thenault sylvain.thenault at logilab.fr
Wed Apr 12 11:46:54 CEST 2017


# HG changeset patch
# User Sylvain Thénault <sylvain.thenault at logilab.fr>
# Date 1491989236 -7200
#      Wed Apr 12 11:27:16 2017 +0200
# Node ID f42a05fef21ff1c0b4196a05d802e0f0add4ddef
# Parent  becdb97e45aa6289012abf891eadb50b814b6837
[doc] Implement an 'include-url' ReST directive

diff --git a/doc/rext.py b/doc/rext.py
--- a/doc/rext.py
+++ b/doc/rext.py
@@ -1,8 +1,10 @@
 """ReST extensions"""
 
 from pkg_resources import Requirement, resource_filename, resource_exists
+import tempfile
+import urllib2
 
 from docutils.parsers.rst.directives import misc, register_directive
 
 
 class PkgResourcesInclude(misc.Include):
@@ -30,5 +32,28 @@ class PkgResourcesInclude(misc.Include):
         self.arguments = [filename]
         return super(PkgResourcesInclude, self).run()
 
 
 register_directive('pkg-resources-include', PkgResourcesInclude)
+
+
+class IncludeURL(misc.Include):
+    """Implement the `include-url` directive, similar to the `include`
+    directive but taking an URL as argument.
+
+    Example:
+
+        .. include-url:: https://framagit.org/saemproject/saem-client/raw/master/README.rst
+    """
+
+    def run(self):
+        url, = self.arguments
+
+        input_stream = urllib2.urlopen(url)
+        with tempfile.NamedTemporaryFile() as output_stream:
+            output_stream.write(input_stream.read())
+            output_stream.flush()
+            self.arguments = [output_stream.name]
+            return super(IncludeURL, self).run()
+
+
+register_directive('include-url', IncludeURL)


More information about the saem-devel mailing list