-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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 #9902 from cdrini/feature/toc-authors-etc
Add authors, subtitle, and description to TOC on books page
- Loading branch information
Showing
6 changed files
with
139 additions
and
54 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
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
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 |
---|---|---|
@@ -1,31 +1,38 @@ | ||
$def with (table_of_contents, ocaid=None, highlighting=False, cls='', attrs='') | ||
$def with (table_of_contents, ocaid=None, cls='', attrs='') | ||
|
||
$ min_level = min(chapter.level for chapter in table_of_contents) | ||
<div class="toc $cls" $:attrs> | ||
$for chapter in table_of_contents: | ||
$ is_link = ocaid and chapter.pagenum and chapter.pagenum.isdigit() | ||
$ tag = 'a' if is_link else 'div' | ||
<$tag | ||
<div | ||
class="toc__entry" | ||
$:cond(is_link, 'href="//archive.org/details/%s/page/%s"' % (ocaid, chapter.pagenum)) | ||
$:cond(is_link, 'data-ol-link-track="BookPage|TOCClick"') | ||
data-level="$chapter.level" | ||
style="margin-left:$((chapter.level - min_level) * 2)ch" | ||
> | ||
<span class="toc__name"> | ||
$ label = chapter.label | ||
$if label and not label.endswith('.'): | ||
$ label = label.strip() + '. ' | ||
$if highlighting: | ||
$# This isn't html injection, because solr returns everything already html escaped except for the em of the highlight | ||
$:label | ||
$:chapter.title | ||
$else: | ||
$label | ||
$chapter.title | ||
</span> | ||
$if chapter.pagenum: | ||
<span class="toc__dots" style="flex:1; border-bottom: 1px dotted;"></span> | ||
<span class="toc__pagenum">$_('Page %s', chapter.pagenum)</span> | ||
</$tag> | ||
$ is_link = ocaid and chapter.pagenum and chapter.pagenum.isdigit() | ||
$ tag = 'a' if is_link else 'div' | ||
<$tag | ||
class="toc__main" | ||
$:cond(is_link, 'href="//archive.org/details/%s/page/%s"' % (ocaid, chapter.pagenum)) | ||
$:cond(is_link, 'data-ol-link-track="BookPage|TOCClick"') | ||
> | ||
<div class="toc__name"> | ||
$ label = chapter.label | ||
$if label and not label.endswith('.'): | ||
$ label = label.strip() + '. ' | ||
|
||
<div class="toc__title">$label $chapter.title</div> | ||
|
||
$if chapter.subtitle: | ||
<div class="toc__subtitle">$chapter.subtitle</div> | ||
$if chapter.authors: | ||
<div class="toc__authors">$:macros.BookByline(chapter.authors)</div> | ||
</div> | ||
$if chapter.pagenum: | ||
<span class="toc__dots"></span> | ||
<span class="toc__pagenum">$_('Page %s', chapter.pagenum)</span> | ||
</$tag> | ||
|
||
$if chapter.description: | ||
<div class="toc__description">$chapter.description</div> | ||
</div> | ||
</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
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,40 @@ | ||
from dataclasses import dataclass | ||
from typing import TypedDict | ||
|
||
from openlibrary.core.models import ThingReferenceDict | ||
|
||
|
||
class AuthorRecord(TypedDict): | ||
name: str | ||
author: ThingReferenceDict | None | ||
|
||
|
||
@dataclass | ||
class TocEntry: | ||
level: int | ||
label: str | None = None | ||
title: str | None = None | ||
pagenum: str | None = None | ||
|
||
authors: list[AuthorRecord] | None = None | ||
subtitle: str | None = None | ||
description: str | None = None | ||
|
||
@staticmethod | ||
def from_dict(d: dict) -> 'TocEntry': | ||
return TocEntry( | ||
level=d.get('level', 0), | ||
label=d.get('label'), | ||
title=d.get('title'), | ||
pagenum=d.get('pagenum'), | ||
authors=d.get('authors'), | ||
subtitle=d.get('subtitle'), | ||
description=d.get('description'), | ||
) | ||
|
||
def is_empty(self) -> bool: | ||
return all( | ||
getattr(self, field) is None | ||
for field in self.__annotations__ | ||
if field != 'level' | ||
) |
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