|
Cookbook /
ConvertTABLESummary: How to convert HTML pages with tables full of data into advanced tables
Version: 17 September 2005
Prerequisites:
Status:
Maintainer: Brooks Kelley
Note: Cookbook.ConvertHTML converts HTML to PmWiki markup "on the fly"
QuestionHow can I make it easier to convert HTML pages with tables full of data into advanced tables? AnswerI had this problem with AzRepeaters.Net. I needed to convert about 20 pages filled with tables of data into a PmWiki's advanced table markup Currently, my solution is a Linux one using the Bash command line and not a PmWiki cookbook recipe. So, I ran this command line on a bash console on the HTML pages I needed to convert.... linuxmachine> cat filecontainingtabledata.html | sed '/^$/d' |sed 's/[ \t]*$//' | tr -d [:cntrl:] | tr -s [:blank:] |sed 's/</\n</g' | sed 's#^<[Tt][Rr].*>#(:cellnr:)#g' |sed 's#^<[Tt][Dd].*>#(:cell:)#g' |sed -e :a -e 's/<[^>]*>//g;/</N;//ba' | tr -d [:cntrl:] |sed 's/(:cell/\n(:cell/g' | grep "^(:cell" > data_in_pmwiki_markup.txt
Let me explain what is going on with this command line. First, be very careful with the syntax. The symbol you see above " | " is the key on your keyboard just about the enter key. It creates a pipe that streams data from one command to another until you get a final output. In other words, it is a necessary part of the command string. I start the stream by using After creating the data stream, I then pipe it into Then it is piped into Then I pipe it into The reason it might be considered clever is that sed works on one line at a time. Even though a lot of data is being shipped thru the pipe, it still is parsed one line at a time. This makes it easier to do line be line editing with a streaming editor like sed. And, as you will see later, I use it to do little but important tricks as the data streams thru each pipe. Then I pipe it again into O.K., now that is done, we can pipe it again into Then, I want to run everything together as one line. You may see why in a moment. I continue piping and use Now that everything is together. I can pipe it into the next step and set up each occurence of (:cell to start a new line. I do that with Now, I finally pipe it for the last time where I just I then edited it in a text editor. I know that Patrick talked about making this into a another PmWiki recipe since most of the cookbook is that. But this will give you a start until that is done. Neat Little Add On to make it work better By the way, in case you do run this script, you will find that you create an extra (:cell:) just after the (:cellnr:). The way to get rid of that extra (:cell:) is to add this to the script by piping the stream into the commands You will have to adjust the spacing of the blanks in sed 's/(:cellnr:) (:cell:)/(:cellnr:)/g' to get it to delete the extra (:cell:). Caveat and work around! My script does remove HTML tags that you might want to include in the final data. I had that problem too. I adjusted the script with a This means you can change tags like <A HREF=http://www.somedomain.com to [[http://www.somedomain.com. Then on the next to final command, you can change the final bit of the tag left over of > to ]] . I had to do this also because AzRepeater.Net has links showing where repeaters are on a map. Notes and Comments''The newest the latest. And please don't forget to end by date and name. Thanks) Versionsdate of publication : 2005-09-02 : name of the cookbook - version 00007
See Also
ContributorsBrooks Kelley 17 September 2005 CommentsI had this same issue, but was using WinXP and SED for Windows. What's worse is, I'd only used SED once before and that was a few years ago, so forgive me if this isn't very elegant. Below is a .BAT file (you can also use .CMD as the extension). It expects a single parameter, a filename to process. The commands are a combination of SED and some DOS batch commands, so you must have SED loaded and running on the Windows path before this .BAT file will work. The other caveat is that this process expects a "DOS CSV file" as saved by Excel. After a bit of flogging, I figured out that it was much simpler to start with a very simple format and then use some fairly simple SED commands to format. So, you can use Excel to open an HTML file, then isolate the data you want, and make sure you delete everything else on the spreadsheet you don't want, then do the "Save As..." and save the data as a DOS CSV file. This creates a very simple comma-delimited file. At that point, just run the ConvertCSVtoPMWikiTABLE.bat program with the file name as the sole parameter. When it finishes, Notepad will open with the converted results, ready to be cut-n-pasted into PmWiki.
User notes? : 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. |