Skip to content

Commit a908684

Browse files
authored
Merge pull request #5785 from stsewd/index-with-real-path-name
Index path with original path name
2 parents 31c056c + 96a85fa commit a908684

File tree

5 files changed

+24
-20
lines changed

5 files changed

+24
-20
lines changed

readthedocs/projects/models.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -1231,19 +1231,19 @@ def get_processed_json(self):
12311231
Both lead to `foo/index.html`
12321232
https://github.com/rtfd/readthedocs.org/issues/5368
12331233
"""
1234-
paths = []
1234+
fjson_paths = []
12351235
basename = os.path.splitext(self.path)[0]
1236-
paths.append(basename + '.fjson')
1236+
fjson_paths.append(basename + '.fjson')
12371237
if basename.endswith('/index'):
12381238
new_basename = re.sub(r'\/index$', '', basename)
1239-
paths.append(new_basename + '.fjson')
1239+
fjson_paths.append(new_basename + '.fjson')
12401240

12411241
full_json_path = self.project.get_production_media_path(
12421242
type_='json', version_slug=self.version.slug, include_file=False
12431243
)
12441244
try:
1245-
for path in paths:
1246-
file_path = os.path.join(full_json_path, path)
1245+
for fjson_path in fjson_paths:
1246+
file_path = os.path.join(full_json_path, fjson_path)
12471247
if os.path.exists(file_path):
12481248
return process_file(file_path)
12491249
except Exception:

readthedocs/search/api.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import logging
22
from pprint import pformat
33

4-
from rest_framework import generics
5-
from rest_framework import serializers
4+
from rest_framework import generics, serializers
65
from rest_framework.exceptions import ValidationError
76
from rest_framework.pagination import PageNumberPagination
87

98
from readthedocs.search.faceted_search import PageSearch
109
from readthedocs.search.utils import get_project_list_or_404
1110

11+
1212
log = logging.getLogger(__name__)
1313

1414

readthedocs/search/documents.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ class PageDocument(RTDDocTypeMixin, DocType):
116116
project = fields.KeywordField(attr='project.slug')
117117
version = fields.KeywordField(attr='version.slug')
118118
path = fields.KeywordField(attr='processed_json.path')
119+
full_path = fields.KeywordField(attr='path')
119120

120121
# Searchable content
121122
title = fields.TextField(attr='processed_json.title')
@@ -153,7 +154,7 @@ def faceted_search(
153154

154155
def get_queryset(self):
155156
"""Overwrite default queryset to filter certain files to index."""
156-
queryset = super(PageDocument, self).get_queryset()
157+
queryset = super().get_queryset()
157158

158159
# Do not index files that belong to non sphinx project
159160
# Also do not index certain files

readthedocs/search/parse_json.py

+14-11
Original file line numberDiff line numberDiff line change
@@ -59,38 +59,41 @@ def generate_sections_from_pyquery(body):
5959
}
6060

6161

62-
def process_file(filename):
63-
"""Read a file from disk and parse it into a structured dict."""
62+
def process_file(fjson_filename):
63+
"""Read the fjson file from disk and parse it into a structured dict."""
6464
try:
65-
with codecs.open(filename, encoding='utf-8', mode='r') as f:
65+
with codecs.open(fjson_filename, encoding='utf-8', mode='r') as f:
6666
file_contents = f.read()
6767
except IOError:
68-
log.info('Unable to read file: %s', filename)
69-
return None
68+
log.info('Unable to read file: %s', fjson_filename)
69+
raise
7070
data = json.loads(file_contents)
7171
sections = []
72+
path = ''
7273
title = ''
7374
body_content = ''
75+
7476
if 'current_page_name' in data:
7577
path = data['current_page_name']
7678
else:
77-
log.info('Unable to index file due to no name %s', filename)
78-
return None
79-
if 'body' in data and data['body']:
79+
log.info('Unable to index file due to no name %s', fjson_filename)
80+
81+
if data.get('body'):
8082
body = PyQuery(data['body'])
8183
body_content = body.text().replace('¶', '')
8284
sections.extend(generate_sections_from_pyquery(body))
8385
else:
84-
log.info('Unable to index content for: %s', filename)
86+
log.info('Unable to index content for: %s', fjson_filename)
87+
8588
if 'title' in data:
8689
title = data['title']
8790
if title.startswith('<'):
8891
title = PyQuery(data['title']).text()
8992
else:
90-
log.info('Unable to index title for: %s', filename)
93+
log.info('Unable to index title for: %s', fjson_filename)
9194

9295
return {
93-
'headers': process_headers(data, filename),
96+
'headers': process_headers(data, fjson_filename),
9497
'content': body_content,
9598
'path': path,
9699
'title': title,

readthedocs/templates/search/elastic_search.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ <h3>
210210

211211
{% elif 'page' in result.meta.index %}
212212

213-
<a href="{% doc_url result.project|get_project result.version result.path %}?highlight={{ query }}">
213+
<a href="{% doc_url result.project|get_project result.version result.full_path %}?highlight={{ query }}">
214214
{{ result.project }} - {{ result.title }}
215215
</a>
216216
{% for fragment in result.meta.highlight.content %}

0 commit comments

Comments
 (0)