林中侠客

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

今天Twitter发布Bootstrap 2.0,修复了许多Bug,增加了不少新样式,同时Bootstrap的代码高亮也是一大亮点,与其他网站的代码高亮不同,淡淡的灰色调配上几个鲜艳而不刺眼的亮色点缀,相当漂亮,看了源码才知道,它是基于google-code-prettify的一套皮肤,于是我就决定把它搬到博客里来,下面是html、css、js的几个demo。

  1. /*This is not code*/
  2. #site-generator .sep {
  3. background: url(images/wordpress.png) center left no-repeat;
  4. color: transparent;
  5. display:inline-block;
  6. height:16px;
  7. line-height:16px;
  8. margin:07px;
  9. width:16px;
  10. }
  1. /*This is not code*/
  2. <!DOCTYPE html>
  3. <htmllang="en"><head>
  4. <metacharset="utf-8">
  5. <title> imshanks | 我行我骕 </title>
  6. <linkrel="stylesheet"href="highlighter/prettify.css"/>
  7. </head>
  8. <body>
  9. <scriptsrc="highlighter/prettify.js"></script>
  10. </body>
  11. </html>
  1. /*This is not code*/
  2. $("#id").hover(function(){
  3. alert("imshanks.com");
  4. });

使用方法如下:

1)下载google-code-prettify
2)在head引入js和css文件

  1. <linkhref="google-code-prettify/prettify.css"type="text/css"rel="stylesheet"/>
  2. <scripttype="text/javascript"src="google-code-prettify/prettify.js"></script>

3)在body加上onload=”prettyPrint()

  1. <bodyonload="prettyPrint()">...</body>

如果你的主题里引入过Jquery.js,也可以加上下面这段代码

  1. $(document).ready(function(){
  2. prettyPrint();
  3. })

设置方法如下

1)把代码放进<pre>...</pre> 或者 <code>...</code> 就可以实行代码高亮了。你也可以指定一种语言,class=”prettyprint Lang-html”,在lang- 后添加以下任何一种语言。“bash”, “c”, “cc”, “cpp”, “cs”, “csh”, “cyc”, “cv”, “htm”, “html”, ”java”, “js”, “m”, “mxml”, “perl”, “pl”, “pm”, “py”, “rb”, “sh”, ”xhtml”, “xml”, “xsl”

2)打开行号
你可以使用linenums打开行号

  1. <preclass=”prettyprintlinenumsLang-html>

喜欢这套样式的童靴下载prettify.css替换就行了。
下面提供我改造的wordpress插件WP-Bootstrap-highlighting-of-code.rar用wordpress的童靴可以安装插件直接设置并使用。

(此插件原版为http://boliquan.com/wp-code-highlight/开发,本人只做了样式修改)

posted on 2014-11-10 16:33  林中侠客  阅读(4689)  评论(0编辑  收藏  举报