-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #104 from openzim/table_of_content
Rewrite libretexts.org Table of Content pages
- Loading branch information
Showing
6 changed files
with
214 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from jinja2 import Template | ||
from zimscraperlib.rewriting.html import HtmlRewriter | ||
|
||
from mindtouch2zim.client import LibraryPage, MindtouchClient | ||
from mindtouch2zim.libretexts.errors import BadBookPageError | ||
|
||
""" | ||
Logic here and in the Jinja2 template comes from | ||
https://cdn.libretexts.net/github/LibreTextsMain/DynamicTOC/dist/dynamicTOC.min.js | ||
Probably coming from | ||
https://github.com/LibreTexts/Libretext/blob/master/public/DynamicTOC/dynamicTOC.js and | ||
https://github.com/LibreTexts/Libretext/blob/master/public/DynamicTOC/dynamicTOC.css | ||
""" | ||
|
||
|
||
def _render_html_from_data(jinja2_template: Template, cover_page: LibraryPage) -> str: | ||
return jinja2_template.render(cover_page=cover_page) | ||
|
||
|
||
def rewrite_table_of_content( | ||
rewriter: HtmlRewriter, | ||
jinja2_template: Template, | ||
mindtouch_client: MindtouchClient, | ||
page: LibraryPage, | ||
) -> str: | ||
""" | ||
Get and statically rewrite the table of content of libretexts.org | ||
""" | ||
|
||
cover_page = mindtouch_client.get_cover_page(page) | ||
if cover_page is None: | ||
raise BadBookPageError() | ||
return rewriter.rewrite( | ||
_render_html_from_data(jinja2_template=jinja2_template, cover_page=cover_page) | ||
).content |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
scraper/src/mindtouch2zim/templates/libretexts.table-of-content.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
<style> | ||
#dynamicTOC { | ||
--toc-font-family: 'Lato', Arial, Helvetica, sans-serif; | ||
} | ||
|
||
#dynamicTOC_title_container { | ||
display: none; | ||
} | ||
|
||
.toc li { | ||
margin: 0.25em 0 0; | ||
} | ||
|
||
.chapter_level { | ||
list-style: none; | ||
} | ||
|
||
.section_level { | ||
list-style-type: circle; | ||
} | ||
|
||
.chapter_entry { | ||
font-size: 1.5rem !important; | ||
font-family: var(--toc-font-family) !important; | ||
} | ||
|
||
.content_entry { | ||
font-size: 1.1rem !important; | ||
font-family: var(--toc-font-family) !important; | ||
} | ||
|
||
.matter_entry { | ||
font-size: 1.25rem !important; | ||
font-family: var(--toc-font-family) !important; | ||
} | ||
|
||
@media print { | ||
#title { | ||
display: none; | ||
} | ||
|
||
#dynamicTOC_title_container { | ||
display: flex; | ||
background: #127bc4; | ||
margin: 0 0 2%; | ||
padding: 0; | ||
width: 100%; | ||
align-items: center; | ||
} | ||
|
||
#dynamicTOC_title_heading { | ||
color: white !important; | ||
font-size: 1.6em !important; | ||
font-family: 'Lato', Arial, serif !important; | ||
text-transform: uppercase; | ||
font-weight: bold; | ||
margin: 0 0 0 1% !important; | ||
padding: 1% 0 !important; | ||
letter-spacing: 0.05em !important; | ||
} | ||
|
||
.content_entry, | ||
.chapter_entry, | ||
.matter_entry { | ||
font-family: var(--toc-font-family) !important; | ||
} | ||
|
||
.chapter_entry { | ||
font-size: 1.25rem !important; | ||
} | ||
|
||
.content_entry { | ||
font-size: 0.75rem !important; | ||
} | ||
|
||
.matter_entry { | ||
font-size: 1rem !important; | ||
margin-bottom: 0.5em !important; | ||
} | ||
} | ||
</style> | ||
<div id="dynamicTOC" aria-live="polite" aria-busy="false"> | ||
<ul class="toc chapter_level"> | ||
{% for chapter in cover_page.children %} {% if chapter.title in ["Front Matter", "Back Matter"] | ||
%} {% for content in chapter.children %} {% if content.title not in ["TitlePage", "InfoPage", | ||
"Table of Contents"] %} | ||
<li> | ||
<h2 class="matter_entry"> | ||
<a href="{{ content.encoded_url }}">{{ content.title }}</a> | ||
</h2> | ||
</li> | ||
{% endif %} {% endfor %} {% else %} | ||
<li> | ||
<h2 class="{{ 'chapter_entry' if chapter.children else 'matter_entry' }}"> | ||
<a href="{{ chapter.encoded_url }}">{{ chapter.title }}</a> | ||
</h2> | ||
{% if chapter.children %} | ||
<ul class="section_level"> | ||
{% for content in chapter.children %} | ||
<li> | ||
<span class="content_entry"> | ||
<a href="{{ content.encoded_url }}">{{ content.title }}</a> | ||
</span> | ||
</li> | ||
{% endfor %} | ||
</ul> | ||
{% endif %} | ||
</li> | ||
{% endif %} {% endfor %} | ||
</ul> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
scraper/tests-integration/libretexts/test_table_of_content.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import pytest | ||
from jinja2 import Environment, FileSystemLoader, select_autoescape | ||
|
||
from mindtouch2zim.client import LibraryPage, MindtouchClient | ||
from mindtouch2zim.constants import ROOT_DIR | ||
from mindtouch2zim.libretexts.table_of_content import _render_html_from_data | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def page_data(client: MindtouchClient) -> LibraryPage: | ||
cover_page = client.get_cover_page_id("15839") | ||
assert cover_page | ||
return client.get_page_tree(cover_page).root | ||
|
||
|
||
def test_render_table_of_content_template(page_data: LibraryPage): | ||
jinja2_env = Environment( | ||
loader=FileSystemLoader(ROOT_DIR.joinpath("templates")), | ||
autoescape=select_autoescape(), | ||
) | ||
template = jinja2_env.get_template("libretexts.table-of-content.html") | ||
assert _render_html_from_data(template, page_data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters