vim-table-mode icon indicating copy to clipboard operation
vim-table-mode copied to clipboard

Tableize pattern for non CSV

Open slarrain opened this issue 2 years ago • 6 comments

So, I have an interesting use case for VTM.

I have a bunch of TAGS on different files/notes. I can generate a list of Tags with the :VimwikiGenerateTagLinks with the following format.

# Generated Tags

## Test0

- [Kredito](Kredito#Kredito)
- [Legalbot](Legalbot#Legalbot)

## TestA

- [TestA1](TestA1#TestA1)

## TestB

- [TestA2](TestA2#TestA2)

Now what I would like to be able to do, is to create a Table with that format. So that I can effectively create tables from tags. I was thinking of the Tableize command, because it can have an {pattern} argument. Is this possible? What would be the pattern in this case? Or there is no pattern that would allow to convert that into tables?

slarrain avatar Mar 25 '22 00:03 slarrain

@slarrain Can you share how that table should finally look like ?

dhruvasagar avatar Mar 25 '22 17:03 dhruvasagar

@slarrain The {pattern} is meant to match the delimiter that separates each column value.

dhruvasagar avatar Mar 25 '22 17:03 dhruvasagar

Thank's for the quick reply.

I was thinking of being able to auto generate from the snippet above something like the following:


| Lead                 | TestA            | TestB            |
|----------------------|------------------|------------------|
| [Kredito](Kredito)   | [TestA1](TestA1) | [TestA2](TestA2) |
| [Legalbot](Legalbot) |                  |                  |

What would be the use case? This functionality would allow you to use a sort of Kanban Board like table. To the task (or Lead or whatever) you put a tag (:tag:) or update one. Then :VimwikirebuildTags and then :VimwikiGenerateTagLinks will create the snippet from the issue. And then being able to create or update the table with the new tags will make it just like a Kanban Board. And I think is just a pattern thing, but I'm not aware of the insides of the package.

slarrain avatar Apr 04 '22 22:04 slarrain

@slarrain This can't be done natively by Tabelize or it's variants because, like I said, {pattern} currently is a regex that defines delimiters for each column value (like for csv that is a ,).

However, i'll try to share a code snippet using Table Mode APIs that can get this desired behavior.

dhruvasagar avatar Apr 05 '22 12:04 dhruvasagar

Following is a snippet that should work :

function! functions#TableizeMarkdown() range
  let line = a:firstline
  while line < a:lastline
    " First subheading
    if getline(line) =~# '^##'
      break
    endif
    let line += 1
  endwhile

  let lines = getline(line, a:lastline)
  let columns = []
  let column = []
  for line in lines
    if empty(line) | continue | endif

    if !empty(column) && line =~# '^##' " Column Header
      call add(columns, column)
      let column = []
    endif

    call add(column, line)
  endfor
  call add(columns, column) " last column

  let rows = []
  for icol in range(len(columns))
    let row = []
    for irow in range(len(columns))
      if irow < len(columns) && icol < len(columns[irow])
        let val = substitute(columns[irow][icol], '^##\|^- ', '', '')
        call add(row, val)
      else
        call add(row, '')
      endif
    endfor
    call add(rows, row)
  endfor

  exec ':' . a:firstline . ',' . a:lastline 'delete'

  let lines = map(rows, {_, row -> '|' . join(row, '|') . '|'})
  call insert(lines, '', 1)
  call append(a:firstline - 1, lines)
  call tablemode#table#AddBorder(a:firstline + 1)
  call tablemode#table#Realign(a:firstline)
endfunction

dhruvasagar avatar May 22 '22 06:05 dhruvasagar

@slarrain ^

dhruvasagar avatar May 22 '22 06:05 dhruvasagar