a_write_html.awk (3862B)
1 #!/bin/awk 2 3 # Add slash escapes to protect hugo 4 # front matter yaml values from awk 5 # Ampersands need to be escaped, else 6 # gsub substitutes amp with matched term 7 function hf(s){ 8 gsub(/[&]/,"\\\\&",s) 9 return s 10 } 11 function df(s){ 12 gsub (/[-T]/," ",$s) 13 $s = substr($s, 1, 16) 14 } 15 16 BEGIN { 17 gsub(/[ ,]/, "", page_menuidentifier) 18 page_menu_id = tolower(page_menuidentifier) 19 20 if (html_type == "playlist") { 21 22 if (today_day == played_day) { 23 24 if (! track_data) { 25 page_header=page_summary=page_summary_title= "Nothing Recently Played" 26 syndicate = 1 27 }else{ 28 29 page_header = page_header_html played_day_h 30 31 # Sanitize tab delimited track data for 32 # rss xml title and description tags 33 split(hf(track_data), a, "\t") 34 track_data = sprintf("%s<br><br>Track: %s<br>Album: %s<br>Artist: %s", 35 a[1], a[2], a[3], a[4]) 36 37 page_summary = "Most recently played:<br><br>" track_data 38 page_summary_title = sprintf("Recently Played %s > %s, %s, %s", 39 $a[1], a[2], a[3], a[4]) 40 syndicate = 1 41 } 42 43 }else{ 44 # Page header and rss tags for historia pages 45 # Many nulled vars here to force default rss behavior 46 # that is otherwise specialized for music's 47 # main currently playing playlist page 48 page_summary = sprintf("%s playlist has been archived to Sonus Historia.", played_day_h) 49 page_summary_title = "" 50 page_header = "" 51 syndicate = 0 52 description = "" 53 rss_title = "" 54 rss_data_file = "" 55 } 56 57 } else if ( html_type == "analysis" ) { 58 page_summary_title = "" 59 page_summary = "" 60 } 61 62 # Use a default for music playlist tables 63 if ( !th_values ) { th_values = "Time::Title::Album::Artist" } 64 if ( !pfields ) { pfields = "1,2,3,4" } 65 66 # Now split into arrays 67 split( th_values, th_values_a, "::" ) 68 pfields_a_l = split( pfields, pfields_a, "," ) 69 } 70 { 71 # Track file inputs w/ x 72 if ( FNR == 1) { x++ } 73 74 # First input file, hugo archetype, sub values 75 if ( x==1 ) { 76 gsub(/%%header%%/, page_header) 77 gsub(/%%menu%%/, page_menu) 78 gsub(/%%menutitle%%/, page_menutitle) 79 gsub(/%%title%%/, page_title) 80 gsub(/%%date%%/, today_date) 81 gsub(/%%day%%/, played_day_h) 82 gsub(/%%menuidentifier%%/, page_menuidentifier) 83 gsub(/%%pagemenuid%%/, page_menu_id) 84 gsub(/%%pagesummary%%/, page_summary) 85 gsub(/%%pagesummarytitle%%/, page_summary_title) 86 gsub(/%%syndicate%%/, syndicate) 87 gsub(/%%rssdata%%/, rss_data_file) 88 gsub(/%%description%%/, description) 89 gsub(/%%rsstitle%%/, rss_title) 90 gsub(/%%tally%%/, tally) 91 92 # Check for foot of body (begins after opening tr tag) 93 # Then store lines from rest of archetype file to print 94 # after data file parsing. 95 if ( is_foot == 1 ) { 96 97 # store rest in html_footer 98 html_footer = html_footer $0 ORS 99 next 100 } else { 101 if ( /%%table%%/ ) { 102 is_foot = 1 103 next 104 } 105 } 106 print 107 } 108 109 # Now db data (input file 2) 110 # Fairly self explanitory 111 if ( x==2 ){ 112 if ( FNR == 1 ) { 113 printf("<table id=\"%s\">\n", html_type) 114 printf("<tr>") 115 #<tr><th>+</th><th>Title</th><th>Album</th><th>Artist</th></tr> 116 for (i=1;i<=pfields_a_l;i++){ 117 printf("<th>%s</th>", th_values_a[pfields_a[i]]) 118 } 119 printf("</tr>\n") 120 printf("<tr class=\"spacer\"><td colspan=\"%s\"> </td></tr>\n", pfields_a_l) 121 122 } 123 124 # gsub(/[0-9]{4}-[0-9]{2}-[0-9]{2}T([0-9]{2}:[0-9]{2}).*/,"&",$1) 125 printf("<tr>") 126 127 # extract time from date in playlist db 128 if (html_type == "playlist") { $1 = substr($1, 12, 5) } 129 130 for(i=1;i<=pfields_a_l;i++){ 131 132 # Format date for analysis sections 133 if (page_menu_id == "tracks") { if (pfields_a[i] == 4 || pfields_a[i] == 5) { df( pfields_a[i] ) }} 134 if (page_menu_id == "artists") { if (pfields_a[i] == 2 || pfields_a[i] == 3) { df( pfields_a[i] ) }} 135 136 printf("<td>%s</td>", $pfields_a[i]) 137 } 138 139 printf("</tr>\n") 140 } 141 } 142 END{ 143 # print body footer from archetype (input file 1) 144 printf("</table>\n") 145 print html_footer 146 }