Update header information.
[org-mode.git] / contrib / lisp / ox-taskjuggler.el
blob46402ba168ad7807fe31dbc3cca0264f8efb016c
1 ;;; ox-taskjuggler.el --- TaskJuggler Back-End for Org Export Engine
2 ;;
3 ;; Copyright (C) 2007-2013 Free Software Foundation, Inc.
4 ;;
5 ;; Emacs Lisp Archive Entry
6 ;; Filename: ox-taskjuggler.el
7 ;; Author: Christian Egli
8 ;; Nicolas Goaziou <n dot goaziou at gmail dot com>
9 ;; Maintainer: Christian Egli
10 ;; Keywords: org, taskjuggler, project planning
11 ;; Description: Converts an Org mode buffer into a TaskJuggler project plan
13 ;; This program is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; This program is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
26 ;; Commentary:
28 ;; This library implements a TaskJuggler exporter for Org mode.
29 ;; TaskJuggler uses a text format to define projects, tasks and
30 ;; resources, so it is a natural fit for Org mode. It can produce all
31 ;; sorts of reports for tasks or resources in either HTML, CSV or PDF.
32 ;; The current version of TaskJuggler requires KDE but the next
33 ;; version is implemented in Ruby and should therefore run on any
34 ;; platform.
36 ;; The exporter does not export all the nodes of a document or
37 ;; strictly follow the order of the nodes in the document.
39 ;; Instead the TaskJuggler exporter looks for a tree that defines the
40 ;; tasks and a optionally tree that defines the resources for this
41 ;; project. It then creates a TaskJuggler file based on these trees
42 ;; and the attributes defined in all the nodes.
44 ;; * Installation
46 ;; Put this file into your load-path and the following line into your
47 ;; ~/.emacs:
49 ;; (add-to-list 'org-export-backends 'taskjuggler)
51 ;; or customize `org-export-backends' variable.
53 ;; The interactive functions are the following:
55 ;; M-x `org-taskjuggler-export'
56 ;; M-x `org-taskjuggler-export-and-open'
58 ;; * Tasks
60 ;; Let's illustrate the usage with a small example. Create your tasks
61 ;; as you usually do with org-mode. Assign efforts to each task using
62 ;; properties (it's easiest to do this in the column view). You
63 ;; should end up with something similar to the example by Peter Jones
64 ;; in:
66 ;; http://www.contextualdevelopment.com/static/artifacts/articles/2008/project-planning/project-planning.org.
68 ;; Now mark the top node of your tasks with a tag named
69 ;; "taskjuggler_project" (or whatever you customized
70 ;; `org-taskjuggler-project-tag' to). You are now ready to export the
71 ;; project plan with `org-taskjuggler-export-and-open' which will
72 ;; export the project plan and open a Gantt chart in TaskJugglerUI.
74 ;; * Resources
76 ;; Next you can define resources and assign those to work on specific
77 ;; tasks. You can group your resources hierarchically. Tag the top
78 ;; node of the resources with "taskjuggler_resource" (or whatever you
79 ;; customized `org-taskjuggler-resource-tag' to). You can optionally
80 ;; assign an identifier (named "resource_id") to the resources (using
81 ;; the standard org properties commands) or you can let the exporter
82 ;; generate identifiers automatically (the exporter picks the first
83 ;; word of the headline as the identifier as long as it is unique, see
84 ;; the documentation of `org-taskjuggler--build-unique-id'). Using that
85 ;; identifier you can then allocate resources to tasks. This is again
86 ;; done with the "allocate" property on the tasks. Do this in column
87 ;; view or when on the task type
89 ;; C-c C-x p allocate RET <resource_id> RET
91 ;; Once the allocations are done you can again export to TaskJuggler
92 ;; and check in the Resource Allocation Graph which person is working
93 ;; on what task at what time.
95 ;; * Export of properties
97 ;; The exporter also takes TODO state information into consideration,
98 ;; i.e. if a task is marked as done it will have the corresponding
99 ;; attribute in TaskJuggler ("complete 100"). Also it will export any
100 ;; property on a task resource or resource node which is known to
101 ;; TaskJuggler, such as limits, vacation, shift, booking, efficiency,
102 ;; journalentry, rate for resources or account, start, note, duration,
103 ;; end, journalentry, milestone, reference, responsible, scheduling,
104 ;; etc for tasks.
106 ;; * Dependencies
108 ;; The exporter will handle dependencies that are defined in the tasks
109 ;; either with the ORDERED attribute (see TODO dependencies in the Org
110 ;; mode manual) or with the BLOCKER attribute (see org-depend.el) or
111 ;; alternatively with a depends attribute. Both the BLOCKER and the
112 ;; depends attribute can be either "previous-sibling" or a reference
113 ;; to an identifier (named "task_id") which is defined for another
114 ;; task in the project. BLOCKER and the depends attribute can define
115 ;; multiple dependencies separated by either space or comma. You can
116 ;; also specify optional attributes on the dependency by simply
117 ;; appending it. The following examples should illustrate this:
119 ;; * Training material
120 ;; :PROPERTIES:
121 ;; :task_id: training_material
122 ;; :ORDERED: t
123 ;; :END:
124 ;; ** Markup Guidelines
125 ;; :PROPERTIES:
126 ;; :Effort: 2d
127 ;; :END:
128 ;; ** Workflow Guidelines
129 ;; :PROPERTIES:
130 ;; :Effort: 2d
131 ;; :END:
132 ;; * Presentation
133 ;; :PROPERTIES:
134 ;; :Effort: 2d
135 ;; :BLOCKER: training_material { gapduration 1d } some_other_task
136 ;; :END:
138 ;;;; * TODO
139 ;; - Look at org-file-properties, org-global-properties and
140 ;; org-global-properties-fixed
141 ;; - What about property inheritance and org-property-inherit-p?
142 ;; - Use TYPE_TODO as an way to assign resources
144 ;;; Code:
146 (eval-when-compile (require 'cl))
148 (require 'ox)
152 ;;; User Variables
154 (defgroup org-export-taskjuggler nil
155 "Options specific for TaskJuggler export back-end."
156 :tag "Org Export TaskJuggler"
157 :group 'org-export)
159 (defcustom org-taskjuggler-extension ".tjp"
160 "Extension of TaskJuggler files."
161 :group 'org-export-taskjuggler
162 :version "24.2"
163 :type 'string)
165 (defcustom org-taskjuggler-project-tag "taskjuggler_project"
166 "Tag marking project's tasks.
167 This tag is used to find the tree containing all the tasks for
168 the project."
169 :group 'org-export-taskjuggler
170 :version "24.2"
171 :type 'string)
173 (defcustom org-taskjuggler-resource-tag "taskjuggler_resource"
174 "Tag marking project's resources.
175 This tag is used to find the tree containing all the resources
176 for the project."
177 :group 'org-export-taskjuggler
178 :version "24.2"
179 :type 'string)
181 (defcustom org-taskjuggler-report-tag "taskjuggler_report"
182 "Tag marking project's reports.
183 This tag is used to find the tree containing all the reports for
184 the project."
185 :group 'org-export-taskjuggler
186 :version "24.2"
187 :type 'string)
189 (defcustom org-taskjuggler-target-version 2.4
190 "Which version of TaskJuggler the exporter is targeting."
191 :group 'org-export-taskjuggler
192 :version "24.2"
193 :type 'number)
195 (defcustom org-taskjuggler-default-project-version "1.0"
196 "Default version string for the project.
197 This value can also be set with the \":VERSION:\" property
198 associated to the headline defining the project."
199 :group 'org-export-taskjuggler
200 :version "24.2"
201 :type 'string)
203 (defcustom org-taskjuggler-default-project-duration 280
204 "Default project duration.
205 The value will be used if no start and end date have been defined
206 in the root node of the task tree, i.e. the tree that has been
207 marked with `org-taskjuggler-project-tag'"
208 :group 'org-export-taskjuggler
209 :version "24.2"
210 :type 'integer)
212 (defcustom org-taskjuggler-default-reports
213 '("taskreport \"Gantt Chart\" {
214 headline \"Project Gantt Chart\"
215 columns hierarchindex, name, start, end, effort, duration, completed, chart
216 timeformat \"%Y-%m-%d\"
217 hideresource 1
218 loadunit shortauto
220 "resourcereport \"Resource Graph\" {
221 headline \"Resource Allocation Graph\"
222 columns no, name, utilization, freeload, chart
223 loadunit shortauto
224 sorttasks startup
225 hidetask ~isleaf()
227 "Default reports for the project."
228 :group 'org-export-taskjuggler
229 :version "24.2"
230 :type '(repeat (string :tag "Report")))
232 (defcustom org-taskjuggler-default-global-header ""
233 "Default global header for the project.
234 This goes before project declaration, and might be useful for
235 early macros."
236 :group 'org-export-taskjuggler
237 :version "24.2"
238 :type '(string :tag "Preamble"))
240 (defcustom org-taskjuggler-default-global-properties
241 "shift s40 \"Part time shift\" {
242 workinghours wed, thu, fri off
245 "Default global properties for the project.
247 Here you typically define global properties such as shifts,
248 accounts, rates, vacation, macros and flags. Any property that
249 is allowed within the TaskJuggler file can be inserted. You
250 could for example include another TaskJuggler file.
252 The global properties are inserted after the project declaration
253 but before any resource and task declarations."
254 :group 'org-export-taskjuggler
255 :version "24.2"
256 :type '(string :tag "Preamble"))
258 (defcustom org-taskjuggler-valid-task-attributes
259 '(account start note duration endbuffer endcredit end
260 flags journalentry length limits maxend maxstart minend
261 minstart period reference responsible scheduling
262 startbuffer startcredit statusnote chargeset charge)
263 "Valid attributes for Taskjuggler tasks.
264 If one of these appears as a property for a headline, it will be
265 exported with the corresponding task."
266 :group 'org-export-taskjuggler)
268 (defcustom org-taskjuggler-valid-resource-attributes
269 '(limits vacation shift booking efficiency journalentry rate
270 workinghours flags)
271 "Valid attributes for Taskjuggler resources.
272 If one of these appears as a property for a headline, it will be
273 exported with the corresponding resource."
274 :group 'org-export-taskjuggler)
276 (defcustom org-taskjuggler-valid-report-attributes
277 '(headline columns definitions timeformat hideresource hidetask
278 loadunit sorttasks formats period)
279 "Valid attributes for Taskjuggler reports.
280 If one of these appears as a property for a headline, it will be
281 exported with the corresponding report."
282 :group 'org-export-taskjuggler)
284 (defcustom org-taskjuggler-keep-project-as-task t
285 "Non-nil keeps the project headline as an umbrella task for all tasks.
286 Setting this to nil will allow maintaining completely separated
287 task buckets, while still sharing the same resources pool."
288 :group 'org-export-taskjuggler
289 :type 'boolean)
293 ;;; Hooks
295 (defvar org-taskjuggler-final-hook nil
296 "Hook run after a TaskJuggler files has been saved.
297 This hook is run with the name of the file as argument.")
301 ;;; Back-End Definition
303 (org-export-define-backend taskjuggler
304 ((template . org-taskjuggler-project-plan))
305 :menu-entry
306 (?J "Export to TaskJuggler"
307 ((?j "As TJP file" (lambda (a s v b) (org-taskjuggler-export a s v)))
308 (?o "As TJP file and open"
309 (lambda (a s v b)
310 (if a (org-taskjuggler-export a s v)
311 (org-taskjuggler-export-and-open s v))))))
312 ;; This property will be used to store unique ids in communication
313 ;; channel. Ids will be retrieved with `org-taskjuggler-get-id'.
314 :options-alist ((:taskjuggler-unique-ids nil nil nil)))
318 ;;; Unique IDs
320 (defun org-taskjuggler-assign-task-ids (tasks info)
321 "Assign a unique ID to each task in TASKS.
322 TASKS is a list of headlines. Return value is an alist between
323 headlines and their associated ID. IDs are hierarchical, which
324 means they only need to be unique among the task siblings."
325 (let* (alist
326 (build-id
327 (lambda (tasks local-ids)
328 (org-element-map tasks 'headline
329 (lambda (task)
330 (let ((id (org-taskjuggler--build-unique-id task local-ids)))
331 (push id local-ids)
332 (push (cons task id) alist)
333 (funcall build-id (org-element-contents task) nil)))
334 info nil 'headline))))
335 (funcall build-id tasks nil)
336 alist))
338 (defun org-taskjuggler-assign-resource-ids (resources info)
339 "Assign a unique ID to each resource within RESOURCES.
340 RESOURCES is a list of headlines. Return value is an alist
341 between headlines and their associated ID."
342 (let (ids)
343 (org-element-map resources 'headline
344 (lambda (resource)
345 (let ((id (org-taskjuggler--build-unique-id resource ids)))
346 (push id ids)
347 (cons resource id)))
348 info)))
352 ;;; Accessors
354 (defun org-taskjuggler-get-project (info)
355 "Return project in parse tree.
356 INFO is a plist used as a communication channel. First headline
357 in buffer with `org-taskjuggler-project-tag' defines the project.
358 If no such task is defined, pick the first headline in buffer.
359 If there is no headline at all, return nil."
360 (or (org-element-map (plist-get info :parse-tree) 'headline
361 (lambda (hl)
362 (and (member org-taskjuggler-project-tag
363 (org-export-get-tags hl info))
364 hl))
365 info t)
366 (org-element-map tree 'headline 'identity info t)))
368 (defun org-taskjuggler-get-id (item info)
369 "Return id for task or resource ITEM.
370 ITEM is a headline. Return value is a string."
371 (cdr (assq item (plist-get info :taskjuggler-unique-ids))))
373 (defun org-taskjuggler-get-name (item)
374 "Return name for task or resource ITEM.
375 ITEM is a headline. Return value is a string."
376 ;; Quote double quotes in name.
377 (replace-regexp-in-string
378 "\"" "\\\"" (org-element-property :raw-value item) t t))
380 (defun org-taskjuggler-get-start (item)
381 "Return start date for task or resource ITEM.
382 ITEM is a headline. Return value is a string or nil if ITEM
383 doesn't have any start date defined.."
384 (let ((scheduled (org-element-property :scheduled item)))
385 (and scheduled (org-timestamp-format scheduled "%Y-%02m-%02d"))))
387 (defun org-taskjuggler-get-end (item)
388 "Return end date for task or resource ITEM.
389 ITEM is a headline. Return value is a string or nil if ITEM
390 doesn't have any end date defined.."
391 (let ((deadline (org-element-property :deadline item)))
392 (and deadline (org-timestamp-format deadline "%Y-%02m-%02d"))))
396 ;;; Internal Functions
398 (defun org-taskjuggler--indent-string (s)
399 "Indent string S by 2 spaces.
400 Return new string. If S is the empty string, return it."
401 (if (equal "" s) s (replace-regexp-in-string "^ *\\S-" " \\&" s)))
403 (defun org-taskjuggler--build-attributes (item attributes)
404 "Return attributes string for task, resource or report ITEM.
405 ITEM is an headline. ATTRIBUTES is a list of symbols
406 representing valid attributes for ITEM."
407 (mapconcat
408 (lambda (attribute)
409 (let ((value (org-element-property
410 (intern (upcase (format ":%s" attribute)))
411 item)))
412 (and value (format "%s %s\n" attribute value))))
413 (remq nil attributes) ""))
415 (defun org-taskjuggler--build-unique-id (item unique-ids)
416 "Return a unique id for a given task or a resource.
417 ITEM is an `headline' type element representing the task or
418 resource. Its id is derived from its name and made unique
419 against UNIQUE-IDS. If the (downcased) first token of the
420 headline is not unique try to add more (downcased) tokens of the
421 headline or finally add more underscore characters (\"_\")."
422 (let* ((parts (org-split-string (org-element-property :raw-value item)))
423 (id (org-taskjuggler--clean-id (downcase (pop parts)))))
424 ;; Try to add more parts of the headline to make it unique.
425 (while (and (car parts) (member id unique-ids))
426 (setq id (concat id "_"
427 (org-taskjuggler--clean-id (downcase (pop parts))))))
428 ;; If it's still not unique, add "_".
429 (while (member id unique-ids)
430 (setq id (concat id "_")))
431 id))
433 (defun org-taskjuggler--clean-id (id)
434 "Clean and return ID to make it acceptable for TaskJuggler.
435 ID is a string."
436 ;; Replace non-ascii by "_".
437 (replace-regexp-in-string
438 "[^a-zA-Z0-9_]" "_"
439 ;; Make sure id doesn't start with a number.
440 (replace-regexp-in-string "^\\([0-9]\\)" "_\\1" id)))
444 ;;; Dependencies
446 (defun org-taskjuggler-resolve-dependencies (task info)
447 "Return a list of all tasks TASK depends on.
448 TASK is an headline. INFO is a plist used as a communication
449 channel."
450 (let ((deps-ids
451 ;; Get all dependencies specified in BLOCKER and DEPENDS task
452 ;; properties. Clean options from them.
453 (let ((deps (concat (org-element-property :BLOCKER task)
454 (org-element-property :DEPENDS task))))
455 (and deps
456 (org-split-string (replace-regexp-in-string "{.*?}" "" deps)
457 "[ ,]* +"))))
458 depends)
459 (when deps-ids
460 ;; Find tasks with :task_id: property matching id in DEPS-IDS.
461 ;; Add them to DEPENDS.
462 (let* ((project (org-taskjuggler-get-project info))
463 (tasks (if org-taskjuggler-keep-project-as-task project
464 (org-element-contents project))))
465 (setq depends
466 (org-element-map tasks 'headline
467 (lambda (task)
468 (let ((task-id (org-element-property :TASK_ID task)))
469 (and task-id (member task-id deps-ids) task)))
470 info)))
471 ;; Check BLOCKER and DEPENDS properties. If "previous-sibling"
472 ;; belongs to DEPS-ID, add it to DEPENDS.
473 (when (and (member-ignore-case "previous-sibling" deps-ids)
474 (not (org-export-first-sibling-p task info)))
475 (let ((prev (org-export-get-previous-element task info)))
476 (and (not (memq prev depends)) (push prev depends)))))
477 ;; Check ORDERED status of parent.
478 (let ((parent (org-export-get-parent task)))
479 (when (and parent
480 (org-element-property :ORDERED parent)
481 (not (org-export-first-sibling-p task info)))
482 (push (org-export-get-previous-element task info) depends)))
483 ;; Return dependencies.
484 depends))
486 (defun org-taskjuggler-format-dependencies (dependencies task info)
487 "Format DEPENDENCIES to match TaskJuggler syntax.
488 DEPENDENCIES is list of dependencies for TASK, as returned by
489 `org-taskjuggler-resolve-depedencies'. TASK is an headline.
490 INFO is a plist used as a communication channel. Return value
491 doesn't include leading \"depends\"."
492 (let ((dep-str (concat (org-element-property :BLOCKER task)
494 (org-element-property :DEPENDS task)))
495 (get-path
496 (lambda (dep)
497 ;; Return path to DEP relatively to TASK.
498 (let ((parent (org-export-get-parent dep))
499 (exclamations 1)
500 (option
501 (let ((id (org-element-property :TASK_ID dep)))
502 (and id
503 (string-match (concat id " +\\({.*?}\\)") dep-str)
504 (org-match-string-no-properties 1))))
505 path)
506 ;; Compute number of exclamation marks.
507 (while (not (org-element-map parent 'headline
508 (lambda (task) (eq task dep))))
509 (incf exclamations)
510 (setq parent (org-export-get-parent parent)))
511 ;; Build path from DEP to PARENT.
512 (while (not (eq parent dep))
513 (push (org-taskjuggler-get-id dep info) path)
514 (setq dep (org-export-get-parent dep)))
515 ;; Return full path. Add dependency options, if any.
516 (concat (make-string exclamations ?!)
517 (mapconcat 'identity path ".")
518 (and option (concat " " option)))))))
519 ;; Return dependencies string, without the leading "depends".
520 (mapconcat (lambda (dep) (funcall get-path dep)) dependencies ", ")))
524 ;;; Translator Functions
526 (defun org-taskjuggler-project-plan (contents info)
527 "Build TaskJuggler project plan.
528 CONTENTS is ignored. INFO is a plist holding export options.
529 Return complete project plan as a string in TaskJuggler syntax."
530 (let* ((tree (plist-get info :parse-tree))
531 (project (or (org-taskjuggler-get-project info)
532 (error "No project specified"))))
533 (concat
534 ;; 1. Insert header.
535 (org-element-normalize-string org-taskjuggler-default-global-header)
536 ;; 2. Insert project.
537 (org-taskjuggler--build-project project info)
538 ;; 3. Insert global properties.
539 (org-element-normalize-string org-taskjuggler-default-global-properties)
540 ;; 4. Insert resources. Provide a default one if none is
541 ;; specified.
542 (let ((main-resources
543 ;; Collect contents from various trees marked with
544 ;; `org-taskjuggler-resource-tag'. Only gather top level
545 ;; resources.
546 (apply 'append
547 (org-element-map tree 'headline
548 (lambda (hl)
549 (and (member org-taskjuggler-resource-tag
550 (org-export-get-tags hl info))
551 (org-element-map (org-element-contents hl) 'headline
552 'identity info nil 'headline)))
553 info nil 'headline))))
554 ;; Assign a unique ID to each resource. Store it under
555 ;; `:taskjuggler-unique-ids' property in INFO.
556 (setq info
557 (plist-put info :taskjuggler-unique-ids
558 (org-taskjuggler-assign-resource-ids
559 main-resources info)))
560 (concat
561 (if main-resources
562 (mapconcat
563 (lambda (resource) (org-taskjuggler--build-resource resource info))
564 main-resources "")
565 (format "resource %s \"%s\" {\n}\n" (user-login-name) user-full-name))
566 ;; 5. Insert tasks.
567 (let ((main-tasks
568 ;; If `org-taskjuggler-keep-project-as-task' is
569 ;; non-nil, there is only one task. Otherwise, every
570 ;; direct children of PROJECT is a top level task.
571 (if org-taskjuggler-keep-project-as-task (list project)
572 (or (org-element-map (org-element-contents project) 'headline
573 'identity info nil 'headline)
574 (error "No task specified")))))
575 ;; Assign a unique ID to each task. Add it to
576 ;; `:taskjuggler-unique-ids' property in INFO.
577 (setq info
578 (plist-put info :taskjuggler-unique-ids
579 (append
580 (org-taskjuggler-assign-task-ids main-tasks info)
581 (plist-get info :taskjuggler-unique-ids))))
582 ;; If no resource is allocated among tasks, allocate one to
583 ;; the first task.
584 (unless (org-element-map main-tasks 'headline
585 (lambda (task) (org-element-property :ALLOCATE task))
586 info t)
587 (org-element-put-property
588 (car main-tasks) :ALLOCATE
589 (or (org-taskjuggler-get-id (car main-resources) info)
590 (user-login-name))))
591 (mapconcat
592 (lambda (task) (org-taskjuggler--build-task task info))
593 main-tasks ""))
594 ;; 6. Insert reports. If no report is defined, insert default
595 ;; reports.
596 (let ((main-reports
597 ;; Collect contents from various trees marked with
598 ;; `org-taskjuggler-report-tag'. Only gather top level
599 ;; reports.
600 (apply 'append
601 (org-element-map tree 'headline
602 (lambda (hl)
603 (and (member org-taskjuggler-report-tag
604 (org-export-get-tags hl info))
605 (org-element-map (org-element-contents hl)
606 'headline 'identity info nil 'headline)))
607 info nil 'headline))))
608 (if main-reports
609 (mapconcat
610 (lambda (report) (org-taskjuggler--build-report report info))
611 main-reports "")
612 (mapconcat 'org-element-normalize-string
613 org-taskjuggler-default-reports ""))))))))
615 (defun org-taskjuggler--build-project (project info)
616 "Return a project declaration.
617 PROJECT is an headline. INFO is a plist used as a communication
618 channel. If no start date is specified, start today. If no end
619 date is specified, end `org-taskjuggler-default-project-duration'
620 days from now."
621 (format "project %s \"%s\" \"%s\" %s %s {\n}\n"
622 (org-taskjuggler-get-id project info)
623 (org-taskjuggler-get-name project)
624 ;; Version is obtained through :TASKJUGGLER_VERSION:
625 ;; property or `org-taskjuggler-default-project-version'.
626 (or (org-element-property :VERSION project)
627 org-taskjuggler-default-project-version)
628 (or (org-taskjuggler-get-start project)
629 (format-time-string "%Y-%m-%d"))
630 (let ((end (org-taskjuggler-get-end project)))
631 (or (and end (format "- %s" end))
632 (format "+%sd" org-taskjuggler-default-project-duration)))))
634 (defun org-taskjuggler--build-resource (resource info)
635 "Return a resource declaration.
637 RESOURCE is an headline. INFO is a plist used as a communication
638 channel.
640 All valid attributes from RESOURCE are inserted. If RESOURCE
641 defines a property \"resource_id\" it will be used as the id for
642 this resource. Otherwise it will use the ID property. If
643 neither is defined a unique id will be associated to it."
644 (concat
645 ;; Opening resource.
646 (format "resource %s \"%s\" {\n"
647 (org-taskjuggler--clean-id
648 (or (org-element-property :RESOURCE_ID resource)
649 (org-element-property :ID resource)
650 (org-taskjuggler-get-id resource info)))
651 (org-taskjuggler-get-name resource))
652 ;; Add attributes.
653 (org-taskjuggler--indent-string
654 (org-taskjuggler--build-attributes
655 resource org-taskjuggler-valid-resource-attributes))
656 ;; Add inner resources.
657 (org-taskjuggler--indent-string
658 (mapconcat
659 'identity
660 (org-element-map (org-element-contents resource) 'headline
661 (lambda (hl) (org-taskjuggler--build-resource hl info))
662 info nil 'headline)
663 ""))
664 ;; Closing resource.
665 "}\n"))
667 (defun org-taskjuggler--build-report (report)
668 "Return a report declaration.
669 REPORT is an headline. INFO is a plist used as a communication
670 channel."
671 (concat
672 ;; Opening report.
673 (format "%s \"%s\" {\n"
674 (or (org-element-property :REPORT_KIND report) "taskreport")
675 (org-taskjuggler-get-name report))
676 ;; Add attributes.
677 (org-taskjuggler--indent-string
678 (org-taskjuggler--build-attributes
679 report org-taskjuggler-valid-report-attributes))
680 ;; Add inner reports.
681 (org-taskjuggler--indent-string
682 (mapconcat
683 'identity
684 (org-element-map (org-element-contents report) 'headline
685 (lambda (hl) (org-taskjuggler--build-report hl info))
686 info nil 'headline)
687 ""))
688 ;; Closing report.
689 "}\n"))
691 (defun org-taskjuggler--build-task (task info)
692 "Return a task declaration.
694 TASK is an headline. INFO is a plist used as a communication
695 channel.
697 All valid attributes from TASK are inserted. If TASK defines
698 a property \"task_id\" it will be used as the id for this task.
699 Otherwise it will use the ID property. If neither is defined
700 a unique id will be associated to it."
701 (let* ((allocate (org-element-property :ALLOCATE task))
702 (complete
703 (if (eq (org-element-property :todo-type task) 'done) "100"
704 (org-element-property :COMPLETE task)))
705 (depends (org-taskjuggler-resolve-dependencies task info))
706 (effort (org-element-property :EFFORT task))
707 (milestone
708 (or (org-element-property :MILESTONE task)
709 (and (org-element-map (org-element-contents task) 'headline
710 'identity info t) ; Has task any child?
711 (not (or effort
712 (org-element-property :LENGTH task)
713 (org-element-property :DURATION task)
714 (and (org-taskjuggler-get-start task)
715 (org-taskjuggler-get-end task))
716 (org-element-property :PERIOD task))))))
717 (priority
718 (let ((pri (org-element-property :priority task)))
719 (and pri
720 (max 1 (/ (* 1000 (- org-lowest-priority pri))
721 (- org-lowest-priority org-highest-priority)))))))
722 (concat
723 ;; Opening task.
724 (format "task %s \"%s\" {\n"
725 (org-taskjuggler-get-id task info)
726 (org-taskjuggler-get-name task))
727 ;; Add default attributes.
728 (and depends
729 (format " depends %s\n"
730 (org-taskjuggler-format-dependencies depends task info)))
731 (and allocate
732 (format " purge %s\n allocate %s\n"
733 ;; Compatibility for previous TaskJuggler versions.
734 (if (>= org-export-taskjuggler-target-version 3.0) "allocate"
735 "allocations")
736 allocate))
737 (and complete (format " complete %s\n" comptete))
738 (and effort
739 (format " effort %s\n"
740 (let* ((minutes (org-duration-string-to-minutes effort))
741 (hours (/ minutes 60.0)))
742 (format "%.1fh" hours))))
743 (and priority (format " priority %s\n" complete))
744 (and milestone " milestone\n")
745 ;; Add other valid attributes.
746 (org-taskjuggler--indent-string
747 (org-taskjuggler--build-attributes
748 task org-taskjuggler-valid-task-attributes))
749 ;; Add inner tasks.
750 (org-taskjuggler--indent-string
751 (mapconcat 'identity
752 (org-element-map (org-element-contents task) 'headline
753 (lambda (hl) (org-taskjuggler--build-task hl info))
754 info nil 'headline)
755 ""))
756 ;; Closing task.
757 "}\n")))
761 ;;; Interactive Functions
763 ;;;###autoload
764 (defun org-taskjuggler-export (&optional async subtreep visible-only)
765 "Export current buffer to a TaskJuggler file.
767 The exporter looks for a tree with tag that matches
768 `org-taskjuggler-project-tag' and takes this as the tasks for
769 this project. The first node of this tree defines the project
770 properties such as project name and project period.
772 If there is a tree with tag that matches
773 `org-taskjuggler-resource-tag' this tree is taken as resources
774 for the project. If no resources are specified, a default
775 resource is created and allocated to the project.
777 Also the TaskJuggler project will be created with default reports
778 as defined in `org-taskjuggler-default-reports'.
780 If narrowing is active in the current buffer, only export its
781 narrowed part.
783 If a region is active, export that region.
785 A non-nil optional argument ASYNC means the process should happen
786 asynchronously. The resulting file should be accessible through
787 the `org-export-stack' interface.
789 When optional argument SUBTREEP is non-nil, export the sub-tree
790 at point, extracting information from the headline properties
791 first.
793 When optional argument VISIBLE-ONLY is non-nil, don't export
794 contents of hidden elements.
796 Return output file's name."
797 (interactive)
798 (let ((outfile
799 (org-export-output-file-name org-taskjuggler-extension subtreep)))
800 (if async
801 (org-export-async-start
802 (lambda (f)
803 (org-export-add-to-stack f 'taskjuggler)
804 (run-hook-with-args 'org-taskjuggler-final-hook f))
805 `(expand-file-name
806 (org-export-to-file 'taskjuggler ,outfile ,subtreep ,visible-only)))
807 (org-export-to-file 'taskjuggler outfile subtreep visible-only)
808 (run-hook-with-args 'org-taskjuggler-final-hook outfile)
809 outfile)))
811 ;;;###autoload
812 (defun org-taskjuggler-export-and-open (&optional subtreep visible-only)
813 "Export current buffer to a TaskJuggler file and open it.
815 The exporter looks for a tree with tag that matches
816 `org-taskjuggler-project-tag' and takes this as the tasks for
817 this project. The first node of this tree defines the project
818 properties such as project name and project period.
820 If there is a tree with tag that matches
821 `org-taskjuggler-resource-tag' this tree is taken as resources
822 for the project. If no resources are specified, a default
823 resource is created and allocated to the project.
825 Also the TaskJuggler project will be created with default reports
826 as defined in `org-taskjuggler-default-reports'.
828 If narrowing is active in the current buffer, only export its
829 narrowed part.
831 If a region is active, export that region.
833 When optional argument SUBTREEP is non-nil, export the sub-tree
834 at point, extracting information from the headline properties
835 first.
837 When optional argument VISIBLE-ONLY is non-nil, don't export
838 contents of hidden elements.
840 Open file with the TaskJuggler GUI."
841 (interactive)
842 (let* ((file (org-taskjuggler-export nil subtreep visible-only))
843 (process-name "TaskJugglerUI")
844 (command (concat process-name " " file)))
845 (start-process-shell-command process-name nil command)))
848 (provide 'ox-taskjuggler)
850 ;; Local variables:
851 ;; generated-autoload-file: "org-loaddefs.el"
852 ;; sentence-end-double-space: t
853 ;; End:
855 ;;; ox-taskjuggler.el ends here