|
Cookbook /
Text2TblSummary: Provide a way to convert arbitrarily structured text to table markup and provide conversion of simple tables to advanced.
Version: 2010-04-06
Prerequisites: PHP 5
Status: alpha
Maintainer: Peter Bowers
Download: text2tbl.phpΔ
Questions answered by this recipe
NOTE: Since pmwiki.org is running PHP4 and this recipe uses PHP5 feel free to use http://pmwiki.qdk.org/pmwiki.php?n=Text2Tbl.Test as a sandbox to see how this works. DescriptionUse structured text to create tables (either converting or directly as markup). NotesThere are 2 "modes" this recipe provides:
There are 2 ways of doing conversion (simple and flextables) and the 2nd way (flextables) also works as markup when it is slightly modified. Conversion type 1 (SIMPLE) -- create tables by typing 2 or more spaces in between columnsThere are 2 basic types of table conversion you can use. The 1st is very simple - it simply converts any occurrences of 2 or more consecutive spaces into a column division. Thus you will enter this: TABLESTART border=1 abc def ghi jkl mnoaa pqr stu vwx zzz TABLEEND and after you press "Save and Edit" or "Save" it will be converted to this: || border=1 ||abc ||def ||ghi || ||jkl ||mnoaa ||pqr || ||stu ||vwx ||zzz || Note that everything is left-justified. Any text you place on the line after "TABLESTART" will be left as options to the table. No other options are possible. This is just for quickly creating a table. Flex Tables (option 2) for source conversion OR directly as markupOption #2 is quite a bit fancier. It allows for converting almost any structured text (i.e., CSV, etc.) into a table. It allows for specifying the alignment of each column, etc. There are 2 basic patterns you will use to surround your structured text:
Note that if you prefer "table" to "tbl" you can use text2table rather than text2tbl or flextable rather than flextbl in each of the above markups interchangeably (:text2tbl:) a,b,c d,e,f ghi,jkl,mno (:text2tblend:) and it will be converted to this when you save: || ||a ||b ||c || ||d ||e ||f || ||ghi ||jkl ||mno || However, you have many different options that you can set. Options appear within the (:text2tbl OPTIONS:) pattern and are specified in the usual opt=val method. The can include these options:
Any unrecognized options are passed through directly as if you had specified For converting from simple to advanced, the following are supported:
Note that specifying sep1,sep2,sep3 is mutually exclusive with specifying sep and is mutually exclusive with specifying fmt. You can specify your separator in exactly one way -- if you choose more than one way then something will be ignored... "Fake" Headers (using !string as the label in the input side) are obtained by using the
that PM recommends when using output=advanced. Cell-specific attributes can be specified as long as you are using output=advanced. Simply specify To convert CSV to a simple table: (:text2tbl sep=, quotes=1 options="border=1":) !Name,!Address,!Phone "Smith, John","123 Main St, Smallville, NC 12345",555-1212 John Smith,123 Main St,555-1212 "Smith, Jane","111 Small St, Mainville, NY 11122",(212) 123-4321 (:text2tblend:) When that is saved it would result in this: || border=1 ||!Name ||!Address ||!Phone || ||Smith, John ||123 Main St, Smallville, NC 12345 ||555-1212 || ||John Smith ||123 Main St ||555-1212 || ||Smith, Jane ||111 Small St, Mainville, NY 11122 ||(212) 123-4321 || To show that you don't have to use the same separator (this is helpful when you have data from a different source that you want to put into a table): (:text2tbl fmt="l;l-c":) Column A ; Column B - Column C apple ; banana - cherry (:text2tblend:) Is identical to this: (:text2tbl sep1=; sep2=- align="llc":) Column A ; Column B - Column C apple ; banana - cherry (:text2tblend:) And both result in this when saved: || ||Column A || Column B || Column C || ||apple || banana || cherry || If you had specified an "output=advanced" directive: (:text2tbl sep1=; sep2=- align="llc" output=advanced:) Column A ; Column B - Column C apple ; banana - cherry (:text2tblend:) it would have ended up like this: (:table :) (:cellnr align=left:)Column A (:cell align=left:) Column B (:cell align=center:) Column C (:cellnr align=left:)apple (:cell align=left:) banana (:cell align=center:) cherry (:tableend:) Converting from simple tables to advanced table directives looks like this (also demonstrating the col & row span with various filler cells): (:text2tbl input=simple output=advanced:) || border=1 || abc ||__||__||__|| || abc ||def+++||ghi++||zzz|| ||xyz||^^^||^^^||zzz|| ||zzz||^^^||yyy||zzz|| (:text2tblend:) The above results (after clicking on "save and edit") in this source: (:table border=1:) (:cellnr align=center colspan=4:) abc (:cellnr align=center:) abc (:cell align=left rowspan=3:)def (:cell align=left rowspan=2:)ghi (:cell align=left:)zzz (:cellnr align=left:)xyz (:cell align=left:)zzz (:cellnr align=left:)zzz (:cell align=left:)yyy (:cell align=left:)zzz (:tableend:) Embedded newlines (by ending a line with Installation
Release Notes
See AlsoContributorsCommentsDirkBlaas 2009-09-12 Thanks for publishing this recipe. It is another good effort to make working with large tables more easy. I came across a small bug: when a column is center or right aligned, the markup for heading (!) is not recognized. For left aligned it is working ok. This happens both in text2tbl mode and flextbl mode. In text2tbl mode you can see that the ! is not placed directly next to the table-seperators (|| !Headertext in stead of ||! Headertext). Thanks for the bug report. I believe I've got it fixed as of this 2009-09-15 version. --Peter Bowers September 15, 2009, at 07:45 AM
User notes +2: If you use, used or reviewed this recipe, you can add your name. These statistics appear in the Cookbook listings and will help newcomers browsing through the wiki. |