忍者ブログ
Yaleで、遊んで学ぶ日々。

Yaleで、遊んで学ぶ日々。

囲碁、ときどきプログラミング、ところにより経済。
[117]  [116]  [115]  [114]  [113]  [112]  [111]  [110]  [109]  [108]  [107
×

[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。

Windows用のTextTreeというフリーソフトを愛用していました。
ルートディレクトリを定めてやると、そこ以下のテキストファイルをツリー上に表示してくれて、閲覧および編集ができるというソフトです。
僕は研究上のアイデアを書き留めたり、プログラミングのtipsをメモするなどの用途で使っていました。
使った感じはAUTLAなどのアウトラインプロセッサとほぼ同じです。
シンプルで動きが軽快なのでTextTreeの方が好きでした。


Linuxでは、gjots2というソフトがTextTreeと同様の機能を提供してくれます。
gjots2はアウトラインプロセッサで、UbuntuならSynaptic Package Managerからインストールできます。
今日はTextTreeからgjots2への移行を行いました。


2つのソフトウェアの大きな相違点は、TextTreeがディレクトリの階層構造を解釈しているのに対し、gjots2(及びその他のアウトラインプロセッサ)は内部に階層構造を持った1つのテキストファイルに情報を保存しているということです。
TextTreeでは、各記事の情報は別々のファイルに保存されています。したがって、他のテキストエディタで個別のファイルを閲覧、編集することも可能です。ツリーの構造には、ディレクトリ構造がそのまま反映されます。
gjots2の場合、記事の中身からツリーの構造までのすべての情報は1つのテキストファイルに保存されます。このファイルを他のエディタで開くことも可能ですが、ちょっとしたプログラム言語調で書かれているのでぱっと見ではツリー構造までは分かりません。下手に編集するとツリー構造を壊す恐れもあるので注意が必要です。興味のある人は開いてみるとよいと思います。


そういうわけで、移行するためにはTextTreeで使っていたディレクトリの構造を、gjots2で使われている「文法」で書き換える必要があります。その作業はシェルスクリプトで行いました。
TextTreeを使っていたけどLinuxを始めて代わりにgjots2を使い始めた人にしか必要がないスクリプトので、需要はほぼゼロだと思いますが、こういう作業はやはりプログラムでやる方がイレギュラーなミスが出ないので良いと思います。バグは法則性を持っているのでランダムなミスより発見が容易です。


TextTreeは日本語の文字コードにShift-JISを使用していますが、gjots2ではこれが文字化けしてしまうので、UTF-8に変換する必要があります。
そのために nkf というソフトウェアを使っています。
これもSynaptic Package Managerからインストールできます。



以下、スクリプト。

ttree2gjots.sh

#!/bin/sh

# help switching from TextTree (http://www.vector.co.jp/soft/winnt/personal/se366853.html) to gjots2.
# Specify the root directory for TextTree, and gjots2 file is out.
# can specify extensions.  quote by " " for multiple.
# default is "txt".
# extension is case sensitive.
# option -d: directories appear before files.
# otherwise, files come above.
# to save into a file, use " > filepath " 

# depends on.
#   nkf

# systax.
#   ttree2gjots.sh [-d] <ttree root> [extensions (can be multiple)]

# example
#   ttree2gjots.sh root/ "txt csv TXT CSV" > ttree.gjots

# author. Kohta Mori
# date. 05/22/2010

######



# get options
d=N
while getopts d option
do
    case "$option" in
    d)
        d=Y
        shift  # shift up argument index to fill the part for the option
        ;;
    esac   
done




# check existence of the root directory
if [ -d "$1" ]
then
    echo #"directory $1 found"
    # do nothing
else
    echo "directory $1 not found"
    exit
fi


# set extensions
if [ $# -eq 2 ]
then
    ext="$2"
else
    ext="txt"
fi




# functions ---------------------------- #
d_routine()
{
    # method to handle all directories within a directory
    # $1: (parent) directory name
   
    for dr in "$1"/*/  # for all sub-directories
    do
        if [ -d "$dr" ]
        then
            # this if clause if necessary
            # to deal with empty folders
           
            echo \\NewEntry
           
            # remove "/" at the end
            l=${#dr}
            t=`expr $l - 1`
            tmp=`echo $dr | cut -c 1-$t`
            echo ${tmp##*/}
           
            echo \\NewFolder   
           
            Folder "$dr"
           
            echo \\EndFolder
        fi
    done

}


f_routine()
{
    # method to handle all files within a directory
    # $1: (parent) directory name
   
    for e in $ext
    do
        for f in "$1"/*.$e
        do
            if [ -f "$f" ]
            then
                fname=${f##*/}  # remove parts before /
                fname=${fname%.*} # remove extension
               
                echo \\NewEntry
                echo "$fname"
                echo "  "
               
                # the contents of the file
                nkf -w "$f" 
                # this reads and outputs the contents of the file
                # in UTF-8.
                echo " "
                # just to add line break at the end of each file
                # error occurs when .... \EndFolder
               
                # end of the file                   
               
            fi
        done
    done   

}



Folder()
{
    # the sub-routine for a folder
    # $1: the path (relative to the current directory) of the folder
   
    # for debugging (to avoid infinite loops) #
    #if [ -d "$1" ]
    #then
    #    echo
    #else
    #    echo "directory $1 not found"
    #    exit
    #fi
    # --------------------------------------- #
 
   
    if [ "$d" = Y ]
    then
       # directories first
       d_routine "$1"
       f_routine "$1"
    else
        # files first
       f_routine "$1"
       d_routine "$1"
    fi
   
   

   
}

# end of functions ---------------------- #



# let's get started! #
Folder "$1"










PR
この記事にコメントする
お名前:
タイトル:
文字色:
メールアドレス:
URL:
コメント:
パスワード:   Vodafone絵文字 i-mode絵文字 Ezweb絵文字
この記事へのトラックバック
この記事にトラックバックする:
Calender
03 2024/04 05
S M T W T F S
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
Search in This Blog
Latest Comments
[03/30 川内のばば山田]
[03/30 川内のばば山田]
[08/06 Aterarie]
[07/05 Agazoger]
[07/01 Thomaskina]
Oldest Posts
Latest Trackbacks
フリーエリア

Barcode
Access Analysis
Powerd by NINJAブログ / Designed by SUSH
Copyright © Yaleで、遊んで学ぶ日々。 All Rights Reserved.
忍者ブログ [PR]