Source

php-libs / zf_highlight / README

# Highlight.js view helper for Zend Framework

- [HeadLink and HeadScript helpers](http://framework.zend.com/manual/en/zend.view.helpers.html).

# Usage

    $this->view->highlight()
            ->setBasePath('js/text/highlight')
            ->setScriptPath('/highlight.pack.js')
            ->setStylePath('/styles/vs.css')
            ->setTabReplace('    ')
            ->setLanguages(array('php', 'js'))
            ->inject();

or

    $this->view->highlight(array(
        'basePath' => 'js/text/highlight',
        'scriptPath' => '/highlight.pack.js',
        'stylePath' => '/styles/vs.css',
        'tabReplace' => '    ',
        'languages' => array('php', 'js'),
    ))->inject();

# Source code

Source code: [Text_View_Helper_Highlight](http://bitbucket.org/vladimir_webdev/zf_highlight/src/).

    <?php
    class Text_View_Helper_Highlight extends Zend_View_Helper_Abstract
    {
        protected $basePath = '';
        protected $scriptPath = '';
        protected $stylePath = '';
        protected $tabReplace = '';
        protected $languages = array();

        public function setBasePath($path)
        {
            $this->basePath = $path;
            return $this;
        }

        public function getBasePath()
        {
            return $this->basePath;
        }

        public function setScriptPath($path)
        {
            $this->scriptPath = $this->getBasePath() . $path;
            return $this;
        }

        public function getScriptPath()
        {
            return $this->scriptPath;
        }

        public function setStylePath($path)
        {
            $this->stylePath = $this->getBasePath() . $path;
            return $this;
        }

        public function getStylePath()
        {
            return $this->stylePath;
        }

        public function setTabReplace($tabReplace)
        {
            $this->tabReplace = $tabReplace;
            return $this;
        }

        public function getTabReplace()
        {
            return $this->tabReplace;
        }

        public function setLanguages(array $languages)
        {
            $this->languages = $languages;
            return $this;
        }

        public function getLanguages()
        {
            return $this->languages;
        }

        public function setOptions(array $options)
        {
            foreach ($options as $key => $value) {
                $methodName = 'set' . $key;
                if (method_exists($this, $methodName)) {
                    $this->$methodName($value);
                } else {
                    throw new Zend_View_Exception('Unknown option "' . $key . '"');
                }
            }
            return $this;
        }

        public function highlight(array $options = array())
        {
            $this->setOptions($options);
            return $this;
        }

        protected function _getScript()
        {
            $script = '';

            $tabReplace = $this->getTabReplace();
            if ('' !== $tabReplace) {
                $tabReplace = str_replace('"', '\"', $tabReplace);
                $script .= 'hljs.tabReplace = "' . $tabReplace . '";' . PHP_EOL;
            }

            $languages = $this->getLanguages();
            if (array() !== $languages) {
                $languages = '"' . implode('", "', $languages) . '"';
            } else {
                $languages = '';
            }
            $script .= 'hljs.initHighlightingOnLoad(' . $languages . ');' . PHP_EOL;

            return $script;
        }

        public function inject()
        {
            $localPath = $this->getScriptPath();
            if (!empty($localPath)) {
                $this->view->headScript()
                        ->appendFile($localPath)
                        ->appendScript($this->_getScript());
            } else {
                throw new Zend_View_Exception('Option "scriptPath" must be set');
            }

            $stylePath = $this->getStylePath();
            if (!empty($stylePath)) {
                $this->view->headLink()->appendStylesheet($stylePath);
            }
        }

        public function __toString()
        {
            $this->inject();
            return '';
        }
    }