Class: Body

Inherits:
Object
  • Object
show all
Includes:
BasicLogging
Defined in:
lib/body.rb

Overview

an object of this class represents the body of a news-article.

Constant Summary collapse

@@config =

a class-level configuration instance.

Configuration.instance

Constants included from BasicLogging

BasicLogging::DEBUG, BasicLogging::ERROR, BasicLogging::FATAL, BasicLogging::INFO, BasicLogging::Levels, BasicLogging::UNKNOWN, BasicLogging::WARN

Instance Method Summary collapse

Methods included from BasicLogging

#clear_log, is_muted?, #level, #log, mute, #set_level, #set_target, #target

Constructor Details

#initialize(article_text) ⇒ Body

reads the body text of the article



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/body.rb', line 27

def initialize(article_text)
  debug 'body intialize'
  # for simplicity.
  line = nil
  #  transform the article into an array.
  line_array = article_text.split($LN)
  # keep only from the first after an empty line  ''
  start_index = line_array.index('')

  # ... to the end of the current array (all that follows '').
  @lines = line_array.slice(start_index + 1, line_array.size)
  debug('initialize(): body lines are ' << @lines.inspect)
end

Instance Method Details

#handle_referencesObject

extract URL or other stuff, if configured for footnotes,



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/body.rb', line 159

def handle_references()
  # a symbol or string to mark the beginning an ending of a future footnote.
  ref_delim = @@config.REFERENCES_DELIMITER
  debug('references delimiter is ' << ref_delim)
  references = Array.new
  body = @lines.join($LN)
  if ref_delim && !ref_delim.strip.empty? 
    unless ref_delim == ref_delim.reverse
      ref_delim.strip!
      ref_rx = Regexp.new(ref_delim.dup << ".*?" << ref_delim.reverse, Regexp::MULTILINE) 
      debug('ref_rx is ' << ref_rx.to_s)
      index = 0
      # I cannot work with an array, here, and apply the pattern
      # to the whole body, over multiple lines, if need be.
      begin
        ref = body.match(ref_rx )
        debug("found reference " << ref.to_s << " (length: " << (ref ? ref.to_s.size.to_s : '0') << ")")
        if ref 
          debug('ref is ' << ref.to_s)
          #  ... This is some presentation thing and I think
          #  it works, too.
          r = ref[0].gsub(/[ \t]+/, ' ').strip
          r.gsub!("\n", "\n   ") 
          references << r  
          index += 1
          body.gsub!(ref[0], format(@@config.REFERENCE_FORMAT, index.to_s ))
        end
      end until ref == nil
      debug("all references found:\n" << references.join('\n'))
    else
      msg = 'The References Delimiter is the same in its reversed form.'
      msg << "#{$LN}Cannot handle references or footnotes!"
      error(msg)
    end

    if(references && !references.empty?) 
      # a line, separating the footnotes from the body of the article
      body << $LN << @@config.REFERENCES_SEPARATOR << $LN
      references.each_with_index do |r, i| 
        r = r.gsub(ref_delim, '').gsub(ref_delim.reverse,'')
        body << (i + 1 ).to_s << ") " << r.strip <<  $LN
      end
    end
  end
  @lines = body.split($LN)
end

#handle_urlsObject

Verify and possibly correct links in the post. Simple.



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/body.rb', line 131

def handle_urls() 
  # Determine here or elsewhere if URLs shall be verified.
  # Default is != no. nil or '' do qualify as default.
  if @@config.VFY_URLS
    @lines.each_with_index do | l, i | 
      # leave cited lines as they are.
      if !l.start_with?( '>') 
        # IMPORTANT --------------------------------->
        # IT IS HENCEFORTH PROHIBITED TO WRITE AN EMAIL-ADDRESS
        # IN THE BODY OF A NEWS-POST AND TO NOT PREPEND IT WITH
        # mailto:
        #  ... Because I do not know what to do in these cases,
        #  and I am the boss!
        #  <----------------------------

        # BUGFIX : Urls with @
        new_line = handle_news(l)   
        # http(s)
        if l.include?('http')
          new_line = handle_http(l)
        end #if http
        @lines[i] = new_line if new_line
      end # if '>'
    end # @lines.each_with_index
  end # if verify
end

#joinObject



121
122
123
# File 'lib/body.rb', line 121

def join 
  return @lines.join("\r\n") << "\r\n"
end

#set_intro(intro) ⇒ Object

If so configured, replace an eventual followup-intro by the one configured for a group. This may depend on other conditions and must be triggered explicitly.



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
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
# File 'lib/body.rb', line 44

def set_intro(intro)
  return if !intro || intro.empty? || @@config.no_intro

  # name of the previous poster
  fup_name = nil
  # the current newsgroup 
  fup_group = nil

  debug('FUP_NAME is ' << @@config.FUP_NAME)
  debug('FUP_GROUP is ' << @@config.FUP_GROUP)
  # The expressions which allow the identification of both
  # in the current article.
  fn = @@config.FUP_NAME
  fg = @@config.FUP_GROUP

  # Okay, this is called parsing, when it is well done.
  # I just try and am happy when it works.
  @lines.each_with_index do |line, i|
    # find the name in the intro-line
    if !fn.strip.empty? && !line.strip.empty? && !fup_name
      # match a name
      fup_name = line.match(Regexp.new(fn) ) do |md|
        # debug("\tmatch: " << md.to_s)
        md.length == 2 ? md[1] : md[0]
      end
      debug("\tfup_name: " << fup_name.to_s) 

      if !fg.strip.empty? && !fup_group
        # match a group
        fup_group = line.match(Regexp.new(fg) ) do |md| 
          debug("\tmatch: " << md.to_s)
          md.length == 2 ? md[1] : nil 
        end 
      end
      debug "group is " << fup_group.to_s

      # All that follows depends on the presence of a name
      # in the intro-string.
      if fup_name && !fup_name.strip.empty?
        # keep the current intro for later
        ointro = line
        line = ''
        while line.strip.empty?
          i = i.next
          line = @lines[i]
        end
        # check if there is a quote, at all
        if(line.start_with?('>'))
          debug("\tfound intro " << ointro)
          # variables are part of the $intro.
          # Do substitutions.
          intro.sub!('%fup_name%', fup_name) if fup_name 
          intro.sub!('%fup_group%', fup_group)  if fup_group 
          debug("\tsetting intro " << intro.to_s)

          # exchange original intro-line against the new one
          @lines[@lines.index(ointro)] = intro.strip 
          # looked complicated because it is.
        end
      end
    end # fn.strip.empty?
  end # lines.each_with_index
end

#set_signature(signature) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/body.rb', line 108

def set_signature(signature)
  # unless no changes requested.
  if signature && !signature.empty? 
    # remove any signature(s) from 
    # the current article
    sigpos = @lines.index('-- ')
    debug('found signature at position ' << sigpos) if sigpos
    @lines = @lines.slice(0, sigpos ) if sigpos
    debug('setting signature ' << signature) if signature
    @lines << "-- " << signature if signature
  end
end