Class: Body

Inherits:
Object
  • Object
show all
Extended by:
Logging
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
@@log =

initialize the class-level logger as configured

init_logger(@@config.log_target, @@config.log_level)

Instance Method Summary collapse

Methods included from Logging

init_logger, log_level=, log_target=

Constructor Details

#initialize(article_text) ⇒ Body

reads the body text of the article



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/body.rb', line 36

def initialize(article_text)
# for simplicity.
@log = @@log 
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)
@log.debug('initialize(): body lines are ' << @lines.inspect)

# if need be, extract references and footnotes.
handle_references

# verify and eventually correct URLs
handle_urls
end

Instance Method Details

#joinObject



139
140
141
# File 'lib/body.rb', line 139

def join 
  return @lines.join($LN) 
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.



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 59

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

  @log.debug('FUP_NAME is ' << @@config.FUP_NAME)
  @log.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|
        @log.debug("\tmatch: " << md.to_s)
        md.length == 2 ? md[1] : md[0]
      end
      @log.debug("\tfup_name: " << fup_name.to_s) 

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

      # 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 citation, at all
        if(line.start_with?('>'))
          @log.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 
          @log.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



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/body.rb', line 120

def set_signature(signature)
  # unless no changes requested.
  # no_sig is for later
  if signature && !signature.empty? && !@@config.no_sig
    nsig = @lines.count{|s| s == "-- "}
    # several signatures should be avoided!
    if nsig > 1 
      @log.debug "Found #{nsig} signatures"
      @log.debug "PSE remove a few and leave ... like only 1 intact."
      exit false
    else
      @log.debug('setting signature ' << signature) if signature
      @lines << "-- " << signature if signature
    end
  else
    return
  end
end