Skip to content

Embed: replace pyquery with selectolax #7988

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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.

63 changes: 13 additions & 50 deletions readthedocs/embed/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import pytest
from django_dynamic_fixture import get
from pyquery import PyQuery
from selectolax.parser import HTMLParser

from readthedocs.builds.constants import LATEST
from readthedocs.embed.views import do_embed
Expand Down Expand Up @@ -49,8 +49,15 @@ def _patch_sphinx_json_file(self, storage_mock, json_file, html_file):
)

def _get_html_content(self, html_file):
section_content = [PyQuery(html_file.open().read()).outerHtml()]
return section_content
content = HTMLParser(html_file.open().read())
# We override all links inside the embed,
# when doing so, the href attribute gets moved to the end.
# Do the same here.
for anchor in content.css('a'):
href = anchor.attributes.get('href')
if href and 'project.readthedocs.io' in href:
anchor.attrs['href'] = href
return content.body.child.html

@mock.patch('readthedocs.embed.views.build_media_storage')
def test_embed_unknown_section(self, storage_mock):
Expand Down Expand Up @@ -119,7 +126,7 @@ def test_embed_sphinx(self, storage_mock, section):
)

expected = {
'content': section_content,
'content': [section_content],
'headers': [
# TODO: return the full id here
{'I Need Secrets (or Environment Variables) in my Build': '#'},
Expand Down Expand Up @@ -175,7 +182,7 @@ def test_embed_sphinx_bibtex(self, storage_mock, section):
)

expected = {
'content': section_content,
'content': [section_content],
'headers': [
{'Getting Started': '#'},
{'Overview': '#overview'},
Expand Down Expand Up @@ -236,7 +243,7 @@ def test_embed_sphinx_glossary(self, storage_mock, section):
)

expected = {
'content': section_content,
'content': [section_content],
'headers': [
{'Glossary': '#'},
],
Expand All @@ -250,47 +257,3 @@ def test_embed_sphinx_glossary(self, storage_mock, section):
}

assert response.data == expected

@mock.patch('readthedocs.embed.views.build_media_storage')
def test_embed_mkdocs(self, storage_mock):
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()
)

self.version.documentation_type = MKDOCS
self.version.save()

response = do_embed(
project=self.project,
version=self.version,
doc='index',
section='Installation',
path='index.html',
)

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.data == expected
Loading