Skip to content

Instantly share code, notes, and snippets.

@sooop
Last active April 14, 2016 10:46
Show Gist options
  • Save sooop/4770321 to your computer and use it in GitHub Desktop.
Save sooop/4770321 to your computer and use it in GitHub Desktop.
# mkTable vim script for making HTML Table
"""" remove empty lines
function! remove_empty_lines()
execute "%g/^[ \t\s]*$/normal dd"
endfunction
"" function to embrace line with tag.
function! embrace_tag(el)
call setline(line('.'), "<".a:el.">".getline('.')."</".a:el.">")
"""" call : call a function
"""" setline > set given line to text.
"""" getline > get text of given line
"""" `a:el` means variable el as function parameter.
endfunction
function! make_table(span)
call remove_empty_lines()
execute "normal gg"
let l:cl = 1
let l:hl = line('$')
"""" make each line into td element
while (l:cl < l:hl)
call embrace_tag("td")
+
"""" move next line ( :exe "+")
let l:cl = l:cl + 1
endwhile
"""" go to top again
execute "normal gg"
let l:cl = 1
"""" join lines by <span>
while l:cl < l:hl
execute "normal ".a:span."J"
+
let l:cl = l:cl + 1
let l:hl = line('$')
endwhile
"""" join lines(now all lines are groups of td elements) and make it tr elements
execute "normal gg"
let l:cl = 1
let l:hl = line('$')
while l:cl < l:hl + 1
call embrace_tag("tr")
+
let l:cl = l:cl + 1
let l:hl = line('$')
endwhile
"""" add <table> tag to beginning of document and end of document.
execute "normal ggO\<ESC>"
execute "normal Go\<ESC>"
call setline(1,"<table>")
call setline(line('$'), "</table>")
setf html
endfunction
function! remove_tag_attributes()
execute "silent %s/width=\"[^\"]*\"//g"
execute "silent %s/bgcolor=\"[^\"]*\"//g"
execute "silent %s/height=\"[^\"]*\"//g"
execute "silent %s/class=\"[^\"]*\"//g"
execute "silent %s/color=\"[^\"]*\"//g"
execute "silent %s/style=\"[^\"]*\"//g"
execute "silent %s/align=\"[^\"]*\"//g"
endfunction
func! make_heading3()
execute "%s/^#.*$/<h3>&<\\/h3>/g"
execute "%s/<h3>#/<h3>/g"
endfunc
func! LinkEmail()
execute "%s/[0-9a-z._\]*@[0-9a-zA-Z._\]*/<a href=\"mailto:&\">&<\\/a>/g"
endfunc
"""" some shortcuts for convenience
map ,t :call make_table();
map ,j v//tr<CR>Jj
map ,h :%s/^[ \t\s]*//g
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment