jsonfeed - randomcrap - random crap programs of varying quality
 (HTM) git clone git://git.codemadness.org/randomcrap
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
       jsonfeed (2448B)
       ---
            1 #!/bin/sh
            2 # JSON Feed (subset) to Atom converter.
            3 # Dependencies: json2tsv, awk.
            4 # JSONfeed specification: https://www.jsonfeed.org/version/1/
            5 # Atom specification:     https://datatracker.ietf.org/doc/html/rfc4287
            6 #
            7 # NOTE: it will not convert all possible JSON Feed fields.
            8 #
            9 # See also the C version: https://codemadness.org/git/jfconvert/
           10 
           11 json2tsv -r -F '\x1f' -R '\x1e' | \
           12 LC_ALL=C awk '
           13 BEGIN {
           14         FS = "\x1f"; RS = "\x1e";
           15         print "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
           16         print "<feed xmlns=\"http://www.w3.org/2005/Atom\" xml:lang=\"en\">";
           17 }
           18 function xmlencode(s) {
           19         gsub("&", "\\&amp;", s);
           20         gsub("<", "\\&lt;", s);
           21         gsub(">", "\\&gt;", s);
           22         gsub("\"", "\\&quot;", s);
           23         gsub("'"'"'", "\\&#39;", s);
           24         return s;
           25 }
           26 function attachment(a) {
           27         if (a["url"] == "")
           28                 return;
           29         if (a["mime_type"] == "")
           30                 a["mime_type"] = "application/octet-stream";
           31         if (a["size_in_bytes"] == "")
           32                 a["size_in_bytes"] = "0";
           33         print "\t<link rel=\"enclosure\" type=\"" xmlencode(a["mime_type"]) "\" href=\"" xmlencode(a["url"]) "\" length=\"" xmlencode(a["size_in_bytes"])  "\" />";
           34         delete a;
           35 }
           36 $1 == ".title" {
           37         print "<title type=\"text\">" xmlencode($3) "</title>";
           38 }
           39 $1 == ".home_page_url" {
           40         print "<link rel=\"alternate\" type=\"text/html\" href=\"" xmlencode($3) "\" />";
           41 }
           42 $1 == ".items[]" {
           43         if (nitems) {
           44                 attachment(a);
           45                 print "</entry>";
           46         }
           47         print "<entry>";
           48         nitems++;
           49 }
           50 ! $1 ~ /^\.items\[\]\./ {
           51         next;
           52 }
           53 $1 ~ /^\.items\[\]\./ {
           54         # process item fields.
           55         field = substr($1, 10);
           56 }
           57 field == "title" || field == "id" || field == "summary" {
           58         print "\t<" field ">" xmlencode($3) "</" field ">";
           59 }
           60 field == "url" {
           61         print "\t<link rel=\"alternate\" type=\"text/html\" href=\"" xmlencode($3) "\" />";
           62 }
           63 field == "date_published" {
           64         print "\t<published>" xmlencode($3) "</published>";
           65 }
           66 field == "date_modified" {
           67         print "\t<updated>" xmlencode($3) "</updated>";
           68 }
           69 field == "content_html" {
           70         print "\t<content type=\"html\">" xmlencode($3) "</content>";
           71 }
           72 field == "content_text" {
           73         print "\t<content type=\"text\">" xmlencode($3) "</content>";
           74 }
           75 field == "authors[].name" || field == "author.name" {
           76         print "\t<author><name>" xmlencode($3) "</name></author>";
           77 }
           78 field == "tags[]" {
           79         print "\t<category term=\"" xmlencode($3) "\" />";
           80 }
           81 # attachments
           82 field == "attachments[]" {
           83         attachment(a);
           84 }
           85 field ~ /^attachments\[\]\.[a-z_]*$/ {
           86         a[substr(field, 15)] = $3;
           87 }
           88 END {
           89         if (nitems) {
           90                 attachment(a);
           91                 print "</entry>";
           92         }
           93         print "</feed>";
           94 }
           95 '