highlightFile.js 560 B

1234567891011121314151617181920212223242526
  1. var fs = require('fs')
  2. , highlight = require('./highlight');
  3. function isFunction (obj) {
  4. return toString.call(obj) == '[object Function]';
  5. }
  6. module.exports = function highlightFile (fullPath, opts, cb) {
  7. if (isFunction(opts)) {
  8. cb = opts;
  9. opts = { };
  10. }
  11. opts = opts || { };
  12. if (opts.json !== false && fullPath.match(/\.json$/i)) {
  13. opts.json = true;
  14. }
  15. fs.readFile(fullPath, 'utf-8', function (err, code) {
  16. if (err) return cb(err);
  17. try {
  18. cb(null, highlight(code, opts));
  19. } catch (e) {
  20. cb(e);
  21. }
  22. });
  23. };