forked from tiddlyweb/twikifier
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwikify
More file actions
executable file
·105 lines (89 loc) · 2.83 KB
/
twikify
File metadata and controls
executable file
·105 lines (89 loc) · 2.83 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
#!/usr/bin/env node
var opts = require('tav').set({
collection: {
note: 'Remote Tiddler Collection URI',
value: null
}
}, "wikify some tiddlywiki text");
// require the jsdom and jquery libraries, via NODE_PATH
// (probably /usr/local/lib/node if you are using npm)
var jsdom = require('jsdom');
var jquery = require('jQuery');
var fs = require('fs');
var http = require('http')
var url = require('url');
// reference our local libraries
var createWikifier = require('./twikifier').createWikifier;
var twik = require('./twik');
// read data to create text and pass to formatText
var run = function(window, wikify, file) {
var text = '';
file.on('data', function(chunk) {
text += chunk;
});
file.on('end', function() {
console.log(twik.formatText(window, wikify, text));
});
};
// open a file stream
var runFile = function(window, wikify, filename) {
var text = '';
stream = fs.createReadStream(filename, { 'encoding': 'utf8' });
run(window, wikify, stream);
}
// open stdin
var runStdin = function(window, wikify) {
var stdin = process.openStdin();
stdin.setEncoding('utf8');
run(window, wikify, stdin);
}
// determine if we are reading from stdin or file stream
var processdata = function(window, wikify) {
var arglength = opts.args.length;
if (arglength) {
for (var i = 0; i < arglength ; i++) {
runFile(window, wikify, opts.args[i]);
}
} else {
runStdin(window, wikify);
}
}
// make the game go
var main = function() {
// create a window in which we host a dom
var uri = opts.collection;
var window = jsdom.jsdom(
'<html><head></head><body><div id="tiddler"></div></body></html>'
).createWindow();
// jquery-ize the window
var jQuery = jquery.create(window);
// Create formatter for the window.
var globals = createWikifier(window, jQuery, {container: uri});
var wikify = globals[0];
var store = globals[1];
var Tiddler = globals[2];
if (uri) {
var parsed_uri = url.parse(uri);
var client = http.createClient(parsed_uri.port ? parsed_uri.port : 80,
parsed_uri.hostname);
var request = client.request('GET', parsed_uri.pathname + '?fat=1',
{'host': parsed_uri.hostname,
'accept': 'application/json'});
request.end();
request.on('response', function(response) {
response.setEncoding('utf8');
var data = ''
response.on('data', function(chunk) {
data += chunk;
});
response.on('end', function() {
twik.loadRemoteTiddlers(store, Tiddler, uri, data);
processdata(window, wikify);
});
});
} else {
processdata(window, wikify);
}
}
if (!module.parent)
main();