Mercurial Hosting > luan
annotate core/src/luan/modules/Io.luan @ 625:a3c1e11fb6aa
rewrite much of Html to be more understandable;
add Lucene html_highlighter();
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 12 Jan 2016 23:52:56 -0700 |
parents | 790d5de23042 |
children | 35dde32c02ab |
rev | line source |
---|---|
321
7f7708e8fdd4
remove import statement
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
320
diff
changeset
|
1 java() |
7f7708e8fdd4
remove import statement
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
320
diff
changeset
|
2 local IoLuan = require "java:luan.modules.IoLuan" |
7f7708e8fdd4
remove import statement
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
320
diff
changeset
|
3 local System = require "java:java.lang.System" |
297
899253043270
remove PackageLuan.load_lib()
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
4 |
503 | 5 local M = {} |
6 | |
7 M.read_console_line = IoLuan.read_console_line | |
8 M.schemes = IoLuan.newSchemes() | |
9 M.uri = IoLuan.uri | |
10 M.stdin = IoLuan.defaultStdin.table() | |
11 M.socket_server = IoLuan.socket_server | |
12 M.stdout = IoLuan.textWriter(System.out) | |
13 M.stderr = IoLuan.textWriter(System.err) | |
298
2ce23c7e2342
remove init.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
297
diff
changeset
|
14 |
2ce23c7e2342
remove init.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
297
diff
changeset
|
15 |
321
7f7708e8fdd4
remove import statement
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
320
diff
changeset
|
16 local Luan = require "luan:Luan" |
554
18504c41b0be
move debug() to Io and remove Debug module;
Franklin Schmidt <fschmidt@gmail.com>
parents:
539
diff
changeset
|
17 local error = Luan.error |
18504c41b0be
move debug() to Io and remove Debug module;
Franklin Schmidt <fschmidt@gmail.com>
parents:
539
diff
changeset
|
18 local to_string = Luan.to_string or error() |
18504c41b0be
move debug() to Io and remove Debug module;
Franklin Schmidt <fschmidt@gmail.com>
parents:
539
diff
changeset
|
19 local type = Luan.type or error() |
18504c41b0be
move debug() to Io and remove Debug module;
Franklin Schmidt <fschmidt@gmail.com>
parents:
539
diff
changeset
|
20 local try = Luan.try or error() |
18504c41b0be
move debug() to Io and remove Debug module;
Franklin Schmidt <fschmidt@gmail.com>
parents:
539
diff
changeset
|
21 local ipairs = Luan.ipairs or error() |
18504c41b0be
move debug() to Io and remove Debug module;
Franklin Schmidt <fschmidt@gmail.com>
parents:
539
diff
changeset
|
22 local pairs = Luan.pairs or error() |
18504c41b0be
move debug() to Io and remove Debug module;
Franklin Schmidt <fschmidt@gmail.com>
parents:
539
diff
changeset
|
23 local values = Luan.values or error() |
18504c41b0be
move debug() to Io and remove Debug module;
Franklin Schmidt <fschmidt@gmail.com>
parents:
539
diff
changeset
|
24 local load = Luan.load or error() |
321
7f7708e8fdd4
remove import statement
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
320
diff
changeset
|
25 local Table = require "luan:Table" |
554
18504c41b0be
move debug() to Io and remove Debug module;
Franklin Schmidt <fschmidt@gmail.com>
parents:
539
diff
changeset
|
26 local unpack = Table.unpack or error() |
539
473e456444ff
Remove object-oriented primitive methods for string and binary
Franklin Schmidt <fschmidt@gmail.com>
parents:
503
diff
changeset
|
27 local String = require "luan:String" |
554
18504c41b0be
move debug() to Io and remove Debug module;
Franklin Schmidt <fschmidt@gmail.com>
parents:
539
diff
changeset
|
28 local encode = String.encode or error() |
18504c41b0be
move debug() to Io and remove Debug module;
Franklin Schmidt <fschmidt@gmail.com>
parents:
539
diff
changeset
|
29 local matches = String.matches or error() |
539
473e456444ff
Remove object-oriented primitive methods for string and binary
Franklin Schmidt <fschmidt@gmail.com>
parents:
503
diff
changeset
|
30 |
320
fed1893821bf
remove global namespace
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
319
diff
changeset
|
31 |
503 | 32 function M.print_to(out,...) |
298
2ce23c7e2342
remove init.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
297
diff
changeset
|
33 local list = {} |
539
473e456444ff
Remove object-oriented primitive methods for string and binary
Franklin Schmidt <fschmidt@gmail.com>
parents:
503
diff
changeset
|
34 for v in values(...) do |
298
2ce23c7e2342
remove init.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
297
diff
changeset
|
35 list[#list+1] = to_string(v) |
2ce23c7e2342
remove init.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
297
diff
changeset
|
36 list[#list+1] = '\t' |
2ce23c7e2342
remove init.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
297
diff
changeset
|
37 end |
554
18504c41b0be
move debug() to Io and remove Debug module;
Franklin Schmidt <fschmidt@gmail.com>
parents:
539
diff
changeset
|
38 if #list > 0 then |
298
2ce23c7e2342
remove init.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
297
diff
changeset
|
39 list[#list] = '\n' |
539
473e456444ff
Remove object-oriented primitive methods for string and binary
Franklin Schmidt <fschmidt@gmail.com>
parents:
503
diff
changeset
|
40 out.write( unpack(list) ) |
298
2ce23c7e2342
remove init.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
297
diff
changeset
|
41 end |
2ce23c7e2342
remove init.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
297
diff
changeset
|
42 end |
2ce23c7e2342
remove init.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
297
diff
changeset
|
43 |
503 | 44 function M.print(...) |
45 M.print_to(M.stdout,...) | |
298
2ce23c7e2342
remove init.luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
297
diff
changeset
|
46 end |
392
8668f3799ad9
add Io.dont_write_when_no
Franklin Schmidt <fschmidt@gmail.com>
parents:
321
diff
changeset
|
47 |
8668f3799ad9
add Io.dont_write_when_no
Franklin Schmidt <fschmidt@gmail.com>
parents:
321
diff
changeset
|
48 |
589
97c8ae330efe
add varargs to Luan.try;
Franklin Schmidt <fschmidt@gmail.com>
parents:
554
diff
changeset
|
49 function M.output_to(out,fn,...) |
97c8ae330efe
add varargs to Luan.try;
Franklin Schmidt <fschmidt@gmail.com>
parents:
554
diff
changeset
|
50 local old_out = M.stdout |
97c8ae330efe
add varargs to Luan.try;
Franklin Schmidt <fschmidt@gmail.com>
parents:
554
diff
changeset
|
51 return try( { |
97c8ae330efe
add varargs to Luan.try;
Franklin Schmidt <fschmidt@gmail.com>
parents:
554
diff
changeset
|
52 function(...) |
97c8ae330efe
add varargs to Luan.try;
Franklin Schmidt <fschmidt@gmail.com>
parents:
554
diff
changeset
|
53 M.stdout = out |
97c8ae330efe
add varargs to Luan.try;
Franklin Schmidt <fschmidt@gmail.com>
parents:
554
diff
changeset
|
54 fn(...) |
97c8ae330efe
add varargs to Luan.try;
Franklin Schmidt <fschmidt@gmail.com>
parents:
554
diff
changeset
|
55 end; |
97c8ae330efe
add varargs to Luan.try;
Franklin Schmidt <fschmidt@gmail.com>
parents:
554
diff
changeset
|
56 finally = function() |
97c8ae330efe
add varargs to Luan.try;
Franklin Schmidt <fschmidt@gmail.com>
parents:
554
diff
changeset
|
57 M.stdout = old_out |
97c8ae330efe
add varargs to Luan.try;
Franklin Schmidt <fschmidt@gmail.com>
parents:
554
diff
changeset
|
58 end; |
97c8ae330efe
add varargs to Luan.try;
Franklin Schmidt <fschmidt@gmail.com>
parents:
554
diff
changeset
|
59 }, ... ) |
97c8ae330efe
add varargs to Luan.try;
Franklin Schmidt <fschmidt@gmail.com>
parents:
554
diff
changeset
|
60 end |
97c8ae330efe
add varargs to Luan.try;
Franklin Schmidt <fschmidt@gmail.com>
parents:
554
diff
changeset
|
61 |
97c8ae330efe
add varargs to Luan.try;
Franklin Schmidt <fschmidt@gmail.com>
parents:
554
diff
changeset
|
62 local uri = M.uri -- make local |
97c8ae330efe
add varargs to Luan.try;
Franklin Schmidt <fschmidt@gmail.com>
parents:
554
diff
changeset
|
63 |
97c8ae330efe
add varargs to Luan.try;
Franklin Schmidt <fschmidt@gmail.com>
parents:
554
diff
changeset
|
64 function M.output_of(fn,...) |
97c8ae330efe
add varargs to Luan.try;
Franklin Schmidt <fschmidt@gmail.com>
parents:
554
diff
changeset
|
65 local string_uri = uri "string:" |
97c8ae330efe
add varargs to Luan.try;
Franklin Schmidt <fschmidt@gmail.com>
parents:
554
diff
changeset
|
66 local out = string_uri.text_writer() |
97c8ae330efe
add varargs to Luan.try;
Franklin Schmidt <fschmidt@gmail.com>
parents:
554
diff
changeset
|
67 M.output_to(out,fn,...) |
97c8ae330efe
add varargs to Luan.try;
Franklin Schmidt <fschmidt@gmail.com>
parents:
554
diff
changeset
|
68 out.close() |
97c8ae330efe
add varargs to Luan.try;
Franklin Schmidt <fschmidt@gmail.com>
parents:
554
diff
changeset
|
69 return string_uri.read_text() |
97c8ae330efe
add varargs to Luan.try;
Franklin Schmidt <fschmidt@gmail.com>
parents:
554
diff
changeset
|
70 end |
97c8ae330efe
add varargs to Luan.try;
Franklin Schmidt <fschmidt@gmail.com>
parents:
554
diff
changeset
|
71 |
97c8ae330efe
add varargs to Luan.try;
Franklin Schmidt <fschmidt@gmail.com>
parents:
554
diff
changeset
|
72 |
422 | 73 -- repr |
74 | |
591
790d5de23042
add "strict" param to Io.repr();
Franklin Schmidt <fschmidt@gmail.com>
parents:
589
diff
changeset
|
75 local function do_repr(obj,strict,done) |
422 | 76 local tp = type(obj) |
77 if tp == "table" then | |
78 if done[obj] == true then | |
591
790d5de23042
add "strict" param to Io.repr();
Franklin Schmidt <fschmidt@gmail.com>
parents:
589
diff
changeset
|
79 strict and error "circular reference" |
422 | 80 %><circular reference><% |
81 return | |
82 end | |
83 done[obj] = true | |
84 %>{<% | |
85 local is_first = true | |
86 local in_list = {} | |
87 for key, value in ipairs(obj) do | |
88 if is_first then is_first = false else %>, <% end | |
591
790d5de23042
add "strict" param to Io.repr();
Franklin Schmidt <fschmidt@gmail.com>
parents:
589
diff
changeset
|
89 do_repr(value,strict,done) |
422 | 90 in_list[key] = true |
91 end | |
92 for key, value in pairs(obj) do | |
93 if in_list[key] ~= true then | |
94 if is_first then is_first = false else %>, <% end | |
539
473e456444ff
Remove object-oriented primitive methods for string and binary
Franklin Schmidt <fschmidt@gmail.com>
parents:
503
diff
changeset
|
95 if type(key) == "string" and matches(key,"[a-zA-Z_][a-zA-Z_0-9]*") ~= nil then |
422 | 96 %><%=key%><% |
424 | 97 elseif type(key) == "table" then |
98 %>[<<%=key%>>]<% | |
422 | 99 else |
591
790d5de23042
add "strict" param to Io.repr();
Franklin Schmidt <fschmidt@gmail.com>
parents:
589
diff
changeset
|
100 %>[<%do_repr(key,strict,done)%>]<% |
422 | 101 end |
591
790d5de23042
add "strict" param to Io.repr();
Franklin Schmidt <fschmidt@gmail.com>
parents:
589
diff
changeset
|
102 %>=<% do_repr(value,strict,done) |
422 | 103 end |
104 end | |
105 %>}<% | |
106 elseif tp == "string" then | |
539
473e456444ff
Remove object-oriented primitive methods for string and binary
Franklin Schmidt <fschmidt@gmail.com>
parents:
503
diff
changeset
|
107 %>"<%=encode(obj)%>"<% |
422 | 108 elseif tp == "nil" or tp == "boolean" or tp == "number" then |
109 %><%=obj%><% | |
110 else | |
591
790d5de23042
add "strict" param to Io.repr();
Franklin Schmidt <fschmidt@gmail.com>
parents:
589
diff
changeset
|
111 strict and error("can't repr type '"..tp.."' of "..obj) |
422 | 112 %><<%=obj%>><% |
113 end | |
114 end | |
115 | |
591
790d5de23042
add "strict" param to Io.repr();
Franklin Schmidt <fschmidt@gmail.com>
parents:
589
diff
changeset
|
116 function M.repr(obj,strict) |
790d5de23042
add "strict" param to Io.repr();
Franklin Schmidt <fschmidt@gmail.com>
parents:
589
diff
changeset
|
117 return M.output_of(do_repr,obj,strict or false,{}) |
422 | 118 end |
119 | |
120 | |
392
8668f3799ad9
add Io.dont_write_when_no
Franklin Schmidt <fschmidt@gmail.com>
parents:
321
diff
changeset
|
121 -- useful for SimplyHTML responsiveness |
8668f3799ad9
add Io.dont_write_when_no
Franklin Schmidt <fschmidt@gmail.com>
parents:
321
diff
changeset
|
122 |
503 | 123 local NO = {} |
124 M.NO = NO | |
392
8668f3799ad9
add Io.dont_write_when_no
Franklin Schmidt <fschmidt@gmail.com>
parents:
321
diff
changeset
|
125 |
503 | 126 function M.dont_write_when_no(write_fn) |
392
8668f3799ad9
add Io.dont_write_when_no
Franklin Schmidt <fschmidt@gmail.com>
parents:
321
diff
changeset
|
127 return function(...) |
539
473e456444ff
Remove object-oriented primitive methods for string and binary
Franklin Schmidt <fschmidt@gmail.com>
parents:
503
diff
changeset
|
128 for v in values(...) do |
392
8668f3799ad9
add Io.dont_write_when_no
Franklin Schmidt <fschmidt@gmail.com>
parents:
321
diff
changeset
|
129 if v == NO then |
8668f3799ad9
add Io.dont_write_when_no
Franklin Schmidt <fschmidt@gmail.com>
parents:
321
diff
changeset
|
130 return |
8668f3799ad9
add Io.dont_write_when_no
Franklin Schmidt <fschmidt@gmail.com>
parents:
321
diff
changeset
|
131 end |
8668f3799ad9
add Io.dont_write_when_no
Franklin Schmidt <fschmidt@gmail.com>
parents:
321
diff
changeset
|
132 end |
8668f3799ad9
add Io.dont_write_when_no
Franklin Schmidt <fschmidt@gmail.com>
parents:
321
diff
changeset
|
133 write_fn(...) |
8668f3799ad9
add Io.dont_write_when_no
Franklin Schmidt <fschmidt@gmail.com>
parents:
321
diff
changeset
|
134 end |
8668f3799ad9
add Io.dont_write_when_no
Franklin Schmidt <fschmidt@gmail.com>
parents:
321
diff
changeset
|
135 end |
503 | 136 |
554
18504c41b0be
move debug() to Io and remove Debug module;
Franklin Schmidt <fschmidt@gmail.com>
parents:
539
diff
changeset
|
137 |
18504c41b0be
move debug() to Io and remove Debug module;
Franklin Schmidt <fschmidt@gmail.com>
parents:
539
diff
changeset
|
138 -- debug |
18504c41b0be
move debug() to Io and remove Debug module;
Franklin Schmidt <fschmidt@gmail.com>
parents:
539
diff
changeset
|
139 |
18504c41b0be
move debug() to Io and remove Debug module;
Franklin Schmidt <fschmidt@gmail.com>
parents:
539
diff
changeset
|
140 function M.debug(prompt) |
18504c41b0be
move debug() to Io and remove Debug module;
Franklin Schmidt <fschmidt@gmail.com>
parents:
539
diff
changeset
|
141 prompt = prompt or "luan_debug> " |
18504c41b0be
move debug() to Io and remove Debug module;
Franklin Schmidt <fschmidt@gmail.com>
parents:
539
diff
changeset
|
142 local function console() |
18504c41b0be
move debug() to Io and remove Debug module;
Franklin Schmidt <fschmidt@gmail.com>
parents:
539
diff
changeset
|
143 return M.read_console_line(prompt) |
18504c41b0be
move debug() to Io and remove Debug module;
Franklin Schmidt <fschmidt@gmail.com>
parents:
539
diff
changeset
|
144 end |
18504c41b0be
move debug() to Io and remove Debug module;
Franklin Schmidt <fschmidt@gmail.com>
parents:
539
diff
changeset
|
145 local env = {} |
18504c41b0be
move debug() to Io and remove Debug module;
Franklin Schmidt <fschmidt@gmail.com>
parents:
539
diff
changeset
|
146 for line in console do |
18504c41b0be
move debug() to Io and remove Debug module;
Franklin Schmidt <fschmidt@gmail.com>
parents:
539
diff
changeset
|
147 try { |
18504c41b0be
move debug() to Io and remove Debug module;
Franklin Schmidt <fschmidt@gmail.com>
parents:
539
diff
changeset
|
148 function() |
18504c41b0be
move debug() to Io and remove Debug module;
Franklin Schmidt <fschmidt@gmail.com>
parents:
539
diff
changeset
|
149 local fn = load(line,"stdin",env,true) |
18504c41b0be
move debug() to Io and remove Debug module;
Franklin Schmidt <fschmidt@gmail.com>
parents:
539
diff
changeset
|
150 M.print( fn() ) |
18504c41b0be
move debug() to Io and remove Debug module;
Franklin Schmidt <fschmidt@gmail.com>
parents:
539
diff
changeset
|
151 end; |
18504c41b0be
move debug() to Io and remove Debug module;
Franklin Schmidt <fschmidt@gmail.com>
parents:
539
diff
changeset
|
152 catch = function(e) |
18504c41b0be
move debug() to Io and remove Debug module;
Franklin Schmidt <fschmidt@gmail.com>
parents:
539
diff
changeset
|
153 M.print(e) |
18504c41b0be
move debug() to Io and remove Debug module;
Franklin Schmidt <fschmidt@gmail.com>
parents:
539
diff
changeset
|
154 end; |
18504c41b0be
move debug() to Io and remove Debug module;
Franklin Schmidt <fschmidt@gmail.com>
parents:
539
diff
changeset
|
155 } |
18504c41b0be
move debug() to Io and remove Debug module;
Franklin Schmidt <fschmidt@gmail.com>
parents:
539
diff
changeset
|
156 end |
18504c41b0be
move debug() to Io and remove Debug module;
Franklin Schmidt <fschmidt@gmail.com>
parents:
539
diff
changeset
|
157 end |
18504c41b0be
move debug() to Io and remove Debug module;
Franklin Schmidt <fschmidt@gmail.com>
parents:
539
diff
changeset
|
158 |
18504c41b0be
move debug() to Io and remove Debug module;
Franklin Schmidt <fschmidt@gmail.com>
parents:
539
diff
changeset
|
159 |
503 | 160 return M |