我正在嘗試使用 Perl 模塊將 Markdown 文本轉換為 HTML 表格。我嘗試使用 Text::Markdown,但它沒有用。
我還嘗試使用 Markdown::Table 并遵循檔案:https ://metacpan.org/pod/Markdown::Table ,但同樣沒有結果。
這是我的代碼:
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 ) );
輸出:
有人可以幫忙嗎?
uj5u.com熱心網友回復:
我建議使用 Perl 的Pandoc模塊,它是Pandoc的包裝器,它是一種將檔案從一種格式轉換為另一種格式的工具。例如,
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);
哪個輸出:
<p>Some <code>standard</code> <strong>Markdown</strong> <em>syntax</em>. Also, a table:</p>
<table>
<thead>
<tr class="header">
<th>country</th>
<th>capital</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>France</td>
<td>Paris</td>
</tr>
<tr class="even">
<td>UK</td>
<td>London</td>
</tr>
<tr class="odd">
<td>Germany</td>
<td>Berlin</td>
</tr>
</tbody>
</table>
請注意,要使其正常作業,您必須安裝 Perl 的 Pandoc 模塊(cpanm Pandoc例如 with)以及 Pandoc 程式(sudo apt install pandoc在 Debian/Ubuntu 上使用 with;請參閱為其他系統安裝 pandoc )。
關于您嘗試使用的其他模塊:
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.
uj5u.com熱心網友回復:
使用Text::MultiMarkdown原 Markdown 不支持表格。并且Text::Markdown只支持原始語法。
Text::MultiMarkdown具有附加功能。
- 專案網站:https ://fletcherpenney.net/multimarkdown/
- 特點:https ://rawgit.com/fletcher/MultiMarkdown-6-Syntax-Guide/master/index.html
另外,您必須更改表格格式。
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;
會產生
<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>
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/425536.html
上一篇:從Perl5.30起不再支持$*
下一篇:如何將條件和回圈放在同一行?
