entries.awk (4097B)
1 #!/bin/awk 2 function date_f(s) 3 { 4 b_first_date = ( first_date == 0 ) 5 ( b_first_date ) && p_date_type = date_type 6 7 # m/d/y date to ymd 8 ( s ~ /\// ) && s = sprintf( "%s%s%s", substr(s, 7, 4), substr(s, 1, 2), substr(s, 4, 2) ) 9 10 # ymd to mktime fmt 11 a = sprintf( "%s %s %s 00 00 00", substr(s, 1, 4), substr(s, 5, 2), substr(s, 7, 2) ) 12 13 b = mktime( a ) 14 if ( s == strftime( "%Y%m%d" ) ) { 15 r = sprintf( "%s %s", stoday, strftime( "%a %d", b ) ) 16 ( b_first_date ) && date_type = "today" 17 18 } else if ( s == tomorrow ) { 19 r = sprintf( "%s %s", stomorrow, strftime( "%a %d", b ) ) 20 ( b_first_date ) && date_type = "tomorrow" 21 22 } else if ( s == yesterday ) { 23 r = sprintf( "%s %s", syesterday, strftime( "%a %d", b ) ) 24 ( b_first_date ) && date_type = "yesterday" 25 26 } else if ( b < plus7 && b > minus7 ) { 27 r = sprintf( "%s", strftime( "%a %d", b ) ) 28 ( b_first_date ) && date_type = "week" 29 30 } else if (substr(s,1,4) == cyr){ 31 r = strftime("%b %d %a", b) 32 ( b_first_date ) && date_type = "year" 33 }else{ 34 r = strftime("%Y-%m-%d %a", b) 35 ( b_first_date ) && date_type = "etal" 36 } 37 return r 38 } 39 40 function date_fmt_from_regex( rgx, mmax ) 41 { 42 x = $0 43 e = 1 44 45 while ( match(x, rgx ) > 0 && first_date != mmax ){ 46 sub( rgx, date_f( substr( x, RSTART, RLENGTH ) ), x ) 47 $0 = sprintf( "%s%s", substr($0, 1, e - 1 ), x ) 48 x = substr( x, RSTART + RLENGTH ) 49 e = e + RSTART+RLENGTH - 1 50 first_date++ 51 } 52 } 53 54 function print_seperator() 55 { 56 m = substr(rdate, 5, 2) 57 y = substr(rdate, 1, 4) 58 if (y != yr) { 59 yr = y 60 print "" 61 print " ---- " 62 print "[ " y " ]" 63 print " ---- " 64 sep=1 65 } 66 if (rdate != pdate && m != mnth) { 67 pdate = rdate 68 mnth = m 69 print "" 70 print " < " m " >" 71 sep=1 72 } 73 if (sep){ return 0 } 74 if (p_date_type != date_type) { 75 if (pr_count > 1) { print "" } 76 } 77 } 78 79 function entry_out() 80 { 81 if ( ! dp ) { 82 print $0 83 return 0 84 } 85 86 pr_count++ 87 88 get_etype() 89 90 m_date = ""; m_desc = ""; m_note = "" 91 92 # Format dates 93 first_date = 0 94 date_fmt_from_regex( "^[0-9]{8}", 1 ) 95 date_fmt_from_regex( "[0-9]{2}/[0-9]{2}/[0-9]{4}", -1 ) 96 97 #Note 98 if ( gsub( />[0-9a-f]{40}/, "" ) > 0 ){ 99 #match($0,/<note>/,m_note) 100 m_note = "<note> " 101 } 102 103 if ( e_type == "apt" ){ 104 # Match recurrence brackets and move 105 if ( match($0, /\{.*\}/ )){ 106 gsub( /<[0-9]+$/, substr($0, RSTART, RLENGTH)" &" ) 107 sub( /[[:space:]]\{[[:alnum:]\->[:space:]]+\}[[:space:]]/, "" ) 108 } 109 apti = index( $0, "|" ) 110 apta = substr( $0, 1, apti - 1 ) 111 aptb = substr( $0, apti + 1 ) 112 split( apta, dates, " -> " ) 113 gsub( /[[:space:]]$/, "", dates[2] ) 114 if ( dates[1] == dates[2] ){ 115 m_date = gensub( "@ ", "", "g", dates[1] ) 116 }else{ 117 split( dates[1], date1, " @ " ) 118 split( dates[2], date2, " @ " ) 119 if( date1[1] == date2[1] ){ 120 m_date = sprintf( "%s %s - %s", date1[1], date1[2], date2[2] ) 121 }else{ 122 m_date = sprintf( "%s %s - %s %s", date1[1], date1[2], date2[1], date2[2] ) 123 } 124 } 125 gsub( /<[0-9]+$/, m_note "&", aptb ) 126 127 if ( dp ) { print_seperator() } 128 print m_date, ">", aptb 129 130 } else if ( e_type == "evt" ) { 131 # Match recurrence brackets and move 132 if ( match($0, /\{.*\}/ )){ 133 gsub( /<[0-9]+$/, substr($0, RSTART, RLENGTH) " &" ) 134 sub( /\{[[:alnum:]\->[:space:]]+\}/, "" ) 135 } 136 gsub( /\[[0-9]+\][[:space:]]*/, "> " ) 137 sub( /<[0-9]+$/, m_note "&" ) 138 139 if ( dp ) { print_seperator() } 140 print $0 141 142 } else if ( e_type == "todo" ) { 143 sub( /<[0-9]+$/, m_note "&" ) 144 print $0 145 } 146 147 return 0 148 } 149 150 function get_etype() 151 { 152 if ( match( $0, ymd_x" @" )){ 153 e_type = "apt" 154 } else if ( match( $0, ymd_x" \\[" )){ 155 e_type = "evt" 156 } else if ( match( $0, /^\[/ )){ 157 e_type = "todo" 158 } 159 } 160 161 BEGIN{ 162 cdate = strftime( "%Y%m%d" ) 163 yr = cyr = substr( cdate, 1, 4 ) 164 mnth = substr( cdate, 5, 2 ) 165 epoch = strftime( "%s" ) 166 ymd_x = "^[0-9]{8}" 167 e_type = "" 168 dmax_ymd = int( strftime( "%Y%m%d", dmax )) 169 pr_count = 0 170 } 171 172 { 173 rdate = $1 174 if ( caltype == "t" ) { 175 entry_out() 176 next 177 } else if ($1 > dmax_ymd) { 178 exit 179 } else if (caltype == "c" || caltype == "a") { 180 if ($1 >= cdate) { 181 entry_out() 182 } 183 next 184 } else if (caltype == "h") { 185 if ($1 < cdate) { 186 entry_out() 187 } 188 next 189 } 190 exit 191 }