ox-taskjuggler.el: Add a note about multiline properties
[org-mode/org-tableheadings.git] / contrib / lisp / ox-taskjuggler.el
blobd4ff7c4a7448a3b976f0de8564d12d73700c1fdd
1 ;;; ox-taskjuggler.el --- TaskJuggler Back-End for Org Export Engine
2 ;;
3 ;; Copyright (C) 2007-2014 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.devalot.com/assets/articles/2008/07/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 '== %title =='
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. \"%title\"
250 anywhere in the reports will be replaced with the document title.
251 If you want to define your own reports you can change them here
252 or simply define the default reports so that they include an
253 external report definition as follows:
255 include reports.tji
257 These default are made to work with tj3. If you are targeting
258 TaskJuggler 2.4 (see `org-taskjuggler-target-version') please
259 change these defaults to something like the following:
261 taskreport \"Gantt Chart\" {
262 headline \"Project Gantt Chart\"
263 columns hierarchindex, name, start, end, effort, duration, completed, chart
264 timeformat \"%Y-%m-%d\"
265 hideresource 1
266 loadunit shortauto
269 resourcereport \"Resource Graph\" {
270 headline \"Resource Allocation Graph\"
271 columns no, name, utilization, freeload, chart
272 loadunit shortauto
273 sorttasks startup
274 hidetask ~isleaf()
276 :group 'org-export-taskjuggler
277 :type '(repeat (string :tag "Report")))
279 (defcustom org-taskjuggler-default-global-header ""
280 "Default global header for the project.
281 This goes before project declaration, and might be useful for
282 early macros."
283 :group 'org-export-taskjuggler
284 :type '(string :tag "Preamble"))
286 (defcustom org-taskjuggler-default-global-properties
287 "shift s40 \"Part time shift\" {
288 workinghours wed, thu, fri off
291 "Default global properties for the project.
293 Here you typically define global properties such as shifts,
294 accounts, rates, vacation, macros and flags. Any property that
295 is allowed within the TaskJuggler file can be inserted. You
296 could for example include another TaskJuggler file.
298 The global properties are inserted after the project declaration
299 but before any resource and task declarations."
300 :group 'org-export-taskjuggler
301 :type '(string :tag "Preamble"))
303 (defcustom org-taskjuggler-valid-task-attributes
304 '(account start note duration endbuffer endcredit end
305 flags journalentry length limits maxend maxstart minend
306 minstart period reference responsible scheduling
307 startbuffer startcredit statusnote chargeset charge)
308 "Valid attributes for Taskjuggler tasks.
309 If one of these appears as a property for a headline, it will be
310 exported with the corresponding task.
312 Note that multiline properties are not supported, so attributes
313 like note or journalentry have to be on a single line."
314 :group 'org-export-taskjuggler)
316 (defcustom org-taskjuggler-valid-resource-attributes
317 '(limits vacation shift booking efficiency journalentry rate
318 workinghours flags)
319 "Valid attributes for Taskjuggler resources.
320 If one of these appears as a property for a headline, it will be
321 exported with the corresponding resource."
322 :group 'org-export-taskjuggler)
324 (defcustom org-taskjuggler-valid-report-attributes
325 '(headline columns definitions timeformat hideresource hidetask
326 loadunit sorttasks formats period)
327 "Valid attributes for Taskjuggler reports.
328 If one of these appears as a property for a headline, it will be
329 exported with the corresponding report."
330 :group 'org-export-taskjuggler)
332 (defcustom org-taskjuggler-process-command
333 "tj3 --silent --no-color --output-dir %o %f"
334 "Command to process a Taskjuggler file.
335 The command will be given to the shell as a command to process a
336 Taskjuggler file. \"%f\" in the command will be replaced by the
337 full file name, \"%o\" by the reports directory (see
338 `org-taskjuggler-reports-directory').
340 If you are targeting Taskjuggler 2.4 (see
341 `org-taskjuggler-target-version') this setting is ignored."
342 :group 'org-export-taskjuggler)
344 (defcustom org-taskjuggler-reports-directory "reports"
345 "Default directory to generate the Taskjuggler reports in.
346 The command `org-taskjuggler-process-command' generates the
347 reports and associated files such as CSS inside this directory.
349 If the directory is not an absolute path it is relative to the
350 directory of the exported file. The directory is created if it
351 doesn't exist.
353 If you are targeting Taskjuggler 2.4 (see
354 `org-taskjuggler-target-version') this setting is ignored."
355 :group 'org-export-taskjuggler)
357 (defcustom org-taskjuggler-keep-project-as-task t
358 "Non-nil keeps the project headline as an umbrella task for all tasks.
359 Setting this to nil will allow maintaining completely separated
360 task buckets, while still sharing the same resources pool."
361 :group 'org-export-taskjuggler
362 :type 'boolean)
366 ;;; Hooks
368 (defvar org-taskjuggler-final-hook nil
369 "Hook run after a TaskJuggler files has been saved.
370 This hook is run with the name of the file as argument.")
374 ;;; Back-End Definition
376 (org-export-define-backend 'taskjuggler
377 '((template . org-taskjuggler-project-plan))
378 :menu-entry
379 '(?J "Export to TaskJuggler"
380 ((?j "As TJP file" (lambda (a s v b) (org-taskjuggler-export a s v)))
381 (?p "As TJP file and process"
382 (lambda (a s v b)
383 (if a (org-taskjuggler-export a s v)
384 (org-taskjuggler-export-and-process s v))))
385 (?o "As TJP file, process and open"
386 (lambda (a s v b)
387 (if a (org-taskjuggler-export a s v)
388 (org-taskjuggler-export-process-and-open s v))))))
389 ;; This property will be used to store unique ids in communication
390 ;; channel. Ids will be retrieved with `org-taskjuggler-get-id'.
391 :options-alist '((:taskjuggler-unique-ids nil nil nil)))
395 ;;; Unique IDs
397 (defun org-taskjuggler-assign-task-ids (tasks info)
398 "Assign a unique ID to each task in TASKS.
399 TASKS is a list of headlines. INFO is a plist used as a
400 communication channel. Return value is an alist between
401 headlines and their associated ID. IDs are hierarchical, which
402 means they only need to be unique among the task siblings."
403 (let* (alist
404 build-id ; For byte-compiler.
405 (build-id
406 (lambda (tasks local-ids)
407 (org-element-map tasks 'headline
408 (lambda (task)
409 (let ((id (org-taskjuggler--build-unique-id task local-ids)))
410 (push id local-ids)
411 (push (cons task id) alist)
412 (funcall build-id (org-element-contents task) nil)))
413 info nil 'headline))))
414 (funcall build-id tasks nil)
415 alist))
417 (defun org-taskjuggler-assign-resource-ids (resources info)
418 "Assign a unique ID to each resource within RESOURCES.
419 RESOURCES is a list of headlines. INFO is a plist used as a
420 communication channel. Return value is an alist between
421 headlines and their associated ID."
422 (let (ids)
423 (org-element-map resources 'headline
424 (lambda (resource)
425 (let ((id (org-taskjuggler--build-unique-id resource ids)))
426 (push id ids)
427 (cons resource id)))
428 info)))
432 ;;; Accessors
434 (defun org-taskjuggler-get-project (info)
435 "Return project in parse tree.
436 INFO is a plist used as a communication channel. First headline
437 in buffer with `org-taskjuggler-project-tag' defines the project.
438 If no such task is defined, pick the first headline in buffer.
439 If there is no headline at all, return nil."
440 (let ((tree (plist-get info :parse-tree)))
441 (or (org-element-map tree 'headline
442 (lambda (hl)
443 (and (member org-taskjuggler-project-tag
444 (org-export-get-tags hl info))
445 hl))
446 info t)
447 (org-element-map tree 'headline 'identity info t))))
449 (defun org-taskjuggler-get-id (item info)
450 "Return id for task or resource ITEM.
451 ITEM is a headline. INFO is a plist used as a communication
452 channel. Return value is a string."
453 (cdr (assq item (plist-get info :taskjuggler-unique-ids))))
455 (defun org-taskjuggler-get-name (item)
456 "Return name for task or resource ITEM.
457 ITEM is a headline. Return value is a string."
458 ;; Quote double quotes in name.
459 (replace-regexp-in-string
460 "\"" "\\\"" (org-element-property :raw-value item) t t))
462 (defun org-taskjuggler-get-start (item)
463 "Return start date for task or resource ITEM.
464 ITEM is a headline. Return value is a string or nil if ITEM
465 doesn't have any start date defined."
466 (let ((scheduled (org-element-property :scheduled item)))
468 (and scheduled (org-timestamp-format scheduled "%Y-%02m-%02d"))
469 (and (memq 'start org-taskjuggler-valid-task-attributes)
470 (org-element-property :START item)))))
472 (defun org-taskjuggler-get-end (item)
473 "Return end date for task or resource ITEM.
474 ITEM is a headline. Return value is a string or nil if ITEM
475 doesn't have any end date defined."
476 (let ((deadline (org-element-property :deadline item)))
477 (and deadline (org-timestamp-format deadline "%Y-%02m-%02d"))))
481 ;;; Internal Functions
483 (defun org-taskjuggler--indent-string (s)
484 "Indent string S by 2 spaces.
485 Return new string. If S is the empty string, return it."
486 (if (equal "" s) s (replace-regexp-in-string "^ *\\S-" " \\&" s)))
488 (defun org-taskjuggler--build-attributes (item attributes)
489 "Return attributes string for task, resource or report ITEM.
490 ITEM is a headline. ATTRIBUTES is a list of symbols
491 representing valid attributes for ITEM."
492 (mapconcat
493 (lambda (attribute)
494 (let ((value (org-element-property
495 (intern (upcase (format ":%s" attribute)))
496 item)))
497 (and value (format "%s %s\n" attribute value))))
498 (remq nil attributes) ""))
500 (defun org-taskjuggler--build-unique-id (item unique-ids)
501 "Return a unique id for a given task or a resource.
502 ITEM is an `headline' type element representing the task or
503 resource. Its id is derived from its name and made unique
504 against UNIQUE-IDS. If the (downcased) first token of the
505 headline is not unique try to add more (downcased) tokens of the
506 headline or finally add more underscore characters (\"_\")."
507 (let ((id (org-string-nw-p (org-element-property :TASK_ID item))))
508 ;; If an id is specified, use it, as long as it's unique.
509 (if (and id (not (member id unique-ids))) id
510 (let* ((parts (org-split-string (org-element-property :raw-value item)))
511 (id (org-taskjuggler--clean-id (downcase (pop parts)))))
512 ;; Try to add more parts of the headline to make it unique.
513 (while (and (car parts) (member id unique-ids))
514 (setq id (concat id "_"
515 (org-taskjuggler--clean-id (downcase (pop parts))))))
516 ;; If it's still not unique, add "_".
517 (while (member id unique-ids)
518 (setq id (concat id "_")))
519 id))))
521 (defun org-taskjuggler--clean-id (id)
522 "Clean and return ID to make it acceptable for TaskJuggler.
523 ID is a string."
524 ;; Replace non-ascii by "_".
525 (replace-regexp-in-string
526 "[^a-zA-Z0-9_]" "_"
527 ;; Make sure id doesn't start with a number.
528 (replace-regexp-in-string "^\\([0-9]\\)" "_\\1" id)))
532 ;;; Dependencies
534 (defun org-taskjuggler-resolve-dependencies (task info)
535 "Return a list of all tasks TASK depends on.
536 TASK is a headline. INFO is a plist used as a communication
537 channel."
538 (let ((deps-ids
539 ;; Get all dependencies specified in BLOCKER and DEPENDS task
540 ;; properties. Clean options from them.
541 (let ((deps (concat (org-element-property :BLOCKER task)
542 (org-element-property :DEPENDS task))))
543 (and deps
544 (org-split-string (replace-regexp-in-string "{.*?}" "" deps)
545 "[ ,]* +"))))
546 depends)
547 (when deps-ids
548 ;; Find tasks with :task_id: property matching id in DEPS-IDS.
549 ;; Add them to DEPENDS.
550 (let* ((project (org-taskjuggler-get-project info))
551 (tasks (if org-taskjuggler-keep-project-as-task project
552 (org-element-contents project))))
553 (setq depends
554 (org-element-map tasks 'headline
555 (lambda (task)
556 (let ((task-id (or (org-element-property :TASK_ID task)
557 (org-element-property :ID task))))
558 (and task-id (member task-id deps-ids) task)))
559 info)))
560 ;; Check BLOCKER and DEPENDS properties. If "previous-sibling"
561 ;; belongs to DEPS-ID, add it to DEPENDS.
562 (when (and (member-ignore-case "previous-sibling" deps-ids)
563 (not (org-export-first-sibling-p task info)))
564 (let ((prev (org-export-get-previous-element task info)))
565 (and (not (memq prev depends)) (push prev depends)))))
566 ;; Check ORDERED status of parent.
567 (let ((parent (org-export-get-parent task)))
568 (when (and parent
569 (org-element-property :ORDERED parent)
570 (not (org-export-first-sibling-p task info)))
571 (push (org-export-get-previous-element task info) depends)))
572 ;; Return dependencies.
573 depends))
575 (defun org-taskjuggler-format-dependencies (dependencies task info)
576 "Format DEPENDENCIES to match TaskJuggler syntax.
577 DEPENDENCIES is list of dependencies for TASK, as returned by
578 `org-taskjuggler-resolve-depedencies'. TASK is a headline.
579 INFO is a plist used as a communication channel. Return value
580 doesn't include leading \"depends\"."
581 (let* ((dep-str (concat (org-element-property :BLOCKER task)
583 (org-element-property :DEPENDS task)))
584 (get-path
585 (lambda (dep)
586 ;; Return path to DEP relatively to TASK.
587 (let ((parent (org-export-get-parent task))
588 (exclamations 1)
589 (option
590 (let ((id (org-element-property :TASK_ID dep)))
591 (and id
592 (string-match (concat id " +\\({.*?}\\)") dep-str)
593 (org-match-string-no-properties 1 dep-str))))
594 path)
595 ;; Compute number of exclamation marks by looking for the
596 ;; common ancestor between TASK and DEP.
597 (while (not (org-element-map parent 'headline
598 (lambda (hl) (eq hl dep))))
599 (incf exclamations)
600 (setq parent (org-export-get-parent parent)))
601 ;; Build path from DEP to PARENT.
602 (while (not (eq parent dep))
603 (push (org-taskjuggler-get-id dep info) path)
604 (setq dep (org-export-get-parent dep)))
605 ;; Return full path. Add dependency options, if any.
606 (concat (make-string exclamations ?!)
607 (mapconcat 'identity path ".")
608 (and option (concat " " option)))))))
609 ;; Return dependencies string, without the leading "depends".
610 (mapconcat (lambda (dep) (funcall get-path dep)) dependencies ", ")))
614 ;;; Translator Functions
616 (defun org-taskjuggler-project-plan (contents info)
617 "Build TaskJuggler project plan.
618 CONTENTS is ignored. INFO is a plist holding export options.
619 Return complete project plan as a string in TaskJuggler syntax."
620 (let* ((tree (plist-get info :parse-tree))
621 (project (or (org-taskjuggler-get-project info)
622 (error "No project specified"))))
623 (concat
624 ;; 1. Insert header.
625 (org-element-normalize-string org-taskjuggler-default-global-header)
626 ;; 2. Insert project.
627 (org-taskjuggler--build-project project info)
628 ;; 3. Insert global properties.
629 (org-element-normalize-string org-taskjuggler-default-global-properties)
630 ;; 4. Insert resources. Provide a default one if none is
631 ;; specified.
632 (let ((main-resources
633 ;; Collect contents from various trees marked with
634 ;; `org-taskjuggler-resource-tag'. Only gather top level
635 ;; resources.
636 (apply 'append
637 (org-element-map tree 'headline
638 (lambda (hl)
639 (and (member org-taskjuggler-resource-tag
640 (org-export-get-tags hl info))
641 (org-element-map (org-element-contents hl) 'headline
642 'identity info nil 'headline)))
643 info nil 'headline))))
644 ;; Assign a unique ID to each resource. Store it under
645 ;; `:taskjuggler-unique-ids' property in INFO.
646 (setq info
647 (plist-put info :taskjuggler-unique-ids
648 (org-taskjuggler-assign-resource-ids
649 main-resources info)))
650 (concat
651 (if main-resources
652 (mapconcat
653 (lambda (resource) (org-taskjuggler--build-resource resource info))
654 main-resources "")
655 (format "resource %s \"%s\" {\n}\n" (user-login-name) user-full-name))
656 ;; 5. Insert tasks.
657 (let ((main-tasks
658 ;; If `org-taskjuggler-keep-project-as-task' is
659 ;; non-nil, there is only one task. Otherwise, every
660 ;; direct children of PROJECT is a top level task.
661 (if org-taskjuggler-keep-project-as-task (list project)
662 (or (org-element-map (org-element-contents project) 'headline
663 'identity info nil 'headline)
664 (error "No task specified")))))
665 ;; Assign a unique ID to each task. Add it to
666 ;; `:taskjuggler-unique-ids' property in INFO.
667 (setq info
668 (plist-put info :taskjuggler-unique-ids
669 (append
670 (org-taskjuggler-assign-task-ids main-tasks info)
671 (plist-get info :taskjuggler-unique-ids))))
672 ;; If no resource is allocated among tasks, allocate one to
673 ;; the first task.
674 (unless (org-element-map main-tasks 'headline
675 (lambda (task) (org-element-property :ALLOCATE task))
676 info t)
677 (org-element-put-property
678 (car main-tasks) :ALLOCATE
679 (or (org-taskjuggler-get-id (car main-resources) info)
680 (user-login-name))))
681 (mapconcat
682 (lambda (task) (org-taskjuggler--build-task task info))
683 main-tasks ""))
684 ;; 6. Insert reports. If no report is defined, insert default
685 ;; reports.
686 (let ((main-reports
687 ;; Collect contents from various trees marked with
688 ;; `org-taskjuggler-report-tag'. Only gather top level
689 ;; reports.
690 (apply 'append
691 (org-element-map tree 'headline
692 (lambda (hl)
693 (and (member org-taskjuggler-report-tag
694 (org-export-get-tags hl info))
695 (org-element-map (org-element-contents hl)
696 'headline 'identity info nil 'headline)))
697 info nil 'headline))))
698 (if main-reports
699 (mapconcat
700 (lambda (report) (org-taskjuggler--build-report report info))
701 main-reports "")
702 ;; insert title in default reports
703 (let* ((title (org-export-data (plist-get info :title) info))
704 (report-title (if (string= title "")
705 (org-taskjuggler-get-name project)
706 title)))
707 (mapconcat
708 'org-element-normalize-string
709 (mapcar
710 (function
711 (lambda (report)
712 (replace-regexp-in-string "%title" report-title report t t)))
713 org-taskjuggler-default-reports) "")))))))))
715 (defun org-taskjuggler--build-project (project info)
716 "Return a project declaration.
717 PROJECT is a headline. INFO is a plist used as a communication
718 channel. If no start date is specified, start today. If no end
719 date is specified, end `org-taskjuggler-default-project-duration'
720 days from now."
721 (format "project %s \"%s\" \"%s\" %s %s {\n}\n"
722 (org-taskjuggler-get-id project info)
723 (org-taskjuggler-get-name project)
724 ;; Version is obtained through :TASKJUGGLER_VERSION:
725 ;; property or `org-taskjuggler-default-project-version'.
726 (or (org-element-property :VERSION project)
727 org-taskjuggler-default-project-version)
728 (or (org-taskjuggler-get-start project)
729 (format-time-string "%Y-%m-%d"))
730 (let ((end (org-taskjuggler-get-end project)))
731 (or (and end (format "- %s" end))
732 (format "+%sd" org-taskjuggler-default-project-duration)))))
734 (defun org-taskjuggler--build-resource (resource info)
735 "Return a resource declaration.
737 RESOURCE is a headline. INFO is a plist used as a communication
738 channel.
740 All valid attributes from RESOURCE are inserted. If RESOURCE
741 defines a property \"resource_id\" it will be used as the id for
742 this resource. Otherwise it will use the ID property. If
743 neither is defined a unique id will be associated to it."
744 (concat
745 ;; Opening resource.
746 (format "resource %s \"%s\" {\n"
747 (org-taskjuggler--clean-id
748 (or (org-element-property :RESOURCE_ID resource)
749 (org-element-property :ID resource)
750 (org-taskjuggler-get-id resource info)))
751 (org-taskjuggler-get-name resource))
752 ;; Add attributes.
753 (org-taskjuggler--indent-string
754 (org-taskjuggler--build-attributes
755 resource org-taskjuggler-valid-resource-attributes))
756 ;; Add inner resources.
757 (org-taskjuggler--indent-string
758 (mapconcat
759 'identity
760 (org-element-map (org-element-contents resource) 'headline
761 (lambda (hl) (org-taskjuggler--build-resource hl info))
762 info nil 'headline)
763 ""))
764 ;; Closing resource.
765 "}\n"))
767 (defun org-taskjuggler--build-report (report info)
768 "Return a report declaration.
769 REPORT is a headline. INFO is a plist used as a communication
770 channel."
771 (concat
772 ;; Opening report.
773 (format "%s \"%s\" {\n"
774 (or (org-element-property :REPORT_KIND report) "taskreport")
775 (org-taskjuggler-get-name report))
776 ;; Add attributes.
777 (org-taskjuggler--indent-string
778 (org-taskjuggler--build-attributes
779 report org-taskjuggler-valid-report-attributes))
780 ;; Add inner reports.
781 (org-taskjuggler--indent-string
782 (mapconcat
783 'identity
784 (org-element-map (org-element-contents report) 'headline
785 (lambda (hl) (org-taskjuggler--build-report hl info))
786 info nil 'headline)
787 ""))
788 ;; Closing report.
789 "}\n"))
791 (defun org-taskjuggler--build-task (task info)
792 "Return a task declaration.
794 TASK is a headline. INFO is a plist used as a communication
795 channel.
797 All valid attributes from TASK are inserted. If TASK defines
798 a property \"task_id\" it will be used as the id for this task.
799 Otherwise it will use the ID property. If neither is defined
800 a unique id will be associated to it."
801 (let* ((allocate (org-element-property :ALLOCATE task))
802 (complete
803 (if (eq (org-element-property :todo-type task) 'done) "100"
804 (org-element-property :COMPLETE task)))
805 (depends (org-taskjuggler-resolve-dependencies task info))
806 (effort (let ((property
807 (intern (concat ":" (upcase org-effort-property)))))
808 (org-element-property property task)))
809 (milestone
810 (or (org-element-property :MILESTONE task)
811 (not (or (org-element-map (org-element-contents task) 'headline
812 'identity info t) ; Has task any child?
813 effort
814 (org-element-property :LENGTH task)
815 (org-element-property :DURATION task)
816 (and (org-taskjuggler-get-start task)
817 (org-taskjuggler-get-end task))
818 (org-element-property :PERIOD task)))))
819 (priority
820 (let ((pri (org-element-property :priority task)))
821 (and pri
822 (max 1 (/ (* 1000 (- org-lowest-priority pri))
823 (- org-lowest-priority org-highest-priority)))))))
824 (concat
825 ;; Opening task.
826 (format "task %s \"%s\" {\n"
827 (org-taskjuggler-get-id task info)
828 (org-taskjuggler-get-name task))
829 ;; Add default attributes.
830 (and depends
831 (format " depends %s\n"
832 (org-taskjuggler-format-dependencies depends task info)))
833 (and allocate
834 (format " purge %s\n allocate %s\n"
835 ;; Compatibility for previous TaskJuggler versions.
836 (if (>= org-taskjuggler-target-version 3.0) "allocate"
837 "allocations")
838 allocate))
839 (and complete (format " complete %s\n" complete))
840 (and effort
841 (format " effort %s\n"
842 (let* ((minutes (org-duration-string-to-minutes effort))
843 (hours (/ minutes 60.0)))
844 (format "%.1fh" hours))))
845 (and priority (format " priority %s\n" priority))
846 (and milestone " milestone\n")
847 ;; Add other valid attributes.
848 (org-taskjuggler--indent-string
849 (org-taskjuggler--build-attributes
850 task org-taskjuggler-valid-task-attributes))
851 ;; Add inner tasks.
852 (org-taskjuggler--indent-string
853 (mapconcat 'identity
854 (org-element-map (org-element-contents task) 'headline
855 (lambda (hl) (org-taskjuggler--build-task hl info))
856 info nil 'headline)
857 ""))
858 ;; Closing task.
859 "}\n")))
863 ;;; Interactive Functions
865 ;;;###autoload
866 (defun org-taskjuggler-export (&optional async subtreep visible-only)
867 "Export current buffer to a TaskJuggler file.
869 The exporter looks for a tree with tag that matches
870 `org-taskjuggler-project-tag' and takes this as the tasks for
871 this project. The first node of this tree defines the project
872 properties such as project name and project period.
874 If there is a tree with tag that matches
875 `org-taskjuggler-resource-tag' this tree is taken as resources
876 for the project. If no resources are specified, a default
877 resource is created and allocated to the project.
879 Also the TaskJuggler project will be created with default reports
880 as defined in `org-taskjuggler-default-reports'.
882 If narrowing is active in the current buffer, only export its
883 narrowed part.
885 If a region is active, export that region.
887 A non-nil optional argument ASYNC means the process should happen
888 asynchronously. The resulting file should be accessible through
889 the `org-export-stack' interface.
891 When optional argument SUBTREEP is non-nil, export the sub-tree
892 at point, extracting information from the headline properties
893 first.
895 When optional argument VISIBLE-ONLY is non-nil, don't export
896 contents of hidden elements.
898 Return output file's name."
899 (interactive)
900 (let ((outfile
901 (org-export-output-file-name org-taskjuggler-extension subtreep)))
902 (org-export-to-file 'taskjuggler outfile
903 async subtreep visible-only nil nil
904 (lambda (file)
905 (run-hook-with-args 'org-taskjuggler-final-hook file) nil))))
907 ;;;###autoload
908 (defun org-taskjuggler-export-and-process (&optional subtreep visible-only)
909 "Export current buffer to a TaskJuggler file and process it.
911 The exporter looks for a tree with tag that matches
912 `org-taskjuggler-project-tag' and takes this as the tasks for
913 this project. The first node of this tree defines the project
914 properties such as project name and project period.
916 If there is a tree with tag that matches
917 `org-taskjuggler-resource-tag' this tree is taken as resources
918 for the project. If no resources are specified, a default
919 resource is created and allocated to the project.
921 Also the TaskJuggler project will be created with default reports
922 as defined in `org-taskjuggler-default-reports'.
924 If narrowing is active in the current buffer, only export its
925 narrowed part.
927 If a region is active, export that region.
929 When optional argument SUBTREEP is non-nil, export the sub-tree
930 at point, extracting information from the headline properties
931 first.
933 When optional argument VISIBLE-ONLY is non-nil, don't export
934 contents of hidden elements.
936 Return a list of reports."
937 (interactive)
938 (let ((file (org-taskjuggler-export nil subtreep visible-only)))
939 (org-taskjuggler-compile file)))
941 ;;;###autoload
942 (defun org-taskjuggler-export-process-and-open (&optional subtreep visible-only)
943 "Export current buffer to a TaskJuggler file, process and open it.
945 Export and process the file using
946 `org-taskjuggler-export-and-process' and open the generated
947 reports with a browser.
949 If you are targeting TaskJuggler 2.4 (see
950 `org-taskjuggler-target-version') the processing and display of
951 the reports is done using the TaskJuggler GUI."
952 (interactive)
953 (if (< org-taskjuggler-target-version 3.0)
954 (let* ((process-name "TaskJugglerUI")
955 (command
956 (concat process-name " "
957 (org-taskjuggler-export nil subtreep visible-only))))
958 (start-process-shell-command process-name nil command))
959 (dolist (report (org-taskjuggler-export-and-process subtreep visible-only))
960 (org-open-file report))))
962 (defun org-taskjuggler-compile (file)
963 "Compile a TaskJuggler file.
965 FILE is the name of the file being compiled. Processing is done
966 through the command given in `org-taskjuggler-process-command'.
968 Return a list of reports."
969 (let* ((full-name (file-truename file))
970 (out-dir
971 (expand-file-name
972 org-taskjuggler-reports-directory (file-name-directory file)))
973 errors)
974 (message (format "Processing TaskJuggler file %s..." file))
975 (save-window-excursion
976 (let ((outbuf (get-buffer-create "*Org Taskjuggler Output*")))
977 (unless (file-directory-p out-dir)
978 (make-directory out-dir t))
979 (with-current-buffer outbuf (erase-buffer))
980 (shell-command
981 (replace-regexp-in-string
982 "%f" (shell-quote-argument full-name)
983 (replace-regexp-in-string
984 "%o" (shell-quote-argument out-dir)
985 org-taskjuggler-process-command t t) t t) outbuf)
986 ;; Collect standard errors from output buffer.
987 (setq errors (org-taskjuggler--collect-errors outbuf)))
988 (if (not errors)
989 (message "Process completed.")
990 (error (format "TaskJuggler failed with errors: %s" errors))))
991 (file-expand-wildcards (format "%s/*.html" out-dir))))
993 (defun org-taskjuggler--collect-errors (buffer)
994 "Collect some kind of errors from \"tj3\" command output.
996 BUFFER is the buffer containing output.
998 Return collected error types as a string, or nil if there was
999 none."
1000 (with-current-buffer buffer
1001 (save-excursion
1002 (goto-char (point-min))
1003 (let ((case-fold-search t)
1004 (errors ""))
1005 (while (re-search-forward "^.+:[0-9]+: \\(.*\\)$" nil t)
1006 (setq errors (concat errors " " (match-string 1))))
1007 (and (org-string-nw-p errors) (org-trim errors))))))
1010 (provide 'ox-taskjuggler)
1012 ;; Local variables:
1013 ;; sentence-end-double-space: t
1014 ;; End:
1016 ;;; ox-taskjuggler.el ends here