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

PDF: use external links for non-PDF content. #110

Merged
merged 1 commit into from Nov 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/HISTORY.txt
Expand Up @@ -5,6 +5,7 @@ Changelog
4.0.2 (unreleased)
------------------

- PDF: use external links for non-PDF content. [jone]
- Remove "Type" column from listing block table in PDF. [jone]
- Fix navigation and search settings for new DX types. [jone]
- Fix chapter's title translation. [jone]
Expand Down
18 changes: 17 additions & 1 deletion ftw/book/latex/hyperlink_subconverter.py
Expand Up @@ -7,12 +7,28 @@
class BookHyperlinkConverter(hyperlink.HyperlinkConverter):

def latex_link(self, url, label, url_label):
if url.startswith(self.get_context().absolute_url()):
if self.is_included_in_book():
return self.latex_anchor(url, label)

return super(BookHyperlinkConverter, self).latex_link(url, label,
url_label)

def is_included_in_book(self):
# Use the raw url from the matcher so that we do not have yet
# any LaTeX conversions applied so that restrictedTraverse will work.
target_url = self.match.groups()[0].replace('%20', ' ')
current_url = self.get_context().absolute_url()
if not target_url.startswith(current_url):
return False

relative_path = target_url.replace(current_url, '').lstrip('/')
target_object = self.get_context().restrictedTraverse(relative_path, None)
if not target_object:
return False

# Only objects with LaTeX views are in the PDF and can be referenced.
return any(self.get_layout().get_views_for(target_object))

def latex_anchor(self, url, label):
context = self.get_context()
path = url.replace(context.absolute_url(),
Expand Down
2 changes: 2 additions & 0 deletions ftw/book/tests/__init__.py
Expand Up @@ -27,6 +27,8 @@ def setUp(self):
'introduction/an-html-block')
self.listingblock = self.example_book.restrictedTraverse(
'historical-background/china/important-documents')
self.lorem_file = self.listingblock.restrictedTraverse(
'lorem.html')
self.table = self.example_book.unrestrictedTraverse(
'historical-background/china/population')
self.textblock = self.example_book.unrestrictedTraverse(
Expand Down
21 changes: 18 additions & 3 deletions ftw/book/tests/test_latex_internal_hyperlinks.py
@@ -1,7 +1,6 @@
from Acquisition import aq_inner
from Acquisition import aq_parent
from ftw.book.tests import FunctionalTestCase
from plone.uuid.interfaces import IUUID


class TestBookInternalHyperlinksLaTeX(FunctionalTestCase):
Expand All @@ -14,6 +13,14 @@ def test_book_internal_hyperlinks_have_internal_reference(self):
'Link to {}!\n'.format(self.latex_hyperref_to(self.textblock2)),
self.get_latex_view_for(self.htmlblock).render())

def test_files_are_not_considered_as_internal_link(self):
self.htmlblock.content = ('Link to file {}!'.format(
self.html_link_to(self.lorem_file)))

self.assertEquals(
'Link to file {}!\n'.format(self.latex_link_to(self.lorem_file)),
self.get_latex_view_for(self.htmlblock).render())

def test_spaces_are_not_escaped(self):
self.grant('Manager')
aq_parent(aq_inner(self.textblock2)).manage_renameObject(
Expand All @@ -24,12 +31,20 @@ def test_spaces_are_not_escaped(self):
self.get_latex_view_for(self.htmlblock).render().strip())

def html_link_to(self, obj):
return '<a class="internal-link" href="resolveuid/{}">{}</a>'.format(
IUUID(self.textblock2), self.textblock2.Title())
return '<a class="internal-link" href="{}">{}</a>'.format(
obj.absolute_url(), obj.Title())

def latex_hyperref_to(self, obj):
convert = self.get_latex_layout(obj).get_converter().convert
return (r'\hyperref[path:%(path)s]{%(title)s\footnote{'
r'See page \pageref{path:%(path)s}}}') % {
'title': convert(obj.Title()),
'path': '/'.join(obj.getPhysicalPath())}

def latex_link_to(self, obj):
convert = self.get_latex_layout(self.example_book).get_converter().convert
return (r'\href{%(url)s}{%(label)s\footnote{\href{%(url)s}'
r'{\url{%(url_label)s}}}}') % {
'label': convert(obj.Title()),
'url': obj.absolute_url(),
'url_label': obj.absolute_url()}