Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search: remove non-generic parser code #10676

Merged
merged 4 commits into from
Aug 31, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions readthedocs/embed/tests/data/mkdocs/latest/index.json

This file was deleted.

36 changes: 3 additions & 33 deletions readthedocs/embed/tests/test_api.py
Original file line number Diff line number Diff line change
@@ -281,14 +281,8 @@ def test_embed_sphinx(self, storage_mock, section, client):
assert response.data == expected
assert response['Cache-tag'] == 'project,project:latest'

@mock.patch('readthedocs.embed.views.build_media_storage')
def test_embed_mkdocs(self, storage_mock, client):
json_file = data_path / 'mkdocs/latest/index.json'
storage_mock.exists.return_value = True
storage_mock.open.side_effect = self._mock_open(
json_file.open().read()
)

def test_embed_mkdocs(self, client):
"""API v2 doesn't support mkdocs."""
self.version.documentation_type = MKDOCS
self.version.save()

@@ -303,31 +297,7 @@ def test_embed_mkdocs(self, storage_mock, client):
}
)

expected = {
'content': mock.ANY, # too long to compare here
'headers': [
{'Overview': 'overview'},
{'Installation': 'installation'},
{'Getting Started': 'getting-started'},
{'Adding pages': 'adding-pages'},
{'Theming our documentation': 'theming-our-documentation'},
{'Changing the Favicon Icon': 'changing-the-favicon-icon'},
{'Building the site': 'building-the-site'},
{'Other Commands and Options': 'other-commands-and-options'},
{'Deploying': 'deploying'},
{'Getting help': 'getting-help'},
],
'url': 'http://project.readthedocs.io/en/latest/index.html',
'meta': {
'project': 'project',
'version': 'latest',
'doc': 'index',
'section': 'Installation',
},
}

assert response.status_code == status.HTTP_200_OK
assert response.data == expected
assert response.status_code == status.HTTP_404_NOT_FOUND

def test_no_access(self, client, settings):
settings.RTD_DEFAULT_FEATURES = {}
59 changes: 3 additions & 56 deletions readthedocs/embed/views.py
Original file line number Diff line number Diff line change
@@ -145,6 +145,7 @@ def do_embed(*, project, version, doc=None, path=None, section=None, url=None):

content = None
headers = None
# Embed API v2 supports Sphinx only.
if version.is_sphinx_type:
file_content = _get_doc_content(
project=project,
@@ -160,18 +161,8 @@ def do_embed(*, project, version, doc=None, path=None, section=None, url=None):
url=url,
)
else:
# TODO: this should read from the html file itself,
# we don't have fjson files for mkdocs.
file_content = _get_doc_content(
project=project,
version=version,
doc=doc,
)
content, headers, section = parse_mkdocs(
content=file_content,
section=section,
url=url,
)
log.info("Using EmbedAPIv2 for a non Sphinx project.")
return None

if content is None:
return None
@@ -310,47 +301,3 @@ def dump(obj):

ret = [dump(clean_references(obj, url)) for obj in query_result]
return ret, headers, section


def parse_mkdocs(content, section, url): # pylint: disable=unused-argument
"""Get the embed content for the section."""
ret = []
headers = []

if not content or not content.get('content'):
return (None, None, section)

body = content['content']
for element in PQ(body)('h2'):
headers.append(recurse_while_none(element))

if not section and headers:
# If no section is sent, return the content of the first one
section = list(headers[0].keys())[0].lower()

if section:
body_obj = PQ(body)
escaped_section = escape_selector(section)
section_list = body_obj(
':header:contains("{title}")'.format(title=str(escaped_section)))
for num in range(len(section_list)):
header2 = section_list.eq(num)
# h2_title = h2.text().strip()
# section_id = h2.attr('id')
h2_content = ""
next_p = header2.next()
while next_p:
if next_p[0].tag == 'h2':
break
h2_html = next_p.outerHtml()
if h2_html:
h2_content += "\n%s\n" % h2_html
next_p = next_p.next()
if h2_content:
ret.append(h2_content)
# ret.append({
# 'id': section_id,
# 'title': h2_title,
# 'content': h2_content,
# })
return (ret, headers, section)
Loading