fix conversion pattern escapes
s-ol
3 years ago
1 | 1 | converts = require '.converts' |
2 | 2 | |
3 | 3 | count = (base, pattern='->') -> select 2, base\gsub pattern, '' |
4 | escape_pattern = (inp) -> "^#{inp\gsub '([-/])', '%%%1'}$" | |
4 | escape_pattern = (inp) -> "^#{inp\gsub '([^%w])', '%%%1'}$" | |
5 | escape_inp = (inp) -> "^#{inp\gsub '([-/])', '%%%1'}$" | |
5 | 6 | |
6 | 7 | -- attempt to find a conversion path from 'have' to 'want' |
7 | 8 | -- * have - start type string or list of type strings |
8 | 9 | -- * want - stop type pattern |
9 | 10 | -- * limit - limit conversion amount |
10 | 11 | -- returns a list of conversion steps |
11 | get_conversions = (want, have, _converts=converts, limit=3) -> | |
12 | get_conversions = (want, have, _converts=converts, limit=5) -> | |
12 | 13 | assert have, 'need starting type(s)' |
13 | 14 | |
14 | 15 | if 'string' == type have |
27 | 28 | return conversions, start |
28 | 29 | else |
29 | 30 | for convert in *_converts |
30 | inp = escape_pattern convert.inp | |
31 | inp = escape_inp convert.inp | |
31 | 32 | continue unless rest\match inp |
32 | 33 | result = rest\gsub inp, convert.out |
33 | 34 | if result |