2017 © Pedro Peláez
 

symfony-bundle code-highlight-bundle

Simple way to display highighted code in our content using highlight.js library.

image

kpicaza/code-highlight-bundle

Simple way to display highighted code in our content using highlight.js library.

  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

CodeHighlightBundle

Simple way to display highighted code in our content using highlight.js library. Works nice with markdown text inputs., (*1)

knpbundles.com, (*2)

1. Instalation:

Add the following to your composer json:, (*3)

// composer.json
"require": {
    ...,
    "components/highlightjs": "^9.0",
    "kpicaza/code-highlight-bundle": "0.1.x-dev",
    ...
}

or open console and type, (*4)

composer require kpicaza/code-highlight-bundle=dev-master

Activate bundle in your kernel:, (*5)

// app/AppKernel.php
public function registerBundles()
{
    $bundles = array(
      ...
      new Kpicaza\Bundle\CodeHighlightBundle\CodeHighlightBundle(),
      ...
}

Enable routing (optional, only if you wish to play demo at http://you_site.com/highlight/hello/{name}):, (*6)

// app/config/routing.yml
code_highlight:
    resource: "@CodeHighlightBundle/Resources/config/routing.yml"
    prefix:   /hightlight

2. Config:

Now we need to update Assetic configuration to enable js and css from highlight.js library, (*7)

// app/config/config.yml
# Assetic Configuration
assetic:
    ...
    bundles:        [ CodeHighlightBundle ] #enables bundle mapping by assetic
    ...
    assets:
        ...
        highlight_js: # enable javascript library
            inputs:
                - %kernel.root_dir%/../vendor/components/highlightjs/highlight.pack.min.js
            output: js/highlight.js
        # Import wanted themes for highlight see https://highlightjs.org/static/demo/
        # for example I'm going to enable two themes, monokai_sublime and github
        highlight_monokai_sublime_css:
            inputs:
                - %kernel.root_dir%/../vendor/components/highlightjs/styles/monokai_sublime.css
            output: css/highlight.css
        highlight_github_css:
            inputs:
                - %kernel.root_dir%/../vendor/components/highlightjs/styles/github.css
            output: css/highlight.css

Now we can import files:, (*8)

php app/console assetic:dump
php app/console assets:install

3. Usage:

3.1 Basic Usage:

Add stylesheet and init javascript in your template, (*9)

<html>
    <title>CodeHighlightBundle Demo</title>
    <head>
        {{ highlight_theme('monokai_sublime') }}
    </head>
    <body>
        ...
        <footer>
            ...
        </footer>
        <!-- Include all JavaScripts, compiled by Assetic -->
        <script src="{{ asset('js/jquery.js') }}"></script>
        {{ highlight_init_js() }}

    </body>
</html>

Add "hljs" and programming language name as classes to html "pre" tag to highlight content, for example:, (*10)

<pre><code>
    class MyClass {
        private $foo;

        public function _construct($foo = null)
        {
            $this->foo = $foo;
        }
    }

</code></pre>

3.2 Advanced Usage:

@todo, (*11)

4. @todo

  • 4.1 Adbanced usage documentation
  • 4.2 only add templating service as depency instead of service_container
  • 4.3 tests
  • 4.4 Avoid text formating issues wysiwyg editors

5. Library docs:

http://highlightjs.readthedocs.org/, (*12)

The Versions

05/01 2016

dev-master

9999999-dev

Simple way to display highighted code in our content using highlight.js library.

  Sources   Download

MIT

The Requires