First of all - sorry, I’m not a native python speaker
Actually I just wanted to quickly dump this tiny thing, I’m confident some of you might find a use for this.
Tabularize will turn this:
Course Name | Course Tutor | Summary | Code | Fee
After the Civil ... | Dr. John Wroughton | The course will ... | H27 | 32€
An Introduction to ... | Mark Cottle | One day course ... | H28 | 18€
The Glory that ... | Valerie Lorenz | Birthplace of democracy, ... | H30 | 18€
into this:
+------------------------+--------------------+------------------------------+------+-----+
| Course Name | Course Tutor | Summary | Code | Fee |
+------------------------+--------------------+------------------------------+------+-----+
| After the Civil ... | Dr. John Wroughton | The course will ... | H27 | 32€ |
+------------------------+--------------------+------------------------------+------+-----+
| An Introduction to ... | Mark Cottle | One day course ... | H28 | 18€ |
+------------------------+--------------------+------------------------------+------+-----+
| The Glory that ... | Valerie Lorenz | Birthplace of democracy, ... | H30 | 18€ |
+------------------------+--------------------+------------------------------+------+-----+
Feel free to improve the code. Actually, really, please do! (and post results back here )
[code]import sublime, sublime_plugin
import array, string
class TabularizeCommand(sublime_plugin.TextCommand):
def run(self, edit):
sels = self.view.sel()
for sel in sels:
if sel.empty():
continue
old = self.view.substr(sel)
new = self.tabularize(old)
edit = self.view.begin_edit();
self.view.replace(edit, sel, new)
self.view.end_edit(edit)
def tabularize(self, old):
rawRows = string.split(old, "\n")
# spaltenanzahl validieren
colCount = string.count(rawRows[0], "|") + 1
for row in rawRows[1:]:
if string.count(row, "|") != colCount - 1:
sublime.error_message("column count does not match")
return old
# spaltenbreiten ermitteln
print colCount
widths = ]
for col in xrange(0, colCount):
max = 0
for row in rawRows:
val = string.split(row, "|")[col].strip()
vLen = len(val)
if vLen > max:
max = vLen
widths.append(max)
new = self.empty_table_line(widths)
# inhaltszeilen zusammenbauen
for row in rawRows:
vals = string.split(row, "|")
line = "|"
for col in xrange(0, len(vals)):
val = vals[col].strip()
vLen = widths[col]
line += " " + string.ljust(val, vLen + 1) + "|"
new += line + "\n"
new += self.empty_table_line(widths)
return new
# baut eine header-, footer- oder trennzeile
def empty_table_line(self, widths):
s = "+"
for width in widths:
s += string.replace(string.ljust("", width + 2), " ", "-")
s += "+"
return s + "\n"[/code]
Edit: Ignore those localized comments, I’m in a rush - sorry!