יום שני, אוגוסט 17, 2015

ap json2ical

בהמשך לתגובה  שהושארה פה תסריט פשוט בבאש שיקח קובץ json שמכיל מאפייני אירוע וייצר ical.
אין לי שמץ איך הדף מרונדר ואם יש תסריט שמרנדר אותו הייתי שמח לייצר לו פאטץ שיידחף פנימה, ראיתי רק קומיטים אבל לא את קוד המקור שייצר את הטבלאות.

הבנתי שעדיף לא לנסות לדרוש לעבוד עם  jcal (כי אם אנשים ייצרו jcal הם כבר יכולים לבנות לבד גם ical).

לא ידעתי במה בדיוק המקור משתמש בשביל היומן לאירועים וגם לא יודע מה יש על השרת אני מניח שניתן להסתמך על bash 4 כמותקן על המערכת.


#!/bin/bash

#Author Boris Shtasman
#License: public domain


# This script was written just as a workaround for AP ical files generation, it suppose to be pure bash without any external dependencies
# Tested with GNU bash, version 4.3.39(1)-release (x86_64-pc-linux-gnu)

#  Input: a json array with DSTART/DEND/CATEGORIES,SUMMERY,LOCATION,DESCRIPTION,URL,X-TYPE and X-ROOMNAME
#  Output: iCal with VEVENTS based on the json file
# 
#Example based on debconf 15 (input.json)
#[
#{
#  DTSTART:20150822T150000Z,
#  DTEND:20150822T154500Z,
#  CATEGORIES:Plenary,
#  SUMMARY:Closing Ceremony,
#  LOCATION:Heidelberg,
#  DESCRIPTION:Good bye to Heidelberg... See you in Cape Town!,
#  URL:https://summit.debconf.org/debconf15/meeting/268/closing-ceremony/,
#  X-TYPE:discussion,
#  X-ROOMNAME:a-heidelberg
#},
#{
#  DTSTART:20150822T120000Z,
#  DTEND:20150822T140000Z,
#  CATEGORIES:"Debian Packaging, Policy, and Infrastructure,Workshop",
#  SUMMARY:"Debian wiki work session",
#  LOCATION:"Stockholm",
#  DESCRIPTION:"We will share knowledge about wiki work and do some work on the wiki, probably in these areas:\N\N* existing bug reports\N* known issues\N* moin bug reports/etc\N* wiki pages about the wiki\N* general wiki content\N\NPlease join us if you would like to help out with the wiki or learn more about it. If there is enough interest we may hold more sessions."
#  URL:"https://summit.debconf.org/debconf15/meeting/260/debian-wiki-work-session/"
#  X-TYPE:"discussion"
#  X-ROOMNAME:"e-stockholm"
#}
#]

FILEPATH=$1
let event_uid=200

cat << EOF
BEGIN:VCALENDAR
PRODID:-//hamakor scripts//EN
VERSION:2.0
CALSCALE:GREGORIAN
METHOD:PUBLISH
X-WR-TIMEZONE:UTC
X-WR-CALNAME:AP16
X-WR-CALDESC:AP16

EOF

#dump an assoative array as VEVNT, expect to have key/value that ical reader could handle
#you shuold provide keys like DSTART/DEND/CATEGORIES,SUMMERY,LOCATION,DESCRIPTION,URL,X-TYPE and X-ROOMNAME

function dump_as_event()
{
  local -n event=$1
  let event_uid=event_uid+1;
  echo "BEGIN:VEVENT"
  echo "UID:$event_uid"

  for key in "${!event[@]}"; do 
    echo "$key:${event[$key]}"
  done 
  echo "END:VEVENT"
}

json_automata_state=""
in_array_state=0
in_qouted_state=0
#current key in json object
key=""
#current value in json object
value=""


declare -A events=()

#in order to avoid getting external bash scripts (or executables), here is a small slow json parser that will call dump_as_event every time when an object had been finished.
#it would be much better to use some other tool, but the goal was to create pure bash solution 
#only one layer of object is implemented

while IFS= read -r -n1 char
do
        case "$char" in 
                "\"") 
                   if [ $in_qouted_state -eq 0 ];then 
                      in_qouted_state=1
                   else
                      in_qouted_state=0
                   fi
                ;;



                "{") #echo "open"
                   json_automata_state="START_OBJECT"
                   continue;
                ;;

                "}") #echo "close"
                   json_automata_state="END_OBJECT"
                   events[$key]="$value"
                   key=""
                   value=""
                   continue;
                ;;

                ",") 
                   case "$json_automata_state" in 
                        "KEY_VALUE") 
                         if [ $in_qouted_state -eq 0 ]; then
                                 events[$key]=$value;
                                 key=""
                                 value=""
                                 json_automata_state="NEXT_KEY"
                                 continue;

                         else
                                         value=$value$char
                                         continue;
                         fi
                         ;;

                         "END_OBJECT")
                           if [ $in_array_state -eq 0 ];then  echo "bad file, comma can be used in an array or inside an object" ; exit 1; fi
                           dump_as_event events
                           events=()
                           key=""
                           value="" 

                         ;;

           esac
                   continue;
                ;;

                ":")

                     if [ $in_qouted_state -eq 0 ]; then
                        json_automata_state="KEY_VALUE"
                        continue;

                     else
                        if [ $json_automata_state == "KEY_VALUE" ]; then 
                           value=$value$char
                           continue;
                        fi
                        exit ": not supported in json_automata_state=$json_automata_state"
                     fi
                ;;

                "[") 
                in_array_state=1
                ;;

                "]")
                in_array_state=0
                ;;
                *) 
                     case "$json_automata_state" in
                     "START_OBJECT") key=$key$char
                        continue;
                        ;;

                     "NEXT_KEY") key=$key$char
                        continue;
                        ;;

                     "KEY_VALUE") value=$value$char
                        continue;
                        ;;

                     " ")
                        continue;
                        ;;

                     "\r")
                        continue;
                        ;;

                     "")
                        continue;
                        ;;
             esac
        esac 
done <$FILEPATH

#make an event from the the last content in array
dump_as_event events
echo "END:VCALENDAR"

אם לא הבנתי ,וצריך לפרסס את הדף הקיים אשמח להערה במקום.

אין תגובות:

הוסף רשומת תגובה