Commit a951eb24 authored by Heddi Heryadi's avatar Heddi Heryadi 💪

Initial commit

parents
node_modules
contrib
quran
==========
node.js interface to Holy Quran. See the javascript and websql versions at http://qzaidi.github.io/quran.
Installation
------------
npm install quran
Usage
-----
var quran = require('quran');
Fetch the first verse of second chapter
```
quran.get(2,1,function(err,verse) {
if (!err) {
console.log('Verse 1: Chapter 1: ' + verse.arabic);
}
});
```
Fetch the first chapter
```
quran.get(1,function(err,verses) {
if (!err) {
console.log('Chapter 1: ' + verses.join(','));
}
});
```
verses is an array, so you can join them in the above.
.get is simply a wrapper on select, so you can directly invoke it
and do advanced filtering, like getting verse 2-4 of first chapter.
```
quran.select({ chapter: 1}, { offset: 1, limit: 3}, function(err,verses) {
if (!err) {
console.log(verses);
}
});
[
{ chapter: 1,
verse: 1,
ar: 'بِسْمِ ٱللَّهِ ٱلرَّحْمَٰنِ ٱلرَّحِيمِ' },
{ chapter: 1,
verse: 2,
ar: 'ٱلْحَمْدُ لِلَّهِ رَبِّ ٱلْعَٰلَمِينَ' },
{ chapter: 1, verse: 3, ar: 'ٱلرَّحْمَٰنِ ٱلرَّحِيمِ' }
]
```
verses is an array of objects, and has additional info (chapter number, verse number).
The second argument to select is optional.
If you want to fetch multiple verses, not necessarily in sequence, use an array to specify this.
```
quran.select({ chapter: 1, verse: [ 2, 4, 6 ]}, function(err,verses) {
if (!err) {
console.log(verses);
}
});
[ { chapter: 1,
verse: 2,
ar: 'ٱلْحَمْدُ لِلَّهِ رَبِّ ٱلْعَٰلَمِينَ'
},
{
chapter: 1,
verse: 4,
ar: 'مَٰلِكِ يَوْمِ ٱلدِّينِ'
},
{ chapter: 1,
verse: 6,
ar: 'ٱهْدِنَا ٱلصِّرَٰطَ ٱلْمُسْتَقِيمَ'
}
]
```
Currently, only arabic text and english, hindi and urdu translations are supported, to limit package size.
You can however, easily add translations to this package. Open an issue if you want to.
To fetch both the arabic text and translation, set the language option to en.
```
quran.select({ chapter: 1}, { offset: 1, limit: 3, language: 'en'}, function(err,verses) {
if (!err) {
console.log(verses);
}
});
```
Similarly for hindi
```
Code:
quran.select({ chapter: 1}, { offset: 1, limit: 3, language: 'hi'}, function(err,verses) {
if (!err) {
console.log(verses);
}
});
Output
[
{ chapter: 1,
verse: 1,
ar: 'بِسْمِ ٱللَّهِ ٱلرَّحْمَٰنِ ٱلرَّحِيمِ',
hi: 'अल्लाह के नाम से जो रहमान व रहीम है।',
translator: 'hindi' },
{ chapter: 1,
verse: 2,
ar: 'ٱلْحَمْدُ لِلَّهِ رَبِّ ٱلْعَٰلَمِينَ',
hi: 'तारीफ़ अल्लाह ही के लिये है जो तमाम क़ायनात का रब है।',
translator: 'hindi' },
{ chapter: 1,
verse: 3,
ar: 'ٱلرَّحْمَٰنِ ٱلرَّحِيمِ',
hi: 'रहमान और रहीम है।',
translator: 'hindi' }
]
```
Want multiple translations at once? Use an array when specifying language
```
quran.select({ chapter: 1, verse: [ 2, 4, 6 ]},
{ language: ['ur', 'en ] }, function(err,verses) {
if (!err) {
console.log(verses);
}
});
[ { chapter: 1,
verse: 2,
ar: 'ٱلْحَمْدُ لِلَّهِ رَبِّ ٱلْعَٰلَمِينَ',
en: 'All praise is due to Allah, the Lord of the Worlds.',
ur: 'ساری تعریف اللہ کے لئے ہے جو عالمین کا پالنے والا ہے'
},
{ chapter: 1,
verse: 4,
ar: 'مَٰلِكِ يَوْمِ ٱلدِّينِ',
en: 'Master of the Day of Judgment.',
ur: 'روزِقیامت کا مالک و مختار ہے'
},
{ chapter: 1,
verse: 6,
ar: 'ٱهْدِنَا ٱلصِّرَٰطَ ٱلْمُسْتَقِيمَ',
en: 'Keep us on the right path.',
ur: 'ہمیں سیدھے راستہ کی ہدایت فرماتا رہ'
}
]
```
You can also fetch meta data about a chapter
```
quran.chapter(1,function(err,info) {
if (!err) {
console.log(info);
}
});
```
Or all the chapters, by omitting the optional argument
```
quran.chapter(function(err,info) {
if (!err) {
console.log(info);
}
});
```
To access by juz, for example, first 10 verses of juz 28, use this pattern
```
quran.juz(28,function(err,j) {
console.log(err || j[0]);
if (!err) {
quran.select({ chapter: j[0].surah }, { offset: j[0].ayah-1, limit: 10 }, function(err,verses) {
console.log(err || verses);
});
}
});
```
To search in a translation, use the search API
```
quran.search('en','islam',function(err,verses) {
// verses is an array of verses matching 'islam'
});
```
TODO
----
The npm module stores quran db as a sqlite database and exposes it via an API. With webSQL, it should be possible to do the same for a pure javascript application, without requiring a server side component. That's the idea behind [Quran browser](http://qzaidi.github.io/quran), and it might be interesting to make a websql version available which supports the same API in browser.
Credits
-------
This work is based on Quran Text and Translations made available by http://tanzil.net.
Sites using this package
------------------------
http://duas.mobi/quran.
To add yours, submit a pull request.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
Quran SQLite3 DB
File added
"use strict";
var quran = require('./quran');
module.exports = quran;
{
"_args": [
[
{
"raw": "quran",
"scope": null,
"escapedName": "quran",
"name": "quran",
"rawSpec": "",
"spec": "latest",
"type": "tag"
},
"J:\\Quran\\qurandb"
]
],
"_from": "quran@latest",
"_id": "quran@0.0.8",
"_inCache": true,
"_location": "/quran",
"_nodeVersion": "2.0.1",
"_npmUser": {
"name": "qzaidi",
"email": "qasim@zaidi.me"
},
"_npmVersion": "2.9.0",
"_phantomChildren": {},
"_requested": {
"raw": "quran",
"scope": null,
"escapedName": "quran",
"name": "quran",
"rawSpec": "",
"spec": "latest",
"type": "tag"
},
"_requiredBy": [
"#USER"
],
"_resolved": "https://registry.npmjs.org/quran/-/quran-0.0.8.tgz",
"_shasum": "953b4918db3fbd332718634768014fc8a631351f",
"_shrinkwrap": null,
"_spec": "quran",
"_where": "J:\\Quran\\qurandb",
"author": {
"name": "Qasim Zaidi",
"email": "qasim@zaidi.me"
},
"bugs": {
"url": "http://github.com/qzaidi/quran/issues"
},
"dependencies": {
"sqlite3": "*"
},
"description": "npm module for Al-Quran (tanzil)",
"devDependencies": {},
"directories": {},
"dist": {
"shasum": "953b4918db3fbd332718634768014fc8a631351f",
"tarball": "https://registry.npmjs.org/quran/-/quran-0.0.8.tgz"
},
"gitHead": "1f425c41a8c91510881230b18e1da7a8c6174a29",
"homepage": "https://github.com/qzaidi/quran#readme",
"keywords": [
"islam",
"quran",
"allah",
"muslim",
"koran",
"hindi",
"urdu"
],
"license": "LGPL",
"main": "index.js",
"maintainers": [
{
"name": "qzaidi",
"email": "qasim@zaidi.me"
}
],
"name": "quran",
"optionalDependencies": {},
"readme": "ERROR: No README data found!",
"repository": {
"type": "git",
"url": "git://github.com/qzaidi/quran.git"
},
"scripts": {
"prepublish": "sh scripts/install",
"test": "echo \"Error: no test specified\" && exit 1"
},
"version": "0.0.8"
}
"use strict";
var sqlite3 = require('sqlite3').verbose();
var dbopts = {
'r': sqlite3.OPEN_READONLY,
'w': sqlite3.OPEN_READWRITE
};
module.exports = function(dbfile,opt) {
var db;
var options = dbopts[opt || 'r'];
var file = __dirname + '/../data/' + dbfile;
db = new sqlite3.Database(file, options);
return db;
}
"use strict";
var util = require('util');
var model = require('./db');
var qurandb = model('qurandb','r');
var quran = {
get: function(chapter,verse,callback) {
if (!callback && typeof (verse) === 'function') {
callback = verse;
verse = undefined;
}
this.select({ chapter: chapter, verse: verse }, function(err,res) {
var verses;
if (!err && res.length) {
verses = res.map(function(x) { return x.ar; });
}
callback(err,verses);
});
},
select: function(filters,options,callback) {
var language;
var params = [];
var query = 'select * from ar a ';
var l = function(x) {
query += ' join ' + x + ' using(chapter,verse)';
};
if (!callback && typeof (options) === 'function') {
callback = options;
options = {};
}
language = options.language || 'ar';
if (language != 'ar') {
if (util.isArray(language)) {
language.forEach(l);
} else {
l(language);
}
}
if (filters) {
Object.keys(filters).forEach(function(k) {
var f = filters[k];
if (f) {
if (util.isArray(f)) {
params.push(' a.' + k + ' in (' + f.join(',') + ')');
} else {
params.push(' a.' + k + '=' + f);
}
}
});
query += ' where ' + params.join(' and ') ;
}
query += ' order by chapter,verse ';
if (options) {
[ 'limit', 'offset' ].forEach(function(x) {
if (options[x]) {
query += x + ' ' + options[x] + ' ';
}
});
}
if (options.debug) {
console.log(query);
}
qurandb.all(query,function(err,res) {
if (!err && res.length == 0) {
err = new Error('Selectors out of range ' + query);
}
callback(err,res);
});
},
chapter: function(chapterNum,callback) {
var query = 'select * from chapters';
if (!callback && typeof(chapterNum) == 'function') {
callback = chapterNum;
chapterNum = undefined;
}
if (chapterNum) {
query += ' where id=' + chapterNum;
}
qurandb.all(query, function(err,res) {
callback(err,res);
});
},
juz: function(juzNum, callback) {
var query = 'select * from juz';
if (!callback && typeof(juzNum) == 'function') {
callback = juzNum;
juzNum = undefined;
}
if (juzNum && juzNum > 30) {
return callback(new Error('Juz Index out of bounds ' + juzNum));
}
if (juzNum) {
query += ' where id=' + juzNum;
}
qurandb.all(query, function(err,res) {
callback(err,res);
});
},
search: function(lang,text,callback) {
// todo:sanitize input
var query = 'select chapter,verse,' + lang + ' from ' + lang + ' where ' + lang + ' like "%' + text + '%";';
qurandb.all(query, function(err,res) {
callback(err,res)
});
}
};
module.exports = quran;
#!/usr/bin/env node
"use strict";
var sqlite3 = require('sqlite3').verbose();
var fs = require('fs');
var util = require('util');
var QuranData = require('../contrib/quran-data');
var db;
/* file format
* arabic
* blank
* english
* blank
* the above repeats ...
*/
function readFile(file,cb) {
fs.readFile(file, function(err,data) {
var lines = data.toString().split('\n');
var rows = [];
var index = 0;
var row = [];
lines.forEach(function(line) {
line = line.trim();
if (!line || line.match(/^#/)) {
return;
}
var row = line.split('|');
rows.push(row);
});
cb(rows);
});
}
function initdb(rows) {
var table = 'ar';
db = new sqlite3.Database('data/qurandb');
db.serialize(function() {
db.run("CREATE TABLE " + table + " (chapter INTEGER,verse INTEGER, " + table + " TEXT)",function(err) {
if (!err) {
var stmt = db.prepare("INSERT INTO " + table + " VALUES (?,?,?)");
rows.forEach(function(row,index) {
stmt.run(row);
if (index % 1000 == 0) {
stmt.finalize();
stmt = db.prepare("INSERT INTO " + table + " VALUES (?,?,?)");
}
});
} else {
console.log(err);
}
loadmeta();
});
});
}
function loadmeta() {
db.serialize(function() {
var sql = 'CREATE TABLE chapters (start INTEGER, ayas INTEGER, ord INTEGER, rukus INTEGER, arname TEXT, tname TEXT, ' +
'enname TEXT, type TEXT, id INTEGER);';
db.run(sql,function(err) {
if (!err) {
var stmt = db.prepare('INSERT INTO chapters values (?,?,?,?,?,?,?,?,?)');
QuranData.Sura.forEach(function(x,idx) {
if (idx && idx < 115) {
x.push(idx);
stmt.run(x);
}
});
stmt.finalize();
} else {
console.log(err);
}
});
db.run('create table juz (surah INTEGER, ayah INTEGER, id INTEGER)', function(err) {
if (!err) {
var stmt = db.prepare('INSERT INTO juz values (?,?,?)');
QuranData.Juz.forEach(function(x,idx) {
x.push(idx);
stmt.run(x);
});
stmt.finalize();
} else {
console.log(err);
}
});
});
}
(function main() {
readFile(process.argv[2],initdb);
}());
#!/usr/bin/env node
"use strict";
var sqlite3 = require('sqlite3').verbose();
var fs = require('fs');
var util = require('util');
var db;
process.on('uncaughtException', function (err) {
console.log('Caught exception: ' + err);
});
/* file format
* chapter|verse|text
*/
function readFile(file,cb) {
fs.readFile(file, function(err,data) {
var lines = data.toString().split('\n');
var rows = [];
var index = 0;
var row = [];
lines.forEach(function(line) {
line = line.trim();
if (!line || line.match(/^#/)) {
return;
}
var row = line.split('|');
if (row.length > 3) {
row[2] += row.pop()
}
rows.push(row);
});
cb(rows);
});
}
function initdb(rows) {
var trans = process.argv[2].split('/').pop();
var sp = trans.split('.');
var table = sp[0];
var translator = sp[1];
db = new sqlite3.Database('data/qurandb');
db.serialize(function() {
db.run("CREATE TABLE " + table + " (chapter INTEGER,verse INTEGER, " + table + " TEXT, translator TEXT)",function(err) {
if (!err) {
rows.forEach(function(row,index) {
var stmt = db.prepare("INSERT INTO " + table + " VALUES (?,?,?,?)");
row.push(translator);
stmt.run(row);
if (index % 1000 == 0) {
stmt.finalize();
}
});
} else {
console.log(err);
}
});
});
}
(function main() {
readFile(process.argv[2],initdb);
}());
#!/bin/sh
QURANTXT=quran-uthmani.txt
TRANSLATIONS="en.shakir ur.jawadi hi.hindi id.indonesian"
abort() {
echo
echo " $@" 1>&2
echo
exit 1
}
log() {
echo " ○ $@"
}
download() {
log "Downloading $2"
if test -e "$2"; then
log "$2 already downloaded, skipping"
else
curl $1 > $2 || abort "failed while downloading $2"
fi
}
mkdir -p contrib data
if test -e contrib/$QURANTXT; then
log 'already downloaded - skipping'
else
curl -d 'quranType=uthmani&marks=true&sajdah=true&alef=true&outType=txt-2&agree=true' http://tanzil.net/pub/download/download.php > contrib/$QURANTXT || abort 'download failed'
fi
download http://tanzil.net/res/text/metadata/quran-data.js contrib/quran-data.js
grep -q module.exports contrib/quran-data.js || echo 'module.exports = QuranData;' >> contrib/quran-data.js
node quran/loader contrib/$QURANTXT arabic
for translation in $TRANSLATIONS
do
download http://tanzil.net/trans/$translation contrib/$translation.txt
node quran/loadtrans contrib/$translation.txt
done
"use strict";
var quran = require('../index');
console.log('Fetching verse 1 of Al Fatiha');
quran.get(1,1,function(err,v) {
console.log(err || v);
});
console.log('Fetching chapter 1 - Al Fatiha');
quran.get(1,function(err,v) {
console.log(err || v.join(','));
});
console.log('Fetch meta info about chapter 1');
quran.chapter(1, function(err,v) {
console.log(err || v);
});
console.log('Fetch meta info about all chapters');
quran.chapter(function(err,v) {
console.log(err || v);
});
console.log('Fetch first 10 verses of juz 2');
quran.juz(28,function(err,j) {
console.log(err || j[0]);
if (!err) {
quran.select({ chapter: j[0].surah }, { offset: j[0].ayah - 1, limit: 10 }, function(err,verses) {
console.log(err || verses);
});
}
});
"use strict";
var quran = require('../index');
console.log('Fetching 3 verses in arabic via select');
quran.select( { chapter: 1 }, { limit : 3 }, function(err,v) {
console.log(err || v);
});
console.log('Fetching first chapter in arabic via select');
quran.select( { chapter: 1 }, function(err,v) {
console.log(err || v);
});
console.log('Fetching all verses of chapter 1 with urdu translation via select');
quran.select( { chapter: 1 }, { language: 'ur' }, function(err,v) {
console.log(err || v);
});
console.log('Fetching verses 4-5 of chapter 1 via select');
quran.select( { chapter: 1, verse: [2,4,6] }, { language: [ 'en', 'ur' ] }, function(err,v) {
console.log(err || v);
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment