Use both scheduled and the start property to determine start
[org-mode.git] / contrib / lisp / ox-taskjuggler.el
blob3fdef649becdae15fa1cadfed6eadf40991e2a70
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
144 ;; - Add support for org-export-with-planning
146 ;;; Code:
148 (eval-when-compile (require 'cl))
150 (require 'ox)
154 ;;; User Variables
156 (defgroup org-export-taskjuggler nil
157 "Options specific for TaskJuggler export back-end."
158 :tag "Org Export TaskJuggler"
159 :group 'org-export)
161 (defcustom org-taskjuggler-extension ".tjp"
162 "Extension of TaskJuggler files."
163 :group 'org-export-taskjuggler
164 :type 'string)
166 (defcustom org-taskjuggler-project-tag "taskjuggler_project"
167 "Tag marking project's tasks.
168 This tag is used to find the tree containing all the tasks for
169 the project."
170 :group 'org-export-taskjuggler
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 :type 'string)
180 (defcustom org-taskjuggler-report-tag "taskjuggler_report"
181 "Tag marking project's reports.
182 This tag is used to find the tree containing all the reports for
183 the project."
184 :group 'org-export-taskjuggler
185 :type 'string)
187 (defcustom org-taskjuggler-target-version 3.0
188 "Which version of TaskJuggler the exporter is targeting.
189 By default a project plan is exported which conforms to version
190 3.x of TaskJuggler. For a project plan that is compatible with
191 versions of TaskJuggler older than 3.0 set this to 2.4.
193 If you change this variable be sure to also change
194 `org-taskjuggler-default-reports' as the format of reports has
195 changed considerably between version 2.x and 3.x of TaskJuggler"
196 :group 'org-export-taskjuggler
197 :type 'number)
199 (defcustom org-taskjuggler-default-project-version "1.0"
200 "Default version string for the project.
201 This value can also be set with the \":VERSION:\" property
202 associated to the headline defining the project."
203 :group 'org-export-taskjuggler
204 :type 'string)
206 (defcustom org-taskjuggler-default-project-duration 280
207 "Default project duration.
208 The value will be used if no start and end date have been defined
209 in the root node of the task tree, i.e. the tree that has been
210 marked with `org-taskjuggler-project-tag'"
211 :group 'org-export-taskjuggler
212 :type 'integer)
214 (defcustom org-taskjuggler-default-reports
215 '("textreport report \"Plan\" {
216 formats html
217 header '== <-query attribute=\"name\"-> =='
219 center -8<-
220 [#Plan Plan] | [#Resource_Allocation Resource Allocation]
221 ----
222 === Plan ===
223 <[report id=\"plan\"]>
224 ----
225 === Resource Allocation ===
226 <[report id=\"resourceGraph\"]>
227 ->8-
230 # A traditional Gantt chart with a project overview.
231 taskreport plan \"\" {
232 headline \"Project Plan\"
233 columns bsi, name, start, end, effort, chart
234 loadunit shortauto
235 hideresource 1
238 # A graph showing resource allocation. It identifies whether each
239 # resource is under- or over-allocated for.
240 resourcereport resourceGraph \"\" {
241 headline \"Resource Allocation Graph\"
242 columns no, name, effort, weekly
243 loadunit shortauto
244 hidetask ~(isleaf() & isleaf_())
245 sorttasks plan.start.up
247 "Default reports for the project.
248 These are sensible default reports to give a good out-of-the-box
249 result when exporting without defining any reports. If you want
250 to define your own reports you can change them here or simply
251 define the default reports so that they include an external
252 report definition as follows:
254 include reports.tji
256 These default are made to work with tj3. If you are targeting
257 TaskJuggler 2.4 (see `org-taskjuggler-target-version') please
258 change these defaults to something like the following:
260 taskreport \"Gantt Chart\" {
261 headline \"Project Gantt Chart\"
262 columns hierarchindex, name, start, end, effort, duration, completed, chart
263 timeformat \"%Y-%m-%d\"
264 hideresource 1
265 loadunit shortauto
268 resourcereport \"Resource Graph\" {
269 headline \"Resource Allocation Graph\"
270 columns no, name, utilization, freeload, chart
271 loadunit shortauto
272 sorttasks startup
273 hidetask ~isleaf()
275 :group 'org-export-taskjuggler
276 :type '(repeat (string :tag "Report")))
278 (defcustom org-taskjuggler-default-global-header ""
279 "Default global header for the project.
280 This goes before project declaration, and might be useful for
281 early macros."
282 :group 'org-export-taskjuggler
283 :type '(string :tag "Preamble"))
285 (defcustom org-taskjuggler-default-global-properties
286 "shift s40 \"Part time shift\" {
287 workinghours wed, thu, fri off
290 "Default global properties for the project.
292 Here you typically define global properties such as shifts,
293 accounts, rates, vacation, macros and flags. Any property that
294 is allowed within the TaskJuggler file can be inserted. You
295 could for example include another TaskJuggler file.
297 The global properties are inserted after the project declaration
298 but before any resource and task declarations."
299 :group 'org-export-taskjuggler
300 :type '(string :tag "Preamble"))
302 (defcustom org-taskjuggler-valid-task-attributes
303 '(account start note duration endbuffer endcredit end
304 flags journalentry length limits maxend maxstart minend
305 minstart period reference responsible scheduling
306 startbuffer startcredit statusnote chargeset charge)
307 "Valid attributes for Taskjuggler tasks.
308 If one of these appears as a property for a headline, it will be
309 exported with the corresponding task."
310 :group 'org-export-taskjuggler)
312 (defcustom org-taskjuggler-valid-resource-attributes
313 '(limits vacation shift booking efficiency journalentry rate
314 workinghours flags)
315 "Valid attributes for Taskjuggler resources.
316 If one of these appears as a property for a headline, it will be
317 exported with the corresponding resource."
318 :group 'org-export-taskjuggler)
320 (defcustom org-taskjuggler-valid-report-attributes
321 '(headline columns definitions timeformat hideresource hidetask
322 loadunit sorttasks formats period)
323 "Valid attributes for Taskjuggler reports.
324 If one of these appears as a property for a headline, it will be
325 exported with the corresponding report."
326 :group 'org-export-taskjuggler)
328 (defcustom org-taskjuggler-process-command
329 "tj3 --silent --no-color --output-dir %o %f"
330 "Command to process a Taskjuggler file.
331 The command will be given to the shell as a command to process a
332 Taskjuggler file. %f in the command will be replaced by the full
333 file name, %o by the reports directory (see
334 `org-taskjuggler-reports-directory').
336 If you are targeting Taskjuggler 2.4 (see
337 `org-taskjuggler-target-version') this setting is ignored."
338 :group 'org-export-taskjuggler)
340 (defcustom org-taskjuggler-reports-directory "reports"
341 "Default directory to generate the Taskjuggler reports in.
342 The command `org-taskjuggler-process-command' generates the
343 reports and associated files such as CSS inside this directory.
345 If the directory is not an absolute path it is relative to the
346 directory of the exported file. The directory is created if it
347 doesn't exist.
349 If you are targeting Taskjuggler 2.4 (see
350 `org-taskjuggler-target-version') this setting is ignored."
351 :group 'org-export-taskjuggler)
353 (defcustom org-taskjuggler-keep-project-as-task t
354 "Non-nil keeps the project headline as an umbrella task for all tasks.
355 Setting this to nil will allow maintaining completely separated
356 task buckets, while still sharing the same resources pool."
357 :group 'org-export-taskjuggler
358 :type 'boolean)
362 ;;; Hooks
364 (defvar org-taskjuggler-final-hook nil
365 "Hook run after a TaskJuggler files has been saved.
366 This hook is run with the name of the file as argument.")
370 ;;; Back-End Definition
372 (org-export-define-backend 'taskjuggler
373 '((template . org-taskjuggler-project-plan))
374 :menu-entry
375 '(?J "Export to TaskJuggler"
376 ((?j "As TJP file" (lambda (a s v b) (org-taskjuggler-export a s v)))
377 (?p "As TJP file and process"
378 (lambda (a s v b)
379 (if a (org-taskjuggler-export a s v)
380 (org-taskjuggler-export-and-process s v))))
381 (?o "As TJP file, process and open"
382 (lambda (a s v b)
383 (if a (org-taskjuggler-export a s v)
384 (org-taskjuggler-export-process-and-open s v))))))
385 ;; This property will be used to store unique ids in communication
386 ;; channel. Ids will be retrieved with `org-taskjuggler-get-id'.
387 :options-alist '((:taskjuggler-unique-ids nil nil nil)))
391 ;;; Unique IDs
393 (defun org-taskjuggler-assign-task-ids (tasks info)
394 "Assign a unique ID to each task in TASKS.
395 TASKS is a list of headlines. INFO is a plist used as a
396 communication channel. Return value is an alist between
397 headlines and their associated ID. IDs are hierarchical, which
398 means they only need to be unique among the task siblings."
399 (let* (alist
400 (build-id
401 (lambda (tasks local-ids)
402 (org-element-map tasks 'headline
403 (lambda (task)
404 (let ((id (org-taskjuggler--build-unique-id task local-ids)))
405 (push id local-ids)
406 (push (cons task id) alist)
407 (funcall build-id (org-element-contents task) nil)))
408 info nil 'headline))))
409 (funcall build-id tasks nil)
410 alist))
412 (defun org-taskjuggler-assign-resource-ids (resources info)
413 "Assign a unique ID to each resource within RESOURCES.
414 RESOURCES is a list of headlines. INFO is a plist used as a
415 communication channel. Return value is an alist between
416 headlines and their associated ID."
417 (let (ids)
418 (org-element-map resources 'headline
419 (lambda (resource)
420 (let ((id (org-taskjuggler--build-unique-id resource ids)))
421 (push id ids)
422 (cons resource id)))
423 info)))
427 ;;; Accessors
429 (defun org-taskjuggler-get-project (info)
430 "Return project in parse tree.
431 INFO is a plist used as a communication channel. First headline
432 in buffer with `org-taskjuggler-project-tag' defines the project.
433 If no such task is defined, pick the first headline in buffer.
434 If there is no headline at all, return nil."
435 (or (org-element-map (plist-get info :parse-tree) 'headline
436 (lambda (hl)
437 (and (member org-taskjuggler-project-tag
438 (org-export-get-tags hl info))
439 hl))
440 info t)
441 (org-element-map tree 'headline 'identity info t)))
443 (defun org-taskjuggler-get-id (item info)
444 "Return id for task or resource ITEM.
445 ITEM is a headline. INFO is a plist used as a communication
446 channel. Return value is a string."
447 (cdr (assq item (plist-get info :taskjuggler-unique-ids))))
449 (defun org-taskjuggler-get-name (item)
450 "Return name for task or resource ITEM.
451 ITEM is a headline. Return value is a string."
452 ;; Quote double quotes in name.
453 (replace-regexp-in-string
454 "\"" "\\\"" (org-element-property :raw-value item) t t))
456 (defun org-taskjuggler-get-start (item)
457 "Return start date for task or resource ITEM.
458 ITEM is a headline. Return value is a string or nil if ITEM
459 doesn't have any start date defined.."
460 (let ((scheduled (org-element-property :scheduled item)))
462 (and scheduled (org-timestamp-format scheduled "%Y-%02m-%02d"))
463 (and (member 'start org-taskjuggler-valid-resource-attributes)
464 (org-element-property :START item)))))
466 (defun org-taskjuggler-get-end (item)
467 "Return end date for task or resource ITEM.
468 ITEM is a headline. Return value is a string or nil if ITEM
469 doesn't have any end date defined.."
470 (let ((deadline (org-element-property :deadline item)))
471 (and deadline (org-timestamp-format deadline "%Y-%02m-%02d"))))
475 ;;; Internal Functions
477 (defun org-taskjuggler--indent-string (s)
478 "Indent string S by 2 spaces.
479 Return new string. If S is the empty string, return it."
480 (if (equal "" s) s (replace-regexp-in-string "^ *\\S-" " \\&" s)))
482 (defun org-taskjuggler--build-attributes (item attributes)
483 "Return attributes string for task, resource or report ITEM.
484 ITEM is a headline. ATTRIBUTES is a list of symbols
485 representing valid attributes for ITEM."
486 (mapconcat
487 (lambda (attribute)
488 (let ((value (org-element-property
489 (intern (upcase (format ":%s" attribute)))
490 item)))
491 (and value (format "%s %s\n" attribute value))))
492 (remq nil attributes) ""))
494 (defun org-taskjuggler--build-unique-id (item unique-ids)
495 "Return a unique id for a given task or a resource.
496 ITEM is an `headline' type element representing the task or
497 resource. Its id is derived from its name and made unique
498 against UNIQUE-IDS. If the (downcased) first token of the
499 headline is not unique try to add more (downcased) tokens of the
500 headline or finally add more underscore characters (\"_\")."
501 (let ((id (org-string-nw-p (org-element-property :TASK_ID item))))
502 ;; If an id is specified, use it, as long as it's unique.
503 (if (and id (not (member id unique-ids))) id
504 (let* ((parts (org-split-string (org-element-property :raw-value item)))
505 (id (org-taskjuggler--clean-id (downcase (pop parts)))))
506 ;; Try to add more parts of the headline to make it unique.
507 (while (and (car parts) (member id unique-ids))
508 (setq id (concat id "_"
509 (org-taskjuggler--clean-id (downcase (pop parts))))))
510 ;; If it's still not unique, add "_".
511 (while (member id unique-ids)
512 (setq id (concat id "_")))
513 id))))
515 (defun org-taskjuggler--clean-id (id)
516 "Clean and return ID to make it acceptable for TaskJuggler.
517 ID is a string."
518 ;; Replace non-ascii by "_".
519 (replace-regexp-in-string
520 "[^a-zA-Z0-9_]" "_"
521 ;; Make sure id doesn't start with a number.
522 (replace-regexp-in-string "^\\([0-9]\\)" "_\\1" id)))
526 ;;; Dependencies
528 (defun org-taskjuggler-resolve-dependencies (task info)
529 "Return a list of all tasks TASK depends on.
530 TASK is a headline. INFO is a plist used as a communication
531 channel."
532 (let ((deps-ids
533 ;; Get all dependencies specified in BLOCKER and DEPENDS task
534 ;; properties. Clean options from them.
535 (let ((deps (concat (org-element-property :BLOCKER task)
536 (org-element-property :DEPENDS task))))
537 (and deps
538 (org-split-string (replace-regexp-in-string "{.*?}" "" deps)
539 "[ ,]* +"))))
540 depends)
541 (when deps-ids
542 ;; Find tasks with :task_id: property matching id in DEPS-IDS.
543 ;; Add them to DEPENDS.
544 (let* ((project (org-taskjuggler-get-project info))
545 (tasks (if org-taskjuggler-keep-project-as-task project
546 (org-element-contents project))))
547 (setq depends
548 (org-element-map tasks 'headline
549 (lambda (task)
550 (let ((task-id (org-element-property :TASK_ID task)))
551 (and task-id (member task-id deps-ids) task)))
552 info)))
553 ;; Check BLOCKER and DEPENDS properties. If "previous-sibling"
554 ;; belongs to DEPS-ID, add it to DEPENDS.
555 (when (and (member-ignore-case "previous-sibling" deps-ids)
556 (not (org-export-first-sibling-p task info)))
557 (let ((prev (org-export-get-previous-element task info)))
558 (and (not (memq prev depends)) (push prev depends)))))
559 ;; Check ORDERED status of parent.
560 (let ((parent (org-export-get-parent task)))
561 (when (and parent
562 (org-element-property :ORDERED parent)
563 (not (org-export-first-sibling-p task info)))
564 (push (org-export-get-previous-element task info) depends)))
565 ;; Return dependencies.
566 depends))
568 (defun org-taskjuggler-format-dependencies (dependencies task info)
569 "Format DEPENDENCIES to match TaskJuggler syntax.
570 DEPENDENCIES is list of dependencies for TASK, as returned by
571 `org-taskjuggler-resolve-depedencies'. TASK is a headline.
572 INFO is a plist used as a communication channel. Return value
573 doesn't include leading \"depends\"."
574 (let ((dep-str (concat (org-element-property :BLOCKER task)
576 (org-element-property :DEPENDS task)))
577 (get-path
578 (lambda (dep)
579 ;; Return path to DEP relatively to TASK.
580 (let ((parent (org-export-get-parent task))
581 (exclamations 1)
582 (option
583 (let ((id (org-element-property :TASK_ID dep)))
584 (and id
585 (string-match (concat id " +\\({.*?}\\)") dep-str)
586 (org-match-string-no-properties 1))))
587 path)
588 ;; Compute number of exclamation marks by looking for the
589 ;; common ancestor between TASK and DEP.
590 (while (not (org-element-map parent 'headline
591 (lambda (hl) (eq hl dep))))
592 (incf exclamations)
593 (setq parent (org-export-get-parent parent)))
594 ;; Build path from DEP to PARENT.
595 (while (not (eq parent dep))
596 (push (org-taskjuggler-get-id dep info) path)
597 (setq dep (org-export-get-parent dep)))
598 ;; Return full path. Add dependency options, if any.
599 (concat (make-string exclamations ?!)
600 (mapconcat 'identity path ".")
601 (and option (concat " " option)))))))
602 ;; Return dependencies string, without the leading "depends".
603 (mapconcat (lambda (dep) (funcall get-path dep)) dependencies ", ")))
607 ;;; Translator Functions
609 (defun org-taskjuggler-project-plan (contents info)
610 "Build TaskJuggler project plan.
611 CONTENTS is ignored. INFO is a plist holding export options.
612 Return complete project plan as a string in TaskJuggler syntax."
613 (let* ((tree (plist-get info :parse-tree))
614 (project (or (org-taskjuggler-get-project info)
615 (error "No project specified"))))
616 (concat
617 ;; 1. Insert header.
618 (org-element-normalize-string org-taskjuggler-default-global-header)
619 ;; 2. Insert project.
620 (org-taskjuggler--build-project project info)
621 ;; 3. Insert global properties.
622 (org-element-normalize-string org-taskjuggler-default-global-properties)
623 ;; 4. Insert resources. Provide a default one if none is
624 ;; specified.
625 (let ((main-resources
626 ;; Collect contents from various trees marked with
627 ;; `org-taskjuggler-resource-tag'. Only gather top level
628 ;; resources.
629 (apply 'append
630 (org-element-map tree 'headline
631 (lambda (hl)
632 (and (member org-taskjuggler-resource-tag
633 (org-export-get-tags hl info))
634 (org-element-map (org-element-contents hl) 'headline
635 'identity info nil 'headline)))
636 info nil 'headline))))
637 ;; Assign a unique ID to each resource. Store it under
638 ;; `:taskjuggler-unique-ids' property in INFO.
639 (setq info
640 (plist-put info :taskjuggler-unique-ids
641 (org-taskjuggler-assign-resource-ids
642 main-resources info)))
643 (concat
644 (if main-resources
645 (mapconcat
646 (lambda (resource) (org-taskjuggler--build-resource resource info))
647 main-resources "")
648 (format "resource %s \"%s\" {\n}\n" (user-login-name) user-full-name))
649 ;; 5. Insert tasks.
650 (let ((main-tasks
651 ;; If `org-taskjuggler-keep-project-as-task' is
652 ;; non-nil, there is only one task. Otherwise, every
653 ;; direct children of PROJECT is a top level task.
654 (if org-taskjuggler-keep-project-as-task (list project)
655 (or (org-element-map (org-element-contents project) 'headline
656 'identity info nil 'headline)
657 (error "No task specified")))))
658 ;; Assign a unique ID to each task. Add it to
659 ;; `:taskjuggler-unique-ids' property in INFO.
660 (setq info
661 (plist-put info :taskjuggler-unique-ids
662 (append
663 (org-taskjuggler-assign-task-ids main-tasks info)
664 (plist-get info :taskjuggler-unique-ids))))
665 ;; If no resource is allocated among tasks, allocate one to
666 ;; the first task.
667 (unless (org-element-map main-tasks 'headline
668 (lambda (task) (org-element-property :ALLOCATE task))
669 info t)
670 (org-element-put-property
671 (car main-tasks) :ALLOCATE
672 (or (org-taskjuggler-get-id (car main-resources) info)
673 (user-login-name))))
674 (mapconcat
675 (lambda (task) (org-taskjuggler--build-task task info))
676 main-tasks ""))
677 ;; 6. Insert reports. If no report is defined, insert default
678 ;; reports.
679 (let ((main-reports
680 ;; Collect contents from various trees marked with
681 ;; `org-taskjuggler-report-tag'. Only gather top level
682 ;; reports.
683 (apply 'append
684 (org-element-map tree 'headline
685 (lambda (hl)
686 (and (member org-taskjuggler-report-tag
687 (org-export-get-tags hl info))
688 (org-element-map (org-element-contents hl)
689 'headline 'identity info nil 'headline)))
690 info nil 'headline))))
691 (if main-reports
692 (mapconcat
693 (lambda (report) (org-taskjuggler--build-report report info))
694 main-reports "")
695 (mapconcat 'org-element-normalize-string
696 org-taskjuggler-default-reports ""))))))))
698 (defun org-taskjuggler--build-project (project info)
699 "Return a project declaration.
700 PROJECT is a headline. INFO is a plist used as a communication
701 channel. If no start date is specified, start today. If no end
702 date is specified, end `org-taskjuggler-default-project-duration'
703 days from now."
704 (format "project %s \"%s\" \"%s\" %s %s {\n}\n"
705 (org-taskjuggler-get-id project info)
706 (org-taskjuggler-get-name project)
707 ;; Version is obtained through :TASKJUGGLER_VERSION:
708 ;; property or `org-taskjuggler-default-project-version'.
709 (or (org-element-property :VERSION project)
710 org-taskjuggler-default-project-version)
711 (or (org-taskjuggler-get-start project)
712 (format-time-string "%Y-%m-%d"))
713 (let ((end (org-taskjuggler-get-end project)))
714 (or (and end (format "- %s" end))
715 (format "+%sd" org-taskjuggler-default-project-duration)))))
717 (defun org-taskjuggler--build-resource (resource info)
718 "Return a resource declaration.
720 RESOURCE is a headline. INFO is a plist used as a communication
721 channel.
723 All valid attributes from RESOURCE are inserted. If RESOURCE
724 defines a property \"resource_id\" it will be used as the id for
725 this resource. Otherwise it will use the ID property. If
726 neither is defined a unique id will be associated to it."
727 (concat
728 ;; Opening resource.
729 (format "resource %s \"%s\" {\n"
730 (org-taskjuggler--clean-id
731 (or (org-element-property :RESOURCE_ID resource)
732 (org-element-property :ID resource)
733 (org-taskjuggler-get-id resource info)))
734 (org-taskjuggler-get-name resource))
735 ;; Add attributes.
736 (org-taskjuggler--indent-string
737 (org-taskjuggler--build-attributes
738 resource org-taskjuggler-valid-resource-attributes))
739 ;; Add inner resources.
740 (org-taskjuggler--indent-string
741 (mapconcat
742 'identity
743 (org-element-map (org-element-contents resource) 'headline
744 (lambda (hl) (org-taskjuggler--build-resource hl info))
745 info nil 'headline)
746 ""))
747 ;; Closing resource.
748 "}\n"))
750 (defun org-taskjuggler--build-report (report info)
751 "Return a report declaration.
752 REPORT is a headline. INFO is a plist used as a communication
753 channel."
754 (concat
755 ;; Opening report.
756 (format "%s \"%s\" {\n"
757 (or (org-element-property :REPORT_KIND report) "taskreport")
758 (org-taskjuggler-get-name report))
759 ;; Add attributes.
760 (org-taskjuggler--indent-string
761 (org-taskjuggler--build-attributes
762 report org-taskjuggler-valid-report-attributes))
763 ;; Add inner reports.
764 (org-taskjuggler--indent-string
765 (mapconcat
766 'identity
767 (org-element-map (org-element-contents report) 'headline
768 (lambda (hl) (org-taskjuggler--build-report hl info))
769 info nil 'headline)
770 ""))
771 ;; Closing report.
772 "}\n"))
774 (defun org-taskjuggler--build-task (task info)
775 "Return a task declaration.
777 TASK is a headline. INFO is a plist used as a communication
778 channel.
780 All valid attributes from TASK are inserted. If TASK defines
781 a property \"task_id\" it will be used as the id for this task.
782 Otherwise it will use the ID property. If neither is defined
783 a unique id will be associated to it."
784 (let* ((allocate (org-element-property :ALLOCATE task))
785 (complete
786 (if (eq (org-element-property :todo-type task) 'done) "100"
787 (org-element-property :COMPLETE task)))
788 (depends (org-taskjuggler-resolve-dependencies task info))
789 (effort (org-element-property :EFFORT task))
790 (milestone
791 (or (org-element-property :MILESTONE task)
792 (not (or (org-element-map (org-element-contents task) 'headline
793 'identity info t) ; Has task any child?
794 effort
795 (org-element-property :LENGTH task)
796 (org-element-property :DURATION task)
797 (and (org-taskjuggler-get-start task)
798 (org-taskjuggler-get-end task))
799 (org-element-property :PERIOD task)))))
800 (priority
801 (let ((pri (org-element-property :priority task)))
802 (and pri
803 (max 1 (/ (* 1000 (- org-lowest-priority pri))
804 (- org-lowest-priority org-highest-priority)))))))
805 (concat
806 ;; Opening task.
807 (format "task %s \"%s\" {\n"
808 (org-taskjuggler-get-id task info)
809 (org-taskjuggler-get-name task))
810 ;; Add default attributes.
811 (and depends
812 (format " depends %s\n"
813 (org-taskjuggler-format-dependencies depends task info)))
814 (and allocate
815 (format " purge %s\n allocate %s\n"
816 ;; Compatibility for previous TaskJuggler versions.
817 (if (>= org-taskjuggler-target-version 3.0) "allocate"
818 "allocations")
819 allocate))
820 (and complete (format " complete %s\n" complete))
821 (and effort
822 (format " effort %s\n"
823 (let* ((minutes (org-duration-string-to-minutes effort))
824 (hours (/ minutes 60.0)))
825 (format "%.1fh" hours))))
826 (and priority (format " priority %s\n" priority))
827 (and milestone " milestone\n")
828 ;; Add other valid attributes.
829 (org-taskjuggler--indent-string
830 (org-taskjuggler--build-attributes
831 task org-taskjuggler-valid-task-attributes))
832 ;; Add inner tasks.
833 (org-taskjuggler--indent-string
834 (mapconcat 'identity
835 (org-element-map (org-element-contents task) 'headline
836 (lambda (hl) (org-taskjuggler--build-task hl info))
837 info nil 'headline)
838 ""))
839 ;; Closing task.
840 "}\n")))
844 ;;; Interactive Functions
846 ;;;###autoload
847 (defun org-taskjuggler-export (&optional async subtreep visible-only)
848 "Export current buffer to a TaskJuggler file.
850 The exporter looks for a tree with tag that matches
851 `org-taskjuggler-project-tag' and takes this as the tasks for
852 this project. The first node of this tree defines the project
853 properties such as project name and project period.
855 If there is a tree with tag that matches
856 `org-taskjuggler-resource-tag' this tree is taken as resources
857 for the project. If no resources are specified, a default
858 resource is created and allocated to the project.
860 Also the TaskJuggler project will be created with default reports
861 as defined in `org-taskjuggler-default-reports'.
863 If narrowing is active in the current buffer, only export its
864 narrowed part.
866 If a region is active, export that region.
868 A non-nil optional argument ASYNC means the process should happen
869 asynchronously. The resulting file should be accessible through
870 the `org-export-stack' interface.
872 When optional argument SUBTREEP is non-nil, export the sub-tree
873 at point, extracting information from the headline properties
874 first.
876 When optional argument VISIBLE-ONLY is non-nil, don't export
877 contents of hidden elements.
879 Return output file's name."
880 (interactive)
881 (let ((outfile
882 (org-export-output-file-name org-taskjuggler-extension subtreep)))
883 (if async
884 (org-export-async-start
885 (lambda (f)
886 (org-export-add-to-stack f 'taskjuggler)
887 (run-hook-with-args 'org-taskjuggler-final-hook f))
888 `(expand-file-name
889 (org-export-to-file 'taskjuggler ,outfile ,subtreep ,visible-only)))
890 (org-export-to-file 'taskjuggler outfile subtreep visible-only)
891 (run-hook-with-args 'org-taskjuggler-final-hook outfile)
892 outfile)))
894 ;;;###autoload
895 (defun org-taskjuggler-export-and-process (&optional subtreep visible-only)
896 "Export current buffer to a TaskJuggler file and process it.
898 The exporter looks for a tree with tag that matches
899 `org-taskjuggler-project-tag' and takes this as the tasks for
900 this project. The first node of this tree defines the project
901 properties such as project name and project period.
903 If there is a tree with tag that matches
904 `org-taskjuggler-resource-tag' this tree is taken as resources
905 for the project. If no resources are specified, a default
906 resource is created and allocated to the project.
908 Also the TaskJuggler project will be created with default reports
909 as defined in `org-taskjuggler-default-reports'.
911 If narrowing is active in the current buffer, only export its
912 narrowed part.
914 If a region is active, export that region.
916 When optional argument SUBTREEP is non-nil, export the sub-tree
917 at point, extracting information from the headline properties
918 first.
920 When optional argument VISIBLE-ONLY is non-nil, don't export
921 contents of hidden elements.
923 Return a list of reports."
924 (interactive)
925 (let ((file (org-taskjuggler-export nil subtreep visible-only)))
926 (org-taskjuggler-compile file)))
928 ;;;###autoload
929 (defun org-taskjuggler-export-process-and-open (&optional subtreep visible-only)
930 "Export current buffer to a TaskJuggler file, process and open it.
932 Export and process the file using
933 `org-taskjuggler-export-and-process' and open the generated
934 reports with a browser.
936 If you are targeting TaskJuggler 2.4 (see
937 `org-taskjuggler-target-version') the processing and display of
938 the reports is done using the TaskJuggler GUI."
939 (interactive)
940 (if (< org-taskjuggler-target-version 3.0)
941 (let* ((process-name "TaskJugglerUI")
942 (command
943 (concat process-name " "
944 (org-taskjuggler-export nil subtreep visible-only))))
945 (start-process-shell-command process-name nil command))
946 (dolist (report (org-taskjuggler-export-and-process subtreep visible-only))
947 (org-open-file report))))
949 (defun org-taskjuggler-compile (file)
950 "Compile a TaskJuggler file.
952 FILE is the name of the file being compiled. Processing is done
953 through the command given in `org-taskjuggler-process-command'.
955 Return a list of reports."
956 (let* ((full-name (file-truename file))
957 (out-dir
958 (expand-file-name
959 org-taskjuggler-reports-directory (file-name-directory file)))
960 errors)
961 (message (format "Processing TaskJuggler file %s..." file))
962 (save-window-excursion
963 (let ((outbuf (get-buffer-create "*Org Taskjuggler Output*")))
964 (unless (file-directory-p out-dir)
965 (make-directory out-dir t))
966 (with-current-buffer outbuf (erase-buffer))
967 (shell-command
968 (replace-regexp-in-string
969 "%f" (shell-quote-argument full-name)
970 (replace-regexp-in-string
971 "%o" (shell-quote-argument out-dir)
972 org-taskjuggler-process-command t t) t t) outbuf)
973 ;; Collect standard errors from output buffer.
974 (setq errors (org-taskjuggler--collect-errors outbuf)))
975 (if (not errors)
976 (message "Process completed.")
977 (error (format "TaskJuggler failed with errors: %s" errors))))
978 (file-expand-wildcards (format "%s/*.html" out-dir))))
980 (defun org-taskjuggler--collect-errors (buffer)
981 "Collect some kind of errors from \"tj3\" command output.
983 BUFFER is the buffer containing output.
985 Return collected error types as a string, or nil if there was
986 none."
987 (with-current-buffer buffer
988 (save-excursion
989 (goto-char (point-min))
990 (let ((case-fold-search t)
991 (errors ""))
992 (while (re-search-forward "^.+:[0-9]+: \\(.*\\)$" nil t)
993 (setq errors (concat errors " " (match-string 1))))
994 (and (org-string-nw-p errors) (org-trim errors))))))
997 (provide 'ox-taskjuggler)
999 ;; Local variables:
1000 ;; sentence-end-double-space: t
1001 ;; End:
1003 ;;; ox-taskjuggler.el ends here