[doc] Implement an 'include-url' ReST directive
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Wed, 12 Apr 2017 11:27:16 +0200
changeset 274 6ef6693a7b38
parent 273 fb4e209dc50c
child 275 911655170d4e
[doc] Implement an 'include-url' ReST directive
doc/rext.py
--- a/doc/rext.py	Wed Apr 12 11:26:24 2017 +0200
+++ b/doc/rext.py	Wed Apr 12 11:27:16 2017 +0200
@@ -1,6 +1,8 @@
 """ReST extensions"""
 
 from pkg_resources import Requirement, resource_filename, resource_exists
+import tempfile
+import urllib2
 
 from docutils.parsers.rst.directives import misc, register_directive
 
@@ -32,3 +34,26 @@
 
 
 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)