diff options
| author | s-ol <s-ol@users.noreply.github.com> | 2019-10-31 16:15:23 +0000 |
|---|---|---|
| committer | s-ol <s-ol@users.noreply.github.com> | 2019-10-31 16:15:23 +0000 |
| commit | 938a524f623ba08430085fe6c33fcc77f94b3127 (patch) | |
| tree | 51ba95431a12e2d84e37697f56e8b8bcd56b1c57 | |
| parent | allow adding and removing of facets (diff) | |
| download | mmm-938a524f623ba08430085fe6c33fcc77f94b3127.tar.gz mmm-938a524f623ba08430085fe6c33fcc77f94b3127.zip | |
add DEBUG pseudo-type for debugging conversion
| -rw-r--r-- | build/server.moon | 10 | ||||
| -rw-r--r-- | mmm/mmmfs/conversion.moon | 55 |
2 files changed, 62 insertions, 3 deletions
diff --git a/build/server.moon b/build/server.moon index fb6dca4..99076bf 100644 --- a/build/server.moon +++ b/build/server.moon @@ -10,7 +10,7 @@ add '?/init.server' require 'mmm' import dir_base, Key, Fileder from require 'mmm.mmmfs.fileder' -import convert from require 'mmm.mmmfs.conversion' +import convert, MermaidDebugger from require 'mmm.mmmfs.conversion' import get_store from require 'mmm.mmmfs.stores' import render from require 'mmm.mmmfs.layout' import Browser from require 'mmm.mmmfs.browser' @@ -88,6 +88,11 @@ class Server end) </script>" + handle_debug: (fileder, facet) => + debugger = MermaidDebugger! + fileder\find facet, nil, nil, nil, debugger + convert 'text/mermaid-graph', 'text/html', debugger\render!, fileder, facet.name + handle: (method, path, facet, value) => fileder = Fileder @store, path @@ -111,6 +116,9 @@ class Server else if facet.type == 'text/html+interactive' @handle_interactive fileder, facet + else if base = facet.type\match '^DEBUG %-> (.*)' + facet.type = base + @handle_debug fileder, facet else if not fileder\has_facet facet.name 404, "facet '#{facet.name}' not found in fileder '#{path}'" else diff --git a/mmm/mmmfs/conversion.moon b/mmm/mmmfs/conversion.moon index c0505a8..863b6af 100644 --- a/mmm/mmmfs/conversion.moon +++ b/mmm/mmmfs/conversion.moon @@ -7,12 +7,51 @@ escape_inp = (inp) -> "^#{inp\gsub '([-/])', '%%%1'}$" local print_conversions +class MermaidDebugger + new: => + nextid = 0 + @type_id = setmetatable {}, __index: (t, k) -> + nextid += 1 + with val = "type-#{nextid}" + t[k] = val + + @cost = {} + @buf = "graph TD\n" + + append: (line) => + @buf ..= " #{line}\n" + + found_type: (type) => + type_id = @type_id[type] + + type_cost: (type, cost) => + if old_cost = @cost[type] + cost = math.min old_cost, cost + @cost[type] = cost + + type_class: (type, klass) => + @append "class #{@type_id[type]} #{klass}" + + found_link: (frm, to, cost) => + @append "#{@type_id[frm]} -- cost: #{cost} --> #{@type_id[to]}" + + render: => + for type, id in pairs @type_id + cost = @cost[type] or -1 + @append "#{id}[\"#{type} [#{cost}]\"]" + + @append "classDef have fill:#ada" + @append "classDef want fill:#add" + + @buf + -- attempt to find a conversion path from 'have' to 'want' -- * have - start type string or list of type strings -- * want - stop type pattern -- * limit - limit conversion amount +-- * debug - a table with debug hooks -- returns a list of conversion steps -get_conversions = (want, have, converts=PLUGINS and PLUGINS.converts, limit=5) -> +get_conversions = (want, have, converts=PLUGINS and PLUGINS.converts, limit=5, debug) -> assert have, 'need starting type(s)' assert converts, 'need to pass list of converts' @@ -30,6 +69,11 @@ get_conversions = (want, have, converts=PLUGINS and PLUGINS.converts, limit=5) - return {}, start if want\match start queue\add { :start, rest: start, conversions: {} }, 0, start + if debug + debug\found_type start + debug\type_cost start, 0 + debug\type_class start, 'have' + best = Queue! while true @@ -53,12 +97,17 @@ get_conversions = (want, have, converts=PLUGINS and PLUGINS.converts, limit=5) - conversions: { { :convert, from: rest, to: result }, table.unpack conversions } } + if debug + debug\found_type result + debug\type_cost result, next_entry.cost + debug\found_link rest, result, convert.cost + if result\match want + debug\type_class result, 'want' if debug best\add next_entry, next_entry.cost else queue\add next_entry, next_entry.cost, result - if solution = best\pop! -- print "BEST: (#{solution.cost})" -- print_conversions solution.conversions @@ -106,6 +155,8 @@ convert = (have, want, value, ...) -> apply_conversions conversions, value, ... { + :MermaidDebugger + :get_conversions :apply_conversions :convert |
