Class: Deplate::Command::ABBREV

Inherits:
Deplate::Command show all
Defined in:
lib/deplate/commands.rb

Constant Summary collapse

@@abbrevn =
0

Class Method Summary collapse

Methods inherited from Deplate::Command

commands, #finish, #format_special, #process, register_as, #setup, #setup_command, update_variables

Methods included from Names

name_match_c, name_match_fs, name_match_sf

Methods inherited from Element

#join_lines, #join_lines_re_zh_cn

Class Method Details

.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 = <<-EOR
            set_rx(#{rx.inspect})
            def setup
                tx, cmd = @deplate.options.abbrevs[[#{rs.inspect}, @deplate.formatter.formatter_name]]
                case cmd
                when Proc
                    tx   = tx.call(self) if tx.kind_of?(Proc)
                    @elt = cmd.call(@container, tx)
                when String
                    @elt = cmd
                else
                    log(['Internal error', 'ABBREV', #{rs.inspect}, cmd.class], :error)
                end
            end
        EOR
        cls = Deplate::Cache.particle(deplate, body, 
                                      :register => true,
                                      :specific => specific,
                                      :unshift => args['priority']
                                     )
    else
        Deplate::Core.log(["No pattern specified", args], :error, src)
    end
end