Skip to content

Commit

Permalink
add simpletable
Browse files Browse the repository at this point in the history
  • Loading branch information
tzmgit committed Nov 20, 2015
1 parent 6700419 commit 4c55eea
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 1 deletion.
5 changes: 5 additions & 0 deletions Default (Linux).sublime-keymap
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
[
{ "key": "selector", "operator": "equal", "operand": "text.restructuredtext" }
]
}, {
"keys": ["ctrl+t", "s"], "command": "simpletable", "context":
[
{ "key": "selector", "operator": "equal", "operand": "text.restructuredtext" }
]
}, {
"keys": ["ctrl+t", "r"], "command": "flowtable", "context":
[
Expand Down
5 changes: 5 additions & 0 deletions Default (OSX).sublime-keymap
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
[
{ "key": "selector", "operator": "equal", "operand": "text.restructuredtext" }
]
}, {
"keys": ["super+shift+t", "s"], "command": "simpletable", "context":
[
{ "key": "selector", "operator": "equal", "operand": "text.restructuredtext" }
]
}, {
"keys": ["super+shift+t", "r"], "command": "flowtable", "context":
[
Expand Down
5 changes: 5 additions & 0 deletions Default (Windows).sublime-keymap
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
[
{ "key": "selector", "operator": "equal", "operand": "text.restructuredtext" }
]
}, {
"keys": ["ctrl+t", "s"], "command": "simpletable", "context":
[
{ "key": "selector", "operator": "equal", "operand": "text.restructuredtext" }
]
}, {
"keys": ["ctrl+t", "r"], "command": "flowtable", "context":
[
Expand Down
63 changes: 62 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ Magic Tables

There is a particular *magic* expansion for tables. Here is how it works:

Grid table
++++++++++

1. Create some kind of table outline, separating column with two or more spaces::


Expand Down Expand Up @@ -247,9 +250,67 @@ Move the cursor to the cell ``12`` and press ``ctrl+t, down``. You'll get this::
`Vincent Driessen's vim-rst-tables <https://github.com/nvie/vim-rst-tables>`_ :

.. note::

The original code of `wcwidth <https://github.com/jquast/wcwidth>`_ was taken to solve alignment issue with CJK characters.

Simple table
++++++++++++

Instead of tables above, a simpler style table is also supported. Here is how it works:

1. Create some kind of table outline, separating column with two or more spaces::


This is paragraph text *before* the table.

Column 1 Column 2
Foo Put two (or more) spaces as a field separator.
Bar Even very very long lines like these are fine, as long as you do not put in line endings here.

This is paragraph text *after* the table.

2. Put your cursor somewhere in the content to convert as table.
3. Press ``ctrl+t, s`` (Linux or Windows) or ``super+shift+t, s`` (Mac). The output will look
something like this::

This is paragraph text *before* the table.

========== ================================================================================================
Column 1 Column 2
========== ================================================================================================
Foo Put two (or more) spaces as a field separator.
Bar Even very very long lines like these are fine, as long as you do not put in line endings here.
========== ================================================================================================

This is paragraph text *after* the table.


Now suppose you add some text in a cell::


========== ================================================================================================
Column 1 Column 2
========== ================================================================================================
Foo is longer now Put two (or more) spaces as a field separator.
Bar Even very very long lines like these are fine, as long as you do not put in line endings here.
========== ================================================================================================

Press the same trigger: magically, the structure will be fixed::


=================== ================================================================================================
Column 1 Column 2
=================== ================================================================================================
Foo is longer now Put two (or more) spaces as a field separator.
Bar Even very very long lines like these are fine, as long as you do not put in line endings here.
=================== ================================================================================================


.. note::

The original code of this feature was taken from
`Vincent Driessen's vim-rst-tables <https://github.com/nvie/vim-rst-tables>`_ :

Smart lists
-----------

Expand Down
1 change: 1 addition & 0 deletions RestructuredText.sublime-completions
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
{ "trigger": "fig", "contents": ".. figure:: ${1:path}\n\n ${2:caption}\n\n$0" },

{ "trigger": "table", "contents": "${1/./=/g}\n${1:heading}\n${1/./=/g}\n${2:row}\n${3:row}\n${4:row}\n${1/./=/g}" },
{ "trigger": "simpletable", "contents": "${1/./=/g}\n${1:heading}\n${1/./=/g}\n${2:row}\n${3:row}\n${4:row}\n${1/./=/g}" },

{ "trigger": "link", "contents": "`${1:link_variable_name}`_ $0\n\n.. _${1}: ${2:http://}"},
{ "trigger": "linki", "contents": "`${1:link_variable_name} <${2:http://}>`_ $0"},
Expand Down
88 changes: 88 additions & 0 deletions simpletable.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# -*- coding: utf-8 -*-

import re
try:
from .helpers import BaseBlockCommand
except ValueError:
from helpers import BaseBlockCommand


class SimpletableCommand(BaseBlockCommand):

_SEPARATOR = ' '

def get_result(self, indent, table):
result = '\n'.join(self._draw_table(indent, table))
result += '\n'
return result

def run(self, edit):
region, lines, indent = self.get_block_bounds()
table = self._parse_table(lines)
result = self.get_result(indent, table)
self.view.replace(edit, region, result)

def _split_table_cells(self, row_string):
return re.split(r'\s\s+', row_string.strip())

def _parse_table(self, raw_lines):
parsed_lines = []
for row_string in raw_lines:
if not self._row_is_separator(row_string):
parsed_lines.append(self._split_table_cells(row_string))
return parsed_lines

def _row_is_separator(self, row):
return re.match('^[\t =]+$', row)

def _table_header_line(self, widths):
linechar = '='
parts = []
for width in widths:
parts.append(linechar * width)
return SimpletableCommand._SEPARATOR.join(parts)

def _get_column_max_widths(self, table):
widths = []
for row in table:
num_fields = len(row)
# dynamically grow
if num_fields >= len(widths):
widths.extend([0] * (num_fields - len(widths)))
for i in range(num_fields):
field_width = len(row[i])
widths[i] = max(widths[i], field_width)
return widths

def _pad_fields(self, row, width_formats):
""" Pad all fields using width formats """
new_row = []
for i in range(len(row)):
col = row[i]
col = width_formats[i] % col
new_row.append(col)
return new_row

def _draw_table(self, indent, table):
if not table:
return []
col_widths = self._get_column_max_widths(table)
# Reserve room for separater
len_sep = len(SimpletableCommand._SEPARATOR)
sep_col_widths = [(col + len_sep) for col in col_widths]
width_formats = [('%-' + str(w) + 's' + SimpletableCommand._SEPARATOR) for w in col_widths]

header_line = self._table_header_line(sep_col_widths)
output = [indent + header_line]
first = True
for row in table:
# draw the lines (num_lines) for this row
row = self._pad_fields(row, width_formats)
output.append(indent + SimpletableCommand._SEPARATOR.join(row))
# draw the under separator for header
if first:
output.append(indent + header_line)
first = False

output.append(indent + header_line)
return output

0 comments on commit 4c55eea

Please sign in to comment.