Method: Deplate::Command::ABBREV.accumulate

Defined in:
lib/deplate/commands.rb

.accumulate(src, array, deplate, text, match, args, cmd) ⇒ Object



774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
# File 'lib/deplate/commands.rb', line 774

def accumulate(src, array, deplate, text, match, args, cmd)
    Deplate::Core.log("%s: %s" % [cmd, text], :debug, src)
    rx  = nil
    rs  = nil
    tx  = nil
    cmd = nil
    catch(:exit) do
        w = args['word'] || args['w'] || args['wd']
        if w
            # rs = %{\\b%s\\b} % Regexp.escape(w)
            # rs = %{\\b%s(?=[^#{Deplate::HyperLink.chars}])} % Regexp.escape(w)
            rs = %{\\b%s(?=([[:punct:][:cntrl:][:space:]]|$))} % Regexp.escape(w)
            # tx = "#{Deplate::Core.remove_backslashes(text.inspect)}"
            # tx = "#{text.inspect}"
            # tx = text.inspect
            tx = text
            throw :exit
        end
        s = args['symbol'] || args['sym']
        if s
            rs = %{`%s} % Regexp.escape(s)
            # tx = "#{Deplate::Core.remove_backslashes(text.inspect)}"
            # tx = text.inspect
            tx = text
            throw :exit
        end
        r = args['regexp'] || args['rx']
        if r
            rs = r
            tx = lambda {|p| p.match[0].gsub(Regexp.new(r), text)}
            throw :exit
        end
    end
    if rs
        if args['plain']
            # cmd = %{@deplate.formatter.plain_text(#{tx})}
            cmd = lambda {|c, t| c.deplate.formatter.plain_text(t)}
            specific = false
        elsif args['native'] or args['ins']
            cmd = lambda {|c, t| t}
            specific = true
        else
            # cmd = %{@deplate.parse_and_format(@container, #{tx}, false)}
            cmd = lambda {|c, t| c.deplate.parse_and_format(c, t, false)}
            specific = false
        end
        deplate.options.abbrevs[[rs, deplate.formatter.formatter_name]] = [tx, cmd]
        rx = Regexp.new("^#{rs}")
        # body = <<-EOR
        #     set_rx(#{rx.inspect})
        #     def setup
        #         @elt = #{cmd}
        #     end
        # EOR
        body = "            set_rx(\#{rx.inspect})\n            def setup\n                tx, cmd = @deplate.options.abbrevs[[\#{rs.inspect}, @deplate.formatter.formatter_name]]\n                case cmd\n                when Proc\n                    tx   = tx.call(self) if tx.kind_of?(Proc)\n                    @elt = cmd.call(@container, tx)\n                when String\n                    @elt = cmd\n                else\n                    log(['Internal error', 'ABBREV', \#{rs.inspect}, cmd.class], :error)\n                end\n            end\n        EOR\n        cls = Deplate::Cache.particle(deplate, body, \n                                      :register => true,\n                                      :specific => specific,\n                                      :unshift => args['priority']\n                                     )\n    else\n        Deplate::Core.log([\"No pattern specified\", args], :error, src)\n    end\nend\n"