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. It processes the original text and changes some details:

intro-lines
signatures
URI formats
Footnotes (and/or a list of references) are created

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



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/body.rb', line 32

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,



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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/body.rb', line 176

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 && ref.length > 0
        if ref 
          #  ... 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')) if !references.empty?
    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
    else
      debug('no refences found')
    end
  end
  @lines = body.split($LN)
end

#handle_urisObject

Verify and possibly correct links in the post. Simple.



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/body.rb', line 147

def handle_uris() 
  # 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 @
        # BUGFIX : email address in the intro 
        new_line = handle_news(l) if !@intro || l.strip != @intro.strip
        # 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



133
134
135
136
137
138
# File 'lib/body.rb', line 133

def join 
  new_body = @lines.join("\r\n") << "\r\n"
  # replace ellipsis 
  new_body.gsub!('...', '')
  return new_body
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.



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
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/body.rb', line 49

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.
          
          # keep this line for reference.
          @intro = intro
        else
          wmsg = 'Text following the intro is not a citation!'
          wmsg << "\n\tIntro will NOT be modified: \"#{ointro}\"!"
          warn wmsg
        end
      end
    end # fn.strip.empty?
  end # lines.each_with_index
end

#set_signature(signature) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/body.rb', line 120

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 in line ' << sigpos.to_s) if sigpos
    @lines = @lines.slice(0, sigpos ) if sigpos
    debug('setting signature ' << signature) if signature
    @lines << "-- " << signature if signature
  end
end