Mercurial Hosting > luan
comparison core/src/luan/modules/Io.luan @ 680:ecd436959855
improve template statements
| author | Franklin Schmidt <fschmidt@gmail.com> |
|---|---|
| date | Thu, 14 Apr 2016 16:38:30 -0600 |
| parents | 35dde32c02ab |
| children | ca169567ce07 |
comparison
equal
deleted
inserted
replaced
| 679:43522473599d | 680:ecd436959855 |
|---|---|
| 25 local Table = require "luan:Table" | 25 local Table = require "luan:Table" |
| 26 local unpack = Table.unpack or error() | 26 local unpack = Table.unpack or error() |
| 27 local String = require "luan:String" | 27 local String = require "luan:String" |
| 28 local encode = String.encode or error() | 28 local encode = String.encode or error() |
| 29 local matches = String.matches or error() | 29 local matches = String.matches or error() |
| 30 | |
| 31 | |
| 32 -- do not change | |
| 33 function M.template_write(...) | |
| 34 return M.stdout.write(...) | |
| 35 end | |
| 30 | 36 |
| 31 | 37 |
| 32 function M.print_to(out,...) | 38 function M.print_to(out,...) |
| 33 local list = {} | 39 local list = {} |
| 34 for v in values(...) do | 40 for v in values(...) do |
| 70 end | 76 end |
| 71 | 77 |
| 72 | 78 |
| 73 -- repr | 79 -- repr |
| 74 | 80 |
| 75 local function do_repr(obj,strict,done) | 81 local function do_repr(out,obj,strict,done) |
| 76 local tp = type(obj) | 82 local tp = type(obj) |
| 77 if tp == "table" then | 83 if tp == "table" then |
| 78 if done[obj] == true then | 84 if done[obj] == true then |
| 79 strict and error "circular reference" | 85 strict and error "circular reference" |
| 80 %><circular reference><% | 86 out.write "<circular reference>" |
| 81 return | 87 return |
| 82 end | 88 end |
| 83 done[obj] = true | 89 done[obj] = true |
| 84 %>{<% | 90 out.write( "{" ) |
| 85 local is_first = true | 91 local is_first = true |
| 86 local in_list = {} | 92 local in_list = {} |
| 87 for key, value in ipairs(obj) do | 93 for key, value in ipairs(obj) do |
| 88 if is_first then is_first = false else %>, <% end | 94 if is_first then is_first = false else out.write ", " end |
| 89 do_repr(value,strict,done) | 95 do_repr(out,value,strict,done) |
| 90 in_list[key] = true | 96 in_list[key] = true |
| 91 end | 97 end |
| 92 for key, value in pairs(obj) do | 98 for key, value in pairs(obj) do |
| 93 if in_list[key] ~= true then | 99 if in_list[key] ~= true then |
| 94 if is_first then is_first = false else %>, <% end | 100 if is_first then is_first = false else out.write ", " end |
| 95 if type(key) == "string" and matches(key,"^[a-zA-Z_][a-zA-Z_0-9]*$") ~= nil then | 101 if type(key) == "string" and matches(key,"^[a-zA-Z_][a-zA-Z_0-9]*$") ~= nil then |
| 96 %><%=key%><% | 102 out.write( key ) |
| 97 elseif type(key) == "table" then | 103 elseif type(key) == "table" then |
| 98 %>[<<%=key%>>]<% | 104 out.write( "[<", key, ">]" ) |
| 99 else | 105 else |
| 100 %>[<%do_repr(key,strict,done)%>]<% | 106 out.write "[" |
| 107 do_repr(out,key,strict,done) | |
| 108 out.write "]" | |
| 101 end | 109 end |
| 102 %>=<% do_repr(value,strict,done) | 110 out.write "=" |
| 111 do_repr(out,value,strict,done) | |
| 103 end | 112 end |
| 104 end | 113 end |
| 105 %>}<% | 114 out.write "}" |
| 106 elseif tp == "string" then | 115 elseif tp == "string" then |
| 107 %>"<%=encode(obj)%>"<% | 116 out.write( '"', encode(obj), '"' ) |
| 108 elseif tp == "nil" or tp == "boolean" or tp == "number" then | 117 elseif tp == "nil" or tp == "boolean" or tp == "number" then |
| 109 %><%=obj%><% | 118 out.write( obj ) |
| 110 else | 119 else |
| 111 strict and error("can't repr type '"..tp.."' of "..obj) | 120 strict and error("can't repr type '"..tp.."' of "..obj) |
| 112 %><<%=obj%>><% | 121 out.write( "<", obj, ">" ) |
| 113 end | 122 end |
| 114 end | 123 end |
| 115 | 124 |
| 116 function M.repr(obj,strict) | 125 function M.repr(obj,strict) |
| 117 return M.output_of(do_repr,obj,strict or false,{}) | 126 local string_uri = uri "string:" |
| 127 local out = string_uri.text_writer() | |
| 128 do_repr(out,obj,strict or false,{}) | |
| 129 out.close() | |
| 130 return string_uri.read_text() | |
| 118 end | 131 end |
| 119 | 132 |
| 120 | 133 |
| 121 -- useful for SimplyHTML responsiveness | 134 -- useful for SimplyHTML responsiveness |
| 122 | 135 |
