Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
oprypin committed Oct 10, 2021
1 parent 987376c commit f007a0d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 34 deletions.
35 changes: 11 additions & 24 deletions mkdocs/contrib/search/search_index.py
Expand Up @@ -57,21 +57,12 @@ def add_entry_from_context(self, page):
tags.
"""

title_parts = [page.title]
if self.config['full_path_in_title']:
page_ancestors = []
full_title = ''
for ancestor in page.ancestors:
title_parts.insert(0, ancestor.title)

if len(page.ancestors):
for S in page.ancestors:
page_ancestors.insert(0, S.title)

hierarchy_line = " / ".join(page_ancestors)

full_title = hierarchy_line + " / " + page.title

page_title = full_title
else:
page_title = page.title
page_title = ' / '.join(title_parts)

# Create the content parser and feed in the HTML for the
# full page. This handles all the parsing and prepares
Expand All @@ -93,13 +84,14 @@ def add_entry_from_context(self, page):
)

if self.config['indexing'] in ['full', 'sections']:
for section in parser.data:
if self.config['full_path_in_title'] and parser.data.index(section) == 0:
self.create_entry_for_section(section, page.toc, url, page_title)
for i, section in enumerate(parser.data):
if self.config['full_path_in_title'] and i == 0:
section_title = page_title
else:
self.create_entry_for_section(section, page.toc, url, '')
section_title = None
self.create_entry_for_section(section, page.toc, url, section_title)

def create_entry_for_section(self, section, toc, abs_url, title):
def create_entry_for_section(self, section, toc, abs_url, title=None):
"""
Given a section on the page, the table of contents and
the absolute url for the page create an entry in the
Expand All @@ -110,13 +102,8 @@ def create_entry_for_section(self, section, toc, abs_url, title):

text = ' '.join(section.text) if self.config['indexing'] == 'full' else ''
if toc_item is not None:
if len(title):
title = title
else:
title = toc_item.title

self._add_entry(
title=title,
title=title or toc_item.title,
text=text,
loc=abs_url + toc_item.url
)
Expand Down
20 changes: 10 additions & 10 deletions mkdocs/tests/search_tests.py
Expand Up @@ -90,8 +90,8 @@ def test_plugin_config_lang(self):
'separator': r'[\s\-]+',
'min_search_length': 3,
'prebuild_index': False,
'full_path_in_title': False,
'indexing': 'full'
'indexing': 'full',
'full_path_in_title': False
}
plugin = search.SearchPlugin()
errors, warnings = plugin.load_config({'lang': 'es'})
Expand All @@ -105,8 +105,8 @@ def test_plugin_config_separator(self):
'separator': r'[\s\-\.]+',
'min_search_length': 3,
'prebuild_index': False,
'full_path_in_title': False,
'indexing': 'full'
'indexing': 'full',
'full_path_in_title': False
}
plugin = search.SearchPlugin()
errors, warnings = plugin.load_config({'separator': r'[\s\-\.]+'})
Expand All @@ -120,8 +120,8 @@ def test_plugin_config_min_search_length(self):
'separator': r'[\s\-]+',
'min_search_length': 2,
'prebuild_index': False,
'full_path_in_title': False,
'indexing': 'full'
'indexing': 'full',
'full_path_in_title': False
}
plugin = search.SearchPlugin()
errors, warnings = plugin.load_config({'min_search_length': 2})
Expand All @@ -135,8 +135,8 @@ def test_plugin_config_prebuild_index(self):
'separator': r'[\s\-]+',
'min_search_length': 3,
'prebuild_index': True,
'full_path_in_title': False,
'indexing': 'full'
'indexing': 'full',
'full_path_in_title': False
}
plugin = search.SearchPlugin()
errors, warnings = plugin.load_config({'prebuild_index': True})
Expand All @@ -150,8 +150,8 @@ def test_plugin_config_indexing(self):
'separator': r'[\s\-]+',
'min_search_length': 3,
'prebuild_index': False,
'full_path_in_title': False,
'indexing': 'titles'
'indexing': 'titles',
'full_path_in_title': False
}
plugin = search.SearchPlugin()
errors, warnings = plugin.load_config({'indexing': 'titles'})
Expand Down

0 comments on commit f007a0d

Please sign in to comment.