Tables and unnumbered headings

This commit is contained in:
RunasSudo 2019-09-09 22:39:45 +10:00
parent 8422daee6b
commit 8cf780813a
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
3 changed files with 146 additions and 9 deletions

View File

@ -35,6 +35,7 @@ class LaTeXRenderer(mistletoe.latex_renderer.LaTeXRenderer):
def __init__(self, *extras):
super().__init__(*extras)
self.render_map['NumberedHeading'] = self.render_numbered_heading
self.render_map['Subrules'] = self.render_subrules
self.render_map['SubrulesItem'] = self.render_subrules_item
self.render_map['Note'] = self.render_note
@ -43,11 +44,31 @@ class LaTeXRenderer(mistletoe.latex_renderer.LaTeXRenderer):
self.heading_last = False
def render_raw_text(self, token):
result = super().render_inner(token)
result = super().render_raw_text(token)
result = result.replace('★★★', r'\texorpdfstring{\freeserif ★★★}{★★★}')
return result
def render_heading(self, token):
if token.level == 1:
heading_last, self.heading_last = self.heading_last, True
return format(r'{\par\vspace{1cm plus 0.3cm minus 0.3cm}\bfseries\fontsize{13pt}{15pt}\selectfont\centering\uppercase{<content>}\phantomsection\addcontentsline{toc}{section}{<content>}\nopagebreak\par}',
content=self.render_inner(token)
)
if token.level == 2:
heading_last, self.heading_last = self.heading_last, True
return format(r'{\par\vspace{<space_above>}\bfseries\fontsize{13pt}{15pt}\selectfont\centering <content>\phantomsection\addcontentsline{toc}{subsection}{<content>}\nopagebreak\par}',
space_above='1cm plus 0.3cm minus 0.3cm' if not heading_last else '0cm',
content=self.render_inner(token)
)
if token.level == 3:
heading_last, self.heading_last = self.heading_last, False
return format(r'{\par\bfseries\makebox[<lmarg>][l]{~}<content>\phantomsection\addcontentsline{toc}{subsubsection}{<content>}\nopagebreak\par}',
content=self.render_inner(token)
)
def render_numbered_heading(self, token):
if token.level == 1:
# Part
heading_last, self.heading_last = self.heading_last, True
@ -117,6 +138,51 @@ class LaTeXRenderer(mistletoe.latex_renderer.LaTeXRenderer):
content=self.render_inner(token)
)
def render_table(self, token):
align = ['X' for col in token.column_align]
if hasattr(token, 'header'):
for i, col in enumerate(token.header.children):
if '[' in col.children[0].content.split(' ')[0]:
param = col.children[0].content[col.children[0].content.index('['):col.children[0].content.index(']')+1]
align[i] += param
result = [
format('{\\footnotesize\\begin{longtabu} to \\dimexpr\\columnwidth-<lmarg>\\relax [r] {'),
' '.join(align),
'}\\toprule\n']
if hasattr(token, 'header'):
result.append(self.render_table_header(token.header) + '\\midrule[\\heavyrulewidth]\n')
for i, row in enumerate(token.children):
result.append(self.render_table_row(row))
if i != len(token.children) - 1:
result.append('\\midrule\n')
result.append(r'\bottomrule\end{longtabu}}')
return ''.join(result)
def render_table_header(self, token):
cells1 = ['{\\bfseries Column ' + self.render_inner(child).split(' ')[0].split('[')[0] + '}' for child in token.children]
cells2 = ['{\\bfseries ' + ' '.join(self.render_inner(child).split(' ')[1:]) + '}' for child in token.children]
return ' & '.join(cells1) + ' \\\\\\midrule\n' + ' & '.join(cells2) + ' \\\\'
def render_table_row(self, token):
cells = [self.render_table_cell(child, colnum) for colnum, child in enumerate(token.children)]
return ' & '.join(cells) + ' \\\\'
def render_table_cell(self, token, colnum):
inner = self.render_inner(token)
if colnum == 0:
return format(r'\makebox[1.8em][l]{<rownum>.}<content>',
rownum=inner.split(' ')[0],
content=' '.join(inner.split(' ')[1:])
)
else:
return inner
def render_document(self, token):
template = r'''
\documentclass[a4paper,12pt]{article}
@ -131,6 +197,7 @@ class LaTeXRenderer(mistletoe.latex_renderer.LaTeXRenderer):
\usepackage{fancyhdr}
\setlength{\emergencystretch}{3em}
\usepackage{microtype}
\usepackage{longtable}\usepackage{tabu}\usepackage{booktabs}
\newlength\notetaglength
\newlength\lastalign\newcommand{\updatealign}{\global\lastalign=\dimexpr\leftskip+\hangindent\relax}
% TOC format

View File

@ -20,8 +20,8 @@ import mistletoe
mistletoe.block_token.remove_token(mistletoe.block_token.BlockCode)
class Heading(mistletoe.block_token.BlockToken):
pattern = re.compile(r'(#{1,6})\s*([0-9A-Z]+)\s+(.+)')
class NumberedHeading(mistletoe.block_token.BlockToken):
pattern = re.compile(r'(#{1,6})\s*([0-9A-Z]+|xx)\s+(.+)')
def __init__(self, match):
self.level, self.label, content = match
@ -45,8 +45,8 @@ class Heading(mistletoe.block_token.BlockToken):
return level, label, content
mistletoe.block_token.remove_token(mistletoe.block_token.Heading)
mistletoe.block_token.add_token(Heading)
#mistletoe.block_token.remove_token(mistletoe.block_token.Heading)
mistletoe.block_token.add_token(NumberedHeading)
class Subrules(mistletoe.block_token.BlockToken):
pattern = re.compile(r'\t*\(([0-9A-Za-z–]+)\)\s+.')

View File

@ -38,6 +38,7 @@ class RTFRenderer(mistletoe.base_renderer.BaseRenderer):
def __init__(self, *extras):
super().__init__(*extras)
self.render_map['NumberedHeading'] = self.render_numbered_heading
self.render_map['Subrules'] = self.render_subrules
self.render_map['SubrulesItem'] = self.render_subrules_item
self.render_map['Note'] = self.render_note
@ -74,6 +75,31 @@ class RTFRenderer(mistletoe.base_renderer.BaseRenderer):
raise Exception('NYI')
def render_heading(self, token):
if token.level == 1:
# Part
heading_last, self.heading_last = self.heading_last, True
return format(r'{\sb<space_above>\keepn\b\fs26\qc\caps <content>\par}',
space_above=cm_to_twip(1),
content=self.render_inner(token)
)
if token.level == 2:
# Division
heading_last, self.heading_last = self.heading_last, True
return format(r'{\sb<space_above>\keepn\b\fs26\qc <content>\par}',
space_above=cm_to_twip(1 if not heading_last else 0),
content=self.render_inner(token)
)
if token.level == 3:
# Section
heading_last, self.heading_last = self.heading_last, False
return format(r'{\keepn\b\li<hangindent> <content>\par}',
hangindent=cm_to_twip(LMARG),
content=self.render_inner(token)
)
def render_numbered_heading(self, token):
if token.level == 1:
# Part
heading_last, self.heading_last = self.heading_last, True
@ -125,13 +151,57 @@ class RTFRenderer(mistletoe.base_renderer.BaseRenderer):
raise Exception('NYI')
def render_table(self, token):
raise Exception('NYI')
result = [format(r'{\sa0\trowd\trgaph120\trleft<lmarg>')]
# Get column weights
weights = []
if hasattr(token, 'header'):
for col in token.header.children:
if '[' in col.children[0].content.split(' ')[0]:
weights.append(int(col.children[0].content[col.children[0].content.index('[')+1:col.children[0].content.index(']')]))
else:
weights.append(1)
else:
weights = [1 for col in token.column_align]
# Get running totals
cellx = [sum(weights[0:i+1]) for i in range(len(weights))]
# Convert to widths
cellx = [x * cm_to_twip(21-LMARG-4) / cellx[-1] + cm_to_twip(LMARG) for x in cellx]
for x in cellx:
result.append(format(r'\clbrdrt\brdrs\clbrdrb\brdrs\cellx<>', int(x)))
result.append(' ')
if hasattr(token, 'header'):
result.append(self.render_table_header(token.header))
for i, row in enumerate(token.children):
result.append(self.render_table_row(row))
result.append(r'}')
return ''.join(result)
def render_table_header(self, token):
cells1 = ['{\\b Column ' + self.render_inner(child).split(' ')[0].split('[')[0] + '\\intbl\\cell}' for child in token.children]
cells2 = ['{\\b ' + ' '.join(self.render_inner(child).split(' ')[1:]) + '\\intbl\\cell}' for child in token.children]
return ''.join(cells1) + '\\row' + ''.join(cells2) + '\\row '
def render_table_row(self, token):
raise Exception('NYI')
return ''.join([self.render_table_cell(cell, i) for i, cell in enumerate(token.children)]) + '\\row '
def render_table_cell(self, token):
raise Exception('NYI')
def render_table_cell(self, token, colnum):
inner = self.render_inner(token)
if colnum == 0:
return format(r'{<rownum>. <content>\intbl\cell}',
rownum=inner.split(' ')[0],
content=' '.join(inner.split(' ')[1:])
)
else:
return format(r'{<content>\intbl\cell}',
content=inner
)
def render_thematic_break(self, token):
raise Exception('NYI')