本文整理汇总了PHP中GeSHi::hsc方法的典型用法代码示例。如果您正苦于以下问题:PHP GeSHi::hsc方法的具体用法?PHP GeSHi::hsc怎么用?PHP GeSHi::hsc使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类GeSHi
的用法示例。
在下文中一共展示了GeSHi::hsc方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: parseToken
/**
* Implements parseToken to format the XML tags.
* It uses the syntax <token type="TYPE" link="URL">.
* The URL is only there if specified.
*
* @param string $token The token to put tags around
* @param string $context_name The name of the context that the tag is in
* @param array $data Miscellaneous data about the context
* @return string The token wrapped in XML
* @todo [blocking 1.2.2] Make it so that CSS is optional
*/
function parseToken($token, $context_name, $data)
{
// Ignore blank tokens
if ('' == $token || geshi_is_whitespace($token)) {
return $token;
}
// Initialize the result variable
$result = '';
// Add the basic tag
$result .= '<token type="' . $context_name . '"';
// Check if we should use an URL
if (isset($data['url'])) {
// Hey, we got an URL! Yayy~
$result .= ' url="' . GeSHi::hsc($data['url']) . '"';
}
// Are we gonna add in CSS?
if ($this->_addCSS) {
// Heh...
$result .= ' css="' . $this->_styler->getStyle($context_name) . '"';
}
// Finish the opening tag
$result .= '>';
// Now add in the token
$result .= '<![CDATA[' . $token . ']]>';
// Add the closing tag
$result .= '</token>\\n';
// Return the result
return $result;
}
示例2: finalise
/**
* Takes the parsed code and various options, and creates the HTML
* surrounding it to make it look nice.
*
* @param string The code already parsed
* @return string The code nicely finalised
* @since 1.0.0
* @access private
*/
function finalise(&$parsed_code)
{
// Remove end parts of important declarations
// This is BUGGY!! My fault for bad code: fix coming in 1.2
// @todo Remove this crap
if ($this->enable_important_blocks && strpos($parsed_code, GeSHi::hsc(GESHI_START_IMPORTANT)) === false) {
$parsed_code = str_replace(GeSHi::hsc(GESHI_END_IMPORTANT), '', $parsed_code);
}
// Add HTML whitespace stuff if we're using the <div> header
if ($this->header_type != GESHI_HEADER_PRE) {
$parsed_code = $this->indent($parsed_code);
}
// purge some unnecessary stuff
$parsed_code = preg_replace('#<span[^>]+>(\\s*)</span>#', '\\1', $parsed_code);
$parsed_code = preg_replace('#<div[^>]+>(\\s*)</div>#', '\\1', $parsed_code);
// If we are using IDs for line numbers, there needs to be an overall
// ID set to prevent collisions.
if ($this->add_ids && !$this->overall_id) {
$this->overall_id = 'geshi-' . substr(md5(microtime()), 0, 4);
}
// Get code into lines
$code = explode("\n", $parsed_code);
$parsed_code = '';
// If we're using line numbers, we insert <li>s and appropriate
// markup to style them (otherwise we don't need to do anything)
if ($this->line_numbers != GESHI_NO_LINE_NUMBERS) {
// If we're using the <pre> header, we shouldn't add newlines because
// the <pre> will line-break them (and the <li>s already do this for us)
$ls = $this->header_type != GESHI_HEADER_PRE ? "\n" : '';
// Set vars to defaults for following loop
$i = 0;
// Foreach line...
for ($i = 0, $n = count($code); $i < $n;) {
//Reset the attributes for a new line ...
$attrs = array();
// Make lines have at least one space in them if they're empty
// BenBE: Checking emptiness using trim instead of relying on blanks
if ('' == trim($code[$i])) {
$code[$i] = ' ';
}
// If this is a "special line"...
if ($this->line_numbers == GESHI_FANCY_LINE_NUMBERS && $i % $this->line_nth_row == $this->line_nth_row - 1) {
// Set the attributes to style the line
if ($this->use_classes) {
//$attr = ' class="li2"';
$attrs['class'][] = 'li2';
$def_attr = ' class="de2"';
} else {
//$attr = ' style="' . $this->line_style2 . '"';
$attrs['style'][] = $this->line_style2;
// This style "covers up" the special styles set for special lines
// so that styles applied to special lines don't apply to the actual
// code on that line
$def_attr = ' style="' . $this->code_style . '"';
}
// Span or div?
$start = "<div{$def_attr}>";
$end = '</div>';
} else {
if ($this->use_classes) {
//$attr = ' class="li1"';
$attrs['class'][] = 'li1';
$def_attr = ' class="de1"';
} else {
//$attr = ' style="' . $this->line_style1 . '"';
$attrs['style'][] = $this->line_style1;
$def_attr = ' style="' . $this->code_style . '"';
}
$start = "<div{$def_attr}>";
$end = '</div>';
}
++$i;
// Are we supposed to use ids? If so, add them
if ($this->add_ids) {
$attrs['id'][] = "{$this->overall_id}-{$i}";
}
if (in_array($i, $this->highlight_extra_lines)) {
if ($this->use_classes) {
if (isset($this->highlight_extra_lines_styles[$i])) {
$attrs['class'][] = "lx{$i}";
} else {
$attrs['class'][] = "ln-xtra";
}
} else {
array_push($attrs['style'], $this->get_line_style($i));
}
}
// Add in the line surrounded by appropriate list HTML
$attr_string = '';
foreach ($attrs as $key => $attr) {
$attr_string .= ' ' . $key . '="' . implode(' ', $attr) . '"';
//.........这里部分代码省略.........
示例3: parse_code
/**
* Returns the code in $this->source, highlighted and surrounded by the
* nessecary HTML.
*
* This should only be called ONCE, cos it's SLOW! If you want to highlight
* the same source multiple times, you're better off doing a whole lot of
* str_replaces to replace the <span>s
*
* @since 1.0.0
*/
function parse_code()
{
// Start the timer
$start_time = microtime();
// Replace all newlines to a common form.
$code = str_replace("\r\n", "\n", $this->source);
$code = str_replace("\r", "\n", $code);
// Firstly, if there is an error, we won't highlight
if ($this->error) {
//Escape the source for output
$result = $this->hsc($this->source);
//This fix is related to SF#1923020, but has to be applied regardless of
//actually highlighting symbols.
$result = str_replace(array('<SEMI>', '<PIPE>'), array(';', '|'), $result);
// Timing is irrelevant
$this->set_time($start_time, $start_time);
$this->finalise($result);
return $result;
}
// make sure the parse cache is up2date
if (!$this->parse_cache_built) {
$this->build_parse_cache();
}
// Initialise various stuff
$length = strlen($code);
$COMMENT_MATCHED = false;
$stuff_to_parse = '';
$endresult = '';
// "Important" selections are handled like multiline comments
// @todo GET RID OF THIS SHIZ
if ($this->enable_important_blocks) {
$this->language_data['COMMENT_MULTI'][GESHI_START_IMPORTANT] = GESHI_END_IMPORTANT;
}
if ($this->strict_mode) {
// Break the source into bits. Each bit will be a portion of the code
// within script delimiters - for example, HTML between < and >
$k = 0;
$parts = array();
$matches = array();
$next_match_pointer = null;
// we use a copy to unset delimiters on demand (when they are not found)
$delim_copy = $this->language_data['SCRIPT_DELIMITERS'];
$i = 0;
while ($i < $length) {
$next_match_pos = $length + 1;
// never true
foreach ($delim_copy as $dk => $delimiters) {
if (is_array($delimiters)) {
foreach ($delimiters as $open => $close) {
// make sure the cache is setup properly
if (!isset($matches[$dk][$open])) {
$matches[$dk][$open] = array('next_match' => -1, 'dk' => $dk, 'open' => $open, 'open_strlen' => strlen($open), 'close' => $close, 'close_strlen' => strlen($close));
}
// Get the next little bit for this opening string
if ($matches[$dk][$open]['next_match'] < $i) {
// only find the next pos if it was not already cached
$open_pos = strpos($code, $open, $i);
if ($open_pos === false) {
// no match for this delimiter ever
unset($delim_copy[$dk][$open]);
continue;
}
$matches[$dk][$open]['next_match'] = $open_pos;
}
if ($matches[$dk][$open]['next_match'] < $next_match_pos) {
//So we got a new match, update the close_pos
$matches[$dk][$open]['close_pos'] = strpos($code, $close, $matches[$dk][$open]['next_match'] + 1);
$next_match_pointer =& $matches[$dk][$open];
$next_match_pos = $matches[$dk][$open]['next_match'];
}
}
} else {
//So we should match an RegExp as Strict Block ...
/**
* The value in $delimiters is expected to be an RegExp
* containing exactly 2 matching groups:
* - Group 1 is the opener
* - Group 2 is the closer
*/
if (!GESHI_PHP_PRE_433 && preg_match($delimiters, $code, $matches_rx, PREG_OFFSET_CAPTURE, $i)) {
//We got a match ...
if (isset($matches_rx['start']) && isset($matches_rx['end'])) {
$matches[$dk] = array('next_match' => $matches_rx['start'][1], 'dk' => $dk, 'close_strlen' => strlen($matches_rx['end'][0]), 'close_pos' => $matches_rx['end'][1]);
} else {
$matches[$dk] = array('next_match' => $matches_rx[1][1], 'dk' => $dk, 'close_strlen' => strlen($matches_rx[2][0]), 'close_pos' => $matches_rx[2][1]);
}
} else {
// no match for this delimiter ever
unset($delim_copy[$dk]);
continue;
//.........这里部分代码省略.........
示例4: geshi_dbg
/**
* Handles debugging by printing a message according to current debug level,
* mask of context and other things.
*
* @param string The message to print out
* @param int The context in which this message is to be printed out in - see
* the GESHI_DBG_* constants
* @param boolean Whether to add a newline to the message
* @param boolean Whether to return the count of errors or not
* @access private
*/
function geshi_dbg($message, $add_nl = true)
{
// shortcut
if (empty($message)) {
if ($add_nl) {
echo "\n";
}
return;
}
//
// Message can have the following symbols at start
//
// @b: bold
// @i: italic
// @o: ok (green colour)
// @w: warn (yellow colour)
// @e: err (red colour)
$start = '';
$end = '';
if ($message[0] == '@') {
$end = '</span>';
switch (substr($message, 0, 2)) {
case '@b':
$start = '<span style="font-weight:bold;">';
break;
case '@i':
$start = '<span style="font-style:italic;">';
break;
case '@o':
$start = '<span style="color:green;background-color:#efe;border:1px solid #393;">';
break;
case '@w':
$start = '<span style="color:#660;background-color:#ffe;border:1px solid #993;">';
break;
case '@e':
$start = '<span style="color:red;background-color:#fee;border:1px solid #933;">';
break;
default:
$end = '';
}
if (!empty($end)) {
$message = substr($message, 2);
}
} elseif (preg_match('#::(?:.*?)\\((?:.*?)\\)#s', $message)) {
$start = '<span style="font-weight:bold;">';
$end = '</span>';
}
if ($add_nl) {
$end .= "\n";
}
echo $start . GeSHi::hsc(str_replace("\n", '', $message)) . $end;
}
示例5: parseToken
/**
* Implements parseToken to format the XML tags.
*
* @param string $token The token to put tags around
* @param string $context_name The name of the context that the tag is in
* @param array $data Miscellaneous data about the context
* @return string The token wrapped in Pango markup
* @todo [blocking 1.2.2] Make it so that CSS is optional
*/
function parseToken($token, $context_name, $data)
{
// Ignore blank tokens
if ('' == $token || geshi_is_whitespace($token)) {
return $token;
}
// Initialize the result variable
$result = '';
$style = $this->_styler->getStyle($context_name);
// Add the basic tag
$result .= '<span ';
$result .= self::_styleToAttributes($style);
// Finish the opening tag
$result .= '>';
// Now add in the token
$result .= GeSHi::hsc($token);
// Add the closing tag
$result .= '</span>';
// Return the result
return $result;
}
示例6: _initContext
//.........这里部分代码省略.........
// First function to try is the user-defined one
if ('' != $init_function) {
$function = 'geshi_' . $context->_languageName . '_' . $init_function;
if (function_exists($function)) {
$function($context);
$context->_initPostProcess();
return;
}
$tried_functions[] = $function;
}
// Next choice is the full context name function
$function = 'geshi_' . str_replace('/', '_', $context_name);
if (function_exists($function)) {
$function($context);
$context->_initPostProcess();
return;
}
$tried_functions[] = $function;
// Next is the dialect shortcut function
$function = 'geshi' . str_replace('/', '_', substr($context_name, strpos($context_name, '/')));
if (function_exists($function)) {
$function($context);
$context->_initPostProcess();
return;
}
$tried_functions[] = $function;
// Final chance is the language shortcut function
$root_language_name = $context->_languageName . "/" . $context->_languageName;
if ($context_name != $root_language_name && "{$root_language_name}/" != substr($context_name, 0, strlen($root_language_name) + 1)) {
$function = 'geshi_' . str_replace('/', '_', $context->_languageName . substr($context_name, strpos($context_name, '/', strpos($context_name, '/') + 1)));
if (function_exists($function)) {
$function($context);
$context->_initPostProcess();
return;
}
$tried_functions[] = $function;
}
// If we are still inside this constructor then none of the functions
// we have tried have been available to call. Time to raise an error.
// This will in general only ever happen to developers building new
// language files, so we can afford to take our time and build a nice
// error message to help them debug it.
//
// If PHP version is greater that 4.3.0 then debug_backtrace
// can give us a nice output of the error that occurs. This
// code shamelessly ripped from libheart, which got it from
// a comment on the php.net manual.
if (function_exists('debug_backtrace')) {
$backtrace = debug_backtrace();
$calls = array();
$backtrace_output = "<pre><strong>Call stack (most recent first):</strong>\n<ul>";
foreach ($backtrace as $bt) {
// Set some defaults for debug values
$bt['file'] = isset($bt['file']) ? $bt['file'] : 'Unknown';
$bt['line'] = isset($bt['line']) ? $bt['line'] : 0;
$bt['class'] = isset($bt['class']) ? $bt['class'] : '';
$bt['type'] = isset($bt['type']) ? $bt['type'] : '';
$bt['args'] = isset($bt['args']) ? $bt['args'] : array();
$args = '';
foreach ($bt['args'] as $arg) {
if (!empty($args)) {
$args .= ', ';
}
switch (gettype($arg)) {
case 'integer':
case 'double':
$args .= $arg;
break;
case 'string':
$arg = substr($arg, 0, 64) . (isset($arg[64]) ? '...' : '');
$args .= '"' . $arg . '"';
break;
case 'array':
$args .= 'array(' . count($arg) . ')';
break;
case 'object':
$args .= 'object(' . get_class($arg) . ')';
break;
case 'resource':
$args .= 'resource(' . strstr($arg, '#') . ')';
break;
case 'boolean':
$args .= $arg ? 'true' : 'false';
break;
case 'NULL':
$args .= 'null';
break;
default:
$args .= 'unknown';
}
}
// Build a new entry for the output.
$backtrace_output .= '<li>' . GeSHi::hsc($bt['class']) . '' . GeSHi::hsc($bt['type']) . '' . '' . GeSHi::hsc($bt['function']) . '' . '(' . GeSHi::hsc($args) . ') at ' . GeSHi::hsc($bt['file']) . ':' . $bt['line'] . "</li>";
}
$backtrace_output .= '</ul></pre>';
} else {
$backtrace_output = '[No backtrace available - debug_backtrace() ' . 'not available]';
}
trigger_error("Could not find function for context {$context_name}\n" . 'looked for ' . implode(', ', $tried_functions) . "\n" . $backtrace_output, E_USER_ERROR);
}
示例7: parseToken
/**
* Implements parseToken to put HTML tags around the tokens
*
* @param string $token The token to put tags around
* @param string $context_name The name of the context that the tag is in
* @param array $data Miscellaneous data about the context
* @return string The token wrapped in the appropriate HTML
*/
function parseToken($token, $context_name, $data)
{
// ignore blank tokens
if ('' == $token || geshi_is_whitespace($token)) {
return $token;
}
$result = '';
if (isset($data['url'])) {
// There's a URL associated with this token
$result .= '<a href="' . GeSHi::hsc($data['url']) . '">';
}
if (!isset($this->contextCSS[$context_name])) {
$this->contextCSS[$context_name] = self::_styleToCSS($this->_styler->getStyle($context_name));
}
$result .= '<span style="' . $this->contextCSS[$context_name] . '" ';
$result .= 'title="' . GeSHi::hsc($context_name) . '">' . GeSHi::hsc($token) . '</span>';
if (isset($data['url'])) {
// Finish the link
$result .= '</a>';
}
return $result;
}