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