Home > Software design >  convert markdown text to html table using perl
convert markdown text to html table using perl

Time:02-14

I'm trying to convert a Markdown text to HTML table with Perl modules. I tried to use Text::Markdown, but it didn't work.

I also tried to use Markdown::Table and follow the documentation: https://metacpan.org/pod/Markdown::Table, but same thing no result.

Here is my code:

use Markdown::Table;

my $markdown = q~
| Id | Name | Role |
 --- --- --- 
| 1 | John Smith | Testrole |
| 2 | Jane Smith | Admin |
~;
 
my @tables = Markdown::Table->parse($markdown);
                        
use Data::Dumper;
Core::Global::Logger::debug(Dumper( $tables[0]->get_table ) );

Output: enter image description here

can anyone help about that please ?

CodePudding user response:

I'd recommend to use Perl's Pandoc module, which is a wrapper around Pandoc, a tool to convert documents from one format to another. For instance,

use Pandoc;

my $markdown = q(
Some `standard` **Markdown** _syntax_. Also, a table:

country | capital
--------|---------
France  | Paris
UK      | London
Germany | Berlin);

print pandoc->convert('markdown' => 'html', $markdown);

Which outputs:

<p>Some <code>standard</code> <strong>Markdown</strong> <em>syntax</em>. Also, a table:</p>
<table>
<thead>
<tr >
<th>country</th>
<th>capital</th>
</tr>
</thead>
<tbody>
<tr >
<td>France</td>
<td>Paris</td>
</tr>
<tr >
<td>UK</td>
<td>London</td>
</tr>
<tr >
<td>Germany</td>
<td>Berlin</td>
</tr>
</tbody>
</table>


Regarding the other modules you tried to use:

  • Markdown::Table is a module that aims at helping you parse and manipulate Markdown tables. It does not generate HTML (nor anything besides Markdown for that matter).

  • Text::Markdown generates HTML from Markdown, but does not support Markdown tables. The syntax of Markdown supported by this module is specified in https://daringfireball.net/projects/markdown/syntax, and does not include tables.
    Still, this module allows you to write HTML inside your Markdown, which could be a workaround, depending on your exact requirements. For instance:

    use Text::Markdown 'markdown';
    
    my $markdown = q(
    This _text_ uses **Markdown** `syntax`. Then a table:
    
    <table>
    <tr><td>country</td><th>capital</th></tr>
    <tr><td>France</td><td>Paris</td></tr>
    <tr><td>UK</td><td>London</td></tr>
    <tr><td>Germany</td><td>Berlin</td></tr>
    </table>);
    
    print markdown( $markdown );
    

    Text::Markdown will convert the Markdown elements to HTML, while preserving the table.

CodePudding user response:

Use Text::MultiMarkdown the original Markdown doesn't support tables. And Text::Markdown only supports the orignal syntax.

Text::MultiMarkdown has additional features.

Additional you have to change the table format.

use v5.10;
use Text::MultiMarkdown qw(markdown);

my $text = q[
| Id | Name | Role |
|---|---|---|
| 1 | John Smith | Testrole |
| 2 | Jane Smith | Admin |
];

# Replace any CRLF/LF line-endings to the native Line-Ending used by Perl
$text =~ s/\R/\n/g;

my $html = markdown($text);

printf "%s\n", $html;

will produce

<table>
<col />
<col />
<col />
<thead>
<tr>
        <th>Id</th>
        <th>Name</th>
        <th>Role</th>
</tr>
</thead>
<tbody>
<tr>
        <td>1</td>
        <td>John Smith</td>
        <td>Testrole</td>
</tr>
<tr>
        <td>2</td>
        <td>Jane Smith</td>
        <td>Admin</td>
</tr>
</tbody>
</table>
  • Related