/** * {@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; }
/** * 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); } }
/** * 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 " " 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('', ' '), SafeMarkup::checkPlain($content)) . '[/geshifilter-' . $tag_name . ']'; }