在Wordpress官方网站的插件列表中搜索一下,可以发现为Wordpress博客增加代码的语法加亮功能的插件可以说是很多很多,大都是基于一些开源的提供语法加亮功能的JS库(如谷歌代码站点使用的Google prettify和 SyntaxHighlighter等等),不过使用插件的话又会在页面中增加一些CSS和Javascript的加载和Wordpress函数的Hook,会减慢页面的加载速度。其实如果只是偶尔的需要代码加亮功能而且代码长度也不是很长的话,最简单而且又不影响性能的方法就是直接使用加亮后代码的Html源代码复制粘贴到文章中就好了,也不用考虑使用pre或者code标签的问题。
很多开源文本编辑器(如Notepad++和Geany)就都可以支持导出语法加亮的代码为Html格式的功能,而且也可以自己自由的修改不同语言格式加亮的主题(颜色、字体和背景等等),导出后是通过Span标签加入的高亮样式,如果有需要修改的地方也可以自行修改CSS代码。具体的Notepad++是通过“插件”菜单中的NppExport插件就可以直接导出代码成Html文件或者到剪贴板,而Geany也类似是从“工具”菜单->“插件管理器”中的导出插件即可,还可以连行号一起导出成Html文件。
以下是一段导出的加亮代码例子:
-
<?php
-
/*
-
* load-cron.php
-
*
-
* Author :admin <[email protected]>
-
*
-
* This program is free software; you can redistribute it and/or modify
-
* it under the terms of the GNU General Public License as published by
-
* the Free Software Foundation; either version 2 of the License, or
-
* (at your option) any later version.
-
*
-
* This program is distributed in the hope that it will be useful,
-
* but WITHOUT ANY WARRANTY; without even the implied warranty of
-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-
* GNU General Public License for more details.
-
*
-
* You should have received a copy of the GNU General Public License
-
* along with this program; if not, write to the Free Software
-
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-
* MA 02110-1301, USA.
-
*
-
*
-
*/
-
-
# function to check log directory and create it if necessary
-
function checkLogDir($path) {
-
-
global $log_path;
-
-
$log_path = $path . ‘/logs’;
-
$htaccess = “Options -Indexes\n“ . “Deny from all\n“;
-
-
-
# Return true if folder is writable
-
-
# if not a .htaccess, protect the dir with a .htaccess file
-
}
-
-
return true;
-
}
-
-
# If dir exists but not writable,then nothing else we can do.
-
echo ‘Log dir already exists but is NOT writable! Please chmod it to 755!<br />’;
-
return false;
-
-
} else {
-
-
#if no log dir,create it
-
-
# protect the dir with a .htaccess file
-
-
# Return true
-
return true;
-
-
} else {
-
# mkdir error or upper dir not writable,then return false
-
echo ‘Upper dir NOT writable or creating log dir returns error!<br />’;
-
return false;
-
}
-
-
}
-
-
}
-
-
# set time zone to GMT+8.
-
-
# get current dir location
-
-
# Filename to save as
-
-
-
-
# Line to write
-
} else {
-
}
-
-
# write to file
-
echo ‘Successfully write to log file!<br />’;
-
-
}
-
else {
-
echo ‘System file /proc/loadavg is NOT readable, script halt!<br />’;
-
}
-
}
-
-
?>
相关文章: