Adding syntax highlighting to web pages (HTML / php)

I use http://softwaremaniacs.org/soft/highlight/en/ to highlight the HTML syntax. I have code to extract php code from wordpress. I am using tags pre / codein my code and the following loop to pull code from Wordpress.

<?php while (have_posts()) : the_post(); ?>
<p><i><?php the_date(); echo "<br />"; ?> </p></i>
<h2><?php the_title(); ?></h2>    
<p><?php the_content(); ?></p>
<?php endwhile;?>

At the top of my php page, I have an import ...

<script type="text/javascript" src="highlight.pack.js"></script>
<script type="text/javascript">
hljs.initHighlightingOnLoad();
</script>

Test.html on the page works fine, but my page does not appear on my web server. Does this have something to do with how I dynamically pull content? How can I do this job?

+3
source share
5 answers

From source code

function initHighlightingOnLoad() {
    var original_arguments = arguments;
    var handler = function(){initHighlighting.apply(null, original_arguments)};
    if (window.addEventListener) {
      window.addEventListener('DOMContentLoaded', handler, false);
      window.addEventListener('load', handler, false);
    } else if (window.attachEvent)
        window.attachEvent('onload', handler);
    else
        window.onload = handler;
}

So this will only work once, BUT

,

hljs.initHighlighting.called = false;
hljs.initHighlighting();

- , , .

, .

+8

: GeSHi PHP.

:

//
// Include the GeSHi library//
include_once 'geshi.php'; 
//// Define some source to highlight, a language to use
// and the path to the language files//
$source = '$foo = 45;
for ( $i = 1; $i < $foo; $i++ ){
  echo "$foo\n";  --$foo;
}';$language = 'php';
 //
// Create a GeSHi object//
 $geshi = new GeSHi($source, $language);
 //
// And echo the result!//
echo $geshi->parse_code();
+3

FSHL (Fast Syntax Highlighter), PHP, , , . .

Dynamically loaded content should already be allocated by the server, which means you won’t need to use JavaScript.

If you insist on using the backlight on the client side, I suggest using SyntaxHighlighter by Alex Gorbatchev, a full-featured, very popular and you will not need an additional PHP-code.

0
source

Source: https://habr.com/ru/post/1761224/


All Articles