Skip to content

Commit

Permalink
Add a rear cover page to a document template
Browse files Browse the repository at this point in the history
  • Loading branch information
brechtm committed Nov 4, 2022
1 parent 4d52286 commit 4a044d5
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
47 changes: 44 additions & 3 deletions tests_regression/sphinx/test-relativepaths/template/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

from rinoh import register_template
from rinoh.attribute import OverrideDefault, Var
from rinoh.dimension import CM
from rinoh.dimension import CM, PT
from rinoh.image import Image
from rinoh.layout import DownExpandingContainer, CONTENT, UpExpandingContainer
from rinoh.paragraph import Paragraph
from rinoh.stylesheets import sphinx_article
from rinoh.template import (DocumentTemplate, ContentsPartTemplate,
BodyPageTemplate, DocumentPartTemplate,
FixedDocumentPartTemplate)
FixedDocumentPartTemplate, PageBase, PageTemplateBase)
from rinoh.text import StyledText

from . page import MyTitlePageTemplate

Expand All @@ -19,23 +22,61 @@ def _flowables(self, document):
return iter([])


class RearCoverPageTemplate(PageTemplateBase):
def page(self, document_part, page_number, chain):
return RearCoverPage(document_part, self, page_number)


REAR_COVER_PAGE_TEXT = r"""
'Some '(style 1) 'Text\n'(style 2)
'Some '(style 1) 'Text'(style 2)
""".replace('\n', '') # work around rinohtype bug


class RearCoverPage(PageBase):
configuration_class = RearCoverPageTemplate

def __init__(self, document_part, template, page_number):
super().__init__(document_part, template, page_number)
margin = 2*CM
split = 20*CM
width = self.width - 2 * margin
self.text = UpExpandingContainer('text', CONTENT, self, left=margin,
bottom=split, width=width)
self.text << Paragraph(StyledText.from_string(REAR_COVER_PAGE_TEXT),
style='rear cover page text')
self.imgs = DownExpandingContainer('images', CONTENT, self,
left=margin, top=split, width=width)
self.imgs << Image(Path(__file__).parent / 'template.png',
width=2*CM, align='right')


class RearCoverPartTemplate(DocumentPartTemplate):
drop_if_empty = OverrideDefault(False)

def _flowables(self, document):
return iter([]) # content is static, placed in RearCoverPage


class MyDocumentTemplate(DocumentTemplate):
identifier = 'my_document_template'

stylesheet = OverrideDefault(sphinx_article)

parts = OverrideDefault(['title', 'front_matter', 'contents'])
parts = OverrideDefault(['title', 'front_matter', 'contents', 'rear_cover'])

# default document part templates
title = MyTitlePartTemplate()
front_matter = FixedDocumentPartTemplate(page_number_format='continue')
contents = ContentsPartTemplate(page_number_format='continue')
rear_cover = RearCoverPartTemplate()

# default page templates
page = BodyPageTemplate(page_size=Var('paper_size'))
title_page = MyTitlePageTemplate(base='page', top_margin=8*CM)
front_matter_page = BodyPageTemplate(base='page')
contents_page = BodyPageTemplate(base='page')
rear_cover_page = RearCoverPageTemplate(base='page')

TEMPLATE_IMAGE = Image(Path(__file__).parent / 'template.png')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@ header_text = $(header_text)
footer_text = IMAGE('template_configuration.png')
'\t'
'{PAGE_NUMBER} / {NUMBER_OF_PAGES}'

[rear_cover_page]
background = 'template_configuration.png', scale=fill

0 comments on commit 4a044d5

Please sign in to comment.