Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion MarkdownTableFormatter.sublime-settings
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,9 @@
"padding": 0,

// how text should be justified when not specified [LEFT, RIGHT, CENTER]
"default_justification": "LEFT"
"default_justification": "LEFT",

// Whether the last column in tables must fit the widest content (set to
// "fixed") or can be variable width (set to "variable")
"last_column_width": "fixed"
}
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ There are two basic ways of using this plugin. Select what you want to format an

## Configuration

```
```json
{
// make plugin verbose in debug console
"verbose": false,
Expand All @@ -31,7 +31,11 @@ There are two basic ways of using this plugin. Select what you want to format an
"padding": 0,

// how text should be justified when not specified [LEFT, RIGHT, CENTER]
"default_justification": "LEFT"
"default_justification": "LEFT",

// Whether the last column in tables must fit the widest content (set to
// "fixed") or can be variable width (set to "variable")
"last_column_width": "fixed"
}
```

Expand Down
6 changes: 4 additions & 2 deletions markdown_table_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def run(self, edit, format_all=False):
padding = settings.get("padding")
justify = settings.get("default_justification")
justify = markdown.table.Justify.from_string[justify]
last_column_width = settings.get("last_column_width")

if verbose:
log.setLevel(logging.DEBUG)
Expand All @@ -43,8 +44,9 @@ def run(self, edit, format_all=False):
for start, end in positions:
prev_table = text[start:end]
log.debug("table found:\n" + prev_table)
new_table = markdown.table.format(prev_table, margin, padding,
justify)
new_table = markdown.table.format(prev_table,
margin, padding, justify,
last_column_width)
log.debug("formatted output:\n" + new_table)

# absolute original table position after some insertion/removal
Expand Down
8 changes: 7 additions & 1 deletion simple_markdown/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ def find_all(text):
return tables


def format(raw_table, margin=1, padding=0, default_justify=Justify.LEFT):
def format(raw_table, margin=1, padding=0, default_justify=Justify.LEFT,
last_column_width="fixed"):
rows = raw_table.splitlines()
# normalize markdown table, add missing leading/trailing '|'
for idx, row in enumerate(rows):
Expand All @@ -52,6 +53,11 @@ def format(raw_table, margin=1, padding=0, default_justify=Justify.LEFT):
text_width = [[len(col) for col in row] for row in matrix]
# determine column width (including space padding/margin)
col_width = [max(size) + margin*2 + padding for size in zip(*text_width)]
if last_column_width == "variable":
# modify the column width setting of last column to use use size of
# heading text of the last column instead of the size of the widest
# text of the last column
col_width[col_cnt-1] = text_width[0][-1] + margin*2 + padding

# get each column justification or apply default
justify = []
Expand Down
13 changes: 13 additions & 0 deletions tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,19 @@ def test_format(self):
table = Table.format(small, margin=1, padding=0)
self.assertEqual(table, expected_small)

expected_table_variable = """\
| Tables | Are | Cool |
|:--------------|:-------------|-----:|
| col 1 is | left-aligned | $1600 |
| col 2 is | centered | $12 |
| zebra stripes | | are neat $1 |
| | | $hello |
| $2 | | |"""

table = Table.format(raw_table, margin=1, padding=0,
last_column_width="variable")
self.assertEqual(table, expected_table_variable)

def test_find_all(self):
junk_tables = """
| Tables | Are | Cool #1 |
Expand Down