/**
  * {@inheritdoc}
  */
 public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state)
 {
     $enabled_languages = GeshiFilter::getEnabledLanguages();
     $element['sourcecode'] = array('#title' => t('Code'), '#type' => 'textarea', '#default_value' => isset($items[$delta]->sourcecode) ? $items[$delta]->sourcecode : NULL);
     $element['language'] = array('#title' => t('Language'), '#type' => 'select', '#default_value' => isset($items[$delta]->language) ? $items[$delta]->language : NULL, '#options' => $enabled_languages);
     return $element;
 }
Example #2
0
 /**
  * Function for generating the external stylesheet.
  *
  * @param bool $force
  *   Force the regeneration of the CSS file.
  */
 public static function generateLanguagesCssFile($force = FALSE)
 {
     $languages = GeshiFilter::getEnabledLanguages();
     // Serialize the array of enabled languages as sort of hash.
     $languages_hash = serialize($languages);
     // Check if generation of the CSS file is needed.
     if ($force || $languages_hash != \Drupal::state()->get('geshifilter_cssfile_languages')) {
         // Build stylesheet.
         $stylesheet = self::generateLanguagesCssRules();
         // Save stylesheet.
         $stylesheet_filename = self::languageCssPath();
         $ret = file_save_data($stylesheet, $stylesheet_filename, FILE_EXISTS_REPLACE);
         if ($ret) {
             drupal_set_message(t('(Re)generated external CSS style sheet %file.', array('%file' => $ret->getFilename())));
         } else {
             drupal_set_message(t('Could not generate external CSS file. Check the settings of your <a href="!filesystem">file system</a>.', array('!filesystem' => Url::fromRoute('system.file_system_settings')->toString())), 'error');
         }
         // Remember for which list of languages the CSS file was generated.
         \Drupal::state()->set('cssfile_languages', $languages_hash);
     }
 }
 /**
  * Helper function for generating a GeSHi object.
  *
  * @param string $source_code
  *   The source code to process.
  * @param string $language
  *   The language to generate a GeSHi object for.
  *
  * @return \GeSHi
  *   Return a Geshi class object.
  */
 public static function geshiFactory($source_code, $language)
 {
     $available_languages = GeshiFilter::getAvailableLanguages();
     $geshi = new \GeSHi($source_code, $language);
     $geshi->set_language_path($available_languages[$language]['language_path']);
     return $geshi;
 }
 /**
  * Function for generating a form table for per language settings.
  *
  * @param string $view
  *   - enabled Only show the enabled languages.
  *   - disabled Only show the disabled languages.
  *   - all Show all languages.
  * @param bool $add_checkbox
  *   When add(TRUE) or not(FALSE) a checkbox to enable languages.
  * @param bool $add_tag_option
  *   When add(TRUE) or not(FALSE) a textbox to set tags.
  *
  * @return array
  *   Return elements to a table with languages.
  */
 protected function perLanguageSettings($view, $add_checkbox, $add_tag_option)
 {
     $config = $this->config('geshifilter.settings');
     $form = array();
     $header = array(t('Language'), t('GeSHi language code'));
     if ($add_tag_option) {
         $header[] = t('Tag/language attribute value');
     }
     $form['language'] = array('#type' => 'table', '#header' => $header, '#empty' => t('Nome language is available.'));
     // Table body.
     $languages = GeshiFilter::getAvailableLanguages();
     foreach ($languages as $language => $language_data) {
         $enabled = $config->get("language.{$language}.enabled", FALSE);
         // Skip items to hide.
         if ($view == 'enabled' && !$enabled || $view == 'disabled' && $enabled) {
             continue;
         }
         // Build language row.
         $form['language'][$language] = array();
         // Add enable/disable checkbox.
         if ($add_checkbox) {
             $form['language'][$language]['enabled'] = array('#type' => 'checkbox', '#default_value' => $enabled, '#title' => $language_data['fullname']);
         } else {
             $form['language'][$language]['fullname'] = array('#type' => 'markup', '#markup' => $language_data['fullname']);
         }
         // Language code.
         $form['language'][$language]['name'] = array('#type' => 'markup', '#markup' => $language);
         // Add a textfield for tags.
         if ($add_tag_option) {
             $form['language'][$language]['tags'] = array('#type' => 'textfield', '#default_value' => $config->get("language.{$language}.tags", ''), '#size' => 20);
         }
     }
     return $form;
 }
Example #5
0
 /**
  * Callback_geshifilter_prepare for preparing input text.
  *
  * Replaces the code tags brackets with geshifilter specific ones to prevent
  * possible messing up by other filters, e.g.
  *   '[python]foo[/python]' to '[geshifilter-python]foo[/geshifilter-python]'.
  * Replaces newlines with "&#10;" to prevent issues with the line break filter
  * Escapes the tricky characters like angle brackets with
  * SafeMarkup::checkPlain() to prevent messing up by other filters like the
  * HTML filter.
  *
  * @param array $match
  *   An array with the pieces from matched string.
  *   - 0: complete matched string.
  *   - 1: opening bracket ('<' or '[').
  *   - 2: tag.
  *   - 3: and.
  *   - 4: attributes.
  *   - 5: closing bracket.
  *   - 6: source code.
  *   - 7: closing tag.
  *
  * @return string
  *   Return escaped code block.
  */
 public function prepareCallback(array $match)
 {
     $tag_name = $match[2];
     $tag_attributes = $match[3];
     $content = $match[6];
     // Get the default highlighting mode.
     $lang = $this->config->get('default_highlighting');
     if ($lang == GeshiFilter::DEFAULT_DONOTHING) {
         // If the default highlighting mode is GeshiFilter::DEFAULT_DONOTHING
         // and there is no language set (with language tag or language attribute),
         // we should not do any escaping in this prepare phase,
         // so that other filters can do their thing.
         $enabled_languages = GeshiFilter::getEnabledLanguages();
         // Usage of language tag?
         list($generic_code_tags, $language_tags, $tag_to_lang) = $this->getTags();
         if (isset($tag_to_lang[$tag_name]) && isset($enabled_languages[$tag_to_lang[$tag_name]])) {
             $lang = $tag_to_lang[$tag_name];
         } else {
             // Get additional settings from the tag attributes.
             $settings = $this->parseAttributes($tag_attributes);
             if ($settings['language'] && isset($enabled_languages[$settings['language']])) {
                 $lang = $settings['language'];
             }
         }
         // If no language was set: prevent escaping and return original string.
         if ($lang == GeshiFilter::DEFAULT_DONOTHING) {
             return $match[0];
         }
     }
     if ($this->decodeEntities()) {
         $content = $this->unencode($content);
     }
     // Return escaped code block.
     return '[geshifilter-' . $tag_name . $tag_attributes . ']' . str_replace(array("\r", "\n"), array('', '&#10;'), SafeMarkup::checkPlain($content)) . '[/geshifilter-' . $tag_name . ']';
 }