2009年9月27日 星期日

Using SyntaxHighlighter on Blogger

1. 下載 SyntaxHighlighter
2. 上傳 scripts 和 styles 資料夾內的檔案至網站上(可只選擇自己需要的程式語言主題)
3. Layout -> Edit HTML -> 將下列程式碼貼在 < /head > 前



<!-- Syntax Highlighter Block start here -->
<link href='[ YOUR_SITE ]/shcore.css' rel='stylesheet' type='text/css'/>
<link href='[ YOUR_SITE ]/shthemedefault.css' id='shTheme' rel='stylesheet' type='text/css'/>
<script src='[ YOUR_SITE ]/shcore.js' type='text/javascript'></script>
<script src='[ YOUR_SITE ]/shbrushbash.js' type='text/javascript'></script>
<script src='[ YOUR_SITE ]/shbrushcpp.js' type='text/javascript'></script>
<script src='[ YOUR_SITE ]/shbrushplain.js' type='text/javascript'></script>
<script src='[ YOUR_SITE ]/shbrushpython.js' type='text/javascript'></script>
<script src='[ YOUR_SITE ]/shbrushsql.js' type='text/javascript'></script>
<script src='[ YOUR_SITE ]/shbrushxml.js' type='text/javascript'></script>
<script class='javascript'>
//<![CDATA[
SyntaxHighlighter.config.bloggerMode = true;
SyntaxHighlighter.all();
//]]>
</script>
<!-- Syntax Highlighter Block End here -->


將 [ YOUR_SITE ] 改成上傳 SyntaxHighlighter 的網址,並注意檔案名稱大小寫,依個人所需增減不同的程式語言(上面是我自己所用的不包含全部)和主題
另外,不要問我為什麼跟一堆教學寫的不同,因為我也是試了一個上午都搞不定,最後複製某網站的原始碼才成功...

使用方法如下:

<pre class="brush: BRUSH ALIASES ">
code
</pre>


把 BRUSH ALIASES 換成要用的程式語言,並記得將 < 換成 &amp;lt; 就行了

Upgrade Comix Bookmarks from 3.x to 4.x

!! Updated 2009/10/07 for comix 4.0.4 have changed the bookmark path

Comix 由 3.x 升級到 4.0時,會自動偵測之前的書籤,但只能選擇刪除,所以就自己寫程式將舊的書籤更新到新格式,順便學一下 Python 。
怠惰的天性使然,這個小程式花了好幾個月才完成(別打我,當兵的時候每次放假只想玩,沒什麼動力寫程式啊...),所以裡面的縮排和註解很亂,請多包涵,有空我會再花時間去看看 Style Guide for Python Code

使用方法:
1. Save as upbmk.py
2. chmod +x upbmk.py
3. ./upbmk.py OLD_BOOKMARK_FILE


#! /usr/bin/env python
# -*- coding: utf-8 -*-
# upbmk.py - Upgrade Comix bookmarks form 3.x to 4.x
# Usage : upbmk.py [ OLD_BOOKMARK_FILE ]

import os, sys, shutil, re
import cPickle
import subprocess

import zipfile, tarfile

if len(sys.argv)==2 :
ob_path=sys.argv[1]
else:
ob_path="./bookmarks_data"

# open current / old book mark file , make backups
# updated 2009/10/07 , for comix 4.0.4 changed bookmark path
# tStr = os.path.expanduser("~/.comix/bookmarks.pickle")
data_path = os.getenv("XDG_DATA_HOME", os.path.expanduser("~/.local/share") )
tStr = os.path.join(data_path,"comix","bookmarks.pickle")
shutil.copyfile( ob_path, "./backup_old_bookmarks_data")
shutil.copyfile( tStr, "./backup_current_bookmarks.pickle")
cbf = open(tStr,'r')
obf = open(ob_path,'r')

#load current and old bookmarks
cbk = []
cbk.append(cPickle.load(cbf))
cbk.append(cPickle.load(cbf))
odata = cPickle.load(obf)
odata2 = cPickle.load(obf)
obk = []
cbf.close()
obf.close()

for num in range(len(odata)):
temp = odata[num],odata2[num]
obk.append( temp )

del cbf,obf,odata,odata2

# distinguish file types
ZIP, RAR, TAR, GZIP, BZIP2 = range(5)
def what_type( filepath ):
try:
if not os.access( filepath , os.R_OK ):
print " You don't have the permission to read ",filepath,\
" ,you better check it out !! "
sys.exit()
if zipfile.is_zipfile( filepath ):
return ZIP
# use magic numbers to distinguish type
opf = open( filepath , 'r' )
magic = opf.read(8)
opf.close
if magic.startswith('\x52\x61\x72\x21\x1a\x07\x00'):
return RAR
if magic.startswith('\x1f\x8b\x08'):
return GZIP
if magic.startswith('\x42\x5a\x68'):
return BZIP2
# this can also read tar.gz , tar.bz2 ,so put it to the last one
if tarfile.is_tarfile( filepath ):
return TAR
# No of above , check it later
else:
return None
except Exception:
print " Error when reading ",filepath
return None

# test if command "rar" ,"unrar" exist or not
rcmd = None
for testcmd in ["rar","unrar"]:
try:
subprocess.Popen( [testcmd], stdout=subprocess.PIPE)
rcmd = testcmd
except Exception:
pass

# count how many image files in the archive
def get_pages( filepath , archive_type ):
all_files=[]
if archive_type == ZIP :
zfile = zipfile.ZipFile( filepath , 'r' )
all_files = zfile.namelist()
elif archive_type in ( TAR , GZIP , BZIP2 ) :
tfile = tarfile.open( filepath , 'r')
all_files = tfile.getnames()
elif archive_type == RAR :
if rcmd != None:
rout=subprocess.Popen([ rcmd,"vb",filepath],stdout=subprocess.PIPE)
# may contain one newline character
all_files = rout.stdout.readlines()
#print all_files
else:
print ' Unable to extract '+ filepath +' with "rar" or "unrar" '
# not an archive , just a image , scan the directory
elif re.search(r"\.(jpg|jpeg|png|gif|tif|tiff)\s?$", filepath, re.I):
all_files = os.listdir( os.path.dirname(filepath) )
# Error on reading files
else:
print ' Unable to handle" '+ filepath +'" , sorry'
return 0
imgs = 0
#count image files
for name in all_files:
if re.search(r"\.(jpg|jpeg|png|gif|tif|tiff)\s?$",name,re.I):
imgs = imgs + 1
# return number of total image files
return imgs

# convert tuple to list for manipulation
# insert filename,total pages,aechive type
obklist = []
for num in range(len(obk)):
obklist.append( list(obk[num]) )
obklist[num].insert( 0 , os.path.basename( obklist[num][0] ) )
file_type = what_type( obklist[num][1] )
obklist[num].append( get_pages( obklist[num][1], file_type) )
obklist[num].append( file_type )

for bmk in obklist:
cbk[1].append( tuple( bmk))

cbf = open(tStr,'wb')
cPickle.dump( cbk[0], cbf, cPickle.HIGHEST_PROTOCOL)
cPickle.dump( cbk[1], cbf, cPickle.HIGHEST_PROTOCOL)
cbf.close()

print " Upgrade Complete !! After Checking the new bookmarks,\n\
remember to delete backup files "

####### print out data inside bookmarks #########

#print len (cbk[1][0]) // 5
#for num in range(len(cbk[1][1])):
# print cbk[1][1][num-1]
#print cbk[1]
#print len(obk)
#print obk

######### bookmark file format ###########

# cbk = [ 'version',('cbookmarks') ]
# cbookmarks = [ (cbmk1),(cbmk2),....]
# cbmk = [ filename , path , bmk_page , total_pages , archive type]
# obk = [ oldbmk1 , oldbmk2 ...]
# oldbmk = [ path , bmk_page ]

2009年9月21日 星期一

Split(Rename) audio files by cue files

Reference:Split lossless audio (ape, flac, wv, wav) by cue file in Ubuntu
Tools: cuetools, shntools,audio file decoder

起因:
有塊 CD 整個壓成一個 *.tta 加 *.cue ,在 Windows 下要播放時,可用 foobar2000 直接讀取 *.cue ,但是在 linux 下似乎沒這麼方便,所以就乾脆把它每首都拆開

過程:
1. *.cue 用 more 下去看是亂碼, 用 gedit 一個個測才查出編碼是CP932
2. 怕命名以後也是亂碼,就用比較笨的方法, tta 先解成 wav,再下 cuebreakpoints *.cue | shnsplit *.tta, 分割成一個個的 split-track*.wav
3. 用 Python 讀 *.cue 進來, str.decode("CP932"), 確定可以正常顯示日文,再找每一首的檔名, os.rename() 重新命名

因為只有一塊要轉,就直接用 Python Interpreter 作業,一首首命名,沒寫什麼程式,單純紀錄一下所用的工具。

自去年入伍服役以後第一次更新 blog, 11 個月的時間總算熬過去了,外面的空氣真好 :)