Giter Club home page Giter Club logo

highlightbundle's Introduction

Presentation

This bundle transforms a part of text representing a source code in html formated text. It provides 5 highlighters :

  • pygment
  • geshi
  • highlight
  • http request to appspot
  • http request to hiliteme

Two caches mechanisms permit to conserve highlighted informations about languages and work made by the highlighter, moreover symfony integrated cache.

Installation

###Standard symfony installation :

Add the following line in composer.json:

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

Activate changes via the command:

php composer.phar update

###Manual installation

Download and decompress the bundle package in vendor/bundles/Highlight manually or via github:

git submodule add git://github.com/nicodmf/HighlightBundle.git vendor/bundles/Highlight`
  • add namespace in the autoloader :
<?php
$loader->registerNamespaces(array(
    //...
    'Highlight'        => __DIR__.'/../vendor/bundles',
    //...
));

Activation

    <?php                                                                                       
    //...                                                                                       
    public function registerBundles()                                                           
    {                                                                                           
        $bundles = array(                                                                       
            //...                                                                               
            new Highlight\Bundle\HighlightBundle(),                                             
            //...                                                                               
        );                                                                                      
    }                                                                                           
    //...                                                                                       
  • add config import in app/config/config.yml
imports:
  #...
  - { resource: "@HighlightBundle/Resources/config/config.yml" }
  #...
  • add the bundle in assetic configuration in app/config/config.yml:
assetic:
    #...
    bundles:        [ HighlightBundle]
    #...

###Test and api### It is possible to add routing properties to test and/or transform this bundle in highlighting server.

  • Add imported route in routing configuration file.
import:
   resource: @HighlightBundle/Resources/config/routing.yml

The new routes are accessible in the url : http://[site]/highlight/. The web service http://[site]/highlight/api or with prefix if it has been defined.

Usage

Highlight can be use in twig or in phptemplates. Css

Except for hiliteme which not propose to transform text with adding css classes and make transform by adding css properties, the colors are configurable by the css file included in the Ressource/public directory. The colors aren't available if you don't add stylesheet in your template. Sorry for this inconvenient way, but at this moment symfony don't propose core mechanisms to link css in submodules.

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

Options

The default options can be overriden by adding options after importation of the bundle config. An example in 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

Highlight can be used as filter, function or parser

Filter

As a filter, highlight take a defined string or a defined string variable, the highlighter to use is optional :

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

Funtion

The function work with same purpose, with another syntax :

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

Block parser

The parser is simply to use, because you don't have obligation to defined a variable. The code wich would be transformate is beetween standard twig tag. The highlighter is always optional :

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

Extends

Provider

It is very simple to add another provider if you want to use one not listed here.

In the provider directory :

  • Create a provider class in providers
  • Add its creation in factory.php In the DependancyInjection directory
  • Update the Configuration.php to added the new parameters

highlightbundle's People

Contributors

alphalemon avatar brunschgi avatar inmarelibero avatar jdewit avatar jsgoupil avatar nicodmf avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

highlightbundle's Issues

Symfony crashes when using provider

When using a provider, Symfony 2.2 crashes

{% highlight 'php' ['geshi'] %}
test
{% endhighlight %}

outputs this:

ob_start();        // line 41
echo "            test
";
$context['source_321381366344038'] = new Twig_Markup(ob_get_clean(), $this->env->getCharset());
$context['language_321381366344038'] = $this->getAttribute("php", "geshi", array(), "array"); // CRASHES HERE
$context['provider_321381366344038'] = null;

The error is:

Impossible to access a key ("geshi") on a "string" variable in ...twig

Geshi should surface more methods

The current call "enable_keyword_links" is not surfaced to the config.

The config should be coded so that it can be automatically picked up from the yml.

Comparing false values from config

When setting linenos to false, Factory.php compares that value with the following equality:

if($v!=null and $v!="")$opt[$k]=$v;

Since

false!=null and false!=""

both resolves to false, then the opt is not assigned a correct boolean.

Consider changing != by !== in both cases

How to acces Providers ?

Hi,

In my action, I'd like to access the main Geshi object for example, so I can could call all methods available in the Geshi class ? It is possible with the current implementation of the bundle ?

Something like that:

$this->container->get('highlight.twig.extension')->getProviders()->getProvider()->getInstance()->getHighlighter()->set_tab_width(2);

It would be great to be able to access providers as services ?

$this->container->get('highlight.geshi')->set_tab_width(2);

Thanks. COil

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.