git.s-ol.nu mmm / 938a524
add DEBUG pseudo-type for debugging conversion s-ol 2 years ago
2 changed file(s) with 62 addition(s) and 3 deletion(s). Raw diff Collapse all Expand all
99 require 'mmm'
1010
1111 import dir_base, Key, Fileder from require 'mmm.mmmfs.fileder'
12 import convert from require 'mmm.mmmfs.conversion'
12 import convert, MermaidDebugger from require 'mmm.mmmfs.conversion'
1313 import get_store from require 'mmm.mmmfs.stores'
1414 import render from require 'mmm.mmmfs.layout'
1515 import Browser from require 'mmm.mmmfs.browser'
8686 if not ok then error(browser) end
8787 end)
8888 </script>"
89
90 handle_debug: (fileder, facet) =>
91 debugger = MermaidDebugger!
92 fileder\find facet, nil, nil, nil, debugger
93 convert 'text/mermaid-graph', 'text/html', debugger\render!, fileder, facet.name
8994
9095 handle: (method, path, facet, value) =>
9196 fileder = Fileder @store, path
110115 else
111116 if facet.type == 'text/html+interactive'
112117 @handle_interactive fileder, facet
118 else if base = facet.type\match '^DEBUG %-> (.*)'
119 facet.type = base
120 @handle_debug fileder, facet
113121 else if not fileder\has_facet facet.name
114122 404, "facet '#{facet.name}' not found in fileder '#{path}'"
115123 else
66
77 local print_conversions
88
9 class MermaidDebugger
10 new: =>
11 nextid = 0
12 @type_id = setmetatable {}, __index: (t, k) ->
13 nextid += 1
14 with val = "type-#{nextid}"
15 t[k] = val
16
17 @cost = {}
18 @buf = "graph TD\n"
19
20 append: (line) =>
21 @buf ..= " #{line}\n"
22
23 found_type: (type) =>
24 type_id = @type_id[type]
25
26 type_cost: (type, cost) =>
27 if old_cost = @cost[type]
28 cost = math.min old_cost, cost
29 @cost[type] = cost
30
31 type_class: (type, klass) =>
32 @append "class #{@type_id[type]} #{klass}"
33
34 found_link: (frm, to, cost) =>
35 @append "#{@type_id[frm]} -- cost: #{cost} --> #{@type_id[to]}"
36
37 render: =>
38 for type, id in pairs @type_id
39 cost = @cost[type] or -1
40 @append "#{id}[\"#{type} [#{cost}]\"]"
41
42 @append "classDef have fill:#ada"
43 @append "classDef want fill:#add"
44
45 @buf
46
947 -- attempt to find a conversion path from 'have' to 'want'
1048 -- * have - start type string or list of type strings
1149 -- * want - stop type pattern
1250 -- * limit - limit conversion amount
51 -- * debug - a table with debug hooks
1352 -- returns a list of conversion steps
14 get_conversions = (want, have, converts=PLUGINS and PLUGINS.converts, limit=5) ->
53 get_conversions = (want, have, converts=PLUGINS and PLUGINS.converts, limit=5, debug) ->
1554 assert have, 'need starting type(s)'
1655 assert converts, 'need to pass list of converts'
1756
2867 for start in *have
2968 return {}, start if want\match start
3069 queue\add { :start, rest: start, conversions: {} }, 0, start
70
71 if debug
72 debug\found_type start
73 debug\type_cost start, 0
74 debug\type_class start, 'have'
3175
3276 best = Queue!
3377
5296 conversions: { { :convert, from: rest, to: result }, table.unpack conversions }
5397 }
5498
99 if debug
100 debug\found_type result
101 debug\type_cost result, next_entry.cost
102 debug\found_link rest, result, convert.cost
103
55104 if result\match want
105 debug\type_class result, 'want' if debug
56106 best\add next_entry, next_entry.cost
57107 else
58108 queue\add next_entry, next_entry.cost, result
59
60109
61110 if solution = best\pop!
62111 -- print "BEST: (#{solution.cost})"
105154 apply_conversions conversions, value, ...
106155
107156 {
157 :MermaidDebugger
158
108159 :get_conversions
109160 :apply_conversions
110161 :convert