diff --git a/codefilter.module b/codefilter.module index 5718d19..082d200 100644 --- a/codefilter.module +++ b/codefilter.module @@ -109,7 +109,7 @@ function codefilter_fix_spaces($text) { */ function codefilter_escape($text, $type = 'code') { // Note, pay attention to odd preg_replace-with-/e behaviour on slashes. - $text = check_plain(str_replace('\"', '"', $text)); + //$text = check_plain(str_replace('\"', '"', $text)); // Protect newlines from line break converter. $text = str_replace(array("\r", "\n"), array('', ' '), $text); @@ -135,26 +135,7 @@ function _codefilter_escape_php_tag_callback($matches) { } /** - * Implements hook_filter_info(). - */ -function codefilter_filter_info() { - $filters['codefilter'] = array( - 'title' => t('Code filter'), - 'type' => FILTER_TYPE_MARKUP_LANGUAGE, - 'description' => t('Allows users to post code verbatim using <code> and <?php ?> tags.'), - 'prepare callback' => '_codefilter_prepare', - 'process callback' => '_codefilter_process', - 'settings callback' => '_codefilter_settings', - 'default settings' => array( - 'nowrap_expand' => FALSE, - ), - 'tips callback' => '_codefilter_tips', - ); - return $filters; -} - -/** - * Implements hook_filter_FILTER_prepare(). + * Implements hook_filter_FILTER_prepare(). //TODO Update this. * * Escape code tags to prevent other filters from acting on them. * , , [?php ?], <% %>, and [% %] tags are escaped. @@ -166,7 +147,7 @@ function _codefilter_prepare($text, $format) { } /** - * Implements hook_filter_FILTER_process(). + * Implements hook_filter_FILTER_process(). //TODO Update this. */ function _codefilter_process($text, $format) { $text = preg_replace_callback('@\[codefilter_code\](.+?)\[/codefilter_code\]@s', '_codefilter_process_code_callback', $text); @@ -180,37 +161,6 @@ function _codefilter_process($text, $format) { } /** - * Implements hook_filter_FILTER_tips(). - */ -function _codefilter_tips($format, $long = FALSE) { - if ($long) { - return t('To post pieces of code, surround them with <code>...</code> tags. For PHP code, you can use <?php ... ?>, which will also colour it based on syntax.'); - } - else { - return t('You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.'); - } -} - -/** - * Settings callback for the codefilter filter. - * - * @see hook_filter_info() - * @see hook_filter_FILTER_settings() - */ -function _codefilter_settings($form, &$form_state, $filter, $format, $defaults) { - $filter->settings += $defaults; - - $elements = array(); - $elements['nowrap_expand'] = array( - '#type' => 'checkbox', - '#title' => t('Expand code boxes on hover.'), - '#description' => t('By default, code boxes inherit text wrapping from the active theme. With this setting, code boxes will not wrap, but will expand to full width on hover (with Javascript).'), - '#default_value' => $filter->settings['nowrap_expand'], - ); - return $elements; -} - -/** * Implements hook_page_build(). */ function codefilter_page_build(&$page) { diff --git a/lib/Drupal/codefilter/Plugin/Filter/CodeFilter.php b/lib/Drupal/codefilter/Plugin/Filter/CodeFilter.php new file mode 100644 index 0000000..a368d6a --- /dev/null +++ b/lib/Drupal/codefilter/Plugin/Filter/CodeFilter.php @@ -0,0 +1,63 @@ + 'checkbox', + '#title' => t('Expand code boxes on hover.'), + '#description' => t('By default, code boxes inherit text wrapping from the active theme. With this setting, code boxes will not wrap, but will expand to full width on hover (with Javascript).'), + '#default_value' => $this->settings['nowrap_expand'], + ); + + return $form; + } + + /** + * {@inheritdoc} + */ + public function process($text, $langcode, $cache, $cache_id) { + $text = _codefilter_prepare($text, $this); + return _codefilter_process($text, $this); + } + + /** + * {@inheritdoc} + */ + public function tips($long = FALSE) { + if ($long) { + return t('To post pieces of code, surround them with <code>...</code> tags. For PHP code, you can use <?php ... ?>, which will also colour it based on syntax.'); + } + else { + return t('You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.'); + } + } + +} diff --git a/lib/Drupal/codefilter/Tests/CodeFilterUnitTest.php b/lib/Drupal/codefilter/Tests/CodeFilterUnitTest.php deleted file mode 100644 index 155f029..0000000 --- a/lib/Drupal/codefilter/Tests/CodeFilterUnitTest.php +++ /dev/null @@ -1,92 +0,0 @@ - 'Codefilter module text filters', - 'description' => 'Tests raw filtering functions.', - 'group' => 'Code Filter', - ); - } - - protected function setUp() { - parent::setUp(); - $this->filter = codefilter_filter_info(); - $this->path = drupal_get_path('module', 'codefilter') . '/tests'; - } - - /** - * Filters text through codefilters prepare and process callbacks. - * - * @param string $text - * The text to filter. - * - * @return string - * The processed text. - */ - protected function filterText($text, $settings = array()) { - $filter =& $this->filter['codefilter']; - // Set up a dummy format using defaults. - $format = new \stdClass(); - $format->settings = array_merge($filter['default settings'], $settings); - $text = call_user_func($filter['prepare callback'], $text, $format); - $text = call_user_func($filter['process callback'], $text, $format); - return $text; - } - - /** - * Checks that path . '/codefilter.php-input.txt'); - $expected = file_get_contents($this->path . '/codefilter.php-output.txt'); - $result = $this->filterText($input); - $this->assertIdentical($expected, $result, 'PHP tags are filtered.'); - } - - /** - * Checks that tags are escaped and highlighted correctly. - */ - function testCodeFilter() { - $input = file_get_contents($this->path . '/codefilter.code-input.txt'); - $expected = file_get_contents($this->path . '/codefilter.code-output.txt'); - $result = $this->filterText($input); - $this->assertIdentical($expected, $result, 'Code tags are filtered.'); - } - - /** - * Checks that CSS classes are added which JS uses for hover events. - */ - function testContainerExpand() { - $input = file_get_contents($this->path . '/codefilter.php-input.txt'); - $settings = array( - 'nowrap_expand' => TRUE, - ); - $result = $this->filterText($input, $settings); - - $this->assertTrue( - strpos($result, '
') !== FALSE, - 'Expand class is added to codefilter blocks that are too long when that option is specified.' - ); - } -} diff --git a/tests/Drupal/codefilter/Tests/CodeFilterUnitTest.php b/tests/Drupal/codefilter/Tests/CodeFilterUnitTest.php new file mode 100644 index 0000000..9772201 --- /dev/null +++ b/tests/Drupal/codefilter/Tests/CodeFilterUnitTest.php @@ -0,0 +1,96 @@ + 'Codefilter module text filters', + 'description' => 'Tests raw filtering functions.', + ); + } + + /** + * {@inheritdoc} + */ + public function setUp() { + include_once __DIR__ . '/../../../../codefilter.module'; + include_once __DIR__ . '/../../../../../../core/includes/unicode.inc'; + } + + /** + * Test codefilter_process_php(). + * + * @dataProvider providerCodefilterProcessPHP + */ + public function testCodefilterProcessPHP($input, $expected) { + $result = codefilter_process_php($input); + $this->assertEquals($expected, $result); + } + + /** + * Test codefilter_process_code(). + * + * @dataProvider providerCodefilterProcessCode + */ + public function testCodefilterProcessCode($input, $expected) { + $result = codefilter_process_code($input); + $this->assertEquals($expected, $result); + } + + /** + * Data provider for testCodefilterProcessPHP(). + */ + public static function providerCodefilterProcessPHP() { + return array( + array( + self::providerCode(), + '
<?php
// Comment is here.
watchdog(\'actions\', \'@count orphaned actions (%orphans) exist in the actions table. !link\', array(\'@count\' => $count, \'%orphans\' => $orphans, \'!link\' => $link), WATCHDOG_INFO);
/**
 * Longer comment is here.
 **/
?>
', + ), + ); + } + + /** + * Data provider for testCodefilterProcessPHP(). + */ + public static function providerCodefilterProcessCode() { + return array( + array( + self::providerCode(), + '
// Comment is here.
watchdog(\'actions\', \'@count orphaned actions (%orphans) exist in the actions table. !link\', array(\'@count\' => $count, \'%orphans\' => $orphans, \'!link\' => $link), WATCHDOG_INFO);
/**
* Longer comment is here.
**/
', + ), + ); + } + + /** + * Provides a string with php code. + * + * @return string + * The php code as a string. + */ + public static function providerCode() { + return << \$count, '%orphans' => \$orphans, '!link' => \$link), WATCHDOG_INFO); +/** + * Longer comment is here. + **/ +EOF; + } +} diff --git a/tests/codefilter.code-input.txt b/tests/codefilter.code-input.txt deleted file mode 100644 index dc9809b..0000000 --- a/tests/codefilter.code-input.txt +++ /dev/null @@ -1,7 +0,0 @@ - -// Comment is here. -watchdog('actions', '@count orphaned actions (%orphans) exist in the actions table. !link', array('@count' => $count, '%orphans' => $orphans, '!link' => $link), WATCHDOG_INFO); -/** - * Longer comment is here. - **/ - \ No newline at end of file diff --git a/tests/codefilter.code-output.txt b/tests/codefilter.code-output.txt deleted file mode 100644 index 6aec2a5..0000000 --- a/tests/codefilter.code-output.txt +++ /dev/null @@ -1 +0,0 @@ -
// Comment is here.
watchdog('actions', '@count orphaned actions (%orphans) exist in the actions table. !link', array('@count' => $count, '%orphans' => $orphans, '!link' => $link), WATCHDOG_INFO);
/**
* Longer comment is here.
**/
\ No newline at end of file diff --git a/tests/codefilter.php-input.txt b/tests/codefilter.php-input.txt deleted file mode 100644 index 20d8372..0000000 --- a/tests/codefilter.php-input.txt +++ /dev/null @@ -1,7 +0,0 @@ - $count, '%orphans' => $orphans, '!link' => $link), WATCHDOG_INFO); -/** - * Longer comment is here. - **/ -?> \ No newline at end of file diff --git a/tests/codefilter.php-output.txt b/tests/codefilter.php-output.txt deleted file mode 100644 index 702e13e..0000000 --- a/tests/codefilter.php-output.txt +++ /dev/null @@ -1 +0,0 @@ -
<?php
// Comment is here.
watchdog('actions', '@count orphaned actions (%orphans) exist in the actions table. !link', array('@count' => $count, '%orphans' => $orphans, '!link' => $link), WATCHDOG_INFO);
/**
 * Longer comment is here.
 **/
?>
\ No newline at end of file