class Dyndoc::DevTagScanner

Constants

TXT_DTAG

Attributes

dtag[RW]

Public Class Methods

new(type=:dtag,start=nil,stop=nil,mode=nil,escape=nil) click to toggle source
Calls superclass method Dyndoc::Scanner::new
# File lib/dyndoc/base/scanner.rb, line 273
def initialize(type=:dtag,start=nil,stop=nil,mode=nil,escape=nil)
  super
  init_tag(@tag_type) if [:dtag].include? @tag_type
end

Public Instance Methods

ajust_with_blck(res) click to toggle source
# File lib/dyndoc/base/scanner.rb, line 701
def ajust_with_blck(res)
  return res unless @tag_blck[:keyword].include? res[0]
  instr=@tag_blck[:keyword][res[0]]
  res_blck,blck=[],nil
  begin
    b=res.shift
    if instr.include? b
      #create the :blck block
      res_blck << b
      #is there an :args block?
      res_blck << res.shift if @tag_arg.include? b
      #if no first tag_blck then put the default :out tag
      blck=[:blck]
      blck << :out unless @@tagblck_set.include? res[0]
    else
      if blck
        blck << b
      else
        #needed for example :case block
        res_blck << b
      end
      #is the end of blck?
      if res.empty? or (instr.include? res[0])
        res_blck << blck if blck
        blck=nil
      end
    end
  end until res.empty?
  res_blck
end
check_until_for_named_tags() click to toggle source
# File lib/dyndoc/base/scanner.rb, line 521
def check_until_for_named_tags
  #if res
  @named_tags.each_index{|i|
    if @scan[3+i]
      @tag_selected=@scan[3+i].to_sym
#puts "here we go: #{@tag_selected}"
      break
    end
  }
  #end
  #return res
end
complete_tag(key,add=true) click to toggle source
# File lib/dyndoc/base/scanner.rb, line 507
def complete_tag(key,add=true)
  #Regexp.escape(key.to_s)+((@dtag[:named_tag][key]) ? @dtag[:named_tag][key] : "")
  if @dtag[:named_tag][key]
    if add
      @named_tags << key
      return nil
    else
      @dtag[:named_tag][key][:tag].sub("_TAG_",Regexp.escape( key.to_s ))
    end
  else
    return Regexp.escape(key.to_s)
  end
end
convert_block(blck) click to toggle source

ATTENTION: Ne pas faire de recurrence dans convert_block à cause de @scan! qui est en unique exemplaire!

# File lib/dyndoc/base/scanner.rb, line 535
    def convert_block(blck)
#puts "split_block";p blck
      @block= blck[:txt]
      inside=blck[:inside]
      style=blck[:type].include? @tag_style
      pre_res=[]
      res=[]
=begin
      ## Mode -> unused now!
      mode=@block[0,@mode[:length]]
      mode2=(@@close[mode] ? @@close[mode] : mode)
=end
      @scan.string=@block
      @scan.pos=0
      ## Instruction
      #@scan.scan_until(/(?:#{@tag_instr.join("|")})/)
      @scan.scan_until(@start)
      instr=@scan[1] #@scan.matched
#p [:instr,instr]
      # next block is a arg block?
      @is_arg=false
      if @tag_instr.include? instr
        instr=instr.to_sym
        instr=@tag_alias[instr] if @tag_alias.include? instr
        if instr.is_a? Array
          res += instr
          instr=instr[0]
        else
          res << instr
        end
        if (@tag_arg+@tag_code).include? instr
          case @dtag[:mode_arg]
          when :find
            res << find_args(inside)
          when :next_block
            @is_arg=true
          end
        end
      else
              instr2=(style ?  :style : :call)
        #pour un éventuel ajout de > ou < à la fin
        if [">","<"].include? instr[-1,1]
          instr2,instr=(instr2.to_s+instr[-1,1]).to_sym,instr[0...-1]
        end
        res << instr2 << [:args, {:txt=>instr,:inside=>[]}]
        instr=instr2
      end
      ## Text block
      ## find keywords
      from=true
#p "todoooo"
      while from
        from=@scan.pos
#p @tag_keyword[instr]
#p /(#{@tag[:block]}?)\s*(#{@tag[:keyword][0]}(?:#{keytags=(@tag_keyword[instr] ? @tag_keyword[instr] : [] ).map{|e| Regexp.escape(e.to_s)}.join("|")})#{@dtag[:empty_keyword][0]}#{@tag[:keyword][1]})/
        tag_keyword=nil
        if @dtag[:empty_keyword][0].empty?
          #no empty tag!
          tag_keyword=@tag_keyword[instr] if @tag_keyword[instr]
        else
          tag_keyword=(@tag_keyword[instr] ? @tag_keyword[instr] : [] )
        end
              keytags=nil
              @named_tags,@tag_selected=[],nil
              if tag_keyword
#p tag_keyword
                if @tag_keyword_reg and @tag_keyword_reg[instr]
                  tag_keyword=@tag_keyword_reg[instr]
                  blocktag_reg=/(#{@tag[:block]}?)\s*(#{@tag[:keyword][0]}#{tag_keyword}#{@dtag[:empty_keyword][0]}#{@tag[:keyword][1]})/
                  tag_reg=/#{tag_keyword}/
                else
                  keytags=tag_keyword.map{|e| complete_tag(e)}.compact.join("|")
#puts "keytags(1)";p keytags
#puts "INSTR="; p instr
                  if @dtag[:with_tagblck].include? instr
                    keytags += "|" unless keytags.empty?
                    keytags += @@keystagblck
                    @named_tags += @@named_tags_blck
                  end
##puts "keytags(2)";p keytags
                  tag_reg=/(?:#{keytags})/
                  unless @named_tags.empty?
                    keytags += "|"+(@named_tags.map{|tag| complete_tag(tag,nil)}.join("|"))
                  end
            ##Dyndoc.warn "keytags",keytags
                  blocktag_reg=/(#{@tag[:block]}?)\s*(#{@tag[:keyword][0]}(?:#{keytags})#{@dtag[:empty_keyword][0]}#{@tag[:keyword][1]})/
                end
##Dyndoc.warn "to scan", @scan.string[@scan.pos..-1]
##Dyndoc.warn "tag_reg",[blocktag_reg,tag_reg]
##p ["tag_reg",[blocktag_reg,tag_reg]]
              end
        if (tag_keyword and (@scan.check_until(blocktag_reg))) #or (!@named_tags.empty? and check_until_for_named_tags)
                check_until_for_named_tags unless @named_tags.empty?
          key=@scan[2]
##Dyndoc.warn p ["keyword",[key.scan(tag_reg),key.scan(tag_reg)[-1],key,@scan[0],@scan[1],@scan[2]]]
##Dyndoc.warn "pre_math,tag_selected",[@scan.pre_match,@tag_selected] if key=="[#tag]"
          res << find_text(from,key,inside)
          @is_arg=false if @is_arg
##Dyndoc.warn "key(AV)",[key,tag_reg] if key=="[#tag]"
                if @tag_selected
#puts "tag_selected";p @tag_selected
                  res << (key=@tag_selected)
                else
            #key=tag_reg.match(key)[0]
                  key= key.scan(tag_reg)[-1]
##Dyndoc.warn "key(AP)",key if key=="tag"
                  res << (key=key.to_sym) if key and !key.empty?
                end
            #res << (key=key[1..-1].to_sym)
      #p @tag_arg
          if @tag_arg.include? key #without @tag_code inside a block
            case @dtag[:mode_arg]
            when :find
              res << find_args(inside)
              from=@scan.pos
            when :next_block
              @is_arg=true
            end
          end
        else
          #Last text block!
#p /(#{@tag[:block]}?)\s*(#{@tag[:stop]})/
          @scan.check_until(/(#{@tag[:block]}?)\s*(#{@tag[:stop]})/)
#puts "last"; p @scan[2];p @scan[0]; p @scan[1]
#p @scan.pre_match
          res << find_text(from,@scan[2],inside)
#p res
          from=false
        end
#puts "from2";p from
      end
#puts "res";p res
      return res
    end
find_args(inside) click to toggle source

Types of result block:

1) :main
2) :args
3) :instr (:if, :case, ...)

Types of parsed block:

1) :text -> main block alternating text and dtag blocks
2) :dtag -> {% ...%}
# File lib/dyndoc/base/scanner.rb, line 438
def find_args(inside)
  ## Instruction delimiter
  delim=@block[@scan.pos,1]
  #p delim
  @scan.pos += delim.bytesize #.length
  st=@scan.pos
  delim=@@close[delim] if @@close[delim]
  #p delim
  ## Arguments
  begin
    @scan.scan_until(/#{Regexp.escape(delim)}/)
  end while @block[@scan.pos-2,1]=='\\'
  sp=@scan.pos - delim.bytesize - 1 #.length - 1
  args=@block.byteslice(st..sp) #@block[st..sp]
  inside2=[]
  (1..(args.split(@re_strange,-1).length-1)).each{ inside2 << inside.shift }
  return [:args,{:txt=>args,:inside=>inside2}]
end
find_text(from,key,inside) click to toggle source
# File lib/dyndoc/base/scanner.rb, line 457
    def find_text(from,key,inside)
#p ["key=",key]
      res,to=nil,nil
      pre=(@next_pre ? @next_pre : nil )
      @next_pre=nil
      if @scan[1].nil? or @scan[1].empty? or @is_arg
#p ["key=",key,@tag_selected]
        if @tag_selected
          @next_pre=key[2...-1].scan(@dtag[:named_tag][@tag_selected.to_sym][:rest])[0][0]
          @next_pre=nil if @next_pre.empty?
        end
        @scan.scan_until(/#{Regexp.escape(key)}/)
        to=@scan.pre_match.bytesize #.length
        ##Dyndoc.warn "TOOOOOOOOOOO",[@scan.pre_match,to]
        res=@block.byteslice(from...to) #@block[from...to]
      else
#p ["pre_match=",@scan.pre_match,"to=",@scan.pre_match.bytesize]
        to=@scan.pre_match.bytesize #.length
        ##Dyndoc.warn "TOOOOOOOOOOO2222",[@scan.pre_match,to]
        delim2=@scan[1]
        delim2=@@open[delim2] if @@open[delim2]
#p ["delim2=",delim2,/#{Regexp.escape(delim2)}/]
#p @block[from...-1]
        @scan.exist?(/#{Regexp.escape(delim2)}/)
        to_tmp=@scan.pre_match.bytesize #.length
#p ["to_tmp=",to_tmp]
        ## pre=@block[from...to_tmp].strip unless pre
#p [:pre,pre]
        pre=@block.byteslice(from...to_tmp).strip unless pre
#p ["pre=",pre]
        from=to_tmp+1
#p ["key:",key,@scan.matched,from,@block[from-1,1]]
        @scan.scan_until(/#{@tag[:block]}\s*#{Regexp.escape(key)}/)
=begin
        @scan.scan_until(/[\]|]\s*#{key}/)
=end
#p ["matched=",@scan.matched]
#p @scan.pre_match
#to=@scan.pre_match.length
        res=@block.byteslice(from...to) #does not work for ruby2 => @block[from...to]
        pre=nil if pre.empty?
#p ["res=",from,to,res,@block.byteslice(from...to)]
      end
    inside2=[]
    (1..(res.split(@re_strange,-1).length-1)).each{ inside2 << inside.shift }
    return [:text,{:name=>pre,:txt=>res,:inside=>inside2}] if pre
    return [(@is_arg ? :args : :text),{:txt=>res,:inside=>inside2}]
    end
get_tag_blck() click to toggle source
# File lib/dyndoc/base/scanner.rb, line 268
def get_tag_blck
  @@tagblck_set
end
init_tag(type=:dtag) click to toggle source
# File lib/dyndoc/base/scanner.rb, line 393
def init_tag(type=:dtag)
  @dtag=@@dtag[type]
  @tag_instr=@dtag[:instr]+@dtag[:alias].keys.map{|e| e.to_s}
  @tag_alias=@dtag[:alias]
  @tag_keyword=@dtag[:keyword]
  @tag_keyword_reg=@dtag[:keyword_reg]
  @tag_arg=@dtag[:arg]
  @tag_code=(@dtag[:tag_code] ? @dtag[:tag_code] : [] )
  @tag_style=@dtag[:style]
  @tag_blck=@dtag[:blck]
  ## deal with fixed blck tags
  @named_tags=[]
  @@keystagblck=@@tagblck_set.map{|e| complete_tag(e)}.compact.join("|") #only once!
  @@named_tags_blck=@named_tags
end
merge_tag(dtag) click to toggle source
# File lib/dyndoc/base/scanner.rb, line 409
def merge_tag(dtag)
  if dtag
    @tag_instr += dtag[:instr] if dtag[:instr]
    @tag_instr += dtag[:alias].keys.map{|e| e.to_s} if dtag[:alias]
    @tag_alias.merge!(dtag[:alias]) if dtag[:alias]
    @tag_keyword.merge!(dtag[:keyword]) if dtag[:keyword]
    @tag_keyword_reg.merge!(dtag[:keyword_reg]) if dtag[:keyword_reg]
    @tag_arg += dtag[:arg] if dtag[:arg]
    if dtag[:blck]
      @tag_blck[:instr] += dtag[:blck][:instr] if dtag[:blck][:instr]
      @tag_blck[:keyword].merge!(dtag[:blck][:keyword]) if dtag[:blck][:keyword]
    end
  end
end
parse_args(blck) click to toggle source
# File lib/dyndoc/base/scanner.rb, line 732
    def parse_args(blck)
      res=[:args]
#puts ":args";p blck[:txt]
      parts=blck[:txt].split(@re_strange,-1)
#p parts
#p blck[:inside]
      blck[:inside].map do |e|
        res << [:main,parts.shift]
        res << parse_block(e)
      end
      res << [:main,parts.shift]
      res
    end
parse_block(blck) click to toggle source
# File lib/dyndoc/base/scanner.rb, line 670
    def parse_block(blck)
      res=convert_block(blck)
#puts "res";p res
      ##Stop scan when :txt instruction to avoid parsing!
      if TXT_DTAG.include? res[0]
        res2=[res[0],rebuild_after_filter(res[1][1])]
        # NO MORE POSSIBLE! res2 << res[1][1][:name] if res[1][1][:name]
#p res2
        return res2
      end
      res2=[]
      res.each{|e|
#p e
        if e.is_a? Array
          case e[0]
            when :args
              res2 << parse_args(e[1])
            when :text
              res2 += parse_text(e[1])
          end
        else
          res2 << e
        end
      }
#p res2
      #make :blck block if necessary
      res2=ajust_with_blck(res2)
#puts "res2";p res2
      return res2
    end
parse_text(blck) click to toggle source
# File lib/dyndoc/base/scanner.rb, line 756
    def parse_text(blck)
#puts "blck";p blck
      res=[]
      blck[:txt].split(@re_strange2,-1).each{|e|
        if e==@strange

          b=blck[:inside].shift
          res_b=parse_block(b)
          if (res_modif=parsed_block_with_modifier(res_b))
            res << res_modif
          end
#puts "parse_text: res_b";p res_b
          res << res_b
        else
          res << [:main,e]
        end
      }
      res=[[:named,blck[:name]]+res] if blck[:name]
      res
    end
parsed_block_with_modifier(res_block) click to toggle source
# File lib/dyndoc/base/scanner.rb, line 746
def parsed_block_with_modifier(res_block)
  res=nil
  instr=res_block[0,1].to_s
  if !["<","<<",">"].include? instr and [">","<"].include?(modif=instr[-1,1])
    res=modif.to_sym
    res_block[0,1]=instr[0...-1].to_sym
  end
  res
end
pretty_print(res,tab=0) click to toggle source
# File lib/dyndoc/base/scanner.rb, line 794
def pretty_print(res,tab=0)
  res.each{|b|
    if b.is_a? Array
      pretty_print(b,tab+1)
    else
      puts "  "*tab+b.inspect+"\n"
    end
  }
end
process(txt) click to toggle source
# File lib/dyndoc/base/scanner.rb, line 789
def process(txt)
  tokenize(txt)
  parse_text(extract)
end
rebuild_after_parse(res) click to toggle source
# File lib/dyndoc/base/scanner.rb, line 777
def rebuild_after_parse(res)
    txt=""
    start=0
    parts=res[:txt].split(@re_strange,-1)
    res[:inside].map do |e|
      txt << parts.shift
      txt << (e[:inside] ? rebuild_after_parse(e) : e[:txt])
    end
    txt << parts.shift
    txt
end
update_tag_keyword(keyword,tags_set=[],args={}) click to toggle source

update an existing tag keywork (ex: document for html structure by adding css js menu or anything needed in the model)

# File lib/dyndoc/base/scanner.rb, line 425
def update_tag_keyword(keyword,tags_set=[],args={})
  @tag_keyword[keyword] += tags_set
  @tag_blck[:keyword][keyword] += new_tags_set if args[:mode] && args[:mode] == :blck
end