Method: MindWords#import

Defined in:
lib/mindwords.rb

#import(raws) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/mindwords.rb', line 76

def import(raws)

  s, type = RXFReader.read raws

  @filepath = raws if type == :file or type == :dfs
  rawlines = (s.strip.gsub(/(^\n|\r)/,'') + "\n").lines.uniq
  rawlines.shift if rawlines.first =~ /<\?mindwords\?>/

  # remove mindwords lines which don't have a hashtag
  lines = rawlines.reject do |line|

    found = line[/^\w+[^#]+$/]

    if found then
      puts ('no hashtag found on this line -> ' + line).warn
    end

    found

  end

  #--- handle indented text, indicated there are *groups* of words

  s2 = lines.join.gsub(/\n\s*$/,'')
  puts s2 if @debug
  a = s2.strip.split(/(?=^\w+)/)
  a2 = a.inject([]) do |r,x|

    if x =~ /\n\s+/ then

      a4 = x.lines[1..-1].map do |line|

        puts 'x.lines[0]: ' + x.lines[0].inspect if @debug
        hashtag = if x.lines[0][/ /] then
          x.lines[0].gsub(/\b\w/) {|x| x.upcase}.gsub(/ /,'')
        else
          x.lines[0]
        end

        "%s #%s" % [line.strip, hashtag]
      end

      r.concat a4

    else
      r << x
    end

  end

  #-- end of indented text handler

  @lines = a2.inject([]) do |r,line|

    # the following does 2 things:
    #      1. splits words separated by a bar (|) onto their own line
    #      2. prefixes a word with an underscore if the word is the
    #         same as the hashtag. That way it's not removed by the
    #         redundancy checker

    raw_words, raw_hashtags = line.split(/(?= #)/,2)
    words = raw_words.split(/ *\| */)
    hashtags = raw_hashtags.scan(/(?<=#)\w+/)

    words.each do |word|

      linex = (word +  raw_hashtags)
      r << (hashtags.include?(word) ? linex.sub!(/\b#{word}\b/, '_\0') \
            : linex)
    end

    r
  end
end