webeith/highlight-bundle

此组件将表示源代码的文本部分转换为html格式的文本,提供5种高亮显示器:pygment、geshi、highlight、向appspot发送http请求、向hiliteme发送http请求

安装: 28

依赖: 0

建议者: 0

安全: 0

星星: 0

关注者: 2

分支: 5

类型:symfony-bundle

dev-master 2014-10-19 13:02 UTC

This package is not auto-updated.

Last update: 2024-09-24 07:47:56 UTC


README

此组件将表示源代码的文本部分转换为html格式的文本。它提供了5种高亮显示器

  • pygment
  • geshi
  • highlight
  • 向appspot发送http请求
  • 向hiliteme发送http请求

两种缓存机制可以保存关于语言和由高亮显示器完成的工作的高亮信息,此外还有集成的symfony缓存。

安装

###标准symfony安装

在composer.json中添加以下行

{
//...
   "require":{
      //....
      "nicodmf/highlight-bundle": "2.1.*"
      //...
   }
}

通过命令激活更改

php composer.phar update

###手动安装

手动或在github上下载并解压组件包到vendor/bundles/Highlight

git submodule add git://github.com/nicodmf/HighlightBundle.git vendor/bundles/Highlight`
  • 在自动加载器中添加命名空间
<?php
$loader->registerNamespaces(array(
    //...
    'Highlight'        => __DIR__.'/../vendor/bundles',
    //...
));

激活

    <?php                                                                                       
    //...                                                                                       
    public function registerBundles()                                                           
    {                                                                                           
        $bundles = array(                                                                       
            //...                                                                               
            new Highlight\Bundle\HighlightBundle(),                                             
            //...                                                                               
        );                                                                                      
    }                                                                                           
    //...                                                                                       
  • 在app/config/config.yml中添加配置导入
imports:
  #...
  - { resource: "@HighlightBundle/Resources/config/config.yml" }
  #...
  • 在app/config/config.yml中添加组件到assetic配置
assetic:
    #...
    bundles:        [ HighlightBundle]
    #...

###测试和API### 可以添加路由属性以测试和/或将此组件转换为高亮显示服务器。

  • 在路由配置文件中添加导入的路由
import:
   resource: @HighlightBundle/Resources/config/routing.yml

新的路由在url中可通过: http://[site]/highlight/ 访问。Web服务 http://[site]/highlight/api 或带有前缀(如果已定义)。

使用方法

高亮显示可以在twig或phptemplates中使用。Css

除了hiliteme不提供通过添加CSS类转换文本和通过添加CSS属性进行转换外,颜色可以通过包含在Ressource/public目录中的CSS文件进行配置。如果未在模板中添加样式表,则颜色不可用。对此不便之处表示歉意,但截至目前,symfony尚未提供核心机制来在子模块中链接CSS。

{% stylesheets '@HighlightBundle/Resources/public/*.css' output='css/a.css' %}
       <link href="{{ asset_url }}" type="text/css" rel="stylesheet" />
{% endstylesheets %}

选项

默认选项可以通过在导入组件配置后添加选项来覆盖。一个示例在app/config/config.yml中

highlight:
   # Each provider in this list are use on after other
   # if language given in template ins't allowed
   providers: [ geshi, httphiliteme, httpappspot, highlight, pygment ]
   # All globals options can be rewrite in a specific provider
   # except cssclass who just added
   globals:
       linenos: true
       blockstyles: ""
       cssclass: highlight

   highlight:
       linenos: true
       blockstyles: ""
       cssclass: highlight

   pygment:
       linenos: true
       blockstyles: ""
       cssclass: pygment

   geshi:
       linenos: false
       # Two possibilities fancy or normal
       linestyle: normal
       cssclass: geshi

   #line number not available with appspot
   httpappspot:
       blockstyles: "overflow:auto;color:white !important;"
       cssclass: pygment appspot

   httphiliteme:
       linenos: false
       #One of : colorful default emacs friendly fruity manni monokai murphy native pastie perldoc tango trac vs
       style: pastie
       #Additionnal css directive for div block
       blockstyles: "overflow:auto;color:white !important;"
       cssclass: pygment hiliteme
       
services:
    highlight.configuration:
        class: Highlight\Bundle\HighlightBundle
        tags:
            - { name: configuration }
    highlight.twig.extension:
        class: Highlight\Bundle\Extension\TwigExtension
        tags:
            - { name: twig.extension }
        arguments: [@translator, @kernel, @service_container ]
        #arguments: [@translator, @templating.globals, @templating.helper.assets ]

In Twig

高亮显示可以作为过滤器、函数或解析器使用

过滤器

作为过滤器,高亮显示接受一个定义的字符串或一个定义的字符串变量,使用的高亮显示器是可选的

{{ aDefinedStringVariable|highlight 'php' ['pygment'] }}

函数

函数具有相同的目的,但语法不同

{{ highlight( aDefinedStringVariable, 'php'[, 'pygment']) }}

块解析器

解析器非常简单易用,因为您没有义务定义变量。将要转换的代码在标准twig标签之间。高亮显示器始终是可选的

{% highlight 'php' ['pygment'] %}
<?php echo "Bonjour à tous"; ?>
{% endhighlight %}

扩展

提供者

如果您想使用此处未列出的提供者,添加另一个提供者非常简单。

在提供者目录中

  • 在提供者中创建一个提供者类
  • 在DependencyInjection目录中的factory.php中添加其创建
  • 更新Configuration.php以添加新参数