prompt: You will be provided with a partial code base and an issue statement explaining a problem to resolve.
  <issue>
  reST listings can’t number lines, while code-blocks do it incompatibly
  1. One can’t do a `.. listing::` in Nikola with line numbers.
  2. In `.. code-block::`s, one must use the reST `:number-lines:` option syntax.  However, Sphinx wants `:linenos:` instead. (#770)
  
  
  </issue>
  <code>
  [start of nikola/plugins/compile/rest/listing.py]
  1 # -*- coding: utf-8 -*-
  2 
  3 # Copyright © 2012-2014 Roberto Alsina and others.
  4 
  5 # Permission is hereby granted, free of charge, to any
  6 # person obtaining a copy of this software and associated
  7 # documentation files (the "Software"), to deal in the
  8 # Software without restriction, including without limitation
  9 # the rights to use, copy, modify, merge, publish,
  10 # distribute, sublicense, and/or sell copies of the
  11 # Software, and to permit persons to whom the Software is
  12 # furnished to do so, subject to the following conditions:
  13 #
  14 # The above copyright notice and this permission notice
  15 # shall be included in all copies or substantial portions of
  16 # the Software.
  17 #
  18 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
  19 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  20 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  21 # PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  22 # OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  23 # OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  24 # OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  25 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26 
  27 
  28 """ Define and register a listing directive using the existing CodeBlock """
  29 
  30 
  31 from __future__ import unicode_literals
  32 from codecs import open as codecs_open  # for patching purposes
  33 import os
  34 try:
  35     from urlparse import urlunsplit
  36 except ImportError:
  37     from urllib.parse import urlunsplit  # NOQA
  38 
  39 from docutils import core
  40 from docutils import nodes
  41 from docutils.parsers.rst import Directive, directives
  42 from docutils.parsers.rst.directives.misc import Include
  43 try:
  44     from docutils.parsers.rst.directives.body import CodeBlock
  45 except ImportError:  # docutils < 0.9 (Debian Sid For The Loss)
  46     class CodeBlock(Directive):
  47         required_arguments = 1
  48         has_content = True
  49         CODE = '<pre>{0}</pre>'
  50 
  51         def run(self):
  52             """ Required by the Directive interface. Create docutils nodes """
  53             return [nodes.raw('', self.CODE.format('\n'.join(self.content)), format='html')]
  54     directives.register_directive('code', CodeBlock)
  55 
  56 
  57 from nikola.plugin_categories import RestExtension
  58 
  59 
  60 class Plugin(RestExtension):
  61 
  62     name = "rest_listing"
  63 
  64     def set_site(self, site):
  65         self.site = site
  66         # Even though listings don't use CodeBlock anymore, I am
  67         # leaving these to make the code directive work with
  68         # docutils < 0.9
  69         directives.register_directive('code-block', CodeBlock)
  70         directives.register_directive('sourcecode', CodeBlock)
  71         directives.register_directive('listing', Listing)
  72         return super(Plugin, self).set_site(site)
  73 
  74 
  75 class Listing(Include):
  76     """ listing directive: create a highlighted block of code from a file in listings/
  77 
  78     Usage:
  79 
  80         .. listing:: nikola.py python
  81            :number-lines:
  82 
  83     """
  84     has_content = False
  85     required_arguments = 1
  86     optional_arguments = 1
  87 
  88     def run(self):
  89         fname = self.arguments.pop(0)
  90         lang = self.arguments.pop(0)
  91         fpath = os.path.join('listings', fname)
  92         self.arguments.insert(0, fpath)
  93         self.options['code'] = lang
  94         with codecs_open(fpath, 'rb+', 'utf8') as fileobject:
  95             self.content = fileobject.read().splitlines()
  96         self.state.document.settings.record_dependencies.add(fpath)
  97         target = urlunsplit(("link", 'listing', fname, '', ''))
  98         generated_nodes = (
  99             [core.publish_doctree('`{0} <{1}>`_'.format(fname, target))[0]])
  100         generated_nodes += self.get_code_from_file(fileobject)
  101         return generated_nodes
  102 
  103     def get_code_from_file(self, data):
  104         """ Create CodeBlock nodes from file object content """
  105         return super(Listing, self).run()
  106 
  107     def assert_has_content(self):
  108         """ Listing has no content, override check from superclass """
  109         pass
  110
  [end of nikola/plugins/compile/rest/listing.py]
  </code>
  
  I need you to solve this issue by generating a single patch file that I can apply directly to this repository using git apply. Please respond with a single patch file in the following format.
  <patch>
  diff --git a/file.py b/file.py
  --- a/file.py
  +++ b/file.py
  @@ -1,27 +1,35 @@
   def euclidean(a, b):
  -    while b:
  -        a, b = b, a % b
  -    return a
  +    if b == 0:
  +        return a
  +    return euclidean(b, a % b)
   
   
   def bresenham(x0, y0, x1, y1):
       points = []
       dx = abs(x1 - x0)
       dy = abs(y1 - y0)
  -    sx = 1 if x0 < x1 else -1
  -    sy = 1 if y0 < y1 else -1
  -    err = dx - dy
  +    x, y = x0, y0
  +    sx = -1 if x0 > x1 else 1
  +    sy = -1 if y0 > y1 else 1
   
  -    while True:
  -        points.append((x0, y0))
  -        if x0 == x1 and y0 == y1:
  -            break
  -        e2 = 2 * err
  -        if e2 > -dy:
  -            err -= dy
  -            x0 += sx
  -        if e2 < dx:
  -            err += dx
  -            y0 += sy
  +    if dx > dy:
  +        err = dx / 2.0
  +        while x != x1:
  +            points.append((x, y))
  +            err -= dy
  +            if err < 0:
  +                y += sy
  +                err += dx
  +            x += sx
  +    else:
  +        err = dy / 2.0
  +        while y != y1:
  +            points.append((x, y))
  +            err -= dx
  +            if err < 0:
  +                x += sx
  +                err += dy
  +            y += sy
  +
  +    points.append((x, y))
       return points
  
  </patch>
golden_diff: diff --git a/nikola/plugins/compile/rest/listing.py b/nikola/plugins/compile/rest/listing.py
  --- a/nikola/plugins/compile/rest/listing.py
  +++ b/nikola/plugins/compile/rest/listing.py
  @@ -56,6 +56,18 @@
   
   from nikola.plugin_categories import RestExtension
   
  +# Add sphinx compatibility option
  +CodeBlock.option_spec['linenos'] = directives.unchanged
  +
  +
  +class FlexibleCodeBlock(CodeBlock):
  +
  +    def run(self):
  +        if 'linenos' in self.options:
  +            self.options['number-lines'] = self.options['linenos']
  +        return super(FlexibleCodeBlock, self).run()
  +CodeBlock = FlexibleCodeBlock
  +
   
   class Plugin(RestExtension):
   
  @@ -71,6 +83,11 @@
           directives.register_directive('listing', Listing)
           return super(Plugin, self).set_site(site)
   
  +# Add sphinx compatibility option
  +listing_spec = Include.option_spec
  +listing_spec['linenos'] = directives.unchanged
  +print(listing_spec)
  +
   
   class Listing(Include):
       """ listing directive: create a highlighted block of code from a file in listings/
  @@ -84,6 +101,7 @@
       has_content = False
       required_arguments = 1
       optional_arguments = 1
  +    option_spec = listing_spec
   
       def run(self):
           fname = self.arguments.pop(0)
  @@ -91,6 +109,8 @@
           fpath = os.path.join('listings', fname)
           self.arguments.insert(0, fpath)
           self.options['code'] = lang
  +        if 'linenos' in self.options:
  +            self.options['number-lines'] = self.options['linenos']
           with codecs_open(fpath, 'rb+', 'utf8') as fileobject:
               self.content = fileobject.read().splitlines()
           self.state.document.settings.record_dependencies.add(fpath)
  
verification_info: {"golden_diff": "diff --git a/nikola/plugins/compile/rest/listing.py b/nikola/plugins/compile/rest/listing.py\n--- a/nikola/plugins/compile/rest/listing.py\n+++ b/nikola/plugins/compile/rest/listing.py\n@@ -56,6 +56,18 @@\n \n from nikola.plugin_categories import RestExtension\n \n+# Add sphinx compatibility option\n+CodeBlock.option_spec['linenos'] = directives.unchanged\n+\n+\n+class FlexibleCodeBlock(CodeBlock):\n+\n+    def run(self):\n+        if 'linenos' in self.options:\n+            self.options['number-lines'] = self.options['linenos']\n+        return super(FlexibleCodeBlock, self).run()\n+CodeBlock = FlexibleCodeBlock\n+\n \n class Plugin(RestExtension):\n \n@@ -71,6 +83,11 @@\n         directives.register_directive('listing', Listing)\n         return super(Plugin, self).set_site(site)\n \n+# Add sphinx compatibility option\n+listing_spec = Include.option_spec\n+listing_spec['linenos'] = directives.unchanged\n+print(listing_spec)\n+\n \n class Listing(Include):\n     \"\"\" listing directive: create a highlighted block of code from a file in listings/\n@@ -84,6 +101,7 @@\n     has_content = False\n     required_arguments = 1\n     optional_arguments = 1\n+    option_spec = listing_spec\n \n     def run(self):\n         fname = self.arguments.pop(0)\n@@ -91,6 +109,8 @@\n         fpath = os.path.join('listings', fname)\n         self.arguments.insert(0, fpath)\n         self.options['code'] = lang\n+        if 'linenos' in self.options:\n+            self.options['number-lines'] = self.options['linenos']\n         with codecs_open(fpath, 'rb+', 'utf8') as fileobject:\n             self.content = fileobject.read().splitlines()\n         self.state.document.settings.record_dependencies.add(fpath)\n", "issue": "reST listings can\u2019t number lines, while code-blocks do it incompatibly\n1. One can\u2019t do a `.. listing::` in Nikola with line numbers.\n2. In `.. code-block::`s, one must use the reST `:number-lines:` option syntax.  However, Sphinx wants `:linenos:` instead. (#770)\n\n", "before_files": [{"content": "# -*- coding: utf-8 -*-\n\n# Copyright \u00a9 2012-2014 Roberto Alsina and others.\n\n# Permission is hereby granted, free of charge, to any\n# person obtaining a copy of this software and associated\n# documentation files (the \"Software\"), to deal in the\n# Software without restriction, including without limitation\n# the rights to use, copy, modify, merge, publish,\n# distribute, sublicense, and/or sell copies of the\n# Software, and to permit persons to whom the Software is\n# furnished to do so, subject to the following conditions:\n#\n# The above copyright notice and this permission notice\n# shall be included in all copies or substantial portions of\n# the Software.\n#\n# THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY\n# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE\n# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR\n# PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS\n# OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR\n# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR\n# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE\n# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n\n\n\"\"\" Define and register a listing directive using the existing CodeBlock \"\"\"\n\n\nfrom __future__ import unicode_literals\nfrom codecs import open as codecs_open  # for patching purposes\nimport os\ntry:\n    from urlparse import urlunsplit\nexcept ImportError:\n    from urllib.parse import urlunsplit  # NOQA\n\nfrom docutils import core\nfrom docutils import nodes\nfrom docutils.parsers.rst import Directive, directives\nfrom docutils.parsers.rst.directives.misc import Include\ntry:\n    from docutils.parsers.rst.directives.body import CodeBlock\nexcept ImportError:  # docutils < 0.9 (Debian Sid For The Loss)\n    class CodeBlock(Directive):\n        required_arguments = 1\n        has_content = True\n        CODE = '<pre>{0}</pre>'\n\n        def run(self):\n            \"\"\" Required by the Directive interface. Create docutils nodes \"\"\"\n            return [nodes.raw('', self.CODE.format('\\n'.join(self.content)), format='html')]\n    directives.register_directive('code', CodeBlock)\n\n\nfrom nikola.plugin_categories import RestExtension\n\n\nclass Plugin(RestExtension):\n\n    name = \"rest_listing\"\n\n    def set_site(self, site):\n        self.site = site\n        # Even though listings don't use CodeBlock anymore, I am\n        # leaving these to make the code directive work with\n        # docutils < 0.9\n        directives.register_directive('code-block', CodeBlock)\n        directives.register_directive('sourcecode', CodeBlock)\n        directives.register_directive('listing', Listing)\n        return super(Plugin, self).set_site(site)\n\n\nclass Listing(Include):\n    \"\"\" listing directive: create a highlighted block of code from a file in listings/\n\n    Usage:\n\n        .. listing:: nikola.py python\n           :number-lines:\n\n    \"\"\"\n    has_content = False\n    required_arguments = 1\n    optional_arguments = 1\n\n    def run(self):\n        fname = self.arguments.pop(0)\n        lang = self.arguments.pop(0)\n        fpath = os.path.join('listings', fname)\n        self.arguments.insert(0, fpath)\n        self.options['code'] = lang\n        with codecs_open(fpath, 'rb+', 'utf8') as fileobject:\n            self.content = fileobject.read().splitlines()\n        self.state.document.settings.record_dependencies.add(fpath)\n        target = urlunsplit((\"link\", 'listing', fname, '', ''))\n        generated_nodes = (\n            [core.publish_doctree('`{0} <{1}>`_'.format(fname, target))[0]])\n        generated_nodes += self.get_code_from_file(fileobject)\n        return generated_nodes\n\n    def get_code_from_file(self, data):\n        \"\"\" Create CodeBlock nodes from file object content \"\"\"\n        return super(Listing, self).run()\n\n    def assert_has_content(self):\n        \"\"\" Listing has no content, override check from superclass \"\"\"\n        pass\n", "path": "nikola/plugins/compile/rest/listing.py"}]}