Created
November 30, 2012 07:22
-
-
Save KainokiKaede/4174293 to your computer and use it in GitHub Desktop.
Converts PukiWiki style array to TeX tabular environment. Function of this script is quite limited.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import StringIO | |
class PukiWikiArrayToTeXTabular(): | |
"""Converts PukiWiki style array to TeX tabular environment.""" | |
def __init__(self, pukiwikiarray_string): | |
"""Receives PukiWiki Style array.""" | |
self.default_align = 'c' | |
self.pukiwikiarray_string = pukiwikiarray_string | |
self.converted_string = self.converter() | |
def converter(self): | |
"""Converts PukiWiki style string to TeX style string.""" | |
pukiwikiarray_string = self.pukiwikiarray_string | |
fi = StringIO.StringIO(pukiwikiarray_string) | |
for line in fi: | |
if line.strip() == '': continue | |
number_of_columns = len(line.split('\n')[0].split('|')) - 2 | |
break | |
tex_style_output = [] | |
fi = StringIO.StringIO(pukiwikiarray_string) | |
for line in fi: | |
if line.strip() == '': continue | |
if number_of_columns != len(line.split('|')) - 2: | |
raise IndexError | |
tex_style_output.append(self.convert_single_line(line)) | |
prefix = ('\\begin{tabular}{' + self.default_align * number_of_columns | |
+ '}\n') | |
postfix = '\n\\end{tabular}' | |
return prefix + '\n'.join(tex_style_output) + postfix | |
def convert_single_line(self, line_string): | |
"""Receives PukiWiki style line, converts to TeX style line.""" | |
default_align = self.default_align | |
multicolumns = 1 | |
tex_style_output = [] | |
# Lines must start and end with "|"; | |
# otherwise, letters outside of those bars will be neglected. | |
for column in line_string.split('|')[1:-1]: | |
if column.startswith('LEFT:'): | |
align = 'l' | |
column = column.split(':')[1] | |
elif column.startswith('CENTER:'): | |
align = 'c' | |
column = column.split(':')[1] | |
elif column.startswith('RIGHT:'): | |
align = 'r' | |
column = column.split(':')[1] | |
else: | |
align = default_align | |
if column == '>': | |
multicolumns += 1 | |
continue | |
if align != default_align or multicolumns > 1: | |
tex_style_output.append('\multicolumn{' | |
+ str(multicolumns) + '}{' + align + '}{' + column + '}') | |
else: tex_style_output.append(column) | |
multicolumns = 1 | |
return ' & '.join(tex_style_output) | |
def get_converted_string(self): | |
"""Returns converted TeX style tabular string.""" | |
return self.converted_string | |
def get_converted_string_with_comments(self): | |
"""Returns converted TeX style tabular string, with raw data in comments.""" | |
outputstr = '' | |
fi = StringIO.StringIO(self.pukiwikiarray_string) | |
for line in fi: | |
if line.strip() == '': continue | |
outputstr += '% ' + line.strip() + '\n' | |
return outputstr + self.converted_string | |
if __name__ == '__main__': | |
string_to_convert =""" | |
|a|>|b|LEFT:c|d| | |
|b|c|>|RIGHT:e|f| | |
""" | |
print PukiWikiArrayToTeXTabular(string_to_convert).get_converted_string_with_comments() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment