个性化阅读
专注于IT技术分析

如何在Symfony 3中将Markdown转换为HTML

本文概述

Markdown是面向Web作家的文本到HTML转换工具。 Markdown允许你使用易于阅读, 易于编写的纯文本格式进行编写, 然后将其转换为结构上有效的XHTML(或HTML)。

如果你使用Markdown, 则有很多优点。它非常灵活, 你可以将文档输出为多种格式。它可以用于在Web上发布(一旦转换为HTML), 用于发送电子邮件的富文本或导入到布局程序中。此外, Markdown不仅从技术角度来看非常快, 因为与手工制作的HTML标签相比, 简单的格式节省了大量时间, 并且通常比使用文字处理器或WYSIWYG编辑器更快。此外, 降价文件(或仅包含内容)往往比HTML文件更轻(如果我们进行大规模的讨论, 它很方便在你的项目中存储数据库)。

在本文中, 你将学习如何使用2个最著名的Markdown解析器库(用于PHP, Parsedown或PHP Markdown)在symfony 3项目中将Markdown转换为HTML。

A.使用Parsedown

Parsedown库声称在PHP中是更好的Markdown解析器(至少比其他用PHP编写的markdown解析器更好)。该库因Github风味而闻名。这意味着它可以处理Github特殊的markdown(如表格, 代码突出显示等)。

为什么要使用Parsedown?

它试图像人类一样阅读Markdown。首先, 它查看了线条。它对线路的起点很感兴趣。这有助于识别块。例如, 它知道如果某行以-(连字符)开头, 那么它可能属于一个列表。一旦识别出块, 它就会继续执行内容。在阅读时, 它会注意特殊字符。这有助于它识别内联元素(或内联)。

作者称此方法为”基于行”。他认为Parsedown是第一个使用它的Markdown解析器。自从Parsedown发布以来, 其他开发人员已使用相同的方法来开发PHP和其他语言的其他Markdown解析器。它通过了大多数CommonMark测试。大多数未通过的测试都处理非常少见的案例。不过, 随着CommonMark的成熟, 合规性应会提高。

安装

在终端中(当你位于项目目录中时)安装执行以下命令的软件包:

composer require erusev/parsedown

如果你需要有关该软件包的更多信息, 请在此处访问官方的Github存储库。

用法

要在项目中使用Parsedown, 请使用控制器顶部的use语句导入Parsedown类:

use Parsedown;

然后在变量中创建该类的新实例, 并使用text方法将markdown转换为HTML:

$Parsedown = new Parsedown();
$html = $Parsedown->text("# Hello World");

例子

以下控制器返回一些示例markdown生成的HTML作为响应:

<?php

namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

/**
 * Import Parsedown in the controller
 */
use Parsedown; 

class DefaultController extends Controller
{
    /**
     * @Route("/", name="homepage")
     */
    public function indexAction(Request $request)
    {   
        // Create a new instance of parsedown
        $Parsedown = new Parsedown();

        // Some markup to parse (it can be the content of a file or a plain string)
        $markdownToParse = <<<EOF
# About Artyom

Artyom.js is a robust and useful wrapper of the webkitSpeechRecognition and speechSynthesis APIs. Besides, artyom allows you to add dynamic commands to your web app (website).

### Speech Recognition

- Quick recognition of voice commands.
- Pause and resume command recognition.
- Artyom has available the soundex algorithm to increase the accuracy of the recognition of commands (disabled by default).
- Works both in desktop browser and mobile device.

### Voice Synthesis

- Synthesize extreme huge blocks of text (+20K words according to the last test).
- onStart and onEnd callbacks **will be always executed independently of the text length**.
- Works both in desktop browser and mobile device.

Read [the changelog to be informed about changes and additions in Artyom.js](http://docs.ourcodeworld.com/projects/artyom-js/documentation/getting-started/official-changelog)


Artyom provides **complete** support for the following languages. Every language needs an initialization code that needs to be provided in the lang property at the initialization.

| |Description |Code for initialization|
------------- | ------------- | ------------- |
|<img src="https://raw.githubusercontent.com/sdkcarlos/sdkcarlos.github.io/master/sites/artyom-resources/images/flag-usa.png" alt="Supported language"/>| English (USA)<br/>English (Great Britain) Great Britain| en-US<br/>en-GB |
|<img src="https://raw.githubusercontent.com/sdkcarlos/sdkcarlos.github.io/master/sites/artyom-resources/images/flag-spanish.png" alt="Supported language"/>| Español | es-ES |
|<img src="https://raw.githubusercontent.com/sdkcarlos/sdkcarlos.github.io/master/sites/artyom-resources/images/flag-german.png" alt="Supported language"/>| Deutsch | de-DE |
| <img src="https://raw.githubusercontent.com/sdkcarlos/sdkcarlos.github.io/master/sites/artyom-resources/images/flag-italy.png" alt="Supported language"/> | Italiano |it-IT |
EOF;

        return new Response($Parsedown->text($markdownToParse));
    }
}

前一个控制器的响应如下所示:

Parsedown HTML示例PHP Symfony

B.使用PHP Markdown

PHP Markdown的公共API包含两个解析器类Markdown和MarkdownExtra, 它们的构造函数, transform和defaultTransform函数以及它们的配置变量。 PHP Markdown库是由Michel Fortin编写的, 它基于John Gruber编写的官方Markdown项目。

为什么选择PHP Markdown

” Markdown”实际上是两件事:纯文本标记语法和最初用Perl编写的将纯文本标记转换为HTML的软件工具。 PHP Markdown是John Gruber原始Markdown程序的PHP移植。

此库软件包需要PHP 5.3或更高版本。

安装

在终端中(当你位于项目目录中时)安装执行以下命令的软件包:

composer require michelf/php-markdown

如果你需要有关该软件包的更多信息, 请在此处访问官方的Github存储库。

用法

要在项目中使用PHP Markdown, 请使用控制器顶部的use语句导入主类:

use Michelf\Markdown;

使用PHP Markdown, 你无需在变量中创建类的新实例, 因为可以使用静态方法。要将markdown转换为html, 请使用静态方法:: defaultTransform:

$html = Markdown::defaultTransform("# Hello World");

例子

以下控制器返回一些示例markdown生成的HTML作为响应:

<?php

namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

/**
 * Import the markdown class
 */
use Michelf\Markdown;

class DefaultController extends Controller
{
    /**
     * @Route("/", name="homepage")
     */
    public function indexAction(Request $request)
    {
        $markdownToParse = <<<EOF
# About Artyom

Artyom.js is a robust and useful wrapper of the webkitSpeechRecognition and speechSynthesis APIs. Besides, artyom allows you to add dynamic commands to your web app (website).

### Speech Recognition

- Quick recognition of voice commands.
- Pause and resume command recognition.
- Artyom has available the soundex algorithm to increase the accuracy of the recognition of commands (disabled by default).
- Works both in desktop browser and mobile device.

### Voice Synthesis

- Synthesize extreme huge blocks of text (+20K words according to the last test).
- onStart and onEnd callbacks **will be always executed independently of the text length**.
- Works both in desktop browser and mobile device.

Read [the changelog to be informed about changes and additions in Artyom.js](http://docs.ourcodeworld.com/projects/artyom-js/documentation/getting-started/official-changelog)


Artyom provides **complete** support for the following languages. Every language needs an initialization code that needs to be provided in the lang property at the initialization.

| |Description |Code for initialization|
------------- | ------------- | ------------- |
|<img src="https://raw.githubusercontent.com/sdkcarlos/sdkcarlos.github.io/master/sites/artyom-resources/images/flag-usa.png" alt="Supported language"/>| English (USA)<br/>English (Great Britain) Great Britain| en-US<br/>en-GB |
|<img src="https://raw.githubusercontent.com/sdkcarlos/sdkcarlos.github.io/master/sites/artyom-resources/images/flag-spanish.png" alt="Supported language"/>| Español | es-ES |
|<img src="https://raw.githubusercontent.com/sdkcarlos/sdkcarlos.github.io/master/sites/artyom-resources/images/flag-german.png" alt="Supported language"/>| Deutsch | de-DE |
| <img src="https://raw.githubusercontent.com/sdkcarlos/sdkcarlos.github.io/master/sites/artyom-resources/images/flag-italy.png" alt="Supported language"/> | Italiano |it-IT |
EOF;
       
        return new Response(Markdown::defaultTransform($markdownToParse));
    }
}

以下控制器的响应如下所示:

PHP Markdown PHP示例

如你所见, 在Github中使用的典型Markdown表未正确呈现。这仅仅是因为Markdown官方文档指出, Markdown没有为表提供任何特殊语法。相反, 它使用HTML <table>语法。但是存在Markdown语法扩展, 它们提供了用于创建简单表的其他语法。

编码愉快!

赞(0)
未经允许不得转载:srcmini » 如何在Symfony 3中将Markdown转换为HTML

评论 抢沙发

评论前必须登录!