Home Download Buy Blog Forum Support

Tabularize

Tabularize

Postby fjdumont on Wed May 30, 2012 8:05 pm

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:

Code: Select all
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:

Code: Select all
+------------------------+--------------------+------------------------------+------+-----+
| 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: Select all
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"


Edit: Ignore those localized comments, I'm in a rush - sorry!
fjdumont
 
Posts: 5
Joined: Thu Oct 13, 2011 9:32 am

Re: Tabularize

Postby kyprios1 on Thu Jun 14, 2012 7:46 pm

Seems lovely, how do I use this? It doesn't show up as an option in the context or any other menu.
kyprios1
 
Posts: 8
Joined: Sat Nov 05, 2011 11:37 pm

Re: Tabularize

Postby vitaLee on Thu Jun 14, 2012 8:56 pm

i'll find use for it too.
@kyprios1
save the command under your User folder.
and add keybinding inside your keymap file.
i placed this inside Default (OSX).sublime-keymap
Code: Select all
{ "keys": ["ctrl+alt+t"], "command": "tabularize" }
vitaLee
 
Posts: 150
Joined: Tue Dec 27, 2011 11:10 pm

Re: Tabularize

Postby jinweijie on Mon Jun 18, 2012 10:09 pm

quite good! thanks!
jinweijie
 
Posts: 4
Joined: Tue Jun 05, 2012 3:38 am

Re: Tabularize

Postby kyprios1 on Wed Jun 20, 2012 11:17 am

Thanks vitaLee, got it working.

Idea for the developer: How about a reverse command?

There may be times we need to make adjustments after the tabularization and patching the "table" borders manually could be a lot of work.
If it was possible to revert the data back to serial formatting it'd make life easier.
kyprios1
 
Posts: 8
Joined: Sat Nov 05, 2011 11:37 pm

Re: Tabularize

Postby jinweijie on Wed Jul 04, 2012 3:16 am

Hi,

I improved the code to support Chinese characters. Thanks!

Code: Select all
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")

      # validate number of columns
      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

      # determine split width/
      widths = []
      for col in xrange(0, colCount):
         max = 0
         for row in rawRows:
            val = string.split(row, "|")[col].strip()
            ch_count = self.get_chs_count(val)
            vLen = len(val) + ch_count
            if vLen > max:
               max = vLen
         widths.append(max)

      new = self.empty_table_line(widths)
     
      # assemble content line by line.
      for row in rawRows:
         vals = string.split(row, "|")
         line = "|"
         for col in xrange(0, len(vals)):
            val = vals[col].strip()
            vLen = widths[col]
            ch_count = self.get_chs_count(val)
            line += " " + string.ljust(val, vLen + 1 - ch_count) + "|"
         new += line + "\n"
         new += self.empty_table_line(widths)

      return new

   # build header, footer with separation line
   def empty_table_line(self, widths):
      s = "+"
      for width in widths:
         s += string.replace(string.ljust("", width + 2), " ", "-")
         s += "+"
      return s + "\n"

   # detect the count of chs chars
   def get_chs_count(self, s):
      l = 0
      for c in s:
         if self.is_chinese(c):
            l += 1
      return l

   # determine whether a char is chs
   def is_chinese(self, uchar):
      if uchar >= u'\u4e00' and uchar<=u'\u9fa5':
         return True
      else:
         return False
jinweijie
 
Posts: 4
Joined: Tue Jun 05, 2012 3:38 am

Re: Tabularize

Postby jinweijie on Wed Jul 04, 2012 3:42 am

kyprios1 wrote:Thanks vitaLee, got it working.

Idea for the developer: How about a reverse command?

There may be times we need to make adjustments after the tabularization and patching the "table" borders manually could be a lot of work.
If it was possible to revert the data back to serial formatting it'd make life easier.


You may do a replacement with regex:
replace
Code: Select all
(([\-]|[\+])+(\n)?)|(^\|)|(\|$)


with empty, then tabularize again :)
jinweijie
 
Posts: 4
Joined: Tue Jun 05, 2012 3:38 am


Return to Plugin Announcements

Who is online

Users browsing this forum: Exabot [Bot] and 10 guests