11#!/usr/bin/ruby
22#-------------------------------------------------------------------------------
33# NotePlan Tools script
4- # by Jonathan Clark, v1.9.2, 13 .2.2021
4+ # by Jonathan Clark, v1.9.2, 15 .2.2021
55#-------------------------------------------------------------------------------
66# See README.md file for details, how to run and configure it.
77# Repository: https://github.com/jgclark/NotePlan-tools/
6060$archive = 0
6161$remove_rescheduled = 1
6262$allNotes = [ ] # to hold all note objects
63- $notes = [ ] # to hold all relevant note objects
63+ $notes = [ ] # to hold all note objects selected for processing
6464$date_today = time_now . strftime ( DATE_TODAY_FORMAT )
6565$npfile_count = -1 # number of NPFile objects created so far (incremented before first use)
6666
@@ -140,7 +140,7 @@ def create_new_note_file(title, ext)
140140
141141def find_or_create_daily_note ( yyyymmdd )
142142 # Read in a note that we want to update. If it doesn't exist, create it.
143- puts " - starting to find_or_create_daily_note for #{ yyyymmdd } "
143+ puts " - starting find_or_create_daily_note for #{ yyyymmdd } " if $verbose > 1
144144 filename = "#{ yyyymmdd } .#{ NOTE_EXT } "
145145 noteToAddTo = nil # for an integer, but starting as nil
146146
@@ -188,6 +188,7 @@ def find_or_create_note(title)
188188 # the future, but for now I'll try to select the most recently-changed if
189189 # there are matching names.
190190
191+ puts " - starting find_or_create_note for '#{ title } '" if $verbose > 1
191192 new_note_id = nil # for an integer, but starting as nil
192193
193194 # First check if it exists in existing notes read in
@@ -287,7 +288,8 @@ def initialize(this_file)
287288 @is_today = false
288289 end
289290
290- puts "Init NPFile #{ @id } from #{ this_file } , updated #{ @modified_time } #{ @line_count } #{ @is_calendar } " if $verbose > 1
291+ modified = ( @modified_time . to_s ) [ 0 ..15 ]
292+ puts " Init NPFile #{ @id } : #{ @line_count } lines from #{ this_file } , updated #{ modified } " . colorize ( InfoColour ) if $verbose > 1
291293 end
292294
293295 # def self.new2(*args)
@@ -300,10 +302,11 @@ def initialize(this_file)
300302 # end
301303
302304 def append_new_line ( new_line )
303- # Append 'line' into position 'line_number'
305+ # Append 'new_line' into position
306+ # TODO: should ideally split on '\n' and add each potential line separately
304307 puts ' append_new_line ...' if $verbose > 1
305308 @lines << new_line
306- @line_count += 1
309+ @line_count = @lines . size
307310 end
308311
309312 def create_events_from_timeblocks
@@ -495,6 +498,19 @@ def clear_empty_tasks_or_headers
495498 puts " - removed #{ cleaned } empty lines" if $verbose > 0
496499 end
497500
501+ def insert_new_line ( new_line , line_number )
502+ # Insert 'new_line' into position 'line_number'
503+ puts ' insert_new_line ...' if $verbose > 1
504+ n = @line_count # start iterating from the end of the array
505+ line_number = n if line_number >= n # don't go beyond current size of @lines
506+ while n >= line_number
507+ @lines [ n + 1 ] = @lines [ n ]
508+ n -= 1
509+ end
510+ @lines [ line_number ] = new_line
511+ @line_count = @lines . size
512+ end
513+
498514 def remove_unwanted_tags_dates
499515 # removes specific tags and >dates from complete or cancelled tasks
500516 puts ' remove_unwanted_tags_dates ...' if $verbose > 1
@@ -548,23 +564,9 @@ def remove_rescheduled
548564 puts " - removed #{ cleaned } scheduled" if $verbose > 0
549565 end
550566
551- def insert_new_line ( new_line , line_number )
552- # Insert 'line' into position 'line_number'
553- puts ' insert_new_line ...' if $verbose > 1
554- n = @line_count # start iterating from the end of the array
555- line_number = n if line_number >= n # don't go beyond current size of @lines
556- while n >= line_number
557- @lines [ n + 1 ] = @lines [ n ]
558- n -= 1
559- end
560- @lines [ line_number ] = new_line
561- @line_count += 1
562- end
563-
564567 def move_daily_ref_to_daily
565568 # Moves items in daily notes with a >date to that corresponding date.
566569 # Checks whether the note exists and if not, creates one first at top level.
567- # TODO: Simplified at the moment to just work on a single line?
568570 puts ' move_daily_ref_to_daily ...' if $verbose > 1
569571 # noteToAddTo = nil
570572 n = 0
@@ -582,16 +584,16 @@ def move_daily_ref_to_daily
582584
583585 # the following regex matches returns an array with one item, so make a string (by join)
584586 # NOTE: thisthe '+?' gets minimum number of chars, to avoid grabbing contents of several [[notes]] in the same line
585- if line =~ /\s >\d {4}\ -\d {2}\ -\d {2}/
587+ if line =~ /\s >\d {4}-\d {2}-\d {2}/
586588 yyyy_mm_dd = ''
587- line . scan ( />(\d {4}\ -\d {2}\ -\d {2})/ ) { |m | yyyy_mm_dd = m . join }
589+ line . scan ( />(\d {4}-\d {2}-\d {2})/ ) { |m | yyyy_mm_dd = m . join }
588590 # puts " - found calendar link >#{yyyy_mm_dd} in header on line #{n + 1} of #{@line_count}" if is_header && ($verbose > 0)
589591 puts " - found calendar link >#{ yyyy_mm_dd } in notes on line #{ n + 1 } of #{ @line_count } " if $verbose > 0 # && !is_header
590592 yyyymmdd = "#{ yyyy_mm_dd [ 0 ..3 ] } #{ yyyy_mm_dd [ 5 ..6 ] } #{ yyyy_mm_dd [ 8 ..9 ] } "
591593 end
592594
593595 # Find the existing daily note to add to, or read in, or create
594- noteToAddTo = find_or_create_daily_note ( yyyymmdd ) - 1 # TODO: check me
596+ noteToAddTo = find_or_create_daily_note ( yyyymmdd )
595597 lines_to_output = ''
596598
597599 # Remove the >date text by finding string points
@@ -606,17 +608,17 @@ def move_daily_ref_to_daily
606608 # If no due date is specified in rest of the line, add date from the title of the calendar file it came from
607609 if line !~ />\d {4}\- \d {2}\- \d {2}/
608610 cal_date = "#{ @title [ 0 ..3 ] } -#{ @title [ 4 ..5 ] } -#{ @title [ 6 ..7 ] } "
609- puts " - '#{ cal_date } ' to add from #{ @title } " if $verbose > 1
611+ puts " - '> #{ cal_date } ' to add from #{ @title } " if $verbose > 1
610612 lines_to_output = line + " <#{ cal_date } \n "
611613 else
612614 lines_to_output = line
613615 end
614616 # Work out indent level of current line
615617 line_indent = ''
616618 line . scan ( /^(\s *)\* / ) { |m | line_indent = m . join }
617- puts " - starting line analysis at line #{ n + 1 } of #{ @line_count } with indent ' #{ line_indent } ' ( #{ line_indent . length } )" if $verbose > 1
619+ puts " - starting line analysis at line #{ n + 1 } of #{ @line_count } ( indent #{ line_indent . length } )" if $verbose > 1
618620
619- # Remove this line from the calendar note TODO: not all being removed
621+ # Remove this line from the calendar note
620622 @lines . delete_at ( n )
621623 @line_count -= 1
622624 moved += 1
@@ -628,7 +630,7 @@ def move_daily_ref_to_daily
628630 # What's the indent of this line?
629631 line_to_check_indent = ''
630632 line_to_check . scan ( /^(\s *)\S / ) { |m | line_to_check_indent = m . join }
631- puts " - for '#{ line_to_check . chomp } ' indent=' #{ line_to_check_indent } ' ( #{ line_to_check_indent . length } )" if $verbose > 1
633+ puts " - for '#{ line_to_check . chomp } ' ( indent #{ line_to_check_indent . length } )" if $verbose > 1
632634 break if line_indent . length >= line_to_check_indent . length
633635
634636 lines_to_output += line_to_check
@@ -663,7 +665,8 @@ def move_daily_ref_to_daily
663665 end
664666 end
665667
666- # append updated line(s) after header lines in the note file
668+ # append updated line(s) in the daily note file
669+ puts " - appending line to daily note id #{ noteToAddTo } ..."
667670 $allNotes[ noteToAddTo ] . append_new_line ( lines_to_output )
668671
669672 # write the note file out
@@ -1264,7 +1267,13 @@ def rewrite_file
12641267 puts " Looking for daily note filenames matching glob_pattern #{ glob_pattern } :" if $verbose > 0
12651268 Dir . glob ( glob_pattern ) . each do |this_file |
12661269 puts " - #{ this_file } " if $verbose > 0
1267- $notes << NPFile . new ( this_file ) unless File . zero? ( this_file ) # read in file unless this file is empty
1270+ # read in file unless this file is empty
1271+ next if File . zero? ( this_file )
1272+
1273+ this_note = NPFile . new ( this_file )
1274+ $allNotes << this_note
1275+ # copy the $allNotes item into $notes array
1276+ $notes << this_note
12681277 end
12691278 end
12701279 rescue StandardError => e
@@ -1278,7 +1287,7 @@ def rewrite_file
12781287 $allNotes. each do |this_note |
12791288 next unless this_note . modified_time > ( time_now - hours_to_process * 60 * 60 )
12801289
1281- # Note has already been read in; so now just find which one to point to
1290+ # copy this relevant $allNotes item into $notes array to process
12821291 $notes << this_note
12831292 end
12841293 rescue StandardError => e
@@ -1294,7 +1303,9 @@ def rewrite_file
12941303 # if modified time (mtime) in the last 24 hours
12951304 next unless File . mtime ( this_file ) > ( time_now - hours_to_process * 60 * 60 )
12961305
1297- # read the calendar file in
1306+ this_note = NPFile . new ( this_file )
1307+ $allNotes << this_note
1308+ # copy the $allNotes item into $notes array
12981309 $notes << NPFile . new ( this_file )
12991310 end
13001311 rescue StandardError => e
0 commit comments