作者descent (「雄辩是银,沉默是金」)
看板Blog
标题Re: [Blogger] 文章插入程式码
时间Sun Oct 2 20:50:38 2016
我用了 code2html 这个工具, 提供你做参考。
在 blog 贴上程式码 (source code ex: c/c++/组合语言), 需要先将程式码转成 html
语法, 没想到花费的力气比我想像中的大, google 好几天, 不是太复杂 (要修改一堆
css/js ) 就是不合我用。我只需要行号和语法颜色。当然, 最好能在 linux 下使用。
code2html
vi 的 :TOhtml
http://www.opinionatedgeek.com/DotNet/Tools/HTMLEncode/Encode.aspx (
https://goo.gl/klBr3 )
http://blog.xuite.net/givemepassxd/blog/40467229 (
https://goo.gl/6nsqxc )
http://fly2sky999.blogspot.tw/2012/03/blogger-css-block-google-code-prettify.html
(
https://goo.gl/7kHhls )
最後选了 code2html (用 apt-get install code2html 轻松搞定)
code2html -H -n -l as 5M_mem_rw.S > 5M_mem_rw.html
code2html -H -n -l c k.c > k.html
用这些 option 出来的结果前後加上
<pre>
</pre>
即可贴到网页/blog 上。
以下是我用的 pre 有边框的效果。
<pre style="border: 1px inset ; margin: 0px; padding: 6px; overflow: auto;
width: 600px; text-align: left;">
</pre>
支援的语言:
descent@ubuntu:1.5$ code2html --mode
Defined modes: ada, ada95, awk, c, c++, cc, cpp, cxx, gpasm, groff, html,
java,
javascript, js, lisp, m4, make, makefile, pas, pascal, patch, perl, plain,
pov,
povray, python, ruby, sh, shellscript, sql.
Defined outputformats: html, html-dark, html-fntlck, html-light, html-nobg,
html-nocolor, html-simple.
方便的 script
h.sh
1 (
https://goo.gl/3d1tcH ) #!/bin/sh
2 (
https://goo.gl/3d1tcH )
3 (
https://goo.gl/3d1tcH ) echo "<fieldset><legend>$1</legend>" > $1.html
4 (
https://goo.gl/3d1tcH ) code2html -H -n -l $2 $1 >> $1.html
5 (
https://goo.gl/3d1tcH ) echo "</fieldset>" >> $1.html
组合语言
./h.sh 5M_mem_rw_mix_c.S gpasm
c 语言
./h.sh k.c c
清除 number link: blogger 会填上 link 的内容, 所以把 a tag 换成 b tag, 使用
sed_cmd 来处理即可。
link
1 (
https://goo.gl/3d1tcH ) <a href="" name="line9"> 9</a> xor %eax,
%eax
2 (
https://goo.gl/3d1tcH ) <b> 9</b> xor %eax, %eax
sed_cmd
1 (
https://goo.gl/3d1tcH ) sed -i -e 's/<\/a>/<\/b>/g' simple_proc.S.html
2 (
https://goo.gl/3d1tcH ) sed -i -e 's/<a name="line[0-9]*">/<b>/g'
simple_proc.S.html
加上 sed 的版本
h.sh
1 #!/bin/sh
2 # use code2html to generate html for blog.
3 # language:
4 # ada, ada95, awk, c, c++, cc, cpp, cxx, gpasm, groff, html, java,
5 # javascript, js, lisp, m4, make, makefile, pas, pascal, patch, perl,
plain, pov,
6 # povray, python, ruby, sh, shellscript, sql.
7
8 if [ "$1" == "" ]; then
9 echo "usage: $0 code_filename language"
10 echo "code2html support language syntax"
11 code2html --mode
12 exit 0
13 #else
14 # echo 'usage: code_filename $1'
15 fi
16 L=plain
17
18 if [ "$2" == "" ]; then
19 echo "language syntax: $L"
20 else
21 L=$2
22 fi
23
24
25 echo "<fieldset><legend>$1</legend>" > $1.html
26 echo '<pre style="white-space: pre-wrap;">' >> $1.html
27 code2html -H -n -l $L $1 >> $1.html
28
#code2html -H -N -l $L $1 >> $1.html
29 echo "</pre>" >> $1.html
30 echo "</fieldset>" >> $1.html
31
32 sed -i -e 's/<\/a>/<\/b>/g' $1.html
33 sed -i -e 's/<a name="line[0-9]*">/<b>/g' $1.html
34
35 echo "generate $1.html"
组合语言和 C 语言的效果:
http://descent-incoming.blogspot.tw/2012/05/0-hello-xyz.html (
https://goo.gl/Ak3qyt )
// 本文使用 Blog2BBS 自动将Blog文章转成缩址的BBS纯文字
http://goo.gl/TZ4E17 //
blog 原文
http://descent-incoming.blogspot.tw/2011/09/source-code-to-html.html
※ 引述《freexq (快乐蕃茄)》之铭言:
: 大家好,请教各位几个问题。
: 我想要在我的 Blooger 写文章
: 文章内会有程式码
: 我希望不只是单纯的 copy & paste
: 因为那样子程式码会变得很丑...
: 所以google了一下,找到范例
: 请看
: http://cookieandcoketw.blogspot.tw/2012/02/java-a020_2137.html
: http://ssc24.blogspot.tw/2011/09/a010.html
: 以上两个blog
: 是我想要美化呈现的程式码例子
: 像是有方框、行号、自动变色等等功能...
: 问题来了,该如何做才能有以上的效果呢?
: 烦请不吝告知,谢谢。
--
纸上得来终觉浅,绝知此事要躬行。
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 223.142.100.144
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Blog/M.1475412648.A.005.html