Module: Antwort::LogicHelpers
- Included in:
- Builder
- Defined in:
- lib/antwort/helpers/logic_helper.rb
Instance Method Summary collapse
- #cleanup_logic(html = '') ⇒ Object
- #convert_helper_wrappers(html = '') ⇒ Object
- #convert_partials_to_includes(html = '') ⇒ Object
- #preserve_assignments(html = '') ⇒ Object
- #preserve_comments(html = '') ⇒ Object
- #preserve_conditionals(html = '') ⇒ Object
- #preserve_leftover_statements(html = '') ⇒ Object
- #preserve_logic(html = '') ⇒ Object
- #preserve_loops(html = '') ⇒ Object
- #preserve_nbsps(html = '') ⇒ Object
- #preserve_variables(html = '') ⇒ Object
- #restore_nbsps(html = '') ⇒ Object
- #restore_variables_in_links(html = '') ⇒ Object
Instance Method Details
#cleanup_logic(html = '') ⇒ Object
75 76 77 78 79 |
# File 'lib/antwort/helpers/logic_helper.rb', line 75 def cleanup_logic(html = '') html.gsub(/({%|{{)(.*?)(<)(.*?)(}}|%})/, '\1\2<\4\5') # < .gsub(/({%|{{)(.*?)(>)(.*?)(}}|%})/, '\1\2>\4\5') # > .gsub(/&&/, '&&') end |
#convert_helper_wrappers(html = '') ⇒ Object
70 71 72 73 |
# File 'lib/antwort/helpers/logic_helper.rb', line 70 def convert_helper_wrappers(html = '') html.gsub(/{{ button(.+?)}}/, '{% button\1%}') .gsub(/{{ image_tag(.+?)}}/, '{% image_tag\1%}') end |
#convert_partials_to_includes(html = '') ⇒ Object
64 65 66 67 68 |
# File 'lib/antwort/helpers/logic_helper.rb', line 64 def convert_partials_to_includes(html = '') html.gsub(/{{ partial :(.+?) }}/, '{% include \1 %}') .gsub(/{% include (.+),\s+locals:(.+?)%}/, '{% include \1 with:\2%}') .gsub(/(<%=\s+?partial\s+?:)(.+?),(.+?)(locals:)((.|\n)+?)(%>)/, '{% include \2 with:\5%}') # multiline partials not caught in presumably leftover variable end |
#preserve_assignments(html = '') ⇒ Object
50 51 52 53 |
# File 'lib/antwort/helpers/logic_helper.rb', line 50 def preserve_assignments(html = '') html.gsub(/<%\s+([A-Za-z0-9_]+)\s*(\|\|=)\s*(.*)\s+%>/, '{% set \1 = \1 || \3 %}') .gsub(/<%\s+([A-Za-z0-9_]+)\s*(=)\s*(.*)\s+%>/, '{% set \1 = \3 %}') end |
#preserve_comments(html = '') ⇒ Object
38 39 40 41 |
# File 'lib/antwort/helpers/logic_helper.rb', line 38 def preserve_comments(html = '') html.gsub(/<%[ \t]*#(.*)%>/, '{#\1#}') .gsub(/{#([^=\s])/, '{# \1') # {#foo #} ->{# foo #} end |
#preserve_conditionals(html = '') ⇒ Object
23 24 25 26 27 28 29 30 |
# File 'lib/antwort/helpers/logic_helper.rb', line 23 def preserve_conditionals(html = '') html.gsub(/<%\s*if (.*?)%>/, '{% if \1 %}') # if .gsub(/<%\s*unless (.*?)%>/, '{% if !( \1) %}') # unless .gsub(/<%\s*elsif(.*?)%>/, '{% elseif\1%}') # elsif .gsub(/<%\s*else\s*%>/, '{% else %}') # else .gsub(/<%\s*end\s*%>/, '{% end %}') # end .gsub(/[ \t]{2,}%}/, ' %}') # remove extra white space, e.g. {% else %} end |
#preserve_leftover_statements(html = '') ⇒ Object
55 56 57 |
# File 'lib/antwort/helpers/logic_helper.rb', line 55 def preserve_leftover_statements(html = '') html.gsub(/<%\s*(.*?)?%>/, '{% \1%}') # no trailing space because group captures it end |
#preserve_logic(html = '') ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/antwort/helpers/logic_helper.rb', line 11 def preserve_logic(html = '') html = preserve_comments(html) html = preserve_conditionals(html) # conditionals before loops, in case we have them inside loops html = preserve_loops(html) html = preserve_variables(html) html = preserve_assignments(html) html = convert_partials_to_includes(html) html = convert_helper_wrappers(html) html = preserve_leftover_statements(html) # must be last html end |
#preserve_loops(html = '') ⇒ Object
32 33 34 35 36 |
# File 'lib/antwort/helpers/logic_helper.rb', line 32 def preserve_loops(html = '') html.gsub(/<%\s+(.*)(\.each\s+do\s+\|)\s*(\S+)\s*(\|\s+)%>/, '{% for \3 in \1 %}') .gsub(/<%\s+(.*)(\.each_with_index\s+do\s+\|)\s*(\S+)\s*,\s*(\S+)\s*(\|\s+)%>/, '{% for \3 in \1 with: {@index: \4} %}') .gsub(/<%\s*end\s*%>/, '{% end %}') end |
#preserve_nbsps(html = '') ⇒ Object
3 4 5 |
# File 'lib/antwort/helpers/logic_helper.rb', line 3 def preserve_nbsps(html = '') html.gsub(/ /, '%nbspace%') end |
#preserve_variables(html = '') ⇒ Object
43 44 45 46 47 48 |
# File 'lib/antwort/helpers/logic_helper.rb', line 43 def preserve_variables(html = '') html.gsub(/\[:(.*?)\]/, '.\1') # a[:b][:c] -> a.b.c .gsub(/\['(.*?)'\]/, '.\1') # a['b'] -> a.b .gsub(/\["(.*?)"\]/, '.\1') # a["b"] -> a.b .gsub(/<%=(.*?)%>/, '{{\1}}') # assume leftover erb output are variables end |
#restore_nbsps(html = '') ⇒ Object
7 8 9 |
# File 'lib/antwort/helpers/logic_helper.rb', line 7 def restore_nbsps(html = '') html.gsub(/%nbspace%/, ' ') end |
#restore_variables_in_links(html = '') ⇒ Object
59 60 61 62 |
# File 'lib/antwort/helpers/logic_helper.rb', line 59 def restore_variables_in_links(html = '') html.gsub('%7B%7B%20', '{{ ') .gsub('%20%7D%7D', ' }}') end |