Chinaunix首页 | 论坛 | 博客
  • 博客访问: 591397
  • 博文数量: 40
  • 博客积分: 7274
  • 博客等级: 少将
  • 技术积分: 410
  • 用 户 组: 普通用户
  • 注册时间: 2005-12-20 15:00
个人简介

Expired

文章分类
文章存档

2011年(1)

2008年(3)

2007年(17)

2006年(10)

2005年(9)

分类: LINUX

2008-03-03 21:30:05

默认用 Docbook编译后生成的HTML文件里面的汉字是用 7-bit 的ASCII表示的,查看其源代码时,无法看到原始中文字符,中文字符全都用类似 〹 的字符串表示的。如:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>6.4.  使用 Tex/Latex 进行文档写作 title>
<link rel="stylesheet" href="../css/docbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.69.1">
<link rel="up" href="auxiliary.html" title="第 6 章  教程制作 ">
<link rel="prev" href="vmware.html" title="6.3. VMWare 虚拟机 ">
head>
如何将其还原成中文字符的“真面目”呢?

Perl 提供了两个函数进行数字和字母之间的转换。ord 函数将字母转换成数字,chr 函数将数字转换成字母。特别当处理的 HTML 数据需要被转换并被储存的时候,使用 这些函数使用特别容易。

下面是一个用于转换的脚本(webascii2utf8.pl)


#!/usr/bin/perl

use strict;
use Encode;

if (scalar(@ARGV) < 2) {
    die "\nUsage: $0 \n\n";
}

my $input = $ARGV[0];
my $output = $ARGV[1];

open INPUT,$input or
    die "ERR: Cannot open file: $input to read ! \n";

open WFD,">:utf8", "$output" or
    die "ERR: Cannot open file: $output to write ! \n";

my $found_charset;
my $line_convert =0;
while (<INPUT>) {
    if (! $found_charset and
        /meta.*Content-Type.*charset=(ISO\-8859\-1)/) {
        s/$1/UTF-8/;
        $found_charset = 1;
        Encode::_utf8_on($_);
        print "FOUND Charset, and convert it to UTF-8\n";
        print WFD $_;
        next;
    }
    if (/&#\d+/) {
        ++ $line_convert;
        s/&#(\d+);/chr($1)/ge;
        Encode::_utf8_on($_);
    }
    print WFD $_;
}
close INPUT;
close WFD;
print "Total $line_convert line(s) converted.\n";


使用方式:./webascii2utf8.pl <原始HTML文件> <转换后的HTML文件>

转换后在文章开头的部分将变成如下内容:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>6.4.  使用 Tex/Latex 进行文档写作 title>
<link rel="stylesheet" href="../css/docbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.69.1">
<link rel="up" href="auxiliary.html" title="第 6 章  教程制作 ">
<link rel="prev" href="vmware.html" title="6.3. VMWare 虚拟机 ">
head>

阅读(3006) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~