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

Modify mwlib.rl to optionally include article headings as TOC entries #16

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions mwlib/rl/_version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
version = __version__ = "0.14.6"
__version_info__ = (0, 14, 6)
version = __version__ = "0.14.6.1"
__version_info__ = (0, 14, 6, 1)
7 changes: 4 additions & 3 deletions mwlib/rl/pagetemplates.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,10 @@ def _startBuild(self, filename=None, canvasmaker=canvas.Canvas):

type2lvl = {'chapter': 0,
'article': 1,
'heading2': 2,
'heading3': 3,
'heading4': 4,
'heading1': 2,
'heading2': 3,
'heading3': 4,
'heading4': 5,
}
got_chapter = False
last_lvl = 0
Expand Down
54 changes: 39 additions & 15 deletions mwlib/rl/pdfstyles.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@

show_title_page = True
show_title_page_footer = True
show_page_header = True
show_page_header = True
show_page_footer = True
page_break_after_article = False

Expand Down Expand Up @@ -100,9 +100,13 @@
# note that no TOC is generated if only one article is rendered
render_toc = True

# indicates the heading levels that should be included in the toc
# [0] = Chapter, [1] = Article, [2-5] = Heading 1-4
render_toc_levels = (1,1,0,1,1,1)

### TABLE CONFIG

tableOverflowTolerance = 20 # max width overflow for tables unit: pt
tableOverflowTolerance = 20 # max width overflow for tables unit: pt
cell_padding = 3
min_rows_for_break = 3 # page breaks before tables are only forced if more than n rows are present

Expand Down Expand Up @@ -198,7 +202,7 @@ def __init__(self, name, parent=None, **kw):
self.backColor = None
self.wordWrap = None
self.textTransform = None

def text_style(mode='p', indent_lvl=0, in_table=0, relsize='normal', text_align=None):
"""
mode: p (normal paragraph), blockquote, center (centered paragraph), footer, figure (figure caption text),
Expand Down Expand Up @@ -252,7 +256,7 @@ def text_style(mode='p', indent_lvl=0, in_table=0, relsize='normal', text_align=

if mode == 'img_attribution':
style.spaceBefore = 2

if mode in ['articlefoot', 'references']:
style.fontSize=small_font_size
style.leading=small_leading
Expand All @@ -263,11 +267,11 @@ def text_style(mode='p', indent_lvl=0, in_table=0, relsize='normal', text_align=
style.borderPadding = 3 # borderPadding is not calculated onto the box dimensions.
style.spaceBefore = 6 # therefore spaceBefore = 3 + borderPadding
style.spaceAfter = 9 # add an extra 3 to spaceAfter, b/c spacing seems to small otherwise

if mode == 'source' or mode == 'preformatted':
style.fontName = mono_font
style.fontName = mono_font
style.flowable = False

if mode == 'list' or mode == 'references':
style.spaceBefore = 0
style.bulletIndent = list_left_indent * max(0, indent_lvl-1)
Expand All @@ -281,7 +285,7 @@ def text_style(mode='p', indent_lvl=0, in_table=0, relsize='normal', text_align=
style.spaceBefore = 16
style.fontName= sans_font
style.alignment = TA_LEFT

if mode == 'booksubtitle':
style.fontSize = 24
style.leading = 30
Expand Down Expand Up @@ -313,16 +317,36 @@ def text_style(mode='p', indent_lvl=0, in_table=0, relsize='normal', text_align=
if mode == 'toc_group':
style.fontSize = 18
style.leading = 22

if mode == 'toc_chapter':
style.fontSize = 14
style.leading = 18

if mode == 'toc_article':
style.fontSize = 10
style.leading = 12
style.leftIndent = para_left_indent

if mode == 'toc_heading1':
style.fontSize = 10
style.leading = 12
style.leftIndent = para_left_indent * sum(render_toc_levels[1:3])

if mode == 'toc_heading2':
style.fontSize = 10
style.leading = 12
style.leftIndent = para_left_indent * sum(render_toc_levels[1:4])

if mode == 'toc_heading3':
style.fontSize = 10
style.leading = 12
style.leftIndent = para_left_indent * sum(render_toc_levels[1:5])

if mode == 'toc_heading4':
style.fontSize = 10
style.leading = 12
style.leftIndent = para_left_indent * sum(render_toc_levels[1:6])

return style

table_style = {'spaceBefore': 0.25*cm,
Expand All @@ -340,7 +364,7 @@ def __init__(self, name, parent=None, **kw):
self.leftIndent = 0
self.rightIndent = 0
self.firstLineIndent = 0
self.alignment = TA_LEFT
self.alignment = TA_LEFT
self.spaceBefore = 12
self.spaceAfter = 6
self.bulletFontName = serif_font
Expand All @@ -352,7 +376,7 @@ def __init__(self, name, parent=None, **kw):
self.textTransform = None
#self.allowWidows = 0
#self.allowOrphans = 0

def heading_style(mode='chapter', lvl=1, text_align=None):

style = BaseHeadingStyle(name='heading_style_%s_%d' % (mode, lvl))
Expand All @@ -372,7 +396,7 @@ def heading_style(mode='chapter', lvl=1, text_align=None):
style.spaceBefore = 20
style.spaceAfter = 2
elif mode == 'section':
lvl = max(min(5,lvl), 1)
lvl = max(min(5,lvl), 1)
style.fontSize = 18 - (lvl - 1) * 2
style.leading = style.fontSize + max(2, min(int(style.fontSize / 5), 3)) # magic: increase in leading is between 2 and 3 depending on fontsize...
style.spaceBefore = min(style.leading, 20)
Expand All @@ -398,10 +422,10 @@ def heading_style(mode='chapter', lvl=1, text_align=None):
style.alignment = TA_RIGHT
elif text_align == 'justify':
style.alignment = TA_JUSTIFY

style.prevent_post_pagebreak = True
return style


# import custom configuration to override configuration values
# if doing so, you need to be careful not to break things...
Expand Down
23 changes: 18 additions & 5 deletions mwlib/rl/rlwriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,8 @@ def writeChapter(self, chapter):

elements.append(self._getPageTemplate(''))
elements.extend([NotAtTopPageBreak(), hr, chapter_para, hr])
elements.append(TocEntry(txt=title, lvl='chapter'))
if pdfstyles.render_toc_levels[0]==1:
elements.append(TocEntry(txt=title, lvl='chapter'))
elements.append(self._getPageTemplate(chapter.next_article_title))
elements.extend(self.renderChildren(chapter))

Expand All @@ -680,14 +681,23 @@ def writeSection(self, obj):
heading_txt = ''
self.formatter.sectiontitle_mode = False

if 1 <= lvl <= 4 and self.inline_mode == 0 and self.table_nesting==0:
try:
render_toc_level = (pdfstyles.render_toc_levels[lvl+1]==1)
except IndexError:
render_toc_level = False


if 1 <= lvl <= 5 and self.inline_mode == 0 and self.table_nesting==0 and render_toc_level is True:
anchor = '<a name="%d"/>' % len(self.bookmarks)
bm_type = 'article' if lvl==1 else 'heading%s' % lvl
self.bookmarks.append((obj.children[0].getAllDisplayText(), bm_type))
else:
anchor = ''
elements = [Paragraph('<font name="%s"><b>%s</b></font>%s' % (headingStyle.fontName, heading_txt, anchor), headingStyle)]

if 2 <= lvl <= 5 and render_toc_level is True:
elements.append(TocEntry(txt=heading_txt, lvl='heading%s' % lvl))

if self.table_size_calc == 0:
obj.removeChild(obj.children[0])
elements.extend(self.renderMixed(obj))
Expand Down Expand Up @@ -727,7 +737,8 @@ def writeArticleMetainfo(self):
elements = []
title = self.formatter.cleanText(_('Article Sources and Contributors'))
elements.append(Paragraph('<b>%s</b>' % title, heading_style(mode='article')))
elements.append(TocEntry(txt=title, lvl='article'))
if pdfstyles.render_toc_levels[1]==1:
elements.append(TocEntry(txt=title, lvl='article'))
for title, url, authors in self.article_meta_info:
authors_text = self._filterAnonIpEdits(authors)
txt = '<b>%(title)s</b> &nbsp;<i>%(source_label)s</i>: %(source)s &nbsp;<i>%(contribs_label)s</i>: %(contribs)s ' % {
Expand All @@ -746,7 +757,8 @@ def writeImageMetainfo(self):
elements = []
title = self.formatter.cleanText(_('Image Sources, Licenses and Contributors'))
elements.append(Paragraph('<b>%s</b>' % title, heading_style(mode='article')))
elements.append(TocEntry(txt=title, lvl='article'))
if pdfstyles.render_toc_levels[1]==1:
elements.append(TocEntry(txt=title, lvl='article'))
for _id, title, url, license, authors in sorted(self.img_meta_info.values()):
authors_text = self._filterAnonIpEdits(authors)
if not license:
Expand Down Expand Up @@ -830,7 +842,8 @@ def writeArticle(self, article):

heading_para = Paragraph('<b>%s</b>%s' % (title, heading_anchor), heading_style("article"))
elements.append(heading_para)
elements.append(TocEntry(txt=title, lvl='article'))
if pdfstyles.render_toc_levels[1]==1:
elements.append(TocEntry(txt=title or article.caption, lvl='article'))

if pdfstyles.show_article_hr:
elements.append(HRFlowable(width='100%', hAlign='LEFT', thickness=1, spaceBefore=0, spaceAfter=10, color=colors.black))
Expand Down
8 changes: 4 additions & 4 deletions mwlib/rl/toc.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def __init__(self):
font_switcher.registerFontDefinitionList(fontconfig.fonts)
font_switcher.registerReportlabFonts(fontconfig.fonts)


def build(self, pdfpath, toc_entries, has_title_page=False, rtl=False):
outpath = os.path.dirname(pdfpath)
tocpath = os.path.join(outpath, 'toc.pdf')
Expand All @@ -35,11 +35,11 @@ def build(self, pdfpath, toc_entries, has_title_page=False, rtl=False):
return self.combinePdfs(pdfpath, tocpath, finalpath, has_title_page)

def _getColWidths(self):
p = Paragraph('<b>%d</b>' % 9999, pdfstyles.text_style(mode='toc_article', text_align='right'))
p = Paragraph('<b>%d</b>' % 9999, pdfstyles.text_style(mode='toc_article', text_align='right'))
w, h = p.wrap(0, pdfstyles.print_height)
# subtracting 30pt below is *probably* necessary b/c of the table margins
return [pdfstyles.print_width - w - 30, w]

def renderToc(self, tocpath, toc_entries, rtl):
doc = SimpleDocTemplate(tocpath, pagesize=(pdfstyles.page_width, pdfstyles.page_height))
elements = []
Expand All @@ -48,7 +48,7 @@ def renderToc(self, tocpath, toc_entries, rtl):
styles = []
col_widths = self._getColWidths()
for row_idx, (lvl, txt, page_num) in enumerate(toc_entries):
if lvl == 'article':
if lvl == 'article' or lvl.startswith('heading'):
page_num = str(page_num)
elif lvl == 'chapter':
page_num = '<b>%d</b>' % page_num
Expand Down