Tweaks to get UTF-8 reading/writing correct on Windows.

This commit is contained in:
Michael Mathews 2011-06-19 10:47:24 +01:00
parent d5a0d3a3d9
commit a8cd6b59de
5 changed files with 41 additions and 40 deletions

View File

@ -1,29 +1,7 @@
function readFileSync(filename, encoding, callback) {
if (typeof arguments[1] === 'function') {
encoding = null;
callback = arguments[1];
}
// TODO: support other encodings
var lines = [],
reader,
input,
s;
// see http://download.oracle.com/javase/1,5.0/docs/api/java/nio/charset/Charset.html
try {
reader = new java.io.InputStreamReader(new java.io.FileInputStream(filename), 'UTF-8');
input = new java.io.BufferedReader(reader);
while( (s = input.readLine()) != null) {
lines.push( new java.lang.String(s.getBytes('UTF-8')) );
}
return lines.join('\n');
}
catch (e) {
throw('Cannot read module file '+filename+', '+e);
}
function readFileSync(filename, encoding) {
encoding = encoding || 'utf-8';
return readFile(filename, encoding);
}
function readdirSync(path) {
@ -149,7 +127,7 @@ function copyFile(inFile, outDir, fileName) {
var bos = new Packages.java.io.BufferedOutputStream(new Packages.java.io.FileOutputStream(outFile), 4096);
var theChar;
while ((theChar = bis.read()) != -1) {
bos.write(theChar);
bos.write(theChar);
}
bos.close();
bis.close();
@ -160,26 +138,33 @@ function toFile(path) {
return parts.pop();
}
function write(path, content, encoding) {
var output = new java.io.BufferedWriter(new java.io.FileWriter(path));
function writeFileSync(filename, data, encoding) {
encoding = encoding || 'utf-8';
var out = new Packages.java.io.PrintWriter(
new Packages.java.io.OutputStreamWriter(
new Packages.java.io.FileOutputStream(filename),
encoding
)
);
try {
//FileWriter always assumes default encoding is OK!
output.write( content );
out.write(data);
}
finally {
output.close();
out.flush();
out.close();
}
}
module.exports = {
readFileSync: readFileSync,
writeFileSync: writeFileSync,
readdirSync: readdirSync,
stat: stat,
ls: ls,
mkPath: mkPath,
toDir: toDir,
copyFile: copyFile,
write: write
copyFile: copyFile
};

View File

@ -283,7 +283,7 @@
var path = outdir + '/' + filename,
html = containerTemplate.call(data, data);
fs.write(path, html, 'UTF-8')
fs.writeFileSync(path, html)
}
}

View File

@ -1,7 +1,7 @@
<!DOCTYPE html>
<html lang="en">
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>JSDoc: <?js= title ?></title>
<script src="http://shjs.sourceforge.net/sh_main.min.js"> </script>

View File

@ -1,5 +1,6 @@
/**
* @desc テスト
* @constructor
* @desc Τεκμηρίωση είναι η επικοινωνία!
*/
test = function() {
Test = function() {
};

View File

@ -145,5 +145,20 @@ testFile('test/t/cases/variations.js');
testFile('test/t/cases/versiontag.js');
report();
var os = java.lang.System.getProperty('os.name'),
isWin = !!os.startsWith('Windows');
/** Add codes to display string in color (red) on the console (if OS supports). */
function red(str) {
if (isWin) { return str; }
else { return '\033[031m' + str + '\033[0m'; }
}
/** Add codes to display string in color (red) on the console (if OS supports). */
function green(str) {
if (isWin) { return str; }
else { return '\033[032m' + str + '\033[0m'; }
}
report();