e1f82bb74aa1bc9f85b968fc3c7bb018f5940746
[org-mode.git] / contrib / lisp / ox-taskjuggler.el
blobe1f82bb74aa1bc9f85b968fc3c7bb018f5940746
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)))
461 (and scheduled (org-timestamp-format scheduled "%Y-%02m-%02d"))))
463 (defun org-taskjuggler-get-end (item)
464 "Return end date for task or resource ITEM.
465 ITEM is a headline. Return value is a string or nil if ITEM
466 doesn't have any end date defined.."
467 (let ((deadline (org-element-property :deadline item)))
468 (and deadline (org-timestamp-format deadline "%Y-%02m-%02d"))))
472 ;;; Internal Functions
474 (defun org-taskjuggler--indent-string (s)
475 "Indent string S by 2 spaces.
476 Return new string. If S is the empty string, return it."
477 (if (equal "" s) s (replace-regexp-in-string "^ *\\S-" " \\&" s)))
479 (defun org-taskjuggler--build-attributes (item attributes)
480 "Return attributes string for task, resource or report ITEM.
481 ITEM is a headline. ATTRIBUTES is a list of symbols
482 representing valid attributes for ITEM."
483 (mapconcat
484 (lambda (attribute)
485 (let ((value (org-element-property
486 (intern (upcase (format ":%s" attribute)))
487 item)))
488 (and value (format "%s %s\n" attribute value))))
489 (remq nil attributes) ""))
491 (defun org-taskjuggler--build-unique-id (item unique-ids)
492 "Return a unique id for a given task or a resource.
493 ITEM is an `headline' type element representing the task or
494 resource. Its id is derived from its name and made unique
495 against UNIQUE-IDS. If the (downcased) first token of the
496 headline is not unique try to add more (downcased) tokens of the
497 headline or finally add more underscore characters (\"_\")."
498 (let ((id (org-string-nw-p (org-element-property :TASK_ID item))))
499 ;; If an id is specified, use it, as long as it's unique.
500 (if (and id (not (member id unique-ids))) id
501 (let* ((parts (org-split-string (org-element-property :raw-value item)))
502 (id (org-taskjuggler--clean-id (downcase (pop parts)))))
503 ;; Try to add more parts of the headline to make it unique.
504 (while (and (car parts) (member id unique-ids))
505 (setq id (concat id "_"
506 (org-taskjuggler--clean-id (downcase (pop parts))))))
507 ;; If it's still not unique, add "_".
508 (while (member id unique-ids)
509 (setq id (concat id "_")))
510 id))))
512 (defun org-taskjuggler--clean-id (id)
513 "Clean and return ID to make it acceptable for TaskJuggler.
514 ID is a string."
515 ;; Replace non-ascii by "_".
516 (replace-regexp-in-string
517 "[^a-zA-Z0-9_]" "_"
518 ;; Make sure id doesn't start with a number.
519 (replace-regexp-in-string "^\\([0-9]\\)" "_\\1" id)))
523 ;;; Dependencies
525 (defun org-taskjuggler-resolve-dependencies (task info)
526 "Return a list of all tasks TASK depends on.
527 TASK is a headline. INFO is a plist used as a communication
528 channel."
529 (let ((deps-ids
530 ;; Get all dependencies specified in BLOCKER and DEPENDS task
531 ;; properties. Clean options from them.
532 (let ((deps (concat (org-element-property :BLOCKER task)
533 (org-element-property :DEPENDS task))))
534 (and deps
535 (org-split-string (replace-regexp-in-string "{.*?}" "" deps)
536 "[ ,]* +"))))
537 depends)
538 (when deps-ids
539 ;; Find tasks with :task_id: property matching id in DEPS-IDS.
540 ;; Add them to DEPENDS.
541 (let* ((project (org-taskjuggler-get-project info))
542 (tasks (if org-taskjuggler-keep-project-as-task project
543 (org-element-contents project))))
544 (setq depends
545 (org-element-map tasks 'headline
546 (lambda (task)
547 (let ((task-id (org-element-property :TASK_ID task)))
548 (and task-id (member task-id deps-ids) task)))
549 info)))
550 ;; Check BLOCKER and DEPENDS properties. If "previous-sibling"
551 ;; belongs to DEPS-ID, add it to DEPENDS.
552 (when (and (member-ignore-case "previous-sibling" deps-ids)
553 (not (org-export-first-sibling-p task info)))
554 (let ((prev (org-export-get-previous-element task info)))
555 (and (not (memq prev depends)) (push prev depends)))))
556 ;; Check ORDERED status of parent.
557 (let ((parent (org-export-get-parent task)))
558 (when (and parent
559 (org-element-property :ORDERED parent)
560 (not (org-export-first-sibling-p task info)))
561 (push (org-export-get-previous-element task info) depends)))
562 ;; Return dependencies.
563 depends))
565 (defun org-taskjuggler-format-dependencies (dependencies task info)
566 "Format DEPENDENCIES to match TaskJuggler syntax.
567 DEPENDENCIES is list of dependencies for TASK, as returned by
568 `org-taskjuggler-resolve-depedencies'. TASK is a headline.
569 INFO is a plist used as a communication channel. Return value
570 doesn't include leading \"depends\"."
571 (let ((dep-str (concat (org-element-property :BLOCKER task)
573 (org-element-property :DEPENDS task)))
574 (get-path
575 (lambda (dep)
576 ;; Return path to DEP relatively to TASK.
577 (let ((parent (org-export-get-parent task))
578 (exclamations 1)
579 (option
580 (let ((id (org-element-property :TASK_ID dep)))
581 (and id
582 (string-match (concat id " +\\({.*?}\\)") dep-str)
583 (org-match-string-no-properties 1))))
584 path)
585 ;; Compute number of exclamation marks by looking for the
586 ;; common ancestor between TASK and DEP.
587 (while (not (org-element-map parent 'headline
588 (lambda (hl) (eq hl dep))))
589 (incf exclamations)
590 (setq parent (org-export-get-parent parent)))
591 ;; Build path from DEP to PARENT.
592 (while (not (eq parent dep))
593 (push (org-taskjuggler-get-id dep info) path)
594 (setq dep (org-export-get-parent dep)))
595 ;; Return full path. Add dependency options, if any.
596 (concat (make-string exclamations ?!)
597 (mapconcat 'identity path ".")
598 (and option (concat " " option)))))))
599 ;; Return dependencies string, without the leading "depends".
600 (mapconcat (lambda (dep) (funcall get-path dep)) dependencies ", ")))
604 ;;; Translator Functions
606 (defun org-taskjuggler-project-plan (contents info)
607 "Build TaskJuggler project plan.
608 CONTENTS is ignored. INFO is a plist holding export options.
609 Return complete project plan as a string in TaskJuggler syntax."
610 (let* ((tree (plist-get info :parse-tree))
611 (project (or (org-taskjuggler-get-project info)
612 (error "No project specified"))))
613 (concat
614 ;; 1. Insert header.
615 (org-element-normalize-string org-taskjuggler-default-global-header)
616 ;; 2. Insert project.
617 (org-taskjuggler--build-project project info)
618 ;; 3. Insert global properties.
619 (org-element-normalize-string org-taskjuggler-default-global-properties)
620 ;; 4. Insert resources. Provide a default one if none is
621 ;; specified.
622 (let ((main-resources
623 ;; Collect contents from various trees marked with
624 ;; `org-taskjuggler-resource-tag'. Only gather top level
625 ;; resources.
626 (apply 'append
627 (org-element-map tree 'headline
628 (lambda (hl)
629 (and (member org-taskjuggler-resource-tag
630 (org-export-get-tags hl info))
631 (org-element-map (org-element-contents hl) 'headline
632 'identity info nil 'headline)))
633 info nil 'headline))))
634 ;; Assign a unique ID to each resource. Store it under
635 ;; `:taskjuggler-unique-ids' property in INFO.
636 (setq info
637 (plist-put info :taskjuggler-unique-ids
638 (org-taskjuggler-assign-resource-ids
639 main-resources info)))
640 (concat
641 (if main-resources
642 (mapconcat
643 (lambda (resource) (org-taskjuggler--build-resource resource info))
644 main-resources "")
645 (format "resource %s \"%s\" {\n}\n" (user-login-name) user-full-name))
646 ;; 5. Insert tasks.
647 (let ((main-tasks
648 ;; If `org-taskjuggler-keep-project-as-task' is
649 ;; non-nil, there is only one task. Otherwise, every
650 ;; direct children of PROJECT is a top level task.
651 (if org-taskjuggler-keep-project-as-task (list project)
652 (or (org-element-map (org-element-contents project) 'headline
653 'identity info nil 'headline)
654 (error "No task specified")))))
655 ;; Assign a unique ID to each task. Add it to
656 ;; `:taskjuggler-unique-ids' property in INFO.
657 (setq info
658 (plist-put info :taskjuggler-unique-ids
659 (append
660 (org-taskjuggler-assign-task-ids main-tasks info)
661 (plist-get info :taskjuggler-unique-ids))))
662 ;; If no resource is allocated among tasks, allocate one to
663 ;; the first task.
664 (unless (org-element-map main-tasks 'headline
665 (lambda (task) (org-element-property :ALLOCATE task))
666 info t)
667 (org-element-put-property
668 (car main-tasks) :ALLOCATE
669 (or (org-taskjuggler-get-id (car main-resources) info)
670 (user-login-name))))
671 (mapconcat
672 (lambda (task) (org-taskjuggler--build-task task info))
673 main-tasks ""))
674 ;; 6. Insert reports. If no report is defined, insert default
675 ;; reports.
676 (let ((main-reports
677 ;; Collect contents from various trees marked with
678 ;; `org-taskjuggler-report-tag'. Only gather top level
679 ;; reports.
680 (apply 'append
681 (org-element-map tree 'headline
682 (lambda (hl)
683 (and (member org-taskjuggler-report-tag
684 (org-export-get-tags hl info))
685 (org-element-map (org-element-contents hl)
686 'headline 'identity info nil 'headline)))
687 info nil 'headline))))
688 (if main-reports
689 (mapconcat
690 (lambda (report) (org-taskjuggler--build-report report info))
691 main-reports "")
692 (mapconcat 'org-element-normalize-string
693 org-taskjuggler-default-reports ""))))))))
695 (defun org-taskjuggler--build-project (project info)
696 "Return a project declaration.
697 PROJECT is a headline. INFO is a plist used as a communication
698 channel. If no start date is specified, start today. If no end
699 date is specified, end `org-taskjuggler-default-project-duration'
700 days from now."
701 (format "project %s \"%s\" \"%s\" %s %s {\n}\n"
702 (org-taskjuggler-get-id project info)
703 (org-taskjuggler-get-name project)
704 ;; Version is obtained through :TASKJUGGLER_VERSION:
705 ;; property or `org-taskjuggler-default-project-version'.
706 (or (org-element-property :VERSION project)
707 org-taskjuggler-default-project-version)
708 (or (org-taskjuggler-get-start project)
709 (format-time-string "%Y-%m-%d"))
710 (let ((end (org-taskjuggler-get-end project)))
711 (or (and end (format "- %s" end))
712 (format "+%sd" org-taskjuggler-default-project-duration)))))
714 (defun org-taskjuggler--build-resource (resource info)
715 "Return a resource declaration.
717 RESOURCE is a headline. INFO is a plist used as a communication
718 channel.
720 All valid attributes from RESOURCE are inserted. If RESOURCE
721 defines a property \"resource_id\" it will be used as the id for
722 this resource. Otherwise it will use the ID property. If
723 neither is defined a unique id will be associated to it."
724 (concat
725 ;; Opening resource.
726 (format "resource %s \"%s\" {\n"
727 (org-taskjuggler--clean-id
728 (or (org-element-property :RESOURCE_ID resource)
729 (org-element-property :ID resource)
730 (org-taskjuggler-get-id resource info)))
731 (org-taskjuggler-get-name resource))
732 ;; Add attributes.
733 (org-taskjuggler--indent-string
734 (org-taskjuggler--build-attributes
735 resource org-taskjuggler-valid-resource-attributes))
736 ;; Add inner resources.
737 (org-taskjuggler--indent-string
738 (mapconcat
739 'identity
740 (org-element-map (org-element-contents resource) 'headline
741 (lambda (hl) (org-taskjuggler--build-resource hl info))
742 info nil 'headline)
743 ""))
744 ;; Closing resource.
745 "}\n"))
747 (defun org-taskjuggler--build-report (report info)
748 "Return a report declaration.
749 REPORT is a headline. INFO is a plist used as a communication
750 channel."
751 (concat
752 ;; Opening report.
753 (format "%s \"%s\" {\n"
754 (or (org-element-property :REPORT_KIND report) "taskreport")
755 (org-taskjuggler-get-name report))
756 ;; Add attributes.
757 (org-taskjuggler--indent-string
758 (org-taskjuggler--build-attributes
759 report org-taskjuggler-valid-report-attributes))
760 ;; Add inner reports.
761 (org-taskjuggler--indent-string
762 (mapconcat
763 'identity
764 (org-element-map (org-element-contents report) 'headline
765 (lambda (hl) (org-taskjuggler--build-report hl info))
766 info nil 'headline)
767 ""))
768 ;; Closing report.
769 "}\n"))
771 (defun org-taskjuggler--build-task (task info)
772 "Return a task declaration.
774 TASK is a headline. INFO is a plist used as a communication
775 channel.
777 All valid attributes from TASK are inserted. If TASK defines
778 a property \"task_id\" it will be used as the id for this task.
779 Otherwise it will use the ID property. If neither is defined
780 a unique id will be associated to it."
781 (let* ((allocate (org-element-property :ALLOCATE task))
782 (complete
783 (if (eq (org-element-property :todo-type task) 'done) "100"
784 (org-element-property :COMPLETE task)))
785 (depends (org-taskjuggler-resolve-dependencies task info))
786 (effort (org-element-property :EFFORT task))
787 (milestone
788 (or (org-element-property :MILESTONE task)
789 (not (or (org-element-map (org-element-contents task) 'headline
790 'identity info t) ; Has task any child?
791 effort
792 (org-element-property :LENGTH task)
793 (org-element-property :DURATION task)
794 (and (org-taskjuggler-get-start task)
795 (org-taskjuggler-get-end task))
796 (org-element-property :PERIOD task)))))
797 (priority
798 (let ((pri (org-element-property :priority task)))
799 (and pri
800 (max 1 (/ (* 1000 (- org-lowest-priority pri))
801 (- org-lowest-priority org-highest-priority)))))))
802 (concat
803 ;; Opening task.
804 (format "task %s \"%s\" {\n"
805 (org-taskjuggler-get-id task info)
806 (org-taskjuggler-get-name task))
807 ;; Add default attributes.
808 (and depends
809 (format " depends %s\n"
810 (org-taskjuggler-format-dependencies depends task info)))
811 (and allocate
812 (format " purge %s\n allocate %s\n"
813 ;; Compatibility for previous TaskJuggler versions.
814 (if (>= org-taskjuggler-target-version 3.0) "allocate"
815 "allocations")
816 allocate))
817 (and complete (format " complete %s\n" complete))
818 (and effort
819 (format " effort %s\n"
820 (let* ((minutes (org-duration-string-to-minutes effort))
821 (hours (/ minutes 60.0)))
822 (format "%.1fh" hours))))
823 (and priority (format " priority %s\n" priority))
824 (and milestone " milestone\n")
825 ;; Add other valid attributes.
826 (org-taskjuggler--indent-string
827 (org-taskjuggler--build-attributes
828 task org-taskjuggler-valid-task-attributes))
829 ;; Add inner tasks.
830 (org-taskjuggler--indent-string
831 (mapconcat 'identity
832 (org-element-map (org-element-contents task) 'headline
833 (lambda (hl) (org-taskjuggler--build-task hl info))
834 info nil 'headline)
835 ""))
836 ;; Closing task.
837 "}\n")))
841 ;;; Interactive Functions
843 ;;;###autoload
844 (defun org-taskjuggler-export (&optional async subtreep visible-only)
845 "Export current buffer to a TaskJuggler file.
847 The exporter looks for a tree with tag that matches
848 `org-taskjuggler-project-tag' and takes this as the tasks for
849 this project. The first node of this tree defines the project
850 properties such as project name and project period.
852 If there is a tree with tag that matches
853 `org-taskjuggler-resource-tag' this tree is taken as resources
854 for the project. If no resources are specified, a default
855 resource is created and allocated to the project.
857 Also the TaskJuggler project will be created with default reports
858 as defined in `org-taskjuggler-default-reports'.
860 If narrowing is active in the current buffer, only export its
861 narrowed part.
863 If a region is active, export that region.
865 A non-nil optional argument ASYNC means the process should happen
866 asynchronously. The resulting file should be accessible through
867 the `org-export-stack' interface.
869 When optional argument SUBTREEP is non-nil, export the sub-tree
870 at point, extracting information from the headline properties
871 first.
873 When optional argument VISIBLE-ONLY is non-nil, don't export
874 contents of hidden elements.
876 Return output file's name."
877 (interactive)
878 (let ((outfile
879 (org-export-output-file-name org-taskjuggler-extension subtreep)))
880 (if async
881 (org-export-async-start
882 (lambda (f)
883 (org-export-add-to-stack f 'taskjuggler)
884 (run-hook-with-args 'org-taskjuggler-final-hook f))
885 `(expand-file-name
886 (org-export-to-file 'taskjuggler ,outfile ,subtreep ,visible-only)))
887 (org-export-to-file 'taskjuggler outfile subtreep visible-only)
888 (run-hook-with-args 'org-taskjuggler-final-hook outfile)
889 outfile)))
891 ;;;###autoload
892 (defun org-taskjuggler-export-and-process (&optional subtreep visible-only)
893 "Export current buffer to a TaskJuggler file and process it.
895 The exporter looks for a tree with tag that matches
896 `org-taskjuggler-project-tag' and takes this as the tasks for
897 this project. The first node of this tree defines the project
898 properties such as project name and project period.
900 If there is a tree with tag that matches
901 `org-taskjuggler-resource-tag' this tree is taken as resources
902 for the project. If no resources are specified, a default
903 resource is created and allocated to the project.
905 Also the TaskJuggler project will be created with default reports
906 as defined in `org-taskjuggler-default-reports'.
908 If narrowing is active in the current buffer, only export its
909 narrowed part.
911 If a region is active, export that region.
913 When optional argument SUBTREEP is non-nil, export the sub-tree
914 at point, extracting information from the headline properties
915 first.
917 When optional argument VISIBLE-ONLY is non-nil, don't export
918 contents of hidden elements.
920 Return a list of reports."
921 (interactive)
922 (let ((file (org-taskjuggler-export nil subtreep visible-only)))
923 (org-taskjuggler-compile file)))
925 ;;;###autoload
926 (defun org-taskjuggler-export-process-and-open (&optional subtreep visible-only)
927 "Export current buffer to a TaskJuggler file, process and open it.
929 Export and process the file using
930 `org-taskjuggler-export-and-process' and open the generated
931 reports with a browser.
933 If you are targeting TaskJuggler 2.4 (see
934 `org-taskjuggler-target-version') the processing and display of
935 the reports is done using the TaskJuggler GUI."
936 (interactive)
937 (if (< org-taskjuggler-target-version 3.0)
938 (let* ((process-name "TaskJugglerUI")
939 (command
940 (concat process-name " "
941 (org-taskjuggler-export nil subtreep visible-only))))
942 (start-process-shell-command process-name nil command))
943 (dolist (report (org-taskjuggler-export-and-process subtreep visible-only))
944 (org-open-file report))))
946 (defun org-taskjuggler-compile (file)
947 "Compile a TaskJuggler file.
949 FILE is the name of the file being compiled. Processing is done
950 through the command given in `org-taskjuggler-process-command'.
952 Return a list of reports."
953 (let* ((full-name (file-truename file))
954 (out-dir
955 (expand-file-name
956 org-taskjuggler-reports-directory (file-name-directory file)))
957 errors)
958 (message (format "Processing TaskJuggler file %s..." file))
959 (save-window-excursion
960 (let ((outbuf (get-buffer-create "*Org Taskjuggler Output*")))
961 (unless (file-directory-p out-dir)
962 (make-directory out-dir t))
963 (with-current-buffer outbuf (erase-buffer))
964 (shell-command
965 (replace-regexp-in-string
966 "%f" (shell-quote-argument full-name)
967 (replace-regexp-in-string
968 "%o" (shell-quote-argument out-dir)
969 org-taskjuggler-process-command t t) t t) outbuf)
970 ;; Collect standard errors from output buffer.
971 (setq errors (org-taskjuggler--collect-errors outbuf)))
972 (if (not errors)
973 (message "Process completed.")
974 (error (format "TaskJuggler failed with errors: %s" errors))))
975 (file-expand-wildcards (format "%s/*.html" out-dir))))
977 (defun org-taskjuggler--collect-errors (buffer)
978 "Collect some kind of errors from \"tj3\" command output.
980 BUFFER is the buffer containing output.
982 Return collected error types as a string, or nil if there was
983 none."
984 (with-current-buffer buffer
985 (save-excursion
986 (goto-char (point-min))
987 (let ((case-fold-search t)
988 (errors ""))
989 (while (re-search-forward "^.+:[0-9]+: \\(.*\\)$" nil t)
990 (setq errors (concat errors " " (match-string 1))))
991 (and (org-string-nw-p errors) (org-trim errors))))))
994 (provide 'ox-taskjuggler)
996 ;; Local variables:
997 ;; sentence-end-double-space: t
998 ;; End:
1000 ;;; ox-taskjuggler.el ends here