Repository: facelessuser/pymdown-extensions

Issue:
Allow overriding language guessing in InlineHilite
### Description

The language guessing in InlineHilite is controlled by `highlight.guess_lang`, and cannot be turned off as long as that option is true. The current `style_plain_text` option allows one to override `guess_lang` to true, but not the other way around.

### Benefits

It should be a legitimate use case where language guessing is desired for code blocks but not for inline code.

### Solution Idea

`style_plain_text` should be deprecated in favor of another option (perhaps also named `guess_lang`) which would exclusively control the language guessing behaviour for InlineHilite.

Discussion:

Patch:
diff --git a/docs/src/markdown/extensions/highlight.md b/docs/src/markdown/extensions/highlight.md
index 1f6d3332..96814eea 100644
--- a/docs/src/markdown/extensions/highlight.md
+++ b/docs/src/markdown/extensions/highlight.md
@@ -110,7 +110,7 @@ apply when Pygments is disabled. Many of these options are demonstrated in the [
 Option                    | Type   | Default               | Description
 ------------------------- | ------ | ----------------------| -----------
 `css_class`               | string | `#!py3 'highlight`    | Default class to apply to the wrapper element on code blocks. Other extensions can override this.
-`guess_lang`              | bool   | `#!py3 False`         | Guess what syntax language should be used if no language is specified. 
+`guess_lang`              | bool   | `#!py3 False`         | Guess what syntax language should be used if no language is specified. `#!py3 True` for always, `#!py3 False` for never, `#!py3 'block'` for block code only, and `#!py3 'inline'` for inline code only.
 `pygments_style`          | string | `#!py3 '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.
diff --git a/pymdownx/highlight.py b/pymdownx/highlight.py
index 22442f23..2d756fc9 100644
--- a/pymdownx/highlight.py
+++ b/pymdownx/highlight.py
@@ -52,7 +52,7 @@ DEFAULT_CONFIG = {
         'Default: True'
     ],
     'guess_lang': [
-        False,
+        0,
         "Automatic language detection - Default: False"
     ],
     'css_class': [
@@ -274,7 +274,7 @@ class Highlight(object):
 
         return self.extend_pygments_lang.get(language.lower(), (language, {}))
 
-    def get_lexer(self, src, language):
+    def get_lexer(self, src, language, inline):
         """Get the Pygments lexer."""
 
         name = language
@@ -291,7 +291,7 @@ class Highlight(object):
             lexer = None
 
         if lexer is None:
-            if self.guess_lang:
+            if (self.guess_lang is True) or (self.guess_lang == 'inline' if inline else self.guess_lang == 'block'):
                 try:
                     lexer = guess_lexer(src)
                     name = lexer.aliases[0]
@@ -332,7 +332,7 @@ class Highlight(object):
                 raise RuntimeError('Pymdownx Highlight requires at least Pygments 2.12+ if enabling Pygments')
 
             # Setup language lexer.
-            lexer, lang_name = self.get_lexer(src, language)
+            lexer, lang_name = self.get_lexer(src, language, inline)
             if self.pygments_lang_class:
                 class_names.insert(0, self.language_prefix + lang_name)
             linenums = self.linenums_style if linenums_enabled else False


Test patch:
diff --git a/tests/test_extensions/test_highlight.py b/tests/test_extensions/test_highlight.py
index a4d57218..733ab250 100644
--- a/tests/test_extensions/test_highlight.py
+++ b/tests/test_extensions/test_highlight.py
@@ -31,6 +31,47 @@ class TestHighlightGuess(util.MdCase):
         )
 
 
+class TestHighlightGuessBlock(util.MdCase):
+    """Test that highlighting works with guessing for block."""
+
+    extension = ['pymdownx.highlight', 'pymdownx.superfences']
+    extension_configs = {
+        'pymdownx.highlight': {
+            'guess_lang': "block"
+        }
+    }
+
+    def test_guess_block(self):
+        """Test guessing for block."""
+
+        self.check_markdown(
+            r'''
+            ```
+            import test
+            test.test()
+            ```
+            ''',
+            '''
+            <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>
+            ''',
+            True
+        )
+
+    def test_no_guess_inline(self):
+        """Test inline code is not language guessed."""
+        self.check_markdown(
+            r'''
+            `int i = std::numeric_limits<int>::min();`
+            ''',
+            '''
+            <p><code>int i = std::numeric_limits&lt;int&gt;::min();</code></p>
+            ''',
+            True
+        )
+
+
 class TestHighlightAutoTitle(util.MdCase):
     """Test title cases."""
 
diff --git a/tests/test_extensions/test_inlinehilite.py b/tests/test_extensions/test_inlinehilite.py
index 4dec349d..5c0b50ae 100644
--- a/tests/test_extensions/test_inlinehilite.py
+++ b/tests/test_extensions/test_inlinehilite.py
@@ -228,6 +228,61 @@ class TestInlineHiliteGuess(util.MdCase):
         )
 
 
+class TestInlineHiliteGuessInline(util.MdCase):
+    """Test inline highlight with guessing set to be inline only."""
+
+    extension = [
+        'pymdownx.highlight',
+        'pymdownx.inlinehilite',
+        'pymdownx.superfences'
+    ]
+    extension_configs = {
+        'pymdownx.highlight': {
+            'guess_lang': 'inline'
+        },
+        'pymdownx.inlinehilite': {
+            'css_class': 'inlinehilite',
+            'style_plain_text': True
+        }
+    }
+
+    def test_guessing_inline(self):
+        """Ensure guessing can be enabled for inline only."""
+
+        self.check_markdown(
+            r'`import module`.',
+            r'<p><code class="inlinehilite"><span class="kn">import</span> <span class="nn">module</span></code>.</p>'
+        )
+
+    def test_no_guessing_block(self):
+        """Ensure block is not guessed when set as inline only."""
+
+        self.check_markdown(
+            r'''
+            ```
+            <!DOCTYPE html>
+            <html>
+            <body>
+            <h1>My great test</h1>
+            <p>Thou shalt be re-educated through labour should this test ever fails.</p>
+            </body>
+            </html>
+            ```
+            ''',
+            r'''
+            <div class="highlight"><pre><span></span><code>&lt;!DOCTYPE html&gt;
+            &lt;html&gt;
+            &lt;body&gt;
+            &lt;h1&gt;My great test&lt;/h1&gt;
+            &lt;p&gt;Thou shalt be re-educated through labour should this test ever fails.&lt;/p&gt;
+            &lt;/body&gt;
+            &lt;/html&gt;
+            </code></pre></div>
+            ''',
+            True
+        )
+
+
 class TestInlineHiliteCodeHilite(util.MdCase):
     """Test inline highlight with CodeHilite."""