开发者

HTML table to “graphical text” for code comments

开发者 https://www.devze.com 2022-12-31 13:23 出处:网络
Is there a tool (ideally command-line-based) that can help in converting the source to HTML tables into “graphical text” (think perhaps ASCII art for HTML tables) for use in code comments, as show b

Is there a tool (ideally command-line-based) that can help in converting the source to HTML tables into “graphical text” (think perhaps ASCII art for HTML tables) for use in code comments, as show below?

For example, given the following HTML table source

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

the tool would output something like the following to be embedded into code comments (like /*…*/):

/*
          A test table with merged cells
+----------+-------------------+----------+--------+ 
|          |      Average      |  other   |  Misc  |
|          +---------+---------+ category +--------|
|          |  height |  weight |          |        |
|----------+---------+---------+----------+--------|
| males    |   1.9   |  0.003  |          |        |
|----------+---------+---------+----------+--------|
| females  |   1.7   |  0.002  |          |       开发者_JAVA百科 |
+----------+---------+---------+----------+--------+
*/

Background: A piece of code that reads values from HTML tables can be annotated with comments depicting text-based graphical representations of complex HTML table layouts. Someone maintaining the code later can then find it easier to understand, for example, how a piece of code is slicing and dicing an HTML table or plucking values at certain cell positions.


  elinks -dump 1 

http://elinks.or.cz/documentation/manpages/elinks.1.html


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've found an online tool: https://tableconvert.com/

  1. Click on Import tab
  2. Choose HTML tab
  3. Paste your HTML data
  4. Click on Import Data

Result:

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

Not perfect but it works :)


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!


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


Ref: Tool to convert CSV data to stackoverflow friendly text only table

Highly recommended a online tools: Convert CSV to ASCII Table

It is my favorite conversion tool designed for table format, and it can convert csv into different friendly plain text tables:

  1. ASCII table (mysql style)
+─────────────+─────────+───────────────────+
| 2012-05-02  | palani  | My first comment  |
+─────────────+─────────+───────────────────+
| 2012-05-02  | raja    | My second comment |
+─────────────+─────────+───────────────────+
| 2012-05-02  | palani  | My third comment  |
+─────────────+─────────+───────────────────+
| 2012-05-03  | raja    | My fourth comment |
+─────────────+─────────+───────────────────+
  1. reStructuredText Grid table
+-------------+---------+-------------------+
| 2012-05-02  | palani  | My first comment  |
+=============+=========+===================+
| 2012-05-02  | raja    | My second comment |
+-------------+---------+-------------------+
| 2012-05-02  | palani  | My third comment  |
+-------------+---------+-------------------+
| 2012-05-03  | raja    | My fourth comment |
+-------------+---------+-------------------+
  1. Unicode table (single line)
┌─────────────┬─────────┬───────────────────┐
| 2012-05-02  | palani  | My first comment  |
├─────────────┼─────────┼───────────────────┤
| 2012-05-02  | raja    | My second comment |
| 2012-05-02  | palani  | My third comment  |
| 2012-05-03  | raja    | My fourth comment |
└─────────────┴─────────┴───────────────────┘
  1. Unicode table
╔═════════════╦═════════╦═══════════════════╗
║ 2012-05-02  ║ palani  ║ My first comment  ║
╠═════════════╬═════════╬═══════════════════╣
║ 2012-05-02  ║ raja    ║ My second comment ║
║ 2012-05-02  ║ palani  ║ My third comment  ║
║ 2012-05-03  ║ raja    ║ My fourth comment ║
╚═════════════╩═════════╩═══════════════════╝

And some other formats of plain text ASCII Table. You can also type "Force separate lines" to decide whether to express the title clearly!

HTML table to “graphical text” for code comments

Unfortunately, it does not yet support cross-rows and cross-columns. I don’t know if it meets your needs.

0

精彩评论

暂无评论...
验证码 换一张
取 消