admin
admin
3221 7 0

用 HIGHLIGHT.JS 在youBBS 实现代码高亮

前几天推荐了一款前端渲染的代码高亮 HIGHLIGHT.JS https://youbbs.org/t/1337
想想这里也经常贴一些代码,就想办法实现一下。
如果在python 环境可以用pygments 实现代码高亮渲染,保存在数据库的是渲染后的纯html 代码。
前端有它自己的好处,就是编辑数据源比较方便,只是浏览器渲染需要一些时间。
下面是代码高亮测试

python

print "hello"
for i in range(10):
    print i
    if i>5:
        print i, '>5'

js

<script type="text/javascript">
alert('hello');
</script>

html

<h1>h1 text</h1>
<p style='color:red'>red text in p tag</a>

<button class="mainButton">Large button</button><br>
<button>Normal button</button><br>
<button class="shyButton">Small button</button>

添加代码高亮的方法:
把代码放在三个“反单引号”(在键盘左上角,Esc 下面,切换到英文输入)之间

0

See Also

Nearby


Discussion (7)

root
root 2013-10-25 12:58

Vps版本也可以吗?不用修改代码?

0
admin
admin 2013-10-25 23:45

@root 代码要修改的。

0
root
root 2013-10-26 18:09

求代码,要不发布一个更新包吧

0
root
root 2013-10-27 00:49

'''echo,helloword,test'''

0
root
root 2013-10-27 00:49

echo,helloword,code-test

0
乖乖念祢
乖乖念祢 2013-10-29 15:16

set_content 函数 添加个正则识别所有字符 失败 看说明应该是把前后`{3}替换成<pre><code> </code></pre>配合js和css就可以的

    if(strpos($text, '`')){
        $text = preg_replace('`{3}([\s\S]+)`{3}','&lt;pre&gt;&lt;code&gt;\1&lt;/code&gt;&lt;/pre&gt;', $text);
    }```
不会还是iis的正则识别不好吧  上次用户名汉字就匹配不了
0
admin
admin 2013-10-30 02:02

@乖乖念祢 还需识别哪些要替换,哪些不要替换,晚点写一下实现方法。

0
Login Topics