/
import.js
131 lines (98 loc) · 5.04 KB
/
import.js
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
exports.exec = function(fileMap, importHelper, lib, registry, callback) {
//////////////////////////////////////
// Imports //
//////////////////////////////////////
// The lib object contains some useful, injected libraries
// It also possible to install local npm modules (current dir) and require() them
var log = exports.log = lib.semlog.log; // https://www.npmjs.com/package/semlog
var chalk = lib.semlog.chalk; // https://www.npmjs.com/package/chalk
var _ = exports._ = lib.lodash; // https://www.npmjs.com/package/lodash
var yaml = lib['js-yaml']; // https://www.npmjs.com/package/js-yaml
//////////////////////////////////////
// Documentation //
//////////////////////////////////////
/**
* Object, containing all generated pages
* The key is the wiki pagename
* the value is a string of the wikitext result
*/
var generatedPages = {};
// The fileMap Object contains all files within the current import folder.
// The key is the filename, the value is the content
// If it is a JSON file, the value is already a JavaScript object
log('[D] Found files: ' + Object.keys(fileMap).join(', '));
// The import Helper contains some useful helper functions
// They assist in creating wikitext.
// If a mobo development model is available it also allows for:
// * Automatic validation of the imported data
// * Automatic completion /
log('[D] Available helper functions: ' + Object.keys(importHelper).join(', '));
// The registry object contains the complete mobo registry object
// It contains:
// * The development model in its original state
// * The intermediary (expanded) model
// * The current settings
// * Statistics
// * The generated wikitext
// * ...
log('[D] Found Registry, containing: ' + Object.keys(registry).join(', '));
log('[D] Current Working Directory: ' + registry.settings.cwd);
//////////////////////////////////////
// Simple Data Import //
//////////////////////////////////////
// Simplest case: Just add wikitext to the generatedPages Object:
var wikitext = '==Automatically imported Page==\n';
wikitext += 'With some minor logic: ' + Math.random() * 5000;
generatedPages['Import Test Page'] = wikitext;
// Pages on the blacklist (see import.yaml) will be ignored
generatedPages['Ignored Page'] = 'Will be ignored, because of the blacklist';
//////////////////////////////////////
// Advanced Data Import //
//////////////////////////////////////
// Import data into existing wiki structure and automatically validate / enhance
// Using random locations from http://beta.json-generator.com/CQJneWj
// Deliberatly sabotage one dataset: (The validation will notice and give feedback)
fileMap.exampleData[2].streetNumber = 'invalid number';
// Iterate the exampleData:
for (var i = 0; i < fileMap.exampleData.length; i++) {
var data = fileMap.exampleData[i];
// Calculate the page name
var pageName = data.streetAdress + ' ' + data.streetNumber;
log('--------------------------------------------------------------------');
log('Importing random data: ' + pageName);
log('--------------------------------------------------------------------');
// Create a new collection that will be transformed into the final wiki page
var locationColl = [];
// Add a Location Template
locationColl.push({
name: 'Location',
template: {
streetAdress: data.streetAdress,
streetNumber: data.streetNumber,
town: data.town
}
});
// Add a Headertabs Template (without content)
locationColl.push({
name: 'HeaderTabs'
});
locationColl.push('==Some arbitrary wikitext==');
locationColl.push('Will be appended at the bottom');
// Validate the Location Collection against the models in the registry
importHelper.validate(locationColl, registry);
// Enhance the Location Collection through Location form
// This rearranges the order of the templates and adds missing (empty) templates
var locationForm = registry.expandedForm.Location;
var locationEnhColl = importHelper.enhanceWithForm(locationColl, locationForm);
// Convert the (enhanced) objColletion to wikitext
var locationWikitext = importHelper.objCollectionToWikitext(locationEnhColl);
log('[i] Resulting wikitext:');
log(locationWikitext);
// Add the page to the generated page object
generatedPages[pageName] = locationWikitext;
}
// Execute the callback when the import is completed.
// This allows to use async actions like AJAX within the import script
// The first parameter is the error object, the second the generated Pages Object
return callback(false, generatedPages);
};