1. Vladimir Mihailenco
  2. php-libs

Source

php-libs / zf_highlight / library / Text / View / Helper / Highlight.php

<?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 '';
    }
}