From ea325aab7ff7bea1b4353f4cb30a88a4324c6f95 Mon Sep 17 00:00:00 2001 From: "Gergely Polonkai (W00d5t0ck)" Date: Tue, 4 Sep 2012 10:42:20 +0200 Subject: [PATCH] Added GeshiBundle to highlight source code Signed-off-by: Gergely Polonkai (W00d5t0ck) --- app/AppKernel.php | 2 + composer.json | 1 + .../FrontBundle/Resources/public/css/code.css | 58 +++++++++++++++++++ .../DependencyInjection/Configuration.php | 29 ++++++++++ .../GergelyPolonkaiGeshiExtension.php | 28 +++++++++ .../GergelyPolonkaiGeshiBundle.php | 9 +++ .../GeshiBundle/Resources/config/services.yml | 8 +++ .../GeshiBundle/Twig/GeshiHighlight.php | 45 ++++++++++++++ 8 files changed, 180 insertions(+) create mode 100644 src/GergelyPolonkai/FrontBundle/Resources/public/css/code.css create mode 100644 src/GergelyPolonkai/GeshiBundle/DependencyInjection/Configuration.php create mode 100644 src/GergelyPolonkai/GeshiBundle/DependencyInjection/GergelyPolonkaiGeshiExtension.php create mode 100644 src/GergelyPolonkai/GeshiBundle/GergelyPolonkaiGeshiBundle.php create mode 100644 src/GergelyPolonkai/GeshiBundle/Resources/config/services.yml create mode 100644 src/GergelyPolonkai/GeshiBundle/Twig/GeshiHighlight.php diff --git a/app/AppKernel.php b/app/AppKernel.php index a97dffd..cbcd68a 100644 --- a/app/AppKernel.php +++ b/app/AppKernel.php @@ -1,4 +1,5 @@ getEnvironment(), array('dev', 'test'))) { diff --git a/composer.json b/composer.json index 257c1c9..db5f94f 100644 --- a/composer.json +++ b/composer.json @@ -24,6 +24,7 @@ "sensio/generator-bundle": "2.1.*", "jms/security-extra-bundle": "1.2.*", "jms/di-extra-bundle": "1.1.*", + "easybook/geshi": "dev-master", "gergelypolonkai/tcpdfbundle": "dev-master" }, "scripts": { diff --git a/src/GergelyPolonkai/FrontBundle/Resources/public/css/code.css b/src/GergelyPolonkai/FrontBundle/Resources/public/css/code.css new file mode 100644 index 0000000..803f2a3 --- /dev/null +++ b/src/GergelyPolonkai/FrontBundle/Resources/public/css/code.css @@ -0,0 +1,58 @@ +/* + Document : code + Created on : 2012.09.04., 10:05:47 + Author : polonkai.gergely + Description: + Purpose of the stylesheet follows. +*/ + +.code { + font-family: monospace; + background-color: #333; + padding: 5px; +} + +.code * { + font-family: monospace; + color: #b5b5b5; +} + +.code .kw1 { + color: #f0e68c; +} + +.code .kw2 { + color: #cd5c5c; +} + +.code .kw3 { + color: #f8dead; +} + +.code .kw4 { + color: #ffa0a0; +} + +.code .re0 { + color: #98fb98; +} + +.code .sy0 { + color: #d9d9d9; +} + +.code .co4 { + color: #87ceeb; +} + +.code .br0 { + color: #f8d8a9; +} + +.code .st_h { + color: #ffa0a0; +} + +.code .me1 { + color: #ffffff; +} \ No newline at end of file diff --git a/src/GergelyPolonkai/GeshiBundle/DependencyInjection/Configuration.php b/src/GergelyPolonkai/GeshiBundle/DependencyInjection/Configuration.php new file mode 100644 index 0000000..0eb34d5 --- /dev/null +++ b/src/GergelyPolonkai/GeshiBundle/DependencyInjection/Configuration.php @@ -0,0 +1,29 @@ +root('gergely_polonkai_geshi'); + + // Here you should define the parameters that are allowed to + // configure your bundle. See the documentation linked above for + // more information on that topic. + + return $treeBuilder; + } +} diff --git a/src/GergelyPolonkai/GeshiBundle/DependencyInjection/GergelyPolonkaiGeshiExtension.php b/src/GergelyPolonkai/GeshiBundle/DependencyInjection/GergelyPolonkaiGeshiExtension.php new file mode 100644 index 0000000..331f216 --- /dev/null +++ b/src/GergelyPolonkai/GeshiBundle/DependencyInjection/GergelyPolonkaiGeshiExtension.php @@ -0,0 +1,28 @@ +processConfiguration($configuration, $configs); + + $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); + $loader->load('services.yml'); + } +} diff --git a/src/GergelyPolonkai/GeshiBundle/GergelyPolonkaiGeshiBundle.php b/src/GergelyPolonkai/GeshiBundle/GergelyPolonkaiGeshiBundle.php new file mode 100644 index 0000000..c1ff2be --- /dev/null +++ b/src/GergelyPolonkai/GeshiBundle/GergelyPolonkaiGeshiBundle.php @@ -0,0 +1,9 @@ +geshi = new GeSHi(); + } + + public function getFilters() + { + return array( + 'geshi_highlight' => new \Twig_Filter_Method($this, 'geshiFilter', array('is_safe' => array('html'))), + ); + } + + public function geshiFilter($code, $lang) + { + $this->geshi->set_language($lang); + $this->geshi->set_source($code); + $this->geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS); + $this->geshi->enable_keyword_links(false); + $this->geshi->enable_classes(); + return $this->geshi->parse_code(); + } + + public function getName() { + return 'geshi_highlighter'; + } +}