ネクストベータ代表Blog

WEBシステム SI屋の代表のブログです。

*

WordPressを読む 77 /blog/wp-content/plugins/crayon-syntax-highlighter/crayon_wp.class.php

   

/blog/wp-content/plugins/crayon-syntax-highlighter/crayon_wp.class.php

const REGEX_INLINE_CLASS = ‘\bcrayon-inline\b’;

const REGEX_CLOSED_NO_CAPTURE = ‘(?:\[\s*crayon\b[^\]]*/\])’;
const REGEX_TAG_NO_CAPTURE = ‘(?:\[\s*crayon\b[^\]]*\].*?\[/crayon\])’;

const REGEX_QUICK_CAPTURE = ‘(?:\[\s*crayon[^\]]*\].*?\[\s*/\s*crayon\s*\])|(?:\[\s*crayon[^\]]*/\s*\])’;

const REGEX_BETWEEN_PARAGRAPH = ‘(?:[^<]*<(?!/?p(\s+[^>]*)?>)[^>]+(\s+[^>]*)?>)*[^<]*((?:\[\s*crayon[^\]]*\].*?\[\s*/\s*crayon\s*\])|(?:\[\s*crayon[^\]]*/\s*\]))(?:[^<]*<(?!/?p(\s+[^>]*)?>)[^>]+(\s+[^>]*)?>)*[^<]*‘;
const REGEX_BETWEEN_PARAGRAPH_SIMPLE = ‘(]*)?>)(.*?)(]*)?>)’;

// For

const REGEX_BR_BEFORE = ‘#<\s*br\s*/?\s*>\s*(\[\s*crayon-\w+\])#msi’;
const REGEX_BR_AFTER = ‘#(\[\s*crayon-\w+\])\s*<\s*br\s*/?\s*>#msi’;

const REGEX_ID = ‘#(?url($url);
$crayon->code($content);
// Set attributes, should be set after URL to allow language auto detection
$crayon->language($lang);
$crayon->title($title);
$crayon->marked($mark);
$crayon->range($range);

$crayon->is_inline($inline);

// Determine if we should highlight
$highlight = array_key_exists(‘highlight’, $atts) ? CrayonUtil::str_to_bool($atts[‘highlight’], FALSE) : TRUE;
$crayon->is_highlighted($highlight);
return $crayon;
}

/* Returns Crayon instance */
public static function instance($extra_attr = array(), $id = NULL) {
CrayonLog::debug(‘instance’);

// Create Crayon
$crayon = new CrayonHighlighter();

/* Load settings and merge shortcode attributes which will override any existing.
* Stores the other shortcode attributes as settings in the crayon. */
if (!empty($extra_attr)) {
$crayon->settings($extra_attr);
}
if (!empty($id)) {
$crayon->id($id);
}

return $crayon;
}

/* For manually highlighting code, useful for other PHP contexts */
public static function highlight($code, $add_tags = FALSE) {
$captures = CrayonWP::capture_crayons(0, $code);
$the_captures = $captures[‘capture’];
if (count($the_captures) == 0 && $add_tags) {
// Nothing captured, so wrap in a pre and try again
$code = ‘

' . $code . '[/crayon]';
$captures = CrayonWP::capture_crayons(0, $code);
$the_captures = $captures['capture'];
}
$the_content = $captures['content'];
foreach ($the_captures as $id => $capture) {
$atts = $capture['atts'];
$no_enqueue = array(
CrayonSettings::ENQUEUE_THEMES => FALSE,
CrayonSettings::ENQUEUE_FONTS => FALSE);
$atts = array_merge($atts, $no_enqueue);
$code = $capture['code'];
$crayon = CrayonWP::shortcode($atts, $code, $id);
$crayon_formatted = $crayon->output(TRUE, FALSE);
$the_content = CrayonUtil::preg_replace_escape_back(self::regex_with_id($id), $crayon_formatted, $the_content, 1, $count);
}

return $the_content;
}

public static function ajax_highlight() {
$code = isset($_POST['code']) ? $_POST['code'] : null;
if (!$code) {
$code = isset($_GET['code']) ? $_GET['code'] : null;
}
if ($code) {
echo self::highlight($code);
} else {
echo "No code specified.";
}
exit();
}

/* Uses the main query */
public static function wp() {
CrayonLog::debug('wp (global)');
global $wp_the_query;
if (isset($wp_the_query->posts)) {
$posts = $wp_the_query->posts;
self::the_posts($posts);
}
}

// TODO put args into an array
public static function capture_crayons($wp_id, $wp_content, $extra_settings = array(), $args = array()) {
extract($args);
CrayonUtil::set_var($callback, NULL);
CrayonUtil::set_var($callback_extra_args, NULL);
CrayonUtil::set_var($ignore, TRUE);
CrayonUtil::set_var($preserve_atts, FALSE);
CrayonUtil::set_var($flags, NULL);
CrayonUtil::set_var($skip_setting_check, FALSE);
CrayonUtil::set_var($just_check, FALSE);

// Will contain captured crayons and altered $wp_content
$capture = array('capture' => array(), 'content' => $wp_content, 'has_captured' => FALSE);

// Flags for which Crayons to convert
$in_flag = self::in_flag($flags);

CrayonLog::debug('capture for id ' . $wp_id . ' len ' . strlen($wp_content));

// Convert

is used inside another

$wp_content = preg_replace_callback('#(?]*)\bclass\s*=\s*(["\'])(.*?)\2([^>]*))?)([^>]*)>(.*?)<\s*/\s*pre\s*>#msi', 'CrayonWP::pre_tag', $wp_content);
}

// Convert mini [php][/php] tags to crayon tags, if needed
if ((CrayonGlobalSettings::val(CrayonSettings::CAPTURE_MINI_TAG) || $skip_setting_check) && $in_flag

', $wp_content);
$wp_content = preg_replace('#(?

', $wp_content); }// Convert to inline tags
if (CrayonGlobalSettings::val(CrayonSettings::CODE_TAG_CAPTURE)) {
$inline = CrayonGlobalSettings::val(CrayonSettings::CODE_TAG_CAPTURE_TYPE) === 0;
$inline_setting = $inline ? 'inline="true"' : '';
$wp_content = preg_replace('#<(\s*code\b)([^>]*)>(.*?)]*>#msi', '

', $wp_content);
}

if ((CrayonGlobalSettings::val(CrayonSettings::INLINE_TAG) || $skip_setting_check) && $in_flag

', $wp_content);
}
// Convert tags to inline crayon tags
$wp_content = preg_replace_callback('#(?]*)\bclass\s*=\s*(["\'])(.*?)\2([^>]*)>(.*?)<\s*/\s*span\s*>#msi', 'CrayonWP::span_tag', $wp_content);
}

// Convert [plain] tags into

, if needed
if ((CrayonGlobalSettings::val(CrayonSettings::PLAIN_TAG) || $skip_setting_check) && $in_flag

tag
$crayon_formatted = CrayonFormatter::plain_code($crayon->code(), $crayon->setting_val(CrayonSettings::DECODE));
} else {
// Apply shortcode to the content
$crayon_formatted = $crayon->output(TRUE, FALSE);
}
// Replace the code with the Crayon
CrayonLog::debug('the_content: id ' . $post_id . ' has UID ' . $id . ' : ' . intval(stripos($the_content, $id) !== FALSE));
$the_content = CrayonUtil::preg_replace_escape_back(self::regex_with_id($id), $crayon_formatted, $the_content, 1, $count);
CrayonLog::debug('the_content: REPLACED for id ' . $post_id . ' from len ' . strlen($the_content_original) . ' to ' . strlen($the_content));
}
}

return $the_content;
}

public static function pre_comment_text($text) {
global $comment;
$comment_id = strval($comment->comment_ID);
if (array_key_exists($comment_id, self::$comment_captures)) {
// Replace with IDs now that we need to
$text = self::$comment_captures[$comment_id];
}
return $text;
}

public static function comment_text($text) {
global $comment;
$comment_id = strval($comment->comment_ID);
// Find if this post has Crayons
if (array_key_exists($comment_id, self::$comment_queue)) {
// XXX We want the plain post content, no formatting
$the_content_original = $text;
// Loop through Crayons
$post_in_queue = self::$comment_queue[$comment_id];

foreach ($post_in_queue as $id => $v) {
$atts = $v['atts'];
$content = $v['code']; // The code we replace post content with
$crayon = self::shortcode($atts, $content, $id);
$crayon_formatted = $crayon->output(TRUE, FALSE);
// Replacing may cause

tags to become disjoint with a

inside them, close and reopen them if needed
if (!$crayon->is_inline()) {
$text = preg_replace_callback('#' . self::REGEX_BETWEEN_PARAGRAPH_SIMPLE . '#msi', 'CrayonWP::add_paragraphs', $text);
}
// Replace the code with the Crayon
$text = CrayonUtil::preg_replace_escape_back(self::regex_with_id($id), $crayon_formatted, $text, 1, $text);
}
}
return $text;
}

public static function add_paragraphs($capture) {
if (count($capture) != 4) {
CrayonLog::debug('add_paragraphs: 0');
return $capture[0];
}
$capture[2] = preg_replace('#(?:<\s*br\s*/\s*>\s*)?(\[\s*crayon-\w+/\])(?:<\s*br\s*/\s*>\s*)?#msi', '

$1

', $capture[2]);
// If

";
} else {
return "

";
}
}

// Capture span tag and extract settings from the class attribute, if present.
public static function span_tag($matches) {
// Only use tags with crayon-inline class
if (preg_match('#' . self::REGEX_INLINE_CLASS . '#mi', $matches[3])) {
// no $atts
$matches[6] = $matches[5];
$matches[5] = '';
return self::class_tag($matches);
} else {
// Don't turn regular s into Crayons
return $matches[0];
}
}

// Capture pre tag and extract settings from the class attribute, if present.
public static function pre_tag($matches) {
return self::class_tag($matches);
}

/**
* Check if the $ notation has been used to ignore

 - Wordpress, プラグイン