From 66675b5650b63e2aecd4ceef4d34f1a1d56b9c95 Mon Sep 17 00:00:00 2001
From: Timo Kaufmann <timokau@zoho.com>
Date: Wed, 22 Jan 2020 00:39:54 +0100
Subject: [PATCH 1/2] python2.pkgs.ipython: use the proper python2 lexer

Since pygments 2.5 `PythonLexer` refers to python3 [1]. We have to be
explicit if we want to use python2.

[1] https://pygments.org/docs/changelog/
---
 pkgs/development/python-modules/ipython/5.nix | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/pkgs/development/python-modules/ipython/5.nix b/pkgs/development/python-modules/ipython/5.nix
index 15e7d00bcec..dd0c97fc3c5 100644
--- a/pkgs/development/python-modules/ipython/5.nix
+++ b/pkgs/development/python-modules/ipython/5.nix
@@ -2,6 +2,7 @@
 , stdenv
 , buildPythonPackage
 , fetchPypi
+, fetchpatch
 # Build dependencies
 , glibcLocales
 # Test dependencies
@@ -36,6 +37,15 @@ buildPythonPackage rec {
     substituteInPlace setup.py --replace "'gnureadline'" " "
   '';
 
+  patches = [
+    # Use the proper pygments lexer for python2 (https://github.com/ipython/ipython/pull/12095)
+    (fetchpatch {
+      name = "python2-lexer.patch";
+      url = "https://github.com/ipython/ipython/pull/12095/commits/8805293b5e4bce9150cc2ad9c5d6d984849ae447.patch";
+      sha256 = "16p4gl7a49v76w33j39ih7yspy6x2d14p9bh4wdpg9cafhw9nbc0";
+    })
+  ];
+
   buildInputs = [ glibcLocales ];
 
   checkInputs = [ nose pygments testpath ] ++ lib.optional isPy27 mock;

From 0811a69fef6e42aa40d859526f0ce9d667d36f52 Mon Sep 17 00:00:00 2001
From: Timo Kaufmann <timokau@zoho.com>
Date: Wed, 22 Jan 2020 23:34:44 +0100
Subject: [PATCH 2/2] python2.pkgs.sphinx: user proper python2 lexer

---
 pkgs/development/python-modules/sphinx/2.nix  |  7 ++++++
 .../python-modules/sphinx/python2-lexer.patch | 22 +++++++++++++++++++
 2 files changed, 29 insertions(+)
 create mode 100644 pkgs/development/python-modules/sphinx/python2-lexer.patch

diff --git a/pkgs/development/python-modules/sphinx/2.nix b/pkgs/development/python-modules/sphinx/2.nix
index b6374989690..541c292fe8f 100644
--- a/pkgs/development/python-modules/sphinx/2.nix
+++ b/pkgs/development/python-modules/sphinx/2.nix
@@ -60,6 +60,13 @@ buildPythonPackage rec {
   # Lots of tests. Needs network as well at some point.
   doCheck = false;
 
+  patches = [
+    # Since pygments 2.5, PythonLexer refers to python3. If we want to use
+    # python2, we need to explicitly specify Python2Lexer.
+    # Not upstreamed since there doesn't seem to be any upstream maintenance
+    # branch for 1.8 (and this patch doesn't make any sense for 2.x).
+    ./python2-lexer.patch
+  ];
   # https://github.com/NixOS/nixpkgs/issues/22501
   # Do not run `python sphinx-build arguments` but `sphinx-build arguments`.
   postPatch = ''
diff --git a/pkgs/development/python-modules/sphinx/python2-lexer.patch b/pkgs/development/python-modules/sphinx/python2-lexer.patch
new file mode 100644
index 00000000000..cf4a243315a
--- /dev/null
+++ b/pkgs/development/python-modules/sphinx/python2-lexer.patch
@@ -0,0 +1,22 @@
+diff --git a/sphinx/highlighting.py b/sphinx/highlighting.py
+index ac2bd1b06..63ca52de2 100644
+--- a/sphinx/highlighting.py
++++ b/sphinx/highlighting.py
+@@ -16,7 +16,7 @@ from pygments.filters import ErrorToken
+ from pygments.formatters import HtmlFormatter, LatexFormatter
+ from pygments.lexer import Lexer  # NOQA
+ from pygments.lexers import get_lexer_by_name, guess_lexer
+-from pygments.lexers import PythonLexer, Python3Lexer, PythonConsoleLexer, \
++from pygments.lexers import Python2Lexer, Python3Lexer, PythonConsoleLexer, \
+     CLexer, TextLexer, RstLexer
+ from pygments.styles import get_style_by_name
+ from pygments.util import ClassNotFound
+@@ -40,7 +40,7 @@ logger = logging.getLogger(__name__)
+ 
+ lexers = dict(
+     none = TextLexer(stripnl=False),
+-    python = PythonLexer(stripnl=False),
++    python = Python2Lexer(stripnl=False),
+     python3 = Python3Lexer(stripnl=False),
+     pycon = PythonConsoleLexer(stripnl=False),
+     pycon3 = PythonConsoleLexer(python3=True, stripnl=False),