doctrine = $doctrine; $this->router = $router; $this->hiliter = $hiliter; } public function getFilters() { return array( 'insert_code_chunks' => new \Twig_Filter_Method($this, 'insertCodeChunks', array('is_safe' => array('html'))), 'remove_code_chunks' => new \Twig_Filter_Method($this, 'removeCodeChunks', array('is_safe' => array('html'))), ); } public function insertCodeChunks($string) { $m = array(); $chunkRepo = $this->doctrine->getRepository('GergelyPolonkaiFrontBundle:CodeChunk'); while ( preg_match( '/\\[\\$ code:([^:]+):([^ ]+) \\$\\]/i', $string, $m, PREG_OFFSET_CAPTURE) ) { $start = $m[0][1]; $fullTag = $m[0][0]; $len = strlen($fullTag); $lang = strtolower($m[1][0]); $slug = strtolower($m[2][0]); $replacement = ''; $chunk = $chunkRepo->findOneBy(array('language' => $lang, 'slug' => $slug)); if ($chunk !== null) { $link = $this->router->generate('GergelyPolonkaiFrontBundle_viewCode', array('language' => $lang, 'slug' => $slug)); $replacement = '

' . $chunk->getTitle() . '

' . $this->hiliter->geshiFilter($chunk->getContent(), $lang) . "\n"; $description = $chunk->getDescription(); if (($description !== null) && ($description != '')) { $replacement .= '
' . $description . "
\n"; } $replacement .= '
'; } $string = substr_replace($string, $replacement, $start, $len); } while ( preg_match( '/\\[\\$ code:([^:]+):(.+?) \\$\\]/is', $string, $m, PREG_OFFSET_CAPTURE) ) { $start = $m[0][1]; $fullTag = $m[0][0]; $len = strlen($fullTag); $lang = strtolower($m[1][0]); $code = $m[2][0]; $replacement = '
' . $this->hiliter->geshiFilter($code, $lang) . '
'; $string = substr_replace($string, $replacement, $start, $len); } return $string; } public function removeCodeChunks($string) { $m = array(); $chunkRepo = $this->doctrine->getRepository('GergelyPolonkaiFrontBundle:CodeChunk'); while ( preg_match( '/\\[\\$ code:([^:]+):([^ ]+) \\$\\]/i', $string, $m, PREG_OFFSET_CAPTURE) ) { $start = $m[0][1]; $fullTag = $m[0][0]; $len = strlen($fullTag); $replacement = ''; $string = substr_replace($string, $replacement, $start, $len); } while ( preg_match( '/\\[\\$ code:([^:]+):(.+?) \\$\\]/is', $string, $m, PREG_OFFSET_CAPTURE) ) { $start = $m[0][1]; $fullTag = $m[0][0]; $len = strlen($fullTag); $replacement = ''; $string = substr_replace($string, $replacement, $start, $len); } return $string; } public function getName() { return "code_chunk"; } }