HTML table to “graphical text” for code comments

青春壹個敷衍的年華 提交于 2019-12-03 15:36:27

There's a tool which does exactly this, written in python:

See: https://github.com/gustavklopp/DashTable

DashTable

HTML table to ASCII table, colspan & Rowspan allowed!

HTML::TreeBuilder plus Text::ASCIITable looks like they would need only a little glue to do the job.

There's another tool (YATG, Yet Another Table Generator) which does exactly this, written in python:

See: https://github.com/10gic/yatg

Example of output (emacs style):

+---------+-----------------+----------+
|         | Average         | Red eyes |
|         +--------+--------+          |
|         | height | weight |          |
+---------+--------+--------+----------+
| Males   | 1.9    | 0.003  | 40%      |
+---------+--------+--------+----------+
| Females | 1.7    | 0.002  | 43%      |
+---------+--------+--------+----------+

Example of output (orgmode style):

| Header content 1 | Header content 2 |
|------------------+------------------|
| Body content 1   | Body content 2   |
| Body content 3   | Body content 4   |
| Body content 5   | Body content 6   |

Example of output (mysql style):

+------------------+------------------+
| Header content 1 | Header content 2 |
+------------------+------------------+
| Body content 1   | Body content 2   |
| Body content 3   | Body content 4   |
| Body content 5   | Body content 6   |
+------------------+------------------+

Example of output (markdown style):

| Header content 1 | Header content 2 |
|------------------|------------------|
| Body content 1   | Body content 2   |
| Body content 3   | Body content 4   |
| Body content 5   | Body content 6   |

I don't know which language are you talking about but I use this function (PHP) for that:

function text_table($data)
{
    $keys = array_keys(end($data));
    $size = array_map('strlen', $keys);

    foreach(array_map('array_values', $data) as $e)
        $size = array_map('max', $size,
            array_map('strlen', $e));

    foreach($size as $n) {
        $form[] = "%-{$n}s";
        $line[] = str_repeat('-', $n);
    }

    $form = '| ' . implode(' | ', $form) . " |\n";
    $line = '+-' . implode('-+-', $line) . "-+\n";
    $rows = array(vsprintf($form, $keys));

    foreach($data as $e)
        $rows[] = vsprintf($form, $e);
    return $line . implode($line, $rows) . $line;
}

Usage:

    echo "<pre>\n";
    echo text_table($array);
    echo "</pre>\n";
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!