Skip to content
This repository has been archived by the owner on Sep 16, 2019. It is now read-only.

Commit

Permalink
Support opening many text encoding other than UTF-8 (with jschardet a…
Browse files Browse the repository at this point in the history
…nd iconv-lite)
  • Loading branch information
yhatt committed Jun 3, 2016
1 parent 5b7effd commit 7affae2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
14 changes: 10 additions & 4 deletions coffee/classes/mds_window.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ MdsMenu = require './mds_menu'
MdsFileHistory = require './mds_file_history'
extend = require 'extend'
fs = require 'fs'
jschardet = require 'jschardet'
iconv_lite = require 'iconv-lite'
Path = require 'path'
MdsManager = new clsMdsManager

Expand Down Expand Up @@ -84,15 +86,19 @@ module.exports = class MdsWindow
fs.readFile fname, (err, txt) =>
return if err

{encoding} = jschardet.detect(txt) ? {}
if encoding isnt 'UTF-8' && encoding isnt 'ascii' && iconv_lite.encodingExists(encoding)
buf = iconv_lite.decode(txt, encoding)
else
buf = txt.toString()

MdsFileHistory.push fname
global.marp.mainMenu.setAppMenu()

if mdsWindow? and mdsWindow.isBufferEmpty()
mdsWindow.trigger 'load', txt.toString(), fname
mdsWindow.trigger 'load', buf, fname
else
new MdsWindow
path: fname
buffer: txt.toString()
new MdsWindow { path: fname, buffer: buf }

loadFromFile: (fname) => MdsWindow.loadFromFile fname, @

Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@
"extend": "^3.0.0",
"github-markdown-css": "^2.2.1",
"highlight.js": "^9.1.0",
"iconv-lite": "^0.4.13",
"jquery": "^2.2.0",
"jschardet": "^1.4.1",
"markdown-it": "^6.0.1",
"markdown-it-emoji": "^1.1.0",
"markdown-it-mark": "^2.0.0",
Expand Down

0 comments on commit 7affae2

Please sign in to comment.