Class: SportDb::PrettyPrinter

Inherits:
Object
  • Object
show all
Includes:
LogUtils::Logging
Defined in:
lib/sportdb/pretty_printer.rb

Constant Summary collapse

SCORES_REGEX =

change to DE_SCORES_REGEX or SCORES_DE_REGEX ??

/\b
 (?<score1>\d{1,2})
   -
 (?<score2>\d{1,2})
 \b
/x

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text, opts = {}) ⇒ PrettyPrinter

Returns a new instance of PrettyPrinter.



103
104
105
106
# File 'lib/sportdb/pretty_printer.rb', line 103

def initialize( text, opts={} )
  @old_text = text
  @opts     = opts
end

Class Method Details

.from_file(path, opts = {}) ⇒ Object



91
92
93
94
95
96
# File 'lib/sportdb/pretty_printer.rb', line 91

def self.from_file( path, opts={} )
  ## note: assume/enfore utf-8 encoding (with or without BOM - byte order mark)
  ## - see textutils/utils.rb
  text = File.read_utf8( path )
  self.from_string( text, opts )
end

.from_string(text, opts = {}) ⇒ Object



98
99
100
# File 'lib/sportdb/pretty_printer.rb', line 98

def self.from_string( text, opts={} )
  self.new( text, opts )
end

Instance Method Details

#patchObject

return new text (and change log??)



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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/sportdb/pretty_printer.rb', line 117

def patch  ## return new text (and change log??)
  new_text = ''
  change_log = []   ## track changes  - rename - use edits? changes? etc.
  line_no = 0

  @old_text.each_line do |line|
    line_no +=1

    # comments allow:
    # 1) #####  (shell/ruby style)
    # 2) --  comment here (haskel/?? style)
    # 3) % comment here (tex/latex style)

    if line =~ /^\s*#/ || line =~ /^\s*--/ || line =~ /^\s*%/
      # skip komments and do NOT copy to result (keep comments secret!)
      logger.debug "[#{line_no}] add comment line >#{line}<"
      new_text << line
      next
    end

    if line =~ /^\s*$/ 
      # kommentar oder leerzeile überspringen 
      logger.debug "[#{line_no}] add blank line >#{line}<"
      new_text << line
      next
    end

    ## do nothing for now
    if line =~ SCORES_REGEX
      ## patch scores format
      logger.debug "** found scores >#{line}<"

      new_line = line.dup
      new_line.gsub!( SCORES_REGEX ) do |m|
        logger.debug " match: >#{m}<  : #{m.class.name}"

        old_scores = m
        ## todo: how to use named captures? possible?
        new_scores = "#{$1}:#{$2}"

        change_log << [line_no, "changed >#{old_scores}< to >#{new_scores}< in >#{line}<"]

        new_scores
      end

      logger.debug "[#{line_no}] add patched line >#{new_line}<"
      new_text << new_line
    else
      logger.debug "[#{line_no}] add >#{line}<"
      new_text << line
    end
  end # each lines

  [new_text, change_log]
end