diff --git a/src/Cmd.cpp b/src/Cmd.cpp index eddc745fc..d7b30a6bd 100644 --- a/src/Cmd.cpp +++ b/src/Cmd.cpp @@ -115,6 +115,7 @@ void Cmd::load () commands.push_back ("_merge"); commands.push_back ("export.csv"); commands.push_back ("export.ical"); + commands.push_back ("export.yaml"); commands.push_back ("history.monthly"); commands.push_back ("history.annual"); commands.push_back ("ghistory.monthly"); @@ -207,6 +208,7 @@ bool Cmd::isReadOnlyCommand () command == "_version" || command == "export.csv" || command == "export.ical" || + command == "export.yaml" || command == "history.monthly" || command == "history.annual" || command == "ghistory.monthly" || diff --git a/src/Context.cpp b/src/Context.cpp index af6e83dca..3f2044a83 100644 --- a/src/Context.cpp +++ b/src/Context.cpp @@ -233,6 +233,7 @@ int Context::dispatch (std::string &out) else if (cmd.command == "stop") { rc = handleStop (out); } else if (cmd.command == "export.csv") { rc = handleExportCSV (out); } else if (cmd.command == "export.ical") { rc = handleExportiCal (out); } + else if (cmd.command == "export.yaml") { rc = handleExportYAML (out); } else if (cmd.command == "import") { rc = handleImport (out); } else if (cmd.command == "duplicate") { rc = handleDuplicate (out); } else if (cmd.command == "edit") { rc = handleEdit (out); } diff --git a/src/Task.cpp b/src/Task.cpp index 0a4cd135f..a955fca0d 100644 --- a/src/Task.cpp +++ b/src/Task.cpp @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // task - a command line task list manager. // -// Copyright 2006 - 2010, Paul Beckingham. +// Copyright 2006 - 2010, Paul Beckingham, Federico Hernandez. // All rights reserved. // // This program is free software; you can redistribute it and/or modify it under @@ -392,6 +392,33 @@ std::string Task::composeCSV () const return out.str (); } +//////////////////////////////////////////////////////////////////////////////// +std::string Task::composeYAML () const +{ + std::stringstream out; + + // Task header. + out << " task:\n"; + + // Get all the supported attribute names. + std::vector names; + Att::allNames (names); + std::sort (names.begin (), names.end ()); + + foreach (name, names) + out << " " << *name << ": " << get (*name) << "\n"; + + // Now the annotations, which are not listed by the Att::allNames call. + std::vector annotations; + getAnnotations (annotations); + foreach (a, annotations) + out << " annotation:\n" + << " entry: " << a->name().substr (12) << "\n" + << " description: " << a->value () << "\n"; + + return out.str (); +} + //////////////////////////////////////////////////////////////////////////////// void Task::getAnnotations (std::vector & annotations) const { diff --git a/src/Task.h b/src/Task.h index a959b804d..69f99e551 100644 --- a/src/Task.h +++ b/src/Task.h @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // task - a command line task list manager. // -// Copyright 2006 - 2010, Paul Beckingham. +// Copyright 2006 - 2010, Paul Beckingham, Federico Hernandez. // All rights reserved. // // This program is free software; you can redistribute it and/or modify it under @@ -44,6 +44,7 @@ public: void parse (const std::string&); std::string composeCSV () const; + std::string composeYAML () const; // Status values. enum status {pending, completed, deleted, recurring, waiting}; diff --git a/src/export.cpp b/src/export.cpp index 0c1250b67..37e5856e6 100644 --- a/src/export.cpp +++ b/src/export.cpp @@ -62,8 +62,6 @@ int handleExportCSV (std::string &outs) << "'description'" << "\n"; - int count = 0; - // Get all the tasks. std::vector tasks; context.tdb.lock (context.config.getBoolean ("locking")); @@ -77,10 +75,7 @@ int handleExportCSV (std::string &outs) context.hooks.trigger ("pre-display", *task); if (task->getStatus () != Task::recurring) - { out << task->composeCSV ().c_str (); - ++count; - } } outs = out.str (); @@ -106,8 +101,6 @@ int handleExportiCal (std::string &outs) << "VERSION:2.0\n" << "PRODID:-//GBF//" << PACKAGE_STRING << "//EN\n"; - int count = 0; - // Get all the tasks. std::vector tasks; context.tdb.lock (context.config.getBoolean ("locking")); @@ -214,7 +207,6 @@ int handleExportiCal (std::string &outs) out << "COMMENT:" << anno->value () << "\n"; out << "END:VTODO\n"; - ++count; } } @@ -228,3 +220,36 @@ int handleExportiCal (std::string &outs) } //////////////////////////////////////////////////////////////////////////////// +int handleExportYAML (std::string &outs) +{ + int rc = 0; + + if (context.hooks.trigger ("pre-export-command")) + { + // YAML header. + std::stringstream out; + out << "%YAML 1.1\n" + << "---\n"; + + // Get all the tasks. + std::vector tasks; + context.tdb.lock (context.config.getBoolean ("locking")); + handleRecurrence (); + context.tdb.load (tasks, context.filter); + context.tdb.commit (); + context.tdb.unlock (); + + foreach (task, tasks) + { + context.hooks.trigger ("pre-display", *task); + out << task->composeYAML ().c_str (); + } + + outs = out.str (); + context.hooks.trigger ("post-export-command"); + } + + return rc; +} + +//////////////////////////////////////////////////////////////////////////////// diff --git a/src/main.h b/src/main.h index b137ea838..5ce0e339f 100644 --- a/src/main.h +++ b/src/main.h @@ -133,6 +133,7 @@ int handleImport (std::string&); // export.cpp int handleExportCSV (std::string &); int handleExportiCal (std::string &); +int handleExportYAML (std::string &); // list template ///////////////////////////////////////////////////////////////////////////////