zoukankan      html  css  js  c++  java
  • 不用插件实现WordPress代码高亮显示

    偶尔在日志中加入一些代码,感觉完全没必要安装代码高亮插件,万一某天不用插件了,页面可能会很乱。其实大部分插件都是在代码中强行加入一些标签,然后用CSS定义样式,通过查看页面源文件可以清楚地看到。 今天推荐的是一款本地转换代码高亮显示的小工具:CodeRenderUnmi 下载:  CodeRenderUnmi.zip (21.1 KB, 407 次下载) 本程序是基于 dp.SyntaxHighlighter 写的代码语法着色的工具。支持的语言有: java/xml/sql/jscript/css/cpp/c#/python/vb/perl/php/ruby/delphi。 可方便用于你的博客中粘贴代码,只要自定相应的样式 (highlight.css 的内容,.Text 支持自定义样式或在模板里加上语法样式),然后复制用这个工具生成的 HTML 代码就能让你的代码着高亮显示。 可以加入更多语种的支持,本程序就是在 dp.SyntaxHighlighter 的基础上扩展了对 Perl 语言的支持,网上可以找到相应语法的 JS 代码和 CSS。语言扩展支持通过在 shCore.js 和 highlight.css 加入相应代码即可。 程序界面 操作很容易,Source Code 中贴上你要着色的代码,然后选择语种,点击 Render 按钮就会在 HTML Code 中生成相应的 HTML 代码,同时在 HTML Preview 中可以预览到效果。 简要说明:Lang 下拉框可以选择所支持的语法,Options 右边的 Gutter、Controls、CollapseAll、FirstLine、Columns 是控制生成的额外的元素,逐一点试试就知道了。每个内容显示(输入)区都提供了 Copy/Paste/Clear 快捷操作链接,还有一个总的 Clear 按钮。 Copy生成 的HTML 代码,在日志编辑窗口切换到HTML源代码编辑模式,粘贴就可以了。 不过要想正确显示代码高亮还需在WordPress主题中加载样式文件“highlight.css”(在下载的压缩包中) 方法: 首先,把highlight.css上传到所使用主题目录中; 其次:打开header.php,查找:
    1. <link rel="stylesheet" type="text/css" href="<?php bloginfo('template_directory'); ?>/style.css" />
    在后面添加:
    1. <link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/highlight.css" />
    当然,你也可以复制highlight.css中的所有代码到你主题style.css中,以上步骤就免了! 如认为默认的样式不符合自己的要求,可以通过修改“highlight.css”中的样式改变代码高亮、边框、背景等颜色,更个性化。 但需要注意的是Wordpress会自动把半角符号替换为全角,别人复制下来的函数代码标点是全角的,无法使用,切记! 解决办法:
    1. 打开并编辑 wp-includes/formatting.php 文件,找到以下代码:   
    2. // static strings   
    3. $curl = str_replace($static_characters, $static_replacements, $curl);   
    4. // regular expressions   
    5. $curl = preg_replace($dynamic_characters, $dynamic_replacements, $curl);   
    6. 将$curl 开头的两句代码注释掉,如下:   
    7. // static strings   
    8. //$curl = str_replace($static_characters, $static_replacements, $curl);   
    9. // regular expressions   
    10. //$curl = preg_replace($dynamic_characters, $dynamic_replacements, $curl);  
    注:使用IE核心浏览器复制代码时会将行号一起复制下来,可以参考这篇文章去掉行号:小技巧:帮你批量删除代码前的行号,或才使用火狐、chrome等浏览器。 PHP:
    1. <div id=“branding” role=“banner”>      
    2. <?php $heading_tag = ( is_home() || is_front_page() ) ? ’h1′ : ’div’; ?>      
    3. <<?php echo $heading_tag; ?> id=“site-title”>      
    4. <span>      
    5. <a href=“<?php echo home_url( ’/' ); ?>” title=“<?php echo esc_attr( get_bloginfo( ’name’, ’display’ ) ); ?>” rel=“home”><?php bloginfo( ’name’ ); ?></a>      
    6. </span>      
    7. </<?php echo $heading_tag; ?>>      
    8. <div id=“site-description”><?php bloginfo( ’description’ ); ?></div>      
    9. <?php      
    10. // Check if this is a post or page, if it has a thumbnail, and if it’s a big one      
    11. if ( is_singular() &&      
    12. has_post_thumbnail( $post->ID ) &&      
    13. /* $src, $width, $height */ $image = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID ), ’post-thumbnail’ ) ) &&      
    14. $image[1] >= HEADER_IMAGE_WIDTH ) :      
    15. // Houston, we have a new header image!      
    16. echo get_the_post_thumbnail( $post->ID, ’post-thumbnail’ );      
    17. else : ?>      
    18. <img src=“<?php header_image(); ?>” width=“<?php echo HEADER_IMAGE_WIDTH; ?>” height=“<?php echo HEADER_IMAGE_HEIGHT; ?>” alt=“” />      
    19. <?php endif; ?>      
    20. </div><!– #branding –>    
      CSS:
    1. #header {   
    2. padding30px 0 0 0;   
    3. }   
    4. #site-title {   
    5. floatleft;   
    6. font-size30px;   
    7. line-height36px;   
    8. margin: 0 0 18px 0;   
    9. width700px;   
    10. }   
    11. #site-title a {   
    12. color#000;   
    13. font-weightbold;   
    14. text-decorationnone;   
    15. }   
    16. #site-description {   
    17. clear: rightright;   
    18. float: rightright;   
    19. font-styleitalic;   
    20. margin14px 0 18px 0;   
    21. width220px;   
    22. }   
    23. /* This is the custom header image */  
    24. #branding img {   
    25. border-top4px solid #000;   
    26. border-bottom1px solid #000;   
    27. clearboth;   
    28. displayblock;   
    29. }  
    jscript:
    1. $(function() {   
    2. $('.related_thumbnail img').animate({"opacity": .5 });   
    3. $('.related_thumbnail img').hover(function() {   
    4. $(this).stop().animate({ "opacity": 1 });   
    5. }, function() {   
    6. $(this).stop().animate({ "opacity": .5 });   
    7. });   
    8. });  
     
  • 相关阅读:
    面向对象的设计模式2
    数据结构
    算法题目1
    5.7(1) 反射
    hashMap原理(java8)
    6.1 接口
    18.1 线程
    13.2 具体的集合
    scrapy(2)——scrapy爬取新浪微博(单机版)
    5.1 类、超类和子类
  • 原文地址:https://www.cnblogs.com/gxldan/p/4066794.html
Copyright © 2011-2022 走看看