Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,12 @@ $ book sm --help

**Notes**:
* The article title is taken from `title` property in the articles front-matter. If this property is not available, the articles filename will be used as title for the summary.
* The option `-s` or `--sortedBy` can not be given `-` as argument, because commander.js will parse it an option. But you can set it in `book.json` as follows.

* The option `-s` or `--sortedBy` can not be given `-` as argument, because commander.js will parse it an option. But you can set it in `book.json` as follows.
* set up the sortedBy and if there are any summaries missing the order, look at the example below and follow,
for example, you have summaries like this `01-elementry-school, 02-middle-school, 03-university, ...`
you realized high school was missing, then You can make correct order in the following way
eg. `01-elementry-school, 02-middle-school, 02a-high-school, 03-university, ...`
not `01-elementry-school, 02-middle-school, 03-high-school, 04-university, ...`

2> Create a `book.json` in the book`s root folder

Expand Down
103 changes: 55 additions & 48 deletions lib/files.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,43 @@
var _ = require('lodash');
var fs = require('fs');
var path = require('path');
var async = require('async');
var color = require('bash-color');
var fm = require('front-matter');

// separate for test
var sort = function(current, next, sortedBy) {
if (current.isDirectory && !next.isDirectory) return -1;
if (!current.isDirectory && next.isDirectory) return 1;
// Sorted if given current sorted hyphen, for example: `-` or `_`
if (sortedBy) {
var pattern = "(^\\d*)" + sortedBy;
var reg = new RegExp(pattern);
if(current.name.match(reg) && next.name.match(reg)){
var currentNum = current.name.match(reg)[1];
var nextNum = next.name.match(reg)[1];
return currentNum - nextNum;
}
}
return current.name.localeCompare(next.name);
};

// Use a loop to read all files
function ReadFile(filePath, filesJson, sortedBy) {
var files;

try {
// Synchronous readdir
files = fs.readdirSync(filePath)
// sort the files: directories first, afterwards files
.map(function(v) {
var stat = fs.statSync(path.resolve(filePath, v));
return {
name: v,
isDirectory: stat.isDirectory()
};
})
.sort(function(a, b) {
if (a.isDirectory && !b.isDirectory) return -1;
if (!a.isDirectory && b.isDirectory) return 1;
// Sorted if given a sorted hyphen, for example: `-` or `_`
if (sortedBy) {
var pattern = "(^[\\da-zA-Z]*)" + sortedBy;
var reg = RegExp(pattern);
if(a.name.match(reg) && b.name.match(reg)){
var aNum = a.name.match(reg)[1];
var bNum = b.name.match(reg)[1];
return aNum - bNum;
}
}
return a.name.localeCompare(b.name);
})
.map(function(v) {
return v.name;
});
function getFrontMatterTitle(newpath)
{
// default to use filename
var title = path.parse(newpath).name;
var content = fs.readFileSync(newpath,'utf8');

files.forEach(walk);
} catch (error) {
filesJson = null; //fixme
console.log(color.red(error.message));
if (!fm.test(content)) return title; // skip if no front matter

var frontMatter = fm(content);
if (typeof frontMatter.attributes.title === "undefined") return title; // skip if no 'title' attributes
// todo: set 'title' via config

return frontMatter.attributes.title;
}

function walk(file) {
Expand All @@ -51,7 +46,7 @@ function ReadFile(filePath, filesJson, sortedBy) {

if (state.isDirectory()) {
filesJson[file] = {};
ReadFile(newpath, filesJson[file], sortedBy);
new ReadFile(newpath, filesJson[file], sortedBy);
// filter empty directories
if (Object.keys(filesJson[file]).length < 1) {
delete filesJson[file];
Expand All @@ -66,20 +61,32 @@ function ReadFile(filePath, filesJson, sortedBy) {
}
}

function getFrontMatterTitle(newpath)
{
// default to use filename
var title = path.parse(newpath).name;
var content = fs.readFileSync(newpath,'utf8');

if (!fm.test(content)) return title; // skip if no front matter

var frontMatter = fm(content);
if (typeof frontMatter.attributes.title === "undefined") return title; // skip if no 'title' attributes
// todo: set 'title' via config
try {
// Synchronous readdir
files = fs.readdirSync(filePath)
// sort the files: directories first, afterwards files
.map(function(v) {
var stat = fs.statSync(path.resolve(filePath, v));
return {
name: v,
isDirectory: stat.isDirectory()
};
})
.sort(function(current, next) {
return sort(current, next, sortedBy);
})
.map(function(v) {
return v.name;
});

return frontMatter.attributes.title;
files.forEach(walk);
} catch (error) {
filesJson = null; //fixme
console.log(color.red(error.message));
}
}

module.exports = ReadFile;
module.exports = {
readFile: ReadFile,
sort: sort
};
2 changes: 1 addition & 1 deletion lib/summary/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var async = require('async');

var bookJson = require('../bookJson');
var utils = require('../utils');
var readFile = require('../files');
var readFile = require('../files').readFile;

/**
* Get a summary from a fold such as `/path/to/your/book` or `../book`
Expand Down
29 changes: 29 additions & 0 deletions test/files.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
var should = require('should');
var sort = require('../lib/files').sort;

describe('files.js', function () {
var DirClass = function(name) {
this.name = name;
this.isDirectory = true;
};

it('test sort with alphabet', function () {
var order = [new DirClass('01-a'), new DirClass('02-a'), new DirClass('03a-a'), new DirClass('03-a'), new DirClass('04-a')];
var sorted = order.sort(function(current, next) {
return sort(current, next, '-');
});

should.deepEqual(sorted,
[new DirClass('01-a'), new DirClass('02-a'), new DirClass('03-a'), new DirClass('03a-a'), new DirClass('04-a')]);
});

it('test sort only digit', function () {
var order = [new DirClass('01-a'), new DirClass('2-a'), new DirClass('7-a'), new DirClass('10-a'), new DirClass('04-a')];
var sorted = order.sort(function(current, next) {
return sort(current, next, '-');
});

should.deepEqual(sorted,
[new DirClass('01-a'), new DirClass('2-a'), new DirClass('04-a'), new DirClass('7-a'), new DirClass('10-a')]);
});
});