maurymmarques/markdown-plugin

一个 CakePHP 插件,简化 PHP Markdown 在 CakePHP 中的使用

dev-master 2015-02-10 05:02 UTC

This package is not auto-updated.

Last update: 2024-09-18 06:49:40 UTC


README

此插件简化了在 CakePHP 中使用 PHP Markdown 的过程

PHP Markdown 是由 John Gruber 编写的 Markdown 程序的 PHP 版本。

“Markdown”有两层含义:一种纯文本标记语法,以及一个将纯文本标记转换为 HTML 以在网络上发布的软件工具。

更多信息: http://michelf.com/projects/php-markdown

对于此插件,Markdown 应用程序位于 Vendor 中

版本

为 CakePHP 2.x 编写

版权

版权 (c) 2011 Maury M. Marques

安装

您可以使用 Composer、GIT Submodule、GIT Clone 或手动方式安装此插件

[使用 Composer]

将插件添加到项目的 composer.json 文件中 - 例如:

{
  "require": {
    "maurymmarques/markdown-plugin": "dev-master"
  },
  "extra": {
    "installer-paths": {
      "app/Plugin/Markdown": ["maurymmarques/markdown-plugin"]
    }
  }
}

然后只需运行 composer install

由于此插件在其自己的 composer.json 中设置了类型 cakephp-plugin,Composer 会将其安装在您的 /Plugin 目录中,而不是在常规的 vendors 文件中。

[GIT Submodule]

在您的应用目录(app/Plugin)中输入:

git submodule add git://github.com/maurymmarques/markdown-cakephp.git Plugin/Markdown
git submodule init
git submodule update

[GIT Clone]

在您的插件目录(app/Pluginplugins)中输入:

git clone https://github.com/maurymmarques/markdown-cakephp.git Markdown

[手动]

  • 下载 Markdown 存档
  • 解压缩下载的文件。
  • 将生成的文件夹重命名为 Markdown
  • 然后将此文件夹复制到 app/Plugin/plugins

配置

在 app/Config/bootstrap.php 中启动插件

CakePlugin::load(array('Markdown' => array('bootstrap' => true)));

使用

使用 插件语法 启用辅助器

如果需要,设置组件以帮助从 Markdown 返回数据。

// in app/Controller/BakeriesController.php
class BakeriesController extends AppController {

		public $helpers = array('Markdown.Markdown');

		public function index() {
			$this->set('textInMarkdownFormat', $yourTextInMarkdownFormat);
		}
}

或者,如果 Markdown 内容在文件中...

// in app/Controller/BakeriesController.php
class BakeriesController extends AppController {

		public $helpers = array('Markdown.Markdown');
		public $components = array('Markdown.Markdown');

		public function index() {
			$this->set('textInMarkdownFormat', $this->Markdown->getFile($pathToMarkdownFile));
		}
}

在视图中可以使用类似以下内容:

// in app/View/Bakeries/index.ctp
echo $this->Markdown->transform($textInMarkdownFormat);

在模型中可以使用类似以下内容:

// in app/Model/Bakery.php
class BakeryModel extends AppModel {
	
	public $actsAs = array('Markdown.Markdown');

	public function beforeSave($options = array()) {
		$this->data[$this->alias]['text_html'] = $this->transform( $this->data[$this->alias]['text_md'] );
		return true;
	}
}