Repository: facelessuser/pymdown-extensions

Issue:
Disable linenums thoroughly
When using `pymdownx.highlight` and set `linenums` to `False`, users can still enable `linenums` like this:

<pre><code>
```linenums="1"
 code
```
</code></pre>

How can I prevent this?

Discussion:
facelessuser: @gir-bot lgtm

Patch:
diff --git a/docs/src/markdown/about/changelog.md b/docs/src/markdown/about/changelog.md
index aca9a3c7..a495ebb9 100644
--- a/docs/src/markdown/about/changelog.md
+++ b/docs/src/markdown/about/changelog.md
@@ -1,5 +1,11 @@
 # Changelog
 
+## 7.2
+
+- **NEW**: Highlight option `linenums` now defaults to `None` and accepts `None`, `True`, or `False`. `None` is
+  disabled by default, but can be enabled per code block. `True` enables line numbers globally. `False` disables
+  globally and cannot be enabled manually per code block.
+
 ## 7.1
 
 - **NEW**: SuperFences will now allow number ranges in `hl_lines` option. (#878)
diff --git a/docs/src/markdown/extensions/highlight.md b/docs/src/markdown/extensions/highlight.md
index 619601f9..df969b1f 100644
--- a/docs/src/markdown/extensions/highlight.md
+++ b/docs/src/markdown/extensions/highlight.md
@@ -101,7 +101,7 @@ Option                    | Type   | Default               | Description
 `pygments_style`          | string | `#!py3thon 'default'` | Set the Pygments' style to use.  This really only has an effect when used with `noclasses`.
 `noclasses`               | bool   | `#!py3 False`         | This will cause the styles to directly be written to the tag's style attribute instead of requiring a stylesheet.
 `use_pygments`            | bool   | `#!py3 True`          | Controls whether Pygments (if available) is used to style the code, or if the code will just be escaped and prepped for a JavaScript syntax highlighter.
-`linenums`                | bool   | `#!py3 False`         | Enable line numbers globally for *block* code.  This will be ignored for *inline* code.
+`linenums`                | bool   | `#!py3 None`          | Enable line numbers globally for *block* code.  This will be ignored for *inline* code. If set to `#!py3 False` line numbers will be disabled globally and can not be turned on, not even per code block.
 `linenums_special`        | int    | `#!py3 1`             | Globally sets the specified nth lines' gutter with the class "special".  This can be overridden in [SuperFences](./superfences.md) per fence if desired.
 `linenums_style`          | string | `#!py3 'table'`       | Controls the output style when `linenums` are enabled. Supported styles are Pygments default `table` and `inline`, but also supported is the pymdown-extensions `pymdownx-inline` which provides a special inline mode, see [Line Number Styles](#line-number-styles) for more info.
 `linenums_class`          | string | `#!py3 'linenums'`    | Controls the name of the line number class when Pygments is not used.
@@ -114,4 +114,9 @@ Option                    | Type   | Default               | Description
 !!! new "New 7.1"
     `linenums_class` was added in `7.1`.
 
+!!! new "New 7.2"
+    `linenums` now accepts `#!py3 None` as the default for allow line numbers to be enabled per code block.
+    `#!py3 False` now disables line numbers globally preventing line numbers even if specified per code block. `True`
+    still enables globally.
+
 --8<-- "refs.txt"
diff --git a/pymdownx/__meta__.py b/pymdownx/__meta__.py
index 50d4e201..b5f65787 100644
--- a/pymdownx/__meta__.py
+++ b/pymdownx/__meta__.py
@@ -185,5 +185,5 @@ def parse_version(ver, pre=False):
     return Version(major, minor, micro, release, pre, post, dev)
 
 
-__version_info__ = Version(7, 1, 0, "final")
+__version_info__ = Version(7, 2, 0, ".dev")
 __version__ = __version_info__._get_canonical()
diff --git a/pymdownx/highlight.py b/pymdownx/highlight.py
index f61f1694..aff651c4 100644
--- a/pymdownx/highlight.py
+++ b/pymdownx/highlight.py
@@ -65,7 +65,7 @@ DEFAULT_CONFIG = {
         'Default false'
     ],
     'linenums': [
-        False,
+        None,
         'Display line numbers in block code output (not inline) - Default: False'
     ],
     'linenums_style': [
@@ -183,7 +183,7 @@ class Highlight(object):
 
     def __init__(
         self, guess_lang=True, pygments_style='default', use_pygments=True,
-        noclasses=False, extend_pygments_lang=None, linenums=False, linenums_special=-1,
+        noclasses=False, extend_pygments_lang=None, linenums=None, linenums_special=-1,
         linenums_style='table', linenums_class='linenums', wrapcode=True
     ):
         """Initialize."""
@@ -254,12 +254,13 @@ class Highlight(object):
         """Highlight code."""
 
         class_names = classes[:] if classes else []
+        linenums_enabled = (self.linenums or (self.linenums is not False and linestart >= 0)) and not inline > 0
 
         # Convert with Pygments.
         if pygments and self.use_pygments:
             # Setup language lexer.
             lexer = self.get_lexer(src, language)
-            linenums = self.linenums_style if (self.linenums or linestart >= 0) and not inline > 0 else False
+            linenums = self.linenums_style if linenums_enabled else False
 
             if class_names:
                 css_class = ' {}'.format('' if not css_class else css_class)
@@ -311,7 +312,7 @@ class Highlight(object):
         else:
             # Format block code for a JavaScript Syntax Highlighter by specifying language.
             classes = class_names
-            linenums = self.linenums_style if (self.linenums or linestart >= 0) and not inline > 0 else False
+            linenums = self.linenums_style if linenums_enabled else False
             if language:
                 classes.append('language-%s' % language)
             class_str = ''


Test patch:
diff --git a/tests/test_extensions/test_highlight.py b/tests/test_extensions/test_highlight.py
index 137897dc..a8f4e3a3 100644
--- a/tests/test_extensions/test_highlight.py
+++ b/tests/test_extensions/test_highlight.py
@@ -271,3 +271,61 @@ class TestHighlightSpecial(util.MdCase):
             ''',  # noqa: E501
             True
         )
+
+
+class TestDisabledLinenums(util.MdCase):
+    """Test with line numbers globally disabled."""
+
+    extension = ['pymdownx.highlight', 'pymdownx.superfences']
+    extension_configs = {
+        'pymdownx.highlight': {
+            'linenums': False
+        }
+    }
+
+    def test_global_disable(self):
+        """Test with line numbers globally disabled."""
+
+        self.check_markdown(
+                r'''
+                ```python linenums="1"
+                import test
+                test.test()
+                ```
+                ''',
+                r'''
+                <div class="highlight"><pre><span></span><code><span class="kn">import</span> <span class="nn">test</span>
+                <span class="n">test</span><span class="o">.</span><span class="n">test</span><span class="p">()</span>
+                </code></pre></div>
+                ''',  # noqa: E501
+                True
+            )
+
+
+class TestDisabledLinenumsNoPygments(util.MdCase):
+    """Test with line numbers globally disabled with no Pygments."""
+
+    extension = ['pymdownx.highlight', 'pymdownx.superfences']
+    extension_configs = {
+        'pymdownx.highlight': {
+            'linenums': False,
+            'use_pygments': False
+        }
+    }
+
+    def test_global_disable(self):
+        """Test with line numbers globally disabled and no Pygments."""
+
+        self.check_markdown(
+                r'''
+                ```python linenums="1"
+                import test
+                test.test()
+                ```
+                ''',
+                r'''
+                <pre class="highlight"><code class="language-python">import test
+                test.test()</code></pre>
+                ''',  # noqa: E501
+                True
+            )