用于代码注释的HTML表格到“图形文本”

时间:2010-05-17 11:59:12

标签: html html-table ascii-art code-comments

是否有一个工具(理想情况下基于命令行)可以帮助将源表转换为HTML表格(对于HTML表格可能是ASCII art),以便在代码注释中使用,如show所示下面?

例如,给定以下HTML表源

<TABLE BORDER=1>
  <CAPTION>A test table with merged cells</CAPTION>
  <TR><TH ROWSPAN=2><TH COLSPAN=2>Average
  <TH ROWSPAN=2>other<BR>category<TH>Misc
  <TR><TH>height<TH>weight
  <TR><TH ALIGN=LEFT>males<TD>1.9<TD>0.003
  <TR><TH ALIGN=LEFT ROWSPAN=2>females<TD>1.7<TD>0.002
</TABLE>

该工具会输出类似以下内容的内容,以嵌入到代码注释中(例如/*…*/):

/*
          A test table with merged cells
+----------+-------------------+----------+--------+ 
|          |      Average      |  other   |  Misc  |
|          +---------+---------+ category +--------|
|          |  height |  weight |          |        |
|----------+---------+---------+----------+--------|
| males    |   1.9   |  0.003  |          |        |
|----------+---------+---------+----------+--------|
| females  |   1.7   |  0.002  |          |        |
+----------+---------+---------+----------+--------+
*/

背景:可以使用描述复杂HTML表格布局的基于文本的图形表示的注释来注释从HTML表中读取值的一段代码。稍后维护代码的人可以更容易理解,例如,一段代码如何切片和切割HTML表格或在某些单元格位置采集值。

6 个答案:

答案 0 :(得分:2)

答案 1 :(得分:2)

HTML::TreeBuilder加上Text::ASCIITable看起来他们只需要一点胶水来完成这项工作。

答案 2 :(得分:1)

还有另一个工具(YATG,Yet Another Table Generator)正是这样做的,用python编写:

请参阅:https://github.com/10gic/yatg

输出示例(emacs样式):

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

输出示例(orgmode样式):

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

输出示例(mysql样式):

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

输出示例(降价样式):

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

答案 3 :(得分:0)

我不知道你在谈论哪种语言,但我使用这个函数(PHP):

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;
}

<强>用法:

    echo "<pre>\n";
    echo text_table($array);
    echo "</pre>\n";

答案 4 :(得分:0)

有一个工具可以完成这个,用python编写:

请参阅:https://github.com/gustavklopp/DashTable

  

DashTable

     

HTML表到ASCII表,colspan&amp; Rowspan允许!

答案 5 :(得分:0)

我找到了一个在线工具:https://tableconvert.com/

  1. 点击导入标签
  2. 选择HTML标签
  3. 粘贴您的 HTML 数据
  4. 点击导入数据

结果:

|         | Average | othercategory | Misc |
|---------|---------|---------------|------|
| height  | weight  |
| males   | 1.9     | 0.003         |
| females | 1.7     | 0.002         |

不完美,但有效:)