nicodmf/highlight-bundle

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

安装: 48

依赖关系: 1

建议者: 0

安全: 0

星星: 21

关注者: 3

分支: 5

开放问题: 3

类型:symfony-bundle

2.1.x-dev 2012-10-20 22:48 UTC

This package is auto-updated.

Last update: 2024-09-12 07:07:22 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

###手动安装

手动下载并解压缩包到vendor/bundles/Highlight或通过github

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或如果已定义前缀,则使用前缀。

使用方法

Highlight可以在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 ]

在Twig中

Highlight可以用作过滤器、函数或解析器

过滤器

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

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

函数

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

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

块解析器

解析器非常容易使用,因为您没有义务定义一个变量。将被转换的代码位于标准twig标签之间。高亮显示器始终是可选的

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

扩展

提供者

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

在提供者目录中

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