Browse files

Improve support for table insertion and formatting.

- Added command to reformat tables: makes them MUCH more readable and maintainable
- Deleted duplicate table insert
- Renamed existing snippet to "Insert Row" instead of just "table row"
  • Loading branch information...
1 parent 6740424 commit bf4643041a20ff4ce6ba2cab8481a0efc10f2782 @tbates tbates committed with infininight Dec 16, 2012
View
147 Commands/Reformat columns to equal width.tmCommand
@@ -0,0 +1,147 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
+<plist version="1.0">
+<dict>
+ <key>beforeRunningCommand</key>
+ <string>nop</string>
+ <key>command</key>
+ <string>#!/usr/bin/php
+&lt;?php
+// creator: tim.bates@ed.ac.uk
+// license = GPL
+// created 12 Dec 2012
+// Purpose: Takes selected mediawiki table like
+/*
+
+| a ||e || u
+| b || elephant || prissy
+| c || minuet-in-g || A
+ and returns it formatted nicely into columns thus:
+
+| a || e || u
+| b || elephant || prissy
+| c || minuet-in-g || A
+*/
+
+// USEAGE notes:
+// Set $debug to 1 to let play with it in a TM window
+
+$debug = 0; // 0 to run properly
+$padWidth = 7; // sea change :-)
+$delimitter = "\|\|";
+$validRowMatch = "^|(?!-)"; // negative look-ahead for |-
+
+function getMaxWidths($aMatrix){
+ $o = array();
+ $n = 0;
+ foreach ($aMatrix as $key =&gt; $row) {
+ if ($n==0) {
+ foreach ($row as $key =&gt; $cell) {
+ $o[] = strlen($cell);
+ }
+ } else {
+ // Compare each to see which is longer
+ $c = 0;
+ foreach ($row as $key =&gt; $cell) {
+ if ($o[$c] &lt; strlen($cell)) { $o[$c] = strlen($cell); }
+ $c++;
+ }
+ }
+ $n++;
+ }
+ return $o;
+}
+
+function stringTo2DArray($stringIn, $delimitter="||"){
+ // rows are lines
+ // strips all white space either side of the delimitter
+ $o = array();
+ $stringIn = trim($stringIn);
+ $rows = explode("\n", $stringIn);
+
+ $pattern = '/^\|\s\s+/'; # white space after | at the beginning of a line
+ $replacement = '| ';
+ $rows = preg_replace($pattern, $replacement, $rows);
+ $find = "/ *" . $delimitter . " */";
+
+ foreach ($rows as $key =&gt; $row) {
+ $row = trim($row);
+ $row = preg_replace($find, $delimitter, $row);
+ $o[] = explode($delimitter, $row);
+ }
+ return $o;
+}
+
+function stringToArray($stringIn, $delimitter=" "){
+ // strips newlines and all white space
+ $stringIn = preg_replace("/[ \t\n]{2,}/", " ", $stringIn); //remove multiple spaces in case some present
+ $stringIn = trim($stringIn);
+ return explode($delimitter, $stringIn);
+}
+
+function arrayToString($aVars, $suffix="1"){
+ $sep = "$suffix ";
+ if ( !is_array($aVars) ) {
+ echo("&lt;font color=\"red\"&gt;arrayToString got $aVars when it expected an array&lt;/font&gt;&lt;br&gt;");
+ return "arrayToString got $aVars when it expected an array";
+ }else{
+ return implode($sep, $aVars). $sep;
+ }
+}
+
+if ($debug) {
+ $input ="|-
+| a ||e || u
+|-
+| b|| elephant || prissy
+|-
+| c || minuet-in-g || A
+";
+ echo "$input\n\n\n";
+}else{
+ $input = file_get_contents('php://stdin');
+ // $input = $_ENV["TM_SELECTED_TEXT"];
+}
+
+// make 2D array;
+$aMatrix = stringTo2DArray($input,$delimitter);
+
+// print_r($aMatrix);
+
+$aPadWidth = getMaxWidths($aMatrix);
+// print_r($aPadWidth);
+
+$outStr = "";
+
+foreach ($aMatrix as $key =&gt; $row) {
+ $c = 0;
+ $newRow = array();
+ foreach ($row as $key =&gt; $cell) {
+ $newRow[] = str_pad($cell, $aPadWidth[$c]);
+ $c++;
+ }
+ $outStr.= trim( implode(" || ", $newRow) ) . "\n";
+}
+echo str_replace("|| ~", "||~ ", $outStr);
+?&gt;
+</string>
+ <key>input</key>
+ <string>selection</string>
+ <key>inputFormat</key>
+ <string>text</string>
+ <key>name</key>
+ <string>Reformat Columns to Equal Width</string>
+ <key>outputCaret</key>
+ <string>afterOutput</string>
+ <key>outputFormat</key>
+ <string>text</string>
+ <key>outputLocation</key>
+ <string>replaceInput</string>
+ <key>scope</key>
+ <string>text.html.mediawiki</string>
+ <key>uuid</key>
+ <string>2ACC7FC8-AF68-48FA-AB6D-6B383149CDD6</string>
+ <key>version</key>
+ <integer>2</integer>
+</dict>
+</plist>
View
13 Snippets/Insert Table.tmSnippet
@@ -3,12 +3,15 @@
<plist version="1.0">
<dict>
<key>content</key>
- <string>{|
-|${1:row1}
+ <string>{| class="sortable wikitable" style="font-size: 100%; text-align: center;"
+! ${1:col1Name}
+! ${2:col2Name}
+! ${3:col3Name}
|-
-|${2:row2}
-|}
-</string>
+| ${4:r1 $1} || ${5:r1 $2} || ${6:r1 $3}
+|-
+| ${7:r1 $1} || ${8:r1 $2} || ${9:r1 $3}
+|}</string>
<key>name</key>
<string>Insert Table</string>
<key>scope</key>
View
4 Snippets/table row.tmSnippet
@@ -7,9 +7,9 @@
|-
||${1:cell}||${2:cell}||</string>
<key>keyEquivalent</key>
- <string>@w</string>
+ <string></string>
<key>name</key>
- <string>table row</string>
+ <string>Insert Row</string>
<key>scope</key>
<string>markup.other.table.row.mediawiki</string>
<key>tabTrigger</key>
View
20 Snippets/table.tmSnippet
@@ -1,20 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
-<plist version="1.0">
-<dict>
- <key>content</key>
- <string>{| class="sortable wikitable" style="font-size: 90%; text-align: center;"
-|-
-||${1:old}||${2:old}||${3:old}
-|}
-</string>
- <key>name</key>
- <string>table</string>
- <key>scope</key>
- <string>source.mediawiki</string>
- <key>tabTrigger</key>
- <string>table</string>
- <key>uuid</key>
- <string>C523B38B-1113-4129-89BA-455792D7AA91</string>
-</dict>
-</plist>

0 comments on commit bf46430

Please sign in to comment.