sponsor Vim development Vim logo Vim Book Ad

intermediate Tip #110: text->html table converter.

 tip karma   Rating 1/3, Viewed by 1125 

created:   September 7, 2001 2:40      complexity:   intermediate
author:   Wenzhi Liang      as of Vim:   5.7

Below are two functions and a mapping which will convert lines of plain text into
HTML table code.
For example, you have several lines like:
-----------------------------------------------
1
2
3

4
5
6
---------------------------------------------------
by visualizing all the 7 lines and press <F5>, you can change the text into
<table><tr>
   <td>1</td>
   <td>2</td>
   <td>3</td>
</tr><tr>
   <td>4</td>
   <td>5</td>
   <td>6</td>
</tr></table>
which will eventually render into a table.
So the rule is:
Every line is a table item, every empty line means starting of a new table row.



"A text->html table code converter
"By: Wenzhi Liang [email protected]
"You can distribute/change this file freely as long as you keep the title area. Thanks

func Table()
let end=line("'>")
let start=line("'<")
let i=start

wh i <= end
exe ":" . i
let e=Empty()
if e == 1
exe "normal I</tr><tr>"
else
exe "normal I<td>A</td>>>"
endif
let i=i+1
endwh

exe "normal o</tr></table><<"
exe ":" . start
exe "normal O<table><tr><<"
endfunc

vmap <F5> <ESC>:call Table()<CR>

func Empty()
let line_nr= line (".")
let a=getline ( line_nr )
let m=match(a, "\\S")
if m == -1
return 1
else
return 0
endif
endfunc

 rate this tip  Life Changing Helpful Unfulfilling 

<<jump between files | Printing with syntax highlighting independent of your normal highlighting >>

Additional Notes

[email protected], September 7, 2001 2:48
Sorry. The character not properly displayed should be <CTL-[>
[email protected], September 12, 2001 2:24
or

:s/\(\S\+\)/<TD>\1<\/TD><CR>:'<,'>s/^\s*$/<\/TR><TR><CR>'>o</TR></TABLE><ESC>'<O<TABLE><TR><ESC>

.. for those that like compact but unreadable
Anonymous, September 12, 2001 2:25
make that

:map <F5> :s/\(\S\+\)/<TD>\1<\/TD><CR>:'<,'>s/^\s*$/<\/TR><TR><CR>'>o</TR></TABLE><ESC>'<O<TABLE><TR><ESC>

to work in the same manner as the original posting
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to [email protected] after searching the archive. Help Bram help Uganda.
Sponsored by Web Concept Group Inc. SourceForge Logo