def replace_attrtables(key, value, fmt, meta): """Replaces attributed tables while storing reference labels.""" if is_attrtable(key, value): # Parse the table content, caption, label = parse_attrtable(value) # Bail out if the label does not conform if not label or not LABEL_PATTERN.match(label): return None # Save the reference references[label] = len(references) + 1 # Adjust caption depending on the output format if fmt == 'latex': caption = list(caption) + [RawInline('tex', r'\label{%s}' % label)] else: caption = ast('Table %d. ' % references[label]) + list(caption) # Return the replacement # pylint: disable=star-args args = [ caption, ] + content if fmt in ('html', 'html5'): anchor = RawInline('html', '<a name="%s"></a>' % label) return [Plain([anchor]), Table(*args)] else: return Table(*args)
def insert_table_labels(key, val, fmt, meta): ''' Insert "Table 3:" style prefixes before table captions and wrap in span with id=table-3 etc. ''' if key == 'Table': caption = val[0] for i, obj in enumerate(caption): if obj['t'] == 'Span': span_val = obj['c'][0][2] if (len(span_val) == 1 and len(span_val[0]) == 2 and span_val[0][0] == 'data-label'): label = span_val[0][1] index = incr_latest_index('table') ref_index = 'table-%d' % index label_map[label] = Label( ref_string='Table %d' % index, ref_index=ref_index, prev_strings=['table', 'tab.'], ) span_index = i caption.pop(span_index) caption.insert(0, Str('Table %d: ' % index)) return Div([ref_index, ['engrafo-table'], []], [Table(*val)])
def process_tables(key, value, fmt, meta): """Processes the attributed tables.""" global has_unnumbered_tables # pylint: disable=global-statement # Process block-level Table elements if key == 'Table': # Inspect the table if len(value) == 5: # Unattributed, bail out has_unnumbered_tables = True if fmt in ['latex']: return [ RawBlock('tex', r'\begin{no-prefix-table-caption}'), Table(*value), RawBlock('tex', r'\end{no-prefix-table-caption}') ] return None # Process the table table = _process_table(value, fmt) # Context-dependent output attrs = table['attrs'] if table['is_unnumbered']: if fmt in ['latex']: return [ RawBlock('tex', r'\begin{no-prefix-table-caption}'), AttrTable(*value), RawBlock('tex', r'\end{no-prefix-table-caption}') ] elif fmt in ['latex']: if table['is_tagged']: # Code in the tags tex = '\n'.join([r'\let\oldthetable=\thetable', r'\renewcommand\thetable{%s}'%\ references[attrs[0]]]) pre = RawBlock('tex', tex) tex = '\n'.join([ r'\let\thetable=\oldthetable', r'\addtocounter{table}{-1}' ]) post = RawBlock('tex', tex) return [pre, AttrTable(*value), post] elif table['is_unreferenceable']: attrs[0] = '' # The label isn't needed any further elif fmt in ('html', 'html5') and LABEL_PATTERN.match(attrs[0]): # Insert anchor anchor = RawBlock('html', '<a name="%s"></a>' % attrs[0]) return [anchor, AttrTable(*value)] elif fmt == 'docx': # As per http://officeopenxml.com/WPhyperlink.php bookmarkstart = \ RawBlock('openxml', '<w:bookmarkStart w:id="0" w:name="%s"/>' %attrs[0]) bookmarkend = \ RawBlock('openxml', '<w:bookmarkEnd w:id="0"/>') return [bookmarkstart, AttrTable(*value), bookmarkend] return None
def table(v): os.system('pandoc -t json {0} > {0}.json'.format(v)) with open('{0}.json'.format(v), 'r+') as f: table_json = json.load(f) arg0, arg1, arg2, arg3, arg4 = table_json['blocks'][0]['c'] os.system('rm {0}.json'.format(v)) return Table(arg0, arg1, arg2, arg3, arg4)
def process_tables(key, value, fmt, meta): # pylint: disable=global-statement global num_refs # Global references counter global references if fmt == "docx" and key == "Table": for i, x in enumerate(value): if re.match(r'.*label.*', str(x).replace('\n', ' ')): num_refs += 1 m = re.match(r".*label.*\'(.*?)\'\]", str(x[0]).replace('\n', ' ')) label = m.group(1) references[label] = num_refs replace_table_label(x[0], num_refs) value[i] = x return Table(*value)
def _add_markup(fmt, table, value): """Adds markup to the output.""" # pylint: disable=global-statement global has_tagged_tables # Flags a tagged tables was found if table['is_unnumbered']: if fmt in ['latex', 'beamer']: # Use the no-prefix-table-caption environment return [ RawBlock('tex', r'\begin{tablenos:no-prefix-table-caption}'), Table(*(value if len(value) == 5 else value[1:])), RawBlock('tex', r'\end{tablenos:no-prefix-table-caption}') ] return None # Nothing to do attrs = table['attrs'] ret = None if fmt in ['latex', 'beamer']: if table['is_tagged']: # A table cannot be tagged if it is unnumbered has_tagged_tables = True ret = [RawBlock('tex', r'\begin{tablenos:tagged-table}[%s]' % \ references[attrs.id].num), AttrTable(*value), RawBlock('tex', r'\end{tablenos:tagged-table}')] elif fmt in ('html', 'html5', 'epub', 'epub2', 'epub3'): if LABEL_PATTERN.match(attrs.id): # Enclose table in hidden div pre = RawBlock('html', '<div id="%s" class="tablenos">' % attrs.id) post = RawBlock('html', '</div>') ret = [pre, AttrTable(*value), post] elif fmt == 'docx': # As per http://officeopenxml.com/WPhyperlink.php bookmarkstart = \ RawBlock('openxml', '<w:bookmarkStart w:id="0" w:name="%s"/>' %attrs.id) bookmarkend = \ RawBlock('openxml', '<w:bookmarkEnd w:id="0"/>') ret = [bookmarkstart, AttrTable(*value), bookmarkend] return ret
def process_tables(key, value, fmt, meta): # pylint: disable=global-statement global num_refs # Global references counter global references global supp_enabled global supp_str if fmt == "docx" and key == "Header": sys.stderr.write('KEY: %s, VALUE: %s\n' % (key, value)) for i, x in enumerate(value): if re.match(r'.*supplement.*', str(x).replace('\n', ' ')): supp_enabled = True supp_str = 'S' num_refs = 0 if fmt == "docx" and key == "Table": for i, x in enumerate(value): if re.match(r'.*label.*', str(x).replace('\n', ' ')): num_refs += 1 m = re.match(r".*label.*\'(.*?)\'\]", str(x[0]).replace('\n', ' ')) label = m.group(1) references[label] = '%s%d' % (supp_str, num_refs) replace_table_label(x[0], '%s%d' % (supp_str, num_refs)) value[i] = x return Table(*value)
def f(key, val, fmt, meta): if key == 'Table': rows = val[4] new_rows = walk(rows, g, fmt, meta) return Table(*val[0:4], new_rows)
def f(key, val, fmt, meta): if key == 'Table': rows = val[4] new_rows = walk(rows, g, fmt, meta) return Table(val[0], val[1], val[2], val[3], new_rows)