Next Previous Contents

3. การใช้ SGML-tools

ผู้ใช้สามารถเช็คไวยกรณ์ที่เขียนในไฟล์ SGML ได้โดย

% sgmlcheck foo.sgml
ในที่นี้จะขอกล่าวถึงการแปลงเอกสารภาษาไทยแบบ SGML ให้เป็นเอกสารแบบอื่นๆ เฉพาะแบบ Plain Text, HTML และ LaTeX เท่านั้น.

3.1 การผลิตเอกสารภาษาไทยแบบ Plain Text

ผู้ใช้สามารถแปลงเอกสารภาษาไทยแบบ SGML ที่เขียนไว้เป็นแบบ Plain Text ได้ด้วยคำสั่ง

% sgml2txt -c latin foo.sgml
ถ้าไม่เกิดข้อผิดพลาดใดๆ ผู้ใช้จะได้ไฟล์ใหม่ที่ชื่อ foo.txt ในไดเรกทอรี่ที่ทำงานอยู่.

3.2 การผลิตเอกสารภาษาไทยแบบ HTML

ผู้ใช้สามารถแปลงเอกสารภาษาไทยแบบ SGML ที่เขียนไว้เป็นแบบ HTML ได้ด้วยคำสั่ง

% sgml2html foo.sgml
ถ้าไม่เกิดข้อผิดพลาดใดๆ ผู้ใช้จะได้ไฟล์ใหม่ที่ชื่อ foo.html และ foo-1.html, foo-2.html, ... ในไดเรกทอรี่ที่ทำงานอยู่. ซึ่ง SGML-tools จะแบ่งเป็นไฟล์ย่อยๆตามหัวข้อ(section)ที่ระบุไว้. ในที่นี้ผู้ใช้ไม่ต้องใส่ option -c latin, เอกสารที่ผลิตออกมาจะเป็นรหัส 8-bit ซึ่งตรงกับรหัส tis-620 ภาษาไทยที่ใช้อยู่บนอินเตอร์เน็ตทั่วไป. เพื่อความสะดวกในการใช้งานยิ่งขึ้น, ผู้เขียนได้เขียน Perl script สำหรับแปลงเอกสาร HTML ที่เขียนในรูปของ &LATIN_CHAR_NAME; ให้เป็นรหัส tis-620 และมี option สั่งให้เรียก cttex มาตัดคำเพื่อความสวยงามด้วย.


#!/usr/bin/perl

################################################################
# sgmlthtml: 
# Run sgml2html and convert latin symbols to Thai characters.
# This program will change, for example, เ to "\340".
#
# by Poonlap Veeratanabutr <poon-v@fedu.uec.ac.jp> 
# $Id: sgmlthtml,v 1.1 1998/09/28 06:51:52 poon-v Exp poon-v $
################################################################


sub error {
    print STDERR "usage: $0 [-c] file.sgml\n";
    print STDERR "       -c , use cttex to fill <WBR>\n";
    exit; 
}    

# command line processing.
if( $#ARGV == -1 ){
    &error;
} elsif ( $#ARGV == 0 && $ARGV[0] ne "-c"){
    $file = $ARGV[0];
    $cut = 0;
} elsif ( $#ARGV == 1 ){
    if( $ARGV[0] eq "-c"){
        $cut = 1;
        $file = $ARGV[1];
    } elsif( $ARGV[1] eq "-c"){
        $cut = 1;
        $file = $ARGV[0];
    } else {
        &error;
    }
} else {
    &error;
}

# run sgml2html
if( system( "sgml2html $file" ) != 0 ){
    exit;
}

# lookup table for what to change and not to change
%lookuptbl = (quot, "quot",amp,"amp","lt","lt","gt","gt",copy,"\251",reg,"\256",
              micro,"\265",Agrave,"\300",Aacute,"\301",Acirc,"\302",Atilde,"\303",
              Auml,"\304",Aring,"\305",AElig,"\306",Ccedil,"\307",Egrave,"\310",
              Eacute,"\311",Ecirc,"\312",Euml,"\313",Igrave,"\314",Iacute,"\315",
              Icirc,"\316",Iduml,"\317",ETH,"\320",eth,"\320",Ntilde,"\321",
              Ograve,"\322",Oacute,"\323",Ocirc,"\324",Otilde,"\325",Ouml,"\326",
              Oslash,"\330",Ugrave,"\331",Uacute,"\332",THORN,"THORN�",Thorn,"Thron",
              szlig,"\337",agrave,"\340",aacute,"\341",acirc,"\342",atilde,"\343",
              auml,"\344",aring,"\345",aelig,"\346",ccedil,"\347",egrave,"\350",
              eacute,"\351",ecirc,"\352",euml,"\353",igrave,"\354",iacute,"\355",
              icirc,"\356",iuml,"\357",eth,"\360",ntilde,"\361",ograve,"\362",
              oacute,"\363",ocirc,"\364",otilde,"\365",ouml,"\366",oslash,"\370",
              ugrave,"\371",uacute,"\372",ucirc,"\373",uuml,"uuml",yacute,"yacute",
              yuml,"yuml");

# separate directory and file name from the given file name
if( $file =~ /(.*)[\/](.+)$/ ){
    $dir = $1;
    $file = $2;
}
$file =~ s/(.+)\.sgml/$1/; # get rootname

if( length( $dir ) == 0 ){
    opendir( DIR, ".");
} else {
    opendir( DIR, "$dir" );
}
@html = grep {/$file.*\.html$/} readdir( DIR ); # get all related html files
closedir( DIR );

foreach $html_file (@html) {
    print "Processing file $html_file\n";
    open( INPUT, "$html_file");
    open( OUTPUT,">$html_file.tmp" );    
    
    while( <INPUT> ){
        $line = $_;
        $beg = index( $line, "&");
        $end = index( $line, ";");
        while( $beg >= 0  && $end > $beg  ){
            $target = substr( $line, $beg+1, $end-$beg-1);
            $thai = $lookuptbl{ $target };
            if( $target eq $thai ){
                $line = sprintf( "%s%s%s", substr( $line, 0, $beg), '&' . $thai . ';', 
                                substr( $line, $end+1 ));
                
            } else {
                $line = sprintf( "%s%s%s", substr( $line, 0, $beg), $thai, 
                                substr( $line, $end+1 ));
            }
            $beg = index( $line, "&", $beg+1);
            $end = index( $line, ";", $beg);
        }
        print OUTPUT "$line";
    }
    close( INPUT );
    close( OUTPUT );
    if( $cut == 1 ){
        system( "cttex 0 < $html_file.tmp > $html_file" );
        system( "rm -f $html_file.tmp" );
    } else {
        rename( "$html_file.tmp", "$html_file");
    }
}

# EOF

ผู้ใช้สามารถเรียกคำสั่ง sgmlthtml แทน sgml2html สำหรับเอกสาร SGML ที่มีภาษาไทยได้ดังนี้

% sgmlthtml foo.sgml -c 
จากตัวอย่างดังกล่าว, sgmlthtml จะเรียกคำสั่ง sgml2html ซึ่งจะผลิตไฟล์แบบ HTML หลายไฟล์. จากนั้นโปรแกรม sgmlthtml จะพยายามเปลี่ยน &LATIN_CHAR_NAME; ให้เป็นรหัส tis-620 (ถ้ามี, โดยปรกติ sgml2html จะผลิตไฟล์ที่เผอิญตรงตาม tis-620 อยู่แล้ว) และเรียก cttex มาตัดคำ(ทุกไฟล์ที่เกี่ยวข้องกับ foo.sgml) ให้การแสดงผลบน browser สวยงามขึ้น. การที่จะใช้คำสั่งนี้, ผู้ใช้ต้องมีโปรแกรม cttex อยู่ในระบบด้วย. หากไม่ต้องการให้ cttex ตัดคำ, ไม่ต้องใส่ option -c ดังตัวอย่าง
% sgmlthtml foo.sgml

3.3 การผลิตเอกสารภาษาไทยแบบ LaTeX

ผู้ใช้ต้องมีชุดโปรแกรม LaTeX และติดตั้งภาษาไทยเรียบร้อยแล้วจึงจะสามารถแปลงเอกสารจาก SGML เป็น LaTeX ได้. การติดตั้งภาษาไทยใน LaTeX สามารถอ่านได้จากเอกสาร " การใช้ภาษาไทยกับ LaTeX".

ผู้ใช้สามารถผลิตเอกสาร LaTeX(.tex) ที่มีภาษาไทยจากเอกสารแบบ SGML ได้โดยคำสั่ง

% sgml2latex --output=tex foo.sgml

จากคำสั่งข้างบน sgml2latex จะสร้างไฟล์ foo.tex ในไดเรกทอรี่ที่ทำงานอยู่. ผู้ใช้ยังไม่สามารถนำไฟล์นี้ไปใช้กับ LaTeX ได้โดยตรง, จะต้องแก้ส่วน preamble ของไฟล์นี้เล็กน้อยก่อนนำไปใช้กับ cttex และ latex ต่อไป.

ผู้ใช้ต้องลบบรรทัดต่อไปนี้ออกจาก preamble ของไฟล์ foo.tex

\usepackage[latin1]{inputenc}
\usepackage{t1enc}
\usepackage{babel}
และใส่บรรทัดต่อไปนี้แทนลงไปแทน
\usepackage{thai}
จากนั้นจึงสั่งคำสั่ง cttex และ latex ตามลำดับดังตัวอย่าง
% cttex < foo.tex > foo_ok.tex
% latex foo_ok.tex

จะเห็นได้ว่าเนื่องจาก SGML-tools ไม่ได้ผลิตมาเพื่อใช้กับภาษาไทยโดยเฉพาะจึงใช้ยุ่งยาก. ผู้เขียนพยายามลดขั้นตอนเหล่านี้โดยเขียน Perl script ชื่อ sgmltlatex ทำหน้าที่ต่างๆเหล่านี้โดยอัตโนมัติ.


#!/usr/bin/perl

###############################################################################
#  sgmltlatex:
#  Processing SGML or LaTeX file for Thai language.
#  
#
#  by Poonlap Veeratanabutr <poon-v@fedu.uec.ac.jp>
#  $Id: sgmltools.sgml,v 1.1 1998/09/28 11:26:44 poon-v Exp poon-v $
###############################################################################
sub error {
    print STDERR "$0 - Processing SGML or LaTeX file for Thai language.\n";
    print STDERR "usage: $0 [-[t|d|p] sgml_file] [-l latex_file] \n";
    print STDERR "       -t , create latex_file file from sgml_file\n";
    print STDERR "       -d , create dvi file from sgml_file and leave latex_file\n";
    print STDERR "       -p , create Postscript file and leave latex_file\n";
    print STDERR "       -l , run latex on latex_file\n";
    exit( -1 );
}

# command line processing
if( $#ARGV != 1 ){
    &error;
} elsif ( $ARGV[0] eq "-l" ){
    $option = "l";
} elsif ( $ARGV[0] eq "-t" ){
    $option = "t";
} elsif ( $ARGV[0] eq "-d" ){
    $option = "d";
} elsif ( $ARGV[0] eq "-p" ){
    $option = "p";
} else {
    &error;
}
$file = $ARGV[1];
$tempfile = "$file" . ".tmp";

if( $option ne "l" ){
    if( system( "sgml2latex --output=tex $file" ) != 0 ){
        exit( -1 );
    }
    
# separate directory and file name from the given file name
    if( $file =~ /(.*)[\/](.+)$/ ){
        $dir = $1;
        $file = $2;
    }
    
    if( $file =~ m/(.+)\.sgml$/ ){
        $rootname = "$1";
    } else {
        $rootname = $file;
    }
    $file = $rootname . ".tex";
    
    print "Processing file $file\n";

    open( INPUT, "$file" );
    open( OUTPUT, ">" . "$tempfile" );
    $preamble = 1;
    while( <INPUT> ){
        if( $preamble == 1 ){
            if( /^\\begin\s*{\s*document\s*}\s*$/ ){
                print OUTPUT "\\usepackage{thai}\n";
                $preamble = 0;
                break;
            }
            s/^\\usepackage.*{\s*inputenc.*}\n//;
            s/^\\usepackage.*{\s*babel.*}\n//;
            s/^\\usepackage.*{\s*t1enc.*}\n//;
            print OUTPUT;
            
        } else {
            print OUTPUT;
        }
    }
    close( INPUT );
    close( OUTPUT );

    
} else {
    unless( $file =~ m/(.+)\.tex$/ ){
        print STDERR "$0 needs .tex file\n";
        &error;
    }
    rename( "$file", "$tempfile" );
}


if( $option eq "t" ){
    rename( "$tempfile", "$file" );
    exit( 0 );
} else {
    system( "cttex < $tempfile > $file" );
    system( "latex $file" );
    rename( "$tempfile", "$file" );
} 

if( $option eq "p" ){
    $psfile = "$rootname" . ".ps";
    $dvifile = "$rootname" . ".dvi";
    system( "dvips -o $psfile $dvifile" );
}

exit( 0 );

#EOF
    

ผู้ใช้สามารถเรียกคำสั่ง sgmltlatex แทน sgml2latex สำหรับเอกสาร SGML ที่มีภาษาไทย. ถ้าต้องการผลิตเอกสารแบบ .dvi,

% sgmltlatex -d foo.sgml
หากไม่มีข้อผิดพลาดเกิดขึ้น, จะได้ foo.dvi ในไดเรกทอรี่ที่ทำงานอยู่. ถ้าต้องการผลิตไฟล์แบบ Postscript ให้สั่งคำสั่ง
% sgmltlatex -p foo.sgml
หรือถ้าต้องการผลิตเอกสารแบบ LaTeX (.tex) ให้ใช้คำสั่ง
% sgmltlatex -t foo.sgml
ในกรณีที่ใช้ option "d" หรือ "p" หรือ "t" จะได้ไฟล์แบบ LaTeX (.tex) ด้วย. ผู้ใช้สามารถแก้ไขเนื้อหาใน foo.tex แล้วสั่งคำสั่งดังนี้
% sgmltlatex -l foo.tex
คำสั่งนี้จะเรียก cttex มาตัดคำและส่งต่อให้ latex จัดการ. หลังจากนั้น sgmltlatex จะบันทึกไฟล์ foo.tex ในรูปที่ผู้ใช้สามารถแก้ไขได้อีก. ซึ่งหมายความว่าผู้ใช้ไม่มีความจำเป็นต้องสั่ง cttex และ latex พร้อมกันอีก.

เนื่องจากเอกสารแบบ LaTeX ที่ผลิตโดย SGML-tools ต้องการ linuxdoc-sgml.sty, qwertz.sty, url.sty, epsfig.sty และ null.sty ในการผลิตไฟล์ .dvi ผู้ใช้ต้อง configure โปรแกรม latex ให้รู้ว่าไฟล์เหล่านี้อยู่ที่ไหนด้วย. วิธีที่ง่ายที่สุดคือก็อปปี้ไฟล์เหล่านี้ซึ่งในกรณีของผู้เขียนจะอยู่ใต้ไดเรกทอรี่ /usr/local/lib/sgml-tools/ มาไว้ที่ไดเรกทอรี่ที่มีไฟล์ .tex อยู่หรือทำ symbolic link ก็ได้.


Next Previous Contents