-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparser.js
More file actions
199 lines (154 loc) · 4.58 KB
/
Copy pathparser.js
File metadata and controls
199 lines (154 loc) · 4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
(function (global) {
if(typeof process !== 'undefined'){
var _ = require("underscore"),
cheerio = require("cheerio"),
sha1 = require('./dep/sha1').sha1,
$ = cheerio.load();
//... Node dependencies
}else{
//... Browser dependencies
var $ = window.$,
_ = window._,
sha1 = window.SHA1;
}
var Parser = function (raw) {
this.raw = raw;
this.teiAsDom = $(raw);
this.isParser = true;
this.structure = this.constructor.structure;
}
Parser.setAllowedTags = function (tags) {
this._checkForWarnings = true;
this._allowedTags = tags;
}
Parser.setStructure = function(structure){
this.structure = structure;
}
Parser.prototype._hashFolio = function(tag){
var pb = tag.find('pb').text(),
id;
if(pb){
id = sha1( pb.replace(/\W/g,'').toLowerCase() );
}else{
id = sha1( tag.text().replace(/\W/g,'').toLowerCase() );
}
return id;
}
Parser.prototype._validateTag = function(tag) {
var allowedTags = this.constructor._allowedTags;
if(!allowedTags[tag.tag]){
return "Not allow tag";
}else{
if(!tag.type && allowedTags[tag.tag].indexOf('default') === -1){
return tag.tag + " cant have empty type";
}else if(tag.type && allowedTags[tag.tag].indexOf(tag.type) === -1){
return tag.type + " is not a supported type on " + tag.tag;
}else {
return null;
}
}
};
Parser.prototype._closest = function(el, tag){
while( el.parent() && el.parent()[0].name !== tag){
el = el.parent();
}
return el.parent();
}
Parser.prototype.getSchema = function(){
var $tei = this.teiAsDom;
var schema = $tei.find('schemaSpec').find('moduleRef').map(function(i, item) { return $(item).attr('key'); });//.toArray();
return schema.toArray ? schema.toArray() : schema;
}
Parser.prototype.getHeader = function(){
var header = {},
$tei = this.teiAsDom,
$header = $tei.find('teiHeader');
header.raw = $header.html();
_.each( this.structure.header ,function(item,i){
header[item] = $header.find(item).text()
});
return header;
}
Parser.prototype.getFront = function(){
var front = {},
$tei = this.teiAsDom,
$front = $tei.find('front');
front.raw = $front.html();
_.each( this.structure.front ,function(item,i){
front[item] = $front.find(item).text()
});
return front;
}
Parser.prototype.getFolios = function(){
var folios = {},
tempRaw = this.raw,
$tei = $(tempRaw),
$folios = $tei.find('text div'),
tags = this.structure.tags,
lineCounter = 0,
parser = this,
checkForWarnings = this.constructor._checkForWarnings;
allowedTags = this.constructor._allowedTags;
console.log('parser', parser.constructor._checkForWarnings);
folios = $folios.map(function(i,item){
var folio = {},
$item = $(item);
if( $item.find('front').length ){
folio.isFront = true;
}
folio.pb = $item.find('pb').text();
folio.hash = parser._hashFolio($item);
$item.find('pb').remove();
folio.raw = $item.html().trim();
folio.lines = folio.raw.split('\n').length;
folio.startLine = lineCounter;
folio.tags = $item.find(tags.join(',')).map(function(i,teiTag){
var tag = {},
$tag = $(teiTag);
tag.tag = teiTag.nodeName.toLowerCase();
tag.content = $tag.text().trim();
$tag.attr('reg') ? tag.reg = $tag.attr('reg') : null;
$tag.attr('type') ? tag.type = $tag.attr('type').toLowerCase() : null;
tag.id = sha1( ( tag.type + (tag.reg || tag.content).replace(/\W/g,'') ).toLowerCase());
// Check for warnings
if(checkForWarnings && allowedTags){
var warning = parser._validateTag(tag);
if(warning){
tag.warning = warning;
folio.hasWarnings = true;
}
}
return tag
});
folio.tags = folio.tags.toArray ? folio.tags.toArray() : folio.tags;
lineCounter += folio.lines;
return folio;
});
return folios.toArray ? folios.toArray() : folios;
}
Parser.prototype.getHeads = function() {
var heads = [],
tempRaw = this.raw,
$tei = $(tempRaw),
parser = this;
$tei.find('head').map(function(i,headTag){
var head = {},
$head = $(headTag),
folio = parser._closest( $head, 'div' );
head.content = $head.text().replace('\n',' ');
head.folio = parser._hashFolio(folio);
heads.push(head);
});
return heads;
};
Parser.prototype.getTei = function(){
var tei = {};
tei.schema = this.getSchema();
tei.header = this.getHeader();
tei.front = this.getFront();
tei.heads = this.getHeads();
tei.folios = this.getFolios();
return tei;
}
global.Parser = Parser;
}(typeof window === 'undefined' ? exports : window));