Advertisement
Guest User

Untitled

a guest
Dec 5th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. import pandocfilters as pf
  4.  
  5. def latex(s):
  6. return pf.RawBlock('latex', s)
  7.  
  8. def inlatex(s):
  9. return pf.RawInline('latex', s)
  10.  
  11. def tbl_caption(s):
  12. return pf.Para([inlatex(r'\caption{')] + s + [inlatex('}')])
  13.  
  14. def tbl_alignment(s):
  15. aligns = {
  16. "AlignDefault": 'l',
  17. "AlignLeft": 'l',
  18. "AlignCenter": 'c',
  19. "AlignRight": 'r',
  20. }
  21. return ''.join([aligns[e['t']] for e in s])
  22.  
  23. def tbl_headers(s):
  24. result = s[0][0]['c'][:]
  25. # Build the columns. Note how the every column value is bold.
  26. # We are still missing "\textbf{" for the first column
  27. # and a "}" for the last column.
  28. for i in range(1, len(s)):
  29. result.append(inlatex(r'} & \textbf{'))
  30. result.extend(s[i][0]['c'])
  31. # Don't forget to close the last column's "\textbf{" before newline
  32. result.append(inlatex(r'} \\ \hline'))
  33. # Put the missing "\textbf{" in front of the list
  34. result.insert(0, inlatex(r'\textbf{'))
  35. # Preprend the command to set the row color in front of everything
  36. result.insert(0, inlatex(r'\rowcolor{grey} '))
  37. return pf.Para(result)
  38.  
  39. def tbl_contents(s):
  40. result = []
  41. for row in s:
  42. para = []
  43. for col in row:
  44. para.extend(col[0]['c'])
  45. para.append(inlatex(' & '))
  46. result.extend(para)
  47. result[-1] = inlatex(r' \\ \hline' '\n')
  48. return pf.Para(result)
  49.  
  50. def do_filter(k, v, f, m):
  51. if k == "Table":
  52. # Ensure every alignment characters is surrounded by a pipes.
  53. # Get the string of the alignment characters
  54. # and split into an array for every characters.
  55. split_alignment = [c for c in tbl_alignment(v[1])]
  56. # Join this list into a single string with pipe symbols
  57. # between them, plus pipes at start and end.
  58. # This results in a boxed table.
  59. new_alignment = "|" + "|".join(split_alignment) + "|"
  60. return [latex(r'\begin{table}[h]'),
  61. latex(r'\centering'),
  62. latex(r'\begin{tabular}{%s} \hline' % new_alignment),
  63. #tbl_headers(v[3]),
  64. tbl_contents(v[4]),
  65. latex(r'\end{tabular}'),
  66. # Put the caption after the tabular so it appears under table.
  67. tbl_caption(v[0]),
  68. latex(r'\end{table}')]
  69.  
  70.  
  71. if __name__ == "__main__":
  72. pf.toJSONFilter(do_filter)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement