Method: InformWriter#new_tag

Defined in:
lib/IFMapper/InformWriter.rb

#new_tag(elem, str) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/IFMapper/InformWriter.rb', line 36

def new_tag(elem, str)
  tag = str.dup
  if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('1.9.0')
    utf = Iconv.new( 'iso-8859-1', 'utf-8' )
    tag = utf.iconv( tag )
  else
    tag = tag.encode( 'utf-8', :invalid => :replace, 
                      :undef => :replace, :replace => '' )
  end

  # Invalid tag characters, replaced with _
  tag.gsub!(/[\s"'\/\\\-&#\,.:;!\?\n\(\)]/,'_')
 
  tag.gsub!(/__/, '')                  # remove reduntant __ repetitions
  tag.sub!(/^([\d]+)_?(.*)/, '\2\1')   # No numbers allowed at start of tag
  tag.downcase!                        # All tags are lowercase

  

  tag = tag[0..31]                     # Max. 32 chars. in tag (inform limit)

  # tag cannot be keyword (Doorway, Room, Class, etc)
  if tag =~ @keyword
    tag << '1'
  end

  if @tags.values.include?(tag)
    idx  = 2
    root = tag[0..29]  # to leave room for number
    while @tags.values.include?(tag)
	tag = "#{root}#{idx}"
	idx += 1
    end
  end
  if elem.kind_of?(String)
    @tags[tag] = tag
  else
    @tags[elem] = tag
  end
  return tag
end