summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--docs/variables.md1
-rw-r--r--lib/page/index.js11
-rw-r--r--package.json4
-rw-r--r--test/page.js16
4 files changed, 29 insertions, 3 deletions
diff --git a/docs/variables.md b/docs/variables.md
index fb85463..aaa9601 100644
--- a/docs/variables.md
+++ b/docs/variables.md
@@ -41,6 +41,7 @@ The following is a reference of the available data during book's parsing and the
| `page.title` | Title of the page |
| `page.previous` | Previous page in the Table of Contents (can be `null`) |
| `page.next` | Next page in the Table of Contents (can be `null`) |
+| `page.dir` | Text direction, based on configuration or detected from content (`rtl` or `ltr`) |
#### Table of Contents Variables
diff --git a/lib/page/index.js b/lib/page/index.js
index bc12529..d8b5fff 100644
--- a/lib/page/index.js
+++ b/lib/page/index.js
@@ -1,6 +1,7 @@
var _ = require('lodash');
var path = require('path');
var parsers = require('gitbook-parsers');
+var direction = require('direction');
var error = require('../utils/error');
var pathUtil = require('../utils/path');
@@ -101,6 +102,13 @@ Page.prototype.getContext = function() {
var next = article? article.next() : null;
var prev = article? article.prev() : null;
+ // Detect text direction in this page
+ var dir = this.book.config.get('direction');
+ if (!dir) {
+ dir = direction(this.content);
+ if (dir == 'neutral') dir = null;
+ }
+
return {
file: {
path: this.path,
@@ -112,7 +120,8 @@ Page.prototype.getContext = function() {
next: next? next.getContext() : null,
previous: prev? prev.getContext() : null,
level: article? article.level : null,
- content: this.content
+ content: this.content,
+ dir: dir
}
};
};
diff --git a/package.json b/package.json
index 5fceb40..da6c1a9 100644
--- a/package.json
+++ b/package.json
@@ -20,7 +20,6 @@
"nunjucks": "2.3.0",
"nunjucks-autoescape": "1.0.0",
"nunjucks-filter": "1.0.0",
- "i18n": "0.5.0",
"semver": "5.0.1",
"npmi": "0.1.1",
"cheerio": "0.19.0",
@@ -47,7 +46,8 @@
"deprecated": "0.0.1",
"rmdir": "1.2.0",
"cp": "0.2.0",
- "cpr": "1.0.0"
+ "cpr": "1.0.0",
+ "direction": "0.1.5"
},
"devDependencies": {
"eslint": "1.5.0",
diff --git a/test/page.js b/test/page.js
index 52d86d8..9afec1c 100644
--- a/test/page.js
+++ b/test/page.js
@@ -29,6 +29,8 @@ describe('Page', function() {
'variables/page/title.md': '{{ page.title }}',
'variables/page/previous.md': '{{ page.previous.title }} {{ page.previous.path }}',
'variables/page/next.md': '{{ page.next.title }} {{ page.next.path }}',
+ 'variables/page/dir/ltr.md': 'This is english: {{ page.dir }}',
+ 'variables/page/dir/rtl.md': 'بسيطة {{ page.dir }}',
'GLOSSARY.md': '# Glossary\n\n\n## abracadabra\n\nthis is the description'
}, [
@@ -312,6 +314,20 @@ describe('Page', function() {
return page.toHTML(output)
.should.be.fulfilledWith('<p>Test Variables variables/page/title.md</p>\n');
});
+
+ describe('page.dir', function() {
+ it('should detect ltr', function() {
+ var page = book.addPage('variables/page/dir/ltr.md');
+ return page.toHTML(output)
+ .should.be.fulfilledWith('<p>This is english: ltr</p>\n');
+ });
+
+ it('should detect rtl', function() {
+ var page = book.addPage('variables/page/dir/rtl.md');
+ return page.toHTML(output)
+ .should.be.fulfilledWith('<p>&#x628;&#x633;&#x64A;&#x637;&#x629; rtl</p>\n');
+ });
+ });
});
describe('Annotations / Glossary', function() {