Class: Tags::TagFriend

Inherits:
Object
  • Object
show all
Includes:
DateFormat
Defined in:
lib/docfolio/tags.rb

Overview

extracts and formats tags and pertaining text from a plain text paragraph

Instance Method Summary collapse

Methods included from DateFormat

#format_date

Instance Method Details

#extract_date(paragraph_text, date) ⇒ Array<String, Array, Time>

TODO:

move function to one of the date or time handling classes

The $LAST_MATCH_INFO global is equivalent to Rexexp.last_match and returns a MatchData object. This can be used as an array, where indices 1 - n are the matched backreferences of the last successful match

String return value

‘paragraph_text’ the same paragraph that was passed to the function but without the matched date character if there were any.

Array return value

‘time_array’ array of 4 integer representing the hours and minutes of the from and to times

Time return value

‘date’ the date in (day month year) of this paragraph taken from the matched date_regex if there was one. Will be nil if there was no match and if the date passed to the function was also nil.

Parameters:

  • paragraph_text (String)

    a paragraph from a DSL text file

  • date (Time)

    of this paragraph. May be nil if not known.

Returns:

  • (Array<String, Array, Time>)

    Array of values to be returned



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/docfolio/tags.rb', line 27

def extract_date(paragraph_text, date)
  time_array = []

  # if text contains a date match
  if date_regex =~ paragraph_text 
    # $' (or $POSTMATCH), contains the characters after the match position
    paragraph_text = $' 

    # strip whitespace if any remaining match or set to empty string 
    # if no match. If there is just white space after the match then
    # this is truncated to an empty string
    paragraph_text.nil? ? paragraph_text = '' : paragraph_text.strip!
    
    # extracts the 'from' and 'to' times from the last match above. the 
    # time_array contains from_hour, from_min, to_hour, to_min, the
    # date parameter is updated if the match found a new date
    time_array, date = date_from_globals($LAST_MATCH_INFO, date)
  end
  [paragraph_text, time_array, date]
end

#extract_tags(paragraph_text) ⇒ Object



48
49
50
# File 'lib/docfolio/tags.rb', line 48

def extract_tags(paragraph_text)
  tag_regex =~ paragraph_text ? extract_tag(paragraph_text) : []
end