Skip to contentSkip to conversation

conf.py: tweak SearchLanguage to be hyphen-friendly

PATCH Superseded Enrico Jörns 3 participants 6 messages May 2, 2025 – May 20, 2025

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 conf.py: tweak SearchLanguage to be hyphen-friendly

This modifies the default indexer split() and js splitQuery() methods to support searching for words with hyphens.

While this might not be an ideal, rock-solid, or fully future-proof solution, it at least allows searching for strings that include hyphens, such as 'bitbake-layers', 'send-error-report', or 'oe-core'.

Below is a bit more detailed explanation of the two modifications done:

1) The default split regex in the sphinx-doc SearchLanguage class is:

| _word_re = re.compile(r'\w+')

which we simply extend to include hyphens '-'.

This will result in a searchindex.js that contains words with hyphens, too.

2) The 'searchtool.js' code notes for its splitQuery() implementation:

   | /**
   |  * Default splitQuery function. Can be overridden in ``sphinx.search`` with a
   |  * custom function per language.
   |  *
   |  * The regular expression works by splitting the string on consecutive characters
   |  * that are not Unicode letters, numbers, underscores, or emoji characters.
   |  * This is the same as ``\W+`` in Python, preserving the surrogate pair area.
   |  */
   | if (typeof splitQuery === "undefined") {
   |   var splitQuery = (query) => query
   |       .split(/[^\p{Letter}\p{Number}_\p{Emoji_Presentation}]+/gu)
   |       .filter(term => term)  // remove remaining empty strings
   | }

The hook for this is documented in the sphinx-docs 'SearchLanguage' class.

   |    .. attribute:: js_splitter_code
   |
   |       Return splitter function of JavaScript version.  The function should be
   |       named as ``splitQuery``.  And it should take a string and return list of
   |       strings.
   |
   |       .. versionadded:: 3.0

We use this to define a simplified splitQuery() function with a split argument that splits on empty spaces only.

[YOCTO #14534]

---
 documentation/conf.py | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
documentation/conf.py +21 -0
index 2aceeb8e7..02397cd20 100644
@@ -13,6 +13,7 @@
 # documentation root, use os.path.abspath to make it absolute, like shown here.
 #
 import os
+import re
 import sys
 import datetime
 try:
@@ -173,6 +174,26 @@ latex_elements = {
     'preamble': '\\usepackage[UTF8]{ctex}\n\\setcounter{tocdepth}{2}',
 }
 
+
+from sphinx.search import SearchLanguage
+from sphinx.search import languages
+class DashFriendlySearchLanguage(SearchLanguage):
+    lang = 'en'
+
+    # Accept words that can include hyphens
+    _word_re = re.compile(r'[\w-]+')
+
+    def split(self, input: str) -> list[str]:
+        return self._word_re.findall(input)
+
+    js_splitter_code = """
+function splitQuery(query) {
+    return query.split(/\\s+/g).filter(term => term.length > 0);
+}
+"""
+
+languages['en'] = DashFriendlySearchLanguage
+
 # Make the EPUB builder prefer PNG to SVG because of issues rendering Inkscape SVG
 from sphinx.builders.epub3 import Epub3Builder
 Epub3Builder.supported_image_types = ['image/png', 'image/gif', 'image/jpeg']
Signature · 1 line
2.39.5
#1 lore raw

Hi Enrico,

Quoted 68 lines · Enrico Jörns via lists.yoctoproject.org

This modifies the default indexer split() and js splitQuery() methods to support searching for words with hyphens.

While this might not be an ideal, rock-solid, or fully future-proof solution, it at least allows searching for strings that include hyphens, such as 'bitbake-layers', 'send-error-report', or 'oe-core'.

Below is a bit more detailed explanation of the two modifications done:

1) The default split regex in the sphinx-doc SearchLanguage class is:

| _word_re = re.compile(r'\w+')

which we simply extend to include hyphens '-'.

This will result in a searchindex.js that contains words with hyphens, too.

2) The 'searchtool.js' code notes for its splitQuery() implementation:

    | /**
    |  * Default splitQuery function. Can be overridden in ``sphinx.search`` with a
    |  * custom function per language.
    |  *
    |  * The regular expression works by splitting the string on consecutive characters
    |  * that are not Unicode letters, numbers, underscores, or emoji characters.
    |  * This is the same as ``\W+`` in Python, preserving the surrogate pair area.
    |  */
    | if (typeof splitQuery === "undefined") {
    |   var splitQuery = (query) => query
    |       .split(/[^\p{Letter}\p{Number}_\p{Emoji_Presentation}]+/gu)
    |       .filter(term => term)  // remove remaining empty strings
    | }

The hook for this is documented in the sphinx-docs 'SearchLanguage' class.

| .. attribute:: js_splitter_code
|
| Return splitter function of JavaScript version. The function should be
| named as ``splitQuery``. And it should take a string and return list of
| strings.
|
| .. versionadded:: 3.0

We use this to define a simplified splitQuery() function with a split argument that splits on empty spaces only.

[YOCTO #14534]

---
  documentation/conf.py | 21 +++++++++++++++++++++
  1 file changed, 21 insertions(+)
documentation/conf.py +1 -0
index 2aceeb8e7..02397cd20 100644
@@ -13,6 +13,7 @@
  # documentation root, use os.path.abspath to make it absolute, like shown here.
  #
  import os
+import re
  import sys
  import datetime
  try:

documentation/conf.py latex_elements = { · line 180

Quoted 7 lines
documentation/conf.py +4 -0
@@ -173,6 +174,26 @@ latex_elements = {
      'preamble': '\\usepackage[UTF8]{ctex}\n\\setcounter{tocdepth}{2}',
  }
 
+
+from sphinx.search import SearchLanguage
+from sphinx.search import languages
+class DashFriendlySearchLanguage(SearchLanguage):

Could you extend SearchEnglish instead, which is I believe what we should be using today?

documentation/conf.py latex_elements = { · line 182

Quoted 2 lines
documentation/conf.py +2 -0
+    lang = 'en'
+

This would then be redundant.

documentation/conf.py latex_elements = { · line 184

Quoted 2 lines
documentation/conf.py +2 -0
+    # Accept words that can include hyphens
+    _word_re = re.compile(r'[\w-]+')

I would recommend to have the dash as first character in case we ever want to expand this regex to avoid inadvertently making it a range, c.f. https://docs.python.org/3/library/re.html#module-re [] section. Or simply escape it so it's explicit what we're expecting from it :)

documentation/conf.py latex_elements = { · line 188

Quoted 4 lines
documentation/conf.py +4 -0
+
+    def split(self, input: str) -> list[str]:
+        return self._word_re.findall(input)
+

This is already what the split method from SearchLanguage does, therefore no need to override it.

documentation/conf.py latex_elements = { · line 191

Quoted 3 lines
documentation/conf.py +3 -0
+    js_splitter_code = """
+function splitQuery(query) {
+    return query.split(/\\s+/g).filter(term => term.length > 0);

Why not simply add '-' to the default splitQuery as in sphinx/themes/basic/static/searchtools.js so we keep as close as possible to the original behavior, just with the added dash?

Cheers,
Quentin

#2 lore raw

Hi quentin,

Quoted 74 lines · Quentin Schulz via lists.yoctoproject.org

Hi Enrico,

Quoted 71 lines · Enrico Jörns via lists.yoctoproject.org

This modifies the default indexer split() and js splitQuery() methods to support searching for words with hyphens.

While this might not be an ideal, rock-solid, or fully future-proof solution, it at least allows searching for strings that include hyphens, such as 'bitbake-layers', 'send-error-report', or 'oe-core'.

Below is a bit more detailed explanation of the two modifications done:

1) The default split regex in the sphinx-doc SearchLanguage class is:

| _word_re = re.compile(r'\w+')

which we simply extend to include hyphens '-'.

This will result in a searchindex.js that contains words with hyphens, too.

2) The 'searchtool.js' code notes for its splitQuery() implementation:

    | /**
    |  * Default splitQuery function. Can be overridden in ``sphinx.search`` with a
    |  * custom function per language.
    |  *
    |  * The regular expression works by splitting the string on consecutive characters
    |  * that are not Unicode letters, numbers, underscores, or emoji characters.
    |  * This is the same as ``\W+`` in Python, preserving the surrogate pair area.
    |  */
    | if (typeof splitQuery === "undefined") {
    |   var splitQuery = (query) => query
    |       .split(/[^\p{Letter}\p{Number}_\p{Emoji_Presentation}]+/gu)
    |       .filter(term => term)  // remove remaining empty strings
    | }

The hook for this is documented in the sphinx-docs 'SearchLanguage' class.

| .. attribute:: js_splitter_code
|
| Return splitter function of JavaScript version. The function should be
| named as ``splitQuery``. And it should take a string and return list of
| strings.
|
| .. versionadded:: 3.0

We use this to define a simplified splitQuery() function with a split argument that splits on empty spaces only.

[YOCTO #14534]

---
  documentation/conf.py | 21 +++++++++++++++++++++
  1 file changed, 21 insertions(+)
documentation/conf.py +1 -0
index 2aceeb8e7..02397cd20 100644
@@ -13,6 +13,7 @@
  # documentation root, use os.path.abspath to make it absolute, like shown here.
  #
  import os
+import re
  import sys
  import datetime
  try:
@@ -173,6 +174,26 @@ latex_elements = {
      'preamble': '\\usepackage[UTF8]{ctex}\n\\setcounter{tocdepth}{2}',
  }
 
Quoted 7 lines

documentation/conf.py latex_elements = { · line 180

Quoted 4 lines
documentation/conf.py +4 -0
+
+from sphinx.search import SearchLanguage
+from sphinx.search import languages
+class DashFriendlySearchLanguage(SearchLanguage):

Could you extend SearchEnglish instead, which is I believe what we should be using today?

A question on top of this: using SearchLanguage instead of SearchEnglish results in _not_ using the stopwords and stemmer code defined in SearchEnglish. Is that intentional? Maybe there is a good reason for not using SearchEnglish, but I'm curious.

Antonin

Signature · 3 lines
Antonin Godard, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#3 lore raw

Hi folks,

Am Montag, dem 12.05.2025 um 17:19 +0200 schrieb Antonin Godard:

Quoted 29 lines
Quoted 20 lines
Quoted 20 lines

---
  documentation/conf.py | 21 +++++++++++++++++++++

  1 file changed, 21 insertions(+)
documentation/conf.py +0 -0
index 2aceeb8e7..02397cd20 100644
@@ -13,6 +13,7 @@
   # documentation root, use os.path.abspath to make it absolute, like shown here.
   #

  import os
+import re
  import sys
  import datetime
  try:

documentation/conf.py +0 -0
@@ -173,6 +174,26 @@ latex_elements = {
       'preamble': '\\usepackage[UTF8]{ctex}\n\\setcounter{tocdepth}{2}',
   }
Quoted 8 lines

documentation/conf.py latex_elements = { · line 180

Quoted 5 lines

documentation/conf.py +4 -0
+
+from sphinx.search import SearchLanguage
+from sphinx.search import languages
+class DashFriendlySearchLanguage(SearchLanguage):

Could you extend SearchEnglish instead, which is I believe what we should be using today?

Quoted 4 lines

A question on top of this: using SearchLanguage instead of SearchEnglish results in _not_ using the stopwords and stemmer code defined in SearchEnglish. Is that intentional? Maybe there is a good reason for not using SearchEnglish, but I'm curious.

I have no strong opinion on this or what's better for a technical documentation.

Searching for 'effectiveness' in the documentation with stemmer enabled gives you results like 'effectively', etc. Surprisingly, the exact occurrence does not match for me anymore and the other results are not highlighted...

But maybe with the intention to be least invasive, using SearchEnglish is fine anyway.

Regards, Enrico

Quoted 2 lines

Antonin

Signature · 4 lines
Pengutronix e.K.                           | Enrico Jörns                |
Embedded Linux Consulting & Support        | https://www.pengutronix.de/ |
Steuerwalder Str. 21                       | Phone: +49-5121-206917-180  |
31137 Hildesheim, Germany                  | Fax:   +49-5121-206917-9    |
#4 lore raw
In reply to Quentin Schulz

Hi Quentin,

Am Montag, dem 05.05.2025 um 11:11 +0200 schrieb Quentin Schulz:

Quoted 19 lines
Quoted 19 lines

---
  documentation/conf.py | 21 +++++++++++++++++++++

  1 file changed, 21 insertions(+)
documentation/conf.py +0 -0
index 2aceeb8e7..02397cd20 100644
@@ -13,6 +13,7 @@
   # documentation root, use os.path.abspath to make it absolute, like shown here.
   #

  import os
+import re
  import sys
  import datetime
  try:

documentation/conf.py +0 -0
@@ -173,6 +174,26 @@ latex_elements = {
       'preamble': '\\usepackage[UTF8]{ctex}\n\\setcounter{tocdepth}{2}',
   }
Quoted 8 lines

documentation/conf.py latex_elements = { · line 180

Quoted 5 lines

documentation/conf.py +4 -0
+
+from sphinx.search import SearchLanguage
+from sphinx.search import languages
+class DashFriendlySearchLanguage(SearchLanguage):

Could you extend SearchEnglish instead, which is I believe what we should be using today?

yes, fine for me.

Quoted 4 lines
Quoted 2 lines

+    lang = 'en'
+

This would then be redundant.

Ok.

Quoted 7 lines
Quoted 2 lines

+    # Accept words that can include hyphens
+    _word_re = re.compile(r'[\w-]+')

I would recommend to have the dash as first character in case we ever want to expand this regex to avoid inadvertently making it a range, c.f.
https://docs.python.org/3/library/re.html#module-re [] section. Or simply escape it so it's explicit what we're expecting from it :)

Yes, might save us from later surprises.

Quoted 7 lines

documentation/conf.py latex_elements = { · line 188

Quoted 4 lines
documentation/conf.py +4 -0
+
+    def split(self, input: str) -> list[str]:
+        return self._word_re.findall(input)
+

This is already what the split method from SearchLanguage does, therefore no need to override it.

Indeed. No idea why I thought this is required.

Quoted 7 lines
Quoted 3 lines

+    js_splitter_code = """
+function splitQuery(query) {
+    return query.split(/\\s+/g).filter(term => term.length > 0);

Why not simply add '-' to the default splitQuery as in sphinx/themes/basic/static/searchtools.js so we keep as close as possible to the original behavior, just with the added dash?

Wasn't sure if including or excluding is better, but maybe having it closer to the original one makes sense.

Will send a v2 soon.

Regards,
Enrico

Quoted 3 lines

Cheers,
Quentin

Signature · 4 lines
Pengutronix e.K.                           | Enrico Jörns                |
Embedded Linux Consulting & Support        | https://www.pengutronix.de/ |
Steuerwalder Str. 21                       | Phone: +49-5121-206917-180  |
31137 Hildesheim, Germany                  | Fax:   +49-5121-206917-9    |
#5 lore raw
In reply to Enrico Jörns

Hi Enrico,

Quoted 84 lines · Enrico Jörns

This modifies the default indexer split() and js splitQuery() methods to support searching for words with hyphens.

While this might not be an ideal, rock-solid, or fully future-proof solution, it at least allows searching for strings that include hyphens, such as 'bitbake-layers', 'send-error-report', or 'oe-core'.

Below is a bit more detailed explanation of the two modifications done:

1) The default split regex in the sphinx-doc SearchLanguage class is:

| _word_re = re.compile(r'\w+')

which we simply extend to include hyphens '-'.

This will result in a searchindex.js that contains words with hyphens, too.

2) The 'searchtool.js' code notes for its splitQuery() implementation:

   | /**
   |  * Default splitQuery function. Can be overridden in ``sphinx.search`` with a
   |  * custom function per language.
   |  *
   |  * The regular expression works by splitting the string on consecutive characters
   |  * that are not Unicode letters, numbers, underscores, or emoji characters.
   |  * This is the same as ``\W+`` in Python, preserving the surrogate pair area.
   |  */
   | if (typeof splitQuery === "undefined") {
   |   var splitQuery = (query) => query
   |       .split(/[^\p{Letter}\p{Number}_\p{Emoji_Presentation}]+/gu)
   |       .filter(term => term)  // remove remaining empty strings
   | }

The hook for this is documented in the sphinx-docs 'SearchLanguage' class.

   |    .. attribute:: js_splitter_code
   |
   |       Return splitter function of JavaScript version.  The function should be
   |       named as ``splitQuery``.  And it should take a string and return list of
   |       strings.
   |
   |       .. versionadded:: 3.0

We use this to define a simplified splitQuery() function with a split argument that splits on empty spaces only.

[YOCTO #14534]

---
 documentation/conf.py | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
documentation/conf.py +14 -0
index 2aceeb8e7..02397cd20 100644
@@ -13,6 +13,7 @@
 # documentation root, use os.path.abspath to make it absolute, like shown here.
 #
 import os
+import re
 import sys
 import datetime
 try:
@@ -173,6 +174,26 @@ latex_elements = {
     'preamble': '\\usepackage[UTF8]{ctex}\n\\setcounter{tocdepth}{2}',
 }
 
+
+from sphinx.search import SearchLanguage
+from sphinx.search import languages
+class DashFriendlySearchLanguage(SearchLanguage):
+    lang = 'en'
+
+    # Accept words that can include hyphens
+    _word_re = re.compile(r'[\w-]+')
+
+    def split(self, input: str) -> list[str]:
+        return self._word_re.findall(input)
+
+    js_splitter_code = """

documentation/conf.py latex_elements = { · line 199

Quoted 10 lines
documentation/conf.py +7 -0
+function splitQuery(query) {
+    return query.split(/\\s+/g).filter(term => term.length > 0);
+}
+"""
+
+languages['en'] = DashFriendlySearchLanguage
+
 # Make the EPUB builder prefer PNG to SVG because of issues rendering Inkscape SVG
 from sphinx.builders.epub3 import Epub3Builder
 Epub3Builder.supported_image_types = ['image/png', 'image/gif', 'image/jpeg']

This is nice thanks. I've tested it with "bitbake-layers" or "ide-sdk", and it gives me much better results.

The searchindex.js generated file grows from 936 bytes to 1180 bytes, I think we can live with that though - just an observation.

Also a side-note: last time I checked, the kernel documentation had the same problem. Maybe a worthy contribution over there too?

Antonin

Signature · 3 lines
Antonin Godard, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com