Showing posts with label shell. Show all posts
Showing posts with label shell. Show all posts

MediaWiki 转 Dokuwiki

    最近在玩wiki,很多年前曾经热衷,现在试了试Dokuwiki,wiki太强了,都可以用来代替wordpress做blog了,还节约了一个mysql... 问题是DokuWiki的markup语法与Wikipedia的MediaWiki稍有出入,于是就有人写了这个脚本转格式,还有在线版本。 详见:tips:mediawiki_to_dokuwiki_converter   我稍稍改了一下,作者的习惯真的和我一样,很脏,哈哈:
#! /bin/sh
# Mediawiki2Dokuwiki Converter
# originally by Johannes Buchner <buchner.johannes [at] gmx.at>
# changes by Frederik Tilkin:  - uses sed instead of perl
#    - resolved some bugs ('''''IMPORTANT!!!''''' becomes //**IMPORTANT!!!**//, // becomes <nowiki>//</nowiki> if it is not in a CODE block)
#     - added functionality (multiple lines starting with a space become CODE blocks)
#
# Licence: GPL (http://www.gnu.org/licenses/gpl.txt)
 
# First escape things that are already DokuWiki but not MediaWiki syntax
# // => <nowiki>//</nowiki>  (only when it is NOT in a PREFORMATTED line, and when it is NOT in a LINK [] !)
# ** =>  <nowiki>**</nowiki  (only when it is NOT in a PREFORMATTED line, NOR on the beginning of a line)
# surround preformatted blocks (lines starting with space) with <PRE> so that it's correctly converted to DokuWiki <CODE> blocks later on
 
# My personal need: Made it accept filename as bash variable and append .dokuwiki. Usage: scriptname.sh MediaWikiTxt.txt

cat "$1" \
 | sed -r -n '
  #starts with a SPACE, so it is part of a code block, just print and do nothing
  /^[ ]/ { p; d }
  #else: replace ALL **... strings (not at beginning of line)
  s/([^^][^\*]*)(\*\*+)/\1<nowiki>\2<\/nowiki>/g
  #   also replace ALL //... strings 
  s/([^\/]*)(\/\/+)/\1<nowiki>\2<\/nowiki>/g
  #  change the ones that have been replaced in a link [] BACK to normal (do it twice in case [http://addres.com http://address.com] ) [quick and dirty]
  s/([\[][^\[]*)(<nowiki>)(\/\/+)(<\/nowiki>)([^\]]*)/\1\3\5/g ; s/([\[][^\[]*)(<nowiki>)(\/\/+)(<\/nowiki>)([^\]]*)/\1\3\5/g
 
  p
   ' \
 | sed -r -n '
  # See also: http://www.grymoire.com/Unix/Sed.html#uh-40
  #  http://en.wikipedia.org/wiki/Regular_expression
  # This is pretty advanced sed syntax, so I ll try to explain as much as possible
  ################################################################################
 
  # if line starts with a space, add it to the hold buffer
  # we do this by 'branching' to :addtopre
  /^ [ ]*[^ ][^ ]*/ b addtopre
  # if line has only whitespace or is empty, the preformatted block is over, so we surround that with <pre>
  # we do this by 'branching' to :outputpre
  /^[ ]*$/ b outputpre
  # if line starts with NO whitespace, the preformatted block is over, so we surround that with <pre>
  /^[^ ].*$/ b outputpre
 
  #else this is a normal line
    #s/(.*)/NORMAL LINE: \1/g; p
   # print the line
   p
   #delete the current pattern space (so new cycle is started -> jumps to top)
   d
 
  # this is a line that should be part of a CODE block
  :addtopre
   #add it to the hold buffer
   H
    #s/(.*)/ADDED LINE: \1/g; p
   # if this is the last line of the file (end-of-file), empty this line and then output this last preformatted block
   $ { s/.*//g
    b outputpre
   }
   #delete the current pattern space (so new cycle is started -> jumps to top)
   d
  # this is where a paragraph is surrounded by <pre></pre>
  :outputpre
    #s/(.*)/END OF CODE LINE: \1/g; p
   # HOLD buffer is exchanged with the pattern space
   x
 
   # IF not empty, surround with <PRE> and PRINT the pattern space
   /(.+)/ {
    # surround it with <pre>
    s/(.+)/<pre>\1<\/pre>/g
    p
   }
   # exchange pattern space and hold buffer again, pattern is now the current line (not part of the preformatted block) and PRINT this line
   x
   p
   #delete the current pattern space   
   s/.*//g
   #and exchange this again with the hold buffer, so that the hold buffer is empty again   
   x
   #delete the current pattern space (so new cycle is started -> jumps to top)
   d
 ' \
    > mediawiki0
 
# Headings
cat mediawiki0 \
   | sed -r 's/^[ ]*=([^=])/<h1> \1/g' \
   | sed -r 's/([^=])=[ ]*$/\1 <\/h1>/g' \
   | sed -r 's/^[ ]*==([^=])/<h2> \1/g' \
   | sed -r 's/([^=])==[ ]*$/\1 <\/h2>/g' \
   | sed -r 's/^[ ]*===([^=])/<h3> \1/g' \
   | sed -r 's/([^=])===[ ]*$/\1 <\/h3>/g' \
   | sed -r 's/^[ ]*====([^=])/<h4> \1/g' \
   | sed -r 's/([^=])====[ ]*$/\1 <\/h4>/g' \
   | sed -r 's/^[ ]*=====([^=])/<h5> \1/g' \
   | sed -r 's/([^=])=====[ ]*$/\1 <\/h5>/g' \
   | sed -r 's/^[ ]*======([^=])/<h6> \1/g' \
   | sed -r 's/([^=])======[ ]*$/\1 <\/h6>/g' \
   > mediawiki1
 
cat mediawiki1 \
   | sed -r 's/<\/?h1>/======/g' \
   | sed -r 's/<\/?h2>/=====/g' \
   | sed -r 's/<\/?h3>/====/g' \
   | sed -r 's/<\/?h4>/===/g' \
   | sed -r 's/<\/?h5>/==/g' \
   | sed -r 's/<\/?h6>/=/g'  \
   > mediawiki2
 
# lists
cat mediawiki2 \
  | sed -r 's/^[*#][*#][*#][*#]\*/          * /g'  \
  | sed -r 's/^[*#][*#][*#]\*/        * /g'    \
  | sed -r 's/^[*#][*#]\*/      * /g'      \
  | sed -r 's/^[*#]\*/    * /g'        \
  | sed -r 's/^\*/  * /g'                  \
  | sed -r 's/^[*#][*#][*#][*#]#/          - /g'  \
  | sed -r 's/^[*#][*#][*#]#/        - /g'    \
  | sed -r 's/^[*#][*#]#/      - /g'      \
  | sed -r 's/^[*#]#/    - /g'        \
  | sed -r 's/^#/  - /g'                   \
  > mediawiki3
 
 
#[url text] => [url|text]
cat mediawiki3 \
  | sed -r 's/([^[]|^)(\[[^] ]*) ([^]]*\])([^]]|$)/\1\2|\3\4/g' \
  > mediawiki4
 
 
#[link] => [[link]]
cat mediawiki4 \
  | sed -r 's/([^[]|^)(\[[^]]*\])([^]]|$)/\1[\2]\3/g' \
  > mediawiki5
 
# bold, italic
cat mediawiki5 \
  | sed -r "s/'''''(.*)'''''/\/\/**\1**\/\//g" \
  | sed -r "s/'''/**/g" \
  | sed -r "s/''/\/\//g" \
  > mediawiki6
 
# talks
cat mediawiki6 \
  | sed -r "s/^[ ]*:/>/g" \
  | sed -r "s/>:/>>/g" \
  | sed -r "s/>>:/>>>/g" \
  | sed -r "s/>>>:/>>>>/g" \
  | sed -r "s/>>>>:/>>>>>/g" \
  | sed -r "s/>>>>>:/>>>>>>/g" \
  | sed -r "s/>>>>>>:/>>>>>>>/g" \
  > mediawiki7
 
 # code
cat mediawiki7 \
   | sed -r "s/<code>/\'\'/g" \
   | sed -r "s/<\/code>/\'\'/g" \
  > mediawiki8
 
 # pre
cat mediawiki8 \
   | sed -r "s/<pre>/<code>/g" \
   | sed -r "s/<\/pre>/<\/code>/g" \
  > mediawiki9
 
 # combined bold and italic
cat mediawiki9 \
   | sed -r "s/\*\*\/\//\/\/\*\*/g"\
   > mediawiki10
 
cat mediawiki10 > "$1".dokuwiki

搞来个的解压缩函数,哈哈,够懒的

dotfiles.org 搞来的解压缩,哈哈
###   Handy Extract Program

extract () {
    if [ -f $1 ] ; then
        case $1 in
            *.tar.bz2)   tar xvjf $1   ;;
            *.tar.gz)    tar xvzf $1   ;;
            *.bz2)       bunzip2 $1    ;;
            *.rar)       unrar x $1    ;;
            *.gz)        gunzip $1     ;;
            *.tar)       tar xvf $1    ;;
            *.tbz2)      tar xvjf $1   ;;
            *.tgz)       tar xvzf $1   ;;
            *.zip)       unzip $1      ;;
            *.Z)         uncompress $1 ;;
            *.7z)        7z x $1       ;;
            *)           echo "'$1' cannot be extracted via >extract<" ;;
        esac
    else
        echo "'$1' is not a valid file"
    fi
}

BBC 的天气预报,似乎不太准哦

此为上海三天内的天气Feed
wget -q -O - http://newsrss.bbc.co.uk/weather/forecast/1713/Next3DaysRSS.xml | grep title | sed -e "s/<[^>]*>//g" -e "s/°//g" | egrep "^[A-Z]"
此外,BBC Weather也有提供iFrame嵌入:



List dir tree in bash

This is a simple "tree" command:
find . -type d | sed -e "s/[^-][^\/]*\//  |/g" -e "s/|\([^ ]\)/|-\1/"
Alias it and have fun!!

recode tips

So why bother iconv, dos2unix, unix2dos when we have recode...
#显示所有有效的字符集及其别名
recode -l | less

#转换Windows下的ansi文件到当前的字符集(自动进行回车换行符的转换)
recode windows-1252.. file_to_change.txt

#转换Windows下的ansi文件到当前的字符集
recode utf-8/CRLF.. file_to_change.txt

#转换Latin9(西欧)字符集文件到utf8
recode iso-8859-15..utf8 file_to_change.txt

#Base64编码
recode ../b64 < file.txt > file.b64

#Quoted-printable格式解码
recode /qp.. < file.txt > file.qp

#将文本文件转换成HTML
recode ..HTML < file.txt > file.html

#在字符表中查找某符号(e.g.欧元)
recode -lf windows-1252 | grep euro

#显示字符在latin-9中的字符映射
echo -n 0x80 | recode latin-9/x1..dump

#显示latin-9编码
echo -n 0x20AC | recode ucs-2/x2..latin-9/x

#显示utf-8编码
echo -n 0x20AC | recode ucs-2/x2..utf-8/x

Toying with cal and date

#显示日历
$cal -3

#显示指定月,年的日历
$cal 9 1752

#这个星期五是几号?
$date -d fri

#今年圣诞礼拜几?
$date --date='25 Dec' +%A

#若干秒后的未来时刻时间计算
$date --date '2009-09-27 13:00:00 UTC 360000 seconds'

#显示当前某地区时间时间(可用tzselect寻找时区)
$TZ=':America/Los_Angeles' date

#定时弹对话框
$echo "DISPLAY=$DISPLAY xmessage ALARM!!" | at "NOW + 30 minutes"

Some wget tips

#网页及附件下载
wget -nd -pHEKk [url]

#断点续传
wget -c [url]

#批量下载某类别文件
wget -r -nd -np -l1 -A '*.jpg' [url]

#定时下载
echo 'wget url' | at 01:00

#限制下载速度(32KB/s)
wget --limit-rate=32k [url]

#检查链接有效性
wget -nv --spider --force-html -i bookmarks.html

Some CLI tips note

# 去掉配置文件里的注释
egrep -v '^[[:space:]]*(#|$)'
.or.
sed '/ *#/d; /^ *$/d'

#做http url链接
echo $1 | sed -e h -e 's@/@\\@g' -e G -e 's@\([^\n]*\)\n\(.*\)@\1@'

#Python 自带当前目录的Http Server
python -m SimpleHTTPServer

#寻找所有不可读的文件
find -type f ! -perm -444

#寻找不可访问的目录
find -type d ! -perm -111

#Tar拷贝目录下的所有文件到目录/where/to/并保持文件属性
( cd /copy/from && tar -c . ) | ( cd /copy/to/ && tar -x -p )

#Tar拷贝目录到远程目录并保持文件属性
( tar -c /copy/from ) | ssh -C user@remote 'cd /copy/to/ && tar -x -p'

Dumping svn repository

Dumping a svn is so easy... I made a windows batch:
@echo off  

call :svndump %DATE:~0,10% reponame
goto :EOF  

:svndump
setlocal
echo [%1%] >> log_backup.txt
echo %TIME% dumping [%2%] start >> log_backup.txt

if not exist %1 mkdir %1
"svnadmin.exe" dump Path\To\Svnrep\%2 > %1\%2.dmp
echo dump success, start compressing... >> log_backup.txt
"7z.exe" a %1\%2.dmp.7z %1\%2.dmp
del /F %1\%2.dmp >> log_backup.txt

echo %TIME% dumping [%2%] finished >> log_backup.txt
echo. >> log_backup.txt
endlocal
goto :EOF  
Windows batch... have to work with windows...Batch is not bad with basic flow control and "mimiced" function call, anyway. After equipped with Gnuwin32.

Perform a Sleep() in windows batch

Need a sleep() in Windows Batch? Here are the ways...

1. ping yourself!
SET SLEEP=ping 127.0.0.1 -n
%SLEEP% 11 > nul

2. awesome TIME calculation from here
    @ECHO OFF
    SETLOCAL EnableExtensions
    CALL :ProcDelay 200
    ECHO %TIME%
    GOTO :EOF

    :ProcDelay delayMSec_
    SETLOCAL EnableExtensions
    FOR /f "tokens=1-4 delims=:. " %%h IN ("%TIME%") DO SET start_=%%h%%i%%j%%k
        :_procwaitloop
        FOR /f "tokens=1-4 delims=:. " %%h IN ("%TIME%") DO SET now_=%%h%%i%%j%%k
        SET /a diff_=%now_%-%start_%
    IF %diff_% LSS %1 GOTO _procwaitloop
    ENDLOCAL & GOTO :EOF
    :EOF

3. windows2003 now provide "timeout" command.

Windows IP 切换脚本

闲来无事,帮同事写了个傻傻的脚本,前提是链接名称都是windows 默认.
 cls  
@echo off
color 0B
echo **************************************
echo *IP 地址切换
echo **************************************
set IP=136.172.202.XXX
set MASK=255.255.255.0
set GATEWAY=136.172.202.254
set DNS=178.182.171.60
set INTERFACE=本地连接
set IPO=137.168.99.XX
set MASKO=255.255.255.0
set GATEWAYO=137.168.99.253
set DNSO=137.168.99.253
set INTERFACEO=本地连接
set IP1=137.168.98.XXX
set MASK1=255.255.255.0
set GATEWAY1=137.168.98.254
set DNS1=
set INTERFACE1=无线网络连接
:MENU
echo.
echo 静态IP(内网136.172网段)设置请按 1
echo 动态IP 设置请按 2
echo 静态IP(外网137.168网段)设置请按 3
echo 静态IP(外网137.168无线网段)设置请按 4
echo.
echo 退出请按任意键
echo.
set /p KEY= [请输入您的选择:]
if %KEY% == 1 (goto INNER)
if %KEY% == 2 (goto DHCP)
if %KEY% == 3 (goto OUTTER)
if %KEY% == 4 (goto WLAN)
else goto END
@echo on
:DHCP
echo.
echo 快速设置IP地址和DNS为“自动获得”
echo.
netsh interface ip set address "本地连接" dhcp
netsh interface ip set dns "本地连接" dhcp
echo 动态IP设置成功!
goto END
:INNER
echo.
echo 您选择了办公内网设置。
echo.
echo 正在更改IP......
netsh interface ip set address name="%INTERFACE%" source=static addr=%IP% mask=%MASK%
echo 正在更改网关......
netsh interface ip set address name="%INTERFACE%" gateway=%GATEWAY% gwmetric=1
echo 正在更改DNS......
netsh interface ip set dns name="%INTERFACE%" source=static addr=%DNS%
echo 固定IP配置%IP%成功!
goto END
:OUTTER
echo.
echo 您选择了Internet内网设置。
echo.
echo 正在更改IP......
netsh interface ip set address name="%INTERFACEO%" source=static addr=%IPO% mask=%MASKO%
echo 正在更改网关......
netsh interface ip set address name="%INTERFACEO%" gateway=%GATEWAYO% gwmetric=1
echo 正在更改DNS......
netsh interface ip set dns name="%INTERFACEO%" source=static addr=%DNSO%
echo 固定IP配置%IPO%成功!
goto END
:WLAN
echo.
echo 您选择了Internet无线内网设置。
echo.
echo 正在更改IP......
netsh interface ip set address name="%INTERFACE1%" source=static addr=%IP1% mask=%MASK1%
echo 正在更改网关......
netsh interface ip set address name="%INTERFACE1%" gateway=%GATEWAY1% gwmetric=1
echo 正在更改DNS......
netsh interface ip set dns name="%INTERFACE1%" source=static addr=%DNS1%
echo 固定IP配置%IP1%成功!
goto END
:END
echo.
pause

Simple script displays rss on conky

Fetch, Parse, and Crop RSS for conky need.
#!/bin/bash
# deps:
# curl
# Usage:
# .conkyrc: ${execi [time] /path/to/script/conky-rss.sh URI LINES TITLENUM}
# URI = Location of feed, ex. http://www.gentoo.org/rdf/en/glsa-index.rdf
# LINES = How many titles to display (default 5)
# TITLENUM = How many times the title of the feed itself is specified, usually 1 or 2 (default 2)
#
# Usage Example
# ${color #98c2c7}BBC News Front Page:
# ${color #c4c4c4}${execi 300 ~/conky-rss.sh http://newsrss.bbc.co.uk/rss/newsonline_world_edition/front_page/rss.xml 5 2}

#RSS Setup
uri=$1 #URI of RSS Feed
lines=$2 #Number of headlines
titlenum=$3 #Number of extra titles

#Script start
if [[ "$uri" == "" ]]; then
echo "No URI specified!" >&2
else
#Set defaults if none specified
if [[ $lines == "" ]]; then lines=5 ; fi
if [[ $titlenum == "" ]]; then titlenum=2 ; fi

#The actual work
curl -s --connect-timeout 30 $uri |\
sed -e 's/<\/title>/\n/g' |\
grep -o '<title>.*' |\
sed -e 's/<title>//' |\
head -n $(($lines + $titlenum)) |\
tail -n $(($lines))
fi

ffmpeg为 老款 ipod 转码

新的都支持H264了,网上一大堆信息,我那个老款的只能这样转...

#!/bin/sh

ffmpeg -i "$1" -f mp4 -vcodec libxvid -maxrate 720k -b 720k -qmin 3 -qmax 5 -bufsize 10240 -g 300 -acodec libfaac -ar 44100 -ab 192k -s 320x240 -aspect 4:3 $2

tiny screan capture script using scrot

# !/bin/sh
# take a screenshot with scrot
# usage: kacha
scrot '%Y-%m-%d_%s_scrot.jpg' -e 'mv $f ~/pic/' -d 5 -q 60 -m
ls -lh ~/pic/

批量改文件名

虽然身在windows,但是利用gnuwin32还是有不少好用的cli util可用:
gnuwin32: http://gnuwin32.sourceforge.net/

比如,最近从VeryCD上下片子比较多,要批量去掉文件名中恼人的中文..

夺命岛.Harper's.Island.SXXEXX.Chi_Eng.HDTVrip.720X396-YYeTs人人影视.rmvb 提取:
ls -1 *.rmvb | sed "s/夺命岛.\(.*\).Chi\(.*\)视\(.*\).rmvb$/""&"" ""\1.rmvb""/" | xargs -n 2 mv


英雄.Heroes.SXXEXX.Chi_Eng.HDTVrip.720X396-YYeTs人人影视.avi 提取
ls -1 *.avi | sed "s/英雄.\(.*\).Chi\(.*\)视\(.*\).avi$/""&"" ""\1.avi""/" | xargs -n 2 mv


多好啊!Windows里面也可以玩熟悉的cli工具们!!

Hide windows console app window

For some reason I don't want the console window.
Only three Lines need for the chaotic VBScript:
 DIM objShell  
set objShell=wscript.createObject("wscript.shell")
iReturn=objShell.Run("the.exe", 0, TRUE)
| More

Twitter Updates