public function process() { $tmr = new CrayonTimer(); $this->runtime = NULL; if ($this->needs_load) { $tmr->start(); $this->load(); $this->runtime[CRAYON_LOAD_TIME] = $tmr->stop(); } if (!empty($this->error) || empty($this->code)) { // Disable highlighting for errors and empty code return; } if ($this->language === NULL) { $this->language_detect(); } if ($this->needs_format) { $tmr->start(); try { // Parse before hand to read modes $code = $this->code; // If inline, then combine lines into one if ($this->is_inline) { $code = preg_replace('#[\\r\\n]+#ms', '', $code); if ($this->setting_val(CrayonSettings::TRIM_WHITESPACE)) { $code = trim($code); } } // Decode html entities (e.g. if using visual editor or manually encoding) if ($this->setting_val(CrayonSettings::DECODE)) { $code = CrayonUtil::html_entity_decode($code); } // Save code so output is plain output is the same $this->code = $code; // Allow mixed if langauge supports it and setting is set CrayonParser::parse($this->language->id()); if (!$this->setting_val(CrayonSettings::MIXED) || !$this->language->mode(CrayonParser::ALLOW_MIXED)) { // Format the code with the generated regex and elements $this->formatted_code = CrayonFormatter::format_code($code, $this->language, $this); } else { // Format the code with Mixed Highlighting $this->formatted_code = CrayonFormatter::format_mixed_code($code, $this->language, $this); } } catch (Exception $e) { $this->error($e->message()); return; } $this->needs_format = FALSE; $this->runtime[CRAYON_FORMAT_TIME] = $tmr->stop(); } }
public static function print_code($hl, $code, $line_numbers = TRUE, $print = TRUE) { global $CRAYON_VERSION; // We can print either block or inline, inline is treated differently, factor out common stuff here $output = ''; // Used for style tag $main_style = $code_style = $toolbar_style = $info_style = $font_style = $line_style = ''; // Unique ID for this instance of Crayon $uid = 'crayon-' . $hl->id(); // Print theme id // We make the assumption that the id is correct (checked in crayon_wp) $theme_id = $hl->setting_val(CrayonSettings::THEME); $theme_id_dashed = CrayonUtil::space_to_hyphen($theme_id); if (!$hl->setting_val(CrayonSettings::ENQUEUE_THEMES)) { $output .= CrayonResources::themes()->get_css($theme_id); } // Print font id // We make the assumption that the id is correct (checked in crayon_wp) $font_id = $hl->setting_val(CrayonSettings::FONT); $font_id_dashed = CrayonUtil::space_to_hyphen($font_id); if (!$hl->setting_val(CrayonSettings::ENQUEUE_FONTS)) { $output .= CrayonResources::fonts()->get_css($font_id); } // Inline margin if ($hl->is_inline()) { $inline_margin = $hl->setting_val(CrayonSettings::INLINE_MARGIN) . 'px !important;'; } // Determine font size // TODO improve logic if ($hl->setting_val(CrayonSettings::FONT_SIZE_ENABLE)) { $_font_size = $hl->setting_val(CrayonSettings::FONT_SIZE); $font_size = $_font_size . 'px !important;'; $_line_height = $hl->setting_val(CrayonSettings::LINE_HEIGHT); // Don't allow line height to be less than font size $line_height = ($_line_height > $_font_size ? $_line_height : $_font_size) . 'px !important;'; $toolbar_height = $font_size * 1.5 . 'px !important;'; $info_height = $font_size * 1.4 . 'px !important;'; $font_style .= "font-size: {$font_size} line-height: {$line_height}"; $toolbar_style .= "font-size: {$font_size}"; $line_style .= "height: {$line_height}"; if ($hl->is_inline()) { $font_style .= "font-size: {$font_size}"; } else { $toolbar_style .= "height: {$toolbar_height} line-height: {$toolbar_height}"; $info_style .= "min-height: {$info_height} line-height: {$info_height}"; } } else { if (!$hl->is_inline()) { if (($font_size = CrayonGlobalSettings::get(CrayonSettings::FONT_SIZE)) !== FALSE) { $font_size = $font_size->def() . 'px !important;'; $line_height = $font_size * 1.4 . 'px !important;'; } } } // This will return from function with inline print if ($hl->is_inline()) { $wrap = !$hl->setting_val(CrayonSettings::INLINE_WRAP) ? 'crayon-syntax-inline-nowrap' : ''; $output .= ' <span id="' . $uid . '" class="crayon-syntax crayon-syntax-inline ' . $wrap . ' crayon-theme-' . $theme_id_dashed . ' crayon-theme-' . $theme_id_dashed . '-inline crayon-font-' . $font_id_dashed . '" style="' . $font_style . '">' . '<span class="crayon-pre crayon-code" style="' . $font_style . '">' . $code . '</span>' . '</span>'; return $output; } // Below code only for block (default) printing // Generate the code lines and separate each line as a div $print_code = ''; $print_nums = ''; $hl->line_count(preg_match_all("#(?:^|(?<=\r\n|\n))[^\r\n]*#", $code, $code_lines)); // The line number to start from $start_line = $hl->setting_val(CrayonSettings::START_LINE); $marking = $hl->setting_val(CrayonSettings::MARKING); $striped = $hl->setting_val(CrayonSettings::STRIPED); $range = $hl->setting_val(CrayonSettings::RANGES) ? $hl->range() : FALSE; for ($i = 1; $i <= $hl->line_count(); $i++) { // Check if the current line is in the range of code to display if ($range) { if ($i < $range[0]) { continue; } else { if ($i > $range[1]) { break; } } } $code_line = $code_lines[0][$i - 1]; // If line is blank, add a space so the div has the correct height if ($code_line == '') { $code_line = ' '; } // Check if the current line has been selected $marked_lines = $hl->marked(); // Check if lines need to be marked as important if ($marking && in_array($i, $marked_lines)) { $marked_num = ' crayon-marked-num'; $marked_line = ' crayon-marked-line'; // If multiple lines are marked, only show borders for top and bottom lines if (!in_array($i - 1, $marked_lines)) { $marked_num .= ' crayon-top'; $marked_line .= ' crayon-top'; } // Single lines are both the top and bottom of the multiple marked lines if (!in_array($i + 1, $marked_lines)) { $marked_num .= ' crayon-bottom'; $marked_line .= ' crayon-bottom'; } } else { $marked_num = $marked_line = ''; } // Stripe odd lines if ($striped && $i % 2 == 0) { $striped_num = ' crayon-striped-num'; $striped_line = ' crayon-striped-line'; } else { $striped_num = $striped_line = ''; } // Generate the lines $line_num = $start_line + $i - 1; $line_id = $uid . '-' . $line_num; $print_code .= '<div class="crayon-line' . $marked_line . $striped_line . '" id="' . $line_id . '">' . $code_line . '</div>'; if (!is_string($line_numbers)) { $print_nums .= '<div class="crayon-num' . $marked_num . $striped_num . '" data-line="' . $line_id . '">' . $line_num . '</div>'; } } // If $line_numbers is a string, display it if (is_string($line_numbers) && !empty($line_numbers)) { $print_nums .= '<div class="crayon-num">' . $line_numbers . '</div>'; } else { if (empty($line_numbers)) { $print_nums = FALSE; } } // Determine whether to print title, encode characters $title = $hl->title(); // Decode if needed if ($hl->setting_val(CrayonSettings::DECODE_ATTRIBUTES)) { $title = CrayonUtil::html_entity_decode($title); } $print_title = '<span class="crayon-title">' . $title . '</span>'; // Determine whether to print language $print_lang = ''; // XXX Use for printing the regex if ($hl->language()) { $lang = $hl->language()->name(); switch ($hl->setting_index(CrayonSettings::SHOW_LANG)) { case 0: if ($hl->language()->id() == CrayonLangs::DEFAULT_LANG) { break; } // Falls through // Falls through case 1: $print_lang = '<span class="crayon-language">' . $lang . '</span>'; break; } } // Disable functionality for errors $error = $hl->error(); // Combined settings for code $code_settings = ''; // Disable mouseover for touchscreen devices and mobiles, if we are told to $touch = FALSE; // Whether we have detected a touchscreen device if ($hl->setting_val(CrayonSettings::TOUCHSCREEN) && CrayonUtil::is_touch()) { $touch = TRUE; $code_settings .= ' touchscreen'; } // Disabling Popup if (!$hl->setting_val(CrayonSettings::POPUP)) { $code_settings .= ' no-popup'; } // Minimize if (!$hl->setting_val(CrayonSettings::MINIMIZE)) { $code_settings .= ' minimize'; } // Draw the plain code and toolbar $toolbar_settings = $print_plain_button = $print_copy_button = ''; $toolbar_index = $hl->setting_index(CrayonSettings::TOOLBAR); if (empty($error) && ($toolbar_index != 2 || $hl->setting_val(CrayonSettings::MINIMIZE))) { // Enable mouseover setting for toolbar if ($toolbar_index == 0 && !$touch) { // No touchscreen detected $toolbar_settings .= ' mouseover'; if ($hl->setting_val(CrayonSettings::TOOLBAR_OVERLAY)) { $toolbar_settings .= ' overlay'; } if ($hl->setting_val(CrayonSettings::TOOLBAR_HIDE)) { $toolbar_settings .= ' hide'; } if ($hl->setting_val(CrayonSettings::TOOLBAR_DELAY)) { $toolbar_settings .= ' delay'; } } else { if ($toolbar_index == 1) { // Always display the toolbar $toolbar_settings .= ' show'; } else { if ($toolbar_index == 2) { $toolbar_settings .= ' never-show'; } } } $buttons = array(); if ($hl->setting_val(CrayonSettings::NUMS_TOGGLE)) { $buttons['nums'] = crayon__('Toggle Line Numbers'); } if ($hl->setting_val(CrayonSettings::PLAIN) && $hl->setting_val(CrayonSettings::PLAIN_TOGGLE)) { $buttons['plain'] = crayon__('Toggle Plain Code'); } if ($hl->setting_val(CrayonSettings::WRAP_TOGGLE)) { $buttons['wrap'] = crayon__('Toggle Line Wrap'); } if ($hl->setting_val(CrayonSettings::EXPAND_TOGGLE)) { $buttons['expand'] = crayon__('Expand Code'); } if (!$touch && $hl->setting_val(CrayonSettings::PLAIN) && $hl->setting_val(CrayonSettings::COPY)) { $buttons['copy'] = crayon__('Copy'); } if ($hl->setting_val(CrayonSettings::POPUP)) { $buttons['popup'] = crayon__('Open Code In New Window'); } $buttons_str = ''; foreach ($buttons as $button => $value) { $buttons_str .= '<div class="crayon-button crayon-' . $button . '-button"'; if (!is_array($value)) { $value = array('title' => $value); } foreach ($value as $k => $v) { $buttons_str .= ' ' . $k . '="' . $v . '"'; } $buttons_str .= '><div class="crayon-button-icon"></div></div>'; } /* The table is rendered invisible by CSS and enabled with JS when asked to. If JS is not enabled or fails, the toolbar won't work so there is no point to display it. */ $print_plus = $hl->is_mixed() && $hl->setting_val(CrayonSettings::SHOW_MIXED) ? '<span class="crayon-mixed-highlight" title="' . crayon__('Contains Mixed Languages') . '"></span>' : ''; $buttons = $print_plus . $buttons_str . $print_lang; $toolbar = ' <div class="crayon-toolbar" data-settings="' . $toolbar_settings . '" style="' . $toolbar_style . '">' . $print_title . ' <div class="crayon-tools" style="' . $toolbar_style . '">' . $buttons . '</div></div> <div class="crayon-info" style="' . $info_style . '"></div>'; } else { $toolbar = $buttons = $plain_settings = ''; } if (empty($error) && $hl->setting_val(CrayonSettings::PLAIN)) { // Different events to display plain code switch ($hl->setting_index(CrayonSettings::SHOW_PLAIN)) { case 0: $plain_settings = 'dblclick'; break; case 1: $plain_settings = 'click'; break; case 2: $plain_settings = 'mouseover'; break; default: $plain_settings = ''; } if ($hl->setting_val(CrayonSettings::SHOW_PLAIN_DEFAULT)) { $plain_settings .= ' show-plain-default'; } $tab = $hl->setting_val(CrayonSettings::TAB_SIZE); // TODO doesn't seem to work at the moment $plain_style = "-moz-tab-size:{$tab}; -o-tab-size:{$tab}; -webkit-tab-size:{$tab}; tab-size:{$tab};"; $readonly = $touch ? '' : 'readonly'; $print_plain = $print_plain_button = ''; $textwrap = !$hl->setting_val(CrayonSettings::WRAP) ? 'wrap="soft"' : ''; $print_plain = '<textarea ' . $textwrap . ' class="crayon-plain print-no" data-settings="' . $plain_settings . '" ' . $readonly . ' style="' . $plain_style . ' ' . $font_style . '">' . "\n" . self::clean_code($hl->code()) . '</textarea>'; } else { $print_plain = $plain_settings = $plain_settings = ''; } // Line numbers visibility $num_vis = $num_settings = ''; if ($line_numbers === FALSE) { $num_vis = 'crayon-invisible'; } else { $num_settings = $hl->setting_val(CrayonSettings::NUMS) ? 'show' : 'hide'; } // Determine scrollbar visibility $code_settings .= $hl->setting_val(CrayonSettings::SCROLL) && !$touch ? ' scroll-always' : ' scroll-mouseover'; // Disable animations if ($hl->setting_val(CrayonSettings::DISABLE_ANIM)) { $code_settings .= ' disable-anim'; } // Wrap if ($hl->setting_val(CrayonSettings::WRAP)) { $code_settings .= ' wrap'; } // Expand if ($hl->setting_val(CrayonSettings::EXPAND)) { $code_settings .= ' expand'; } // Determine dimensions if ($hl->setting_val(CrayonSettings::HEIGHT_SET)) { $height_style = self::dimension_style($hl, CrayonSettings::HEIGHT); // XXX Only set height for main, not code (if toolbar always visible, code will cover main) if ($hl->setting_index(CrayonSettings::HEIGHT_UNIT) == 0) { $main_style .= $height_style; } } if ($hl->setting_val(CrayonSettings::WIDTH_SET)) { $width_style = self::dimension_style($hl, CrayonSettings::WIDTH); $code_style .= $width_style; if ($hl->setting_index(CrayonSettings::WIDTH_UNIT) == 0) { $main_style .= $width_style; } } // Determine margins if ($hl->setting_val(CrayonSettings::TOP_SET)) { $code_style .= ' margin-top: ' . $hl->setting_val(CrayonSettings::TOP_MARGIN) . 'px;'; } if ($hl->setting_val(CrayonSettings::BOTTOM_SET)) { $code_style .= ' margin-bottom: ' . $hl->setting_val(CrayonSettings::BOTTOM_MARGIN) . 'px;'; } if ($hl->setting_val(CrayonSettings::LEFT_SET)) { $code_style .= ' margin-left: ' . $hl->setting_val(CrayonSettings::LEFT_MARGIN) . 'px;'; } if ($hl->setting_val(CrayonSettings::RIGHT_SET)) { $code_style .= ' margin-right: ' . $hl->setting_val(CrayonSettings::RIGHT_MARGIN) . 'px;'; } // Determine horizontal alignment $align_style = ' float: none;'; switch ($hl->setting_index(CrayonSettings::H_ALIGN)) { case 1: $align_style = ' float: left;'; break; case 2: $align_style = ' float: none; margin-left: auto; margin-right: auto;'; break; case 3: $align_style = ' float: right;'; break; } $code_style .= $align_style; // Determine allowed float elements if ($hl->setting_val(CrayonSettings::FLOAT_ENABLE)) { $clear_style = ' clear: none;'; } else { $clear_style = ' clear: both;'; } $code_style .= $clear_style; // Determine if operating system is mac $crayon_os = CrayonUtil::is_mac() ? 'mac' : 'pc'; // Produce output $output .= ' <div id="' . $uid . '" class="crayon-syntax crayon-theme-' . $theme_id_dashed . ' crayon-font-' . $font_id_dashed . ' crayon-os-' . $crayon_os . ' print-yes notranslate" data-settings="' . $code_settings . '" style="' . $code_style . ' ' . $font_style . '"> ' . $toolbar . ' <div class="crayon-plain-wrap">' . $print_plain . '</div>' . ' <div class="crayon-main" style="' . $main_style . '"> <table class="crayon-table"> <tr class="crayon-row">'; if ($print_nums !== FALSE) { $output .= ' <td class="crayon-nums ' . $num_vis . '" data-settings="' . $num_settings . '"> <div class="crayon-nums-content" style="' . $font_style . '">' . $print_nums . '</div> </td>'; } // XXX $output .= ' <td class="crayon-code"><div class="crayon-pre" style="' . $font_style . '">' . $print_code . '</div></td> </tr> </table> </div> </div>'; // Debugging stats $runtime = $hl->runtime(); if (!$hl->setting_val(CrayonSettings::DISABLE_RUNTIME) && is_array($runtime) && !empty($runtime)) { $output = '<!-- Crayon Syntax Highlighter v' . $CRAYON_VERSION . ' -->' . CRAYON_NL . $output . CRAYON_NL . '<!-- '; foreach ($hl->runtime() as $type => $time) { $output .= '[' . $type . ': ' . sprintf('%.4f seconds', $time) . '] '; } $output .= '-->' . CRAYON_NL; } // Determine whether to print to screen or save if ($print) { echo $output; } else { return $output; } }