Merge branch 'maint'
[org-mode.git] / contrib / lisp / ox-taskjuggler.el
blob5dd9b22025e73113fbabaf8008676a7402b1dbf9
1 ;;; ox-taskjuggler.el --- TaskJuggler Back-End for Org Export Engine
2 ;;
3 ;; Copyright (C) 2007-2017 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-project-attributes
317 '(timingresolution timezone alertlevels currency currencyformat
318 dailyworkinghours extend includejournalentry now numberformat
319 outputdir scenario shorttimeformat timeformat trackingscenario
320 weekstartsmonday weekstartssunday workinghours
321 yearlyworkingdays)
322 "Valid attributes for Taskjuggler project.
323 If one of these appears as a property for a headline that is a
324 project definition, it will be exported with the corresponding
325 task. Attribute 'timingresolution' should be the first in the
326 list."
327 :group 'org-export-taskjuggler)
329 (defcustom org-taskjuggler-valid-resource-attributes
330 '(limits vacation shift booking efficiency journalentry rate
331 workinghours flags)
332 "Valid attributes for Taskjuggler resources.
333 If one of these appears as a property for a headline, it will be
334 exported with the corresponding resource."
335 :group 'org-export-taskjuggler)
337 (defcustom org-taskjuggler-valid-report-attributes
338 '(headline columns definitions timeformat hideresource hidetask
339 loadunit sorttasks formats period)
340 "Valid attributes for Taskjuggler reports.
341 If one of these appears as a property for a headline, it will be
342 exported with the corresponding report."
343 :group 'org-export-taskjuggler)
345 (defcustom org-taskjuggler-process-command
346 "tj3 --silent --no-color --output-dir %o %f"
347 "Command to process a Taskjuggler file.
348 The command will be given to the shell as a command to process a
349 Taskjuggler file. \"%f\" in the command will be replaced by the
350 full file name, \"%o\" by the reports directory (see
351 `org-taskjuggler-reports-directory').
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-reports-directory "reports"
358 "Default directory to generate the Taskjuggler reports in.
359 The command `org-taskjuggler-process-command' generates the
360 reports and associated files such as CSS inside this directory.
362 If the directory is not an absolute path it is relative to the
363 directory of the exported file. The directory is created if it
364 doesn't exist.
366 If you are targeting Taskjuggler 2.4 (see
367 `org-taskjuggler-target-version') this setting is ignored."
368 :group 'org-export-taskjuggler)
370 (defcustom org-taskjuggler-keep-project-as-task t
371 "Non-nil keeps the project headline as an umbrella task for all tasks.
372 Setting this to nil will allow maintaining completely separated
373 task buckets, while still sharing the same resources pool."
374 :group 'org-export-taskjuggler
375 :type 'boolean)
379 ;;; Hooks
381 (defvar org-taskjuggler-final-hook nil
382 "Hook run after a TaskJuggler files has been saved.
383 This hook is run with the name of the file as argument.")
387 ;;; Back-End Definition
389 (org-export-define-backend 'taskjuggler
390 '((template . org-taskjuggler-project-plan))
391 :menu-entry
392 '(?J "Export to TaskJuggler"
393 ((?j "As TJP file" (lambda (a s v b) (org-taskjuggler-export a s v)))
394 (?p "As TJP file and process"
395 (lambda (a s v b)
396 (if a (org-taskjuggler-export a s v)
397 (org-taskjuggler-export-and-process s v))))
398 (?o "As TJP file, process and open"
399 (lambda (a s v b)
400 (if a (org-taskjuggler-export a s v)
401 (org-taskjuggler-export-process-and-open s v))))))
402 ;; This property will be used to store unique ids in communication
403 ;; channel. Ids will be retrieved with `org-taskjuggler-get-id'.
404 :options-alist '((:taskjuggler-unique-ids nil nil nil)))
408 ;;; Unique IDs
410 (defun org-taskjuggler-assign-task-ids (tasks info)
411 "Assign a unique ID to each task in TASKS.
412 TASKS is a list of headlines. INFO is a plist used as a
413 communication channel. Return value is an alist between
414 headlines and their associated ID. IDs are hierarchical, which
415 means they only need to be unique among the task siblings."
416 (let* (alist
417 build-id ; For byte-compiler.
418 (build-id
419 (lambda (tasks local-ids)
420 (org-element-map tasks 'headline
421 (lambda (task)
422 (let ((id (org-taskjuggler--build-unique-id task local-ids)))
423 (push id local-ids)
424 (push (cons task id) alist)
425 (funcall build-id (org-element-contents task) nil)))
426 info nil 'headline))))
427 (funcall build-id tasks nil)
428 alist))
430 (defun org-taskjuggler-assign-resource-ids (resources info)
431 "Assign a unique ID to each resource within RESOURCES.
432 RESOURCES is a list of headlines. INFO is a plist used as a
433 communication channel. Return value is an alist between
434 headlines and their associated ID."
435 (let (ids)
436 (org-element-map resources 'headline
437 (lambda (resource)
438 (let ((id (org-taskjuggler--build-unique-id resource ids)))
439 (push id ids)
440 (cons resource id)))
441 info)))
445 ;;; Accessors
447 (defun org-taskjuggler-get-project (info)
448 "Return project in parse tree.
449 INFO is a plist used as a communication channel. First headline
450 in buffer with `org-taskjuggler-project-tag' defines the project.
451 If no such task is defined, pick the first headline in buffer.
452 If there is no headline at all, return nil."
453 (let ((tree (plist-get info :parse-tree)))
454 (or (org-element-map tree 'headline
455 (lambda (hl)
456 (and (member org-taskjuggler-project-tag
457 (org-export-get-tags hl info))
458 hl))
459 info t)
460 (org-element-map tree 'headline 'identity info t))))
462 (defun org-taskjuggler-get-id (item info)
463 "Return id for task or resource ITEM.
464 ITEM is a headline. INFO is a plist used as a communication
465 channel. Return value is a string."
466 (cdr (assq item (plist-get info :taskjuggler-unique-ids))))
468 (defun org-taskjuggler-get-name (item)
469 "Return name for task or resource ITEM.
470 ITEM is a headline. Return value is a string."
471 ;; Quote double quotes in name.
472 (replace-regexp-in-string
473 "\"" "\\\"" (org-element-property :raw-value item) t t))
475 (defun org-taskjuggler-get-start (item)
476 "Return start date for task or resource ITEM.
477 ITEM is a headline. Return value is a string or nil if ITEM
478 doesn't have any start date defined."
479 (let ((scheduled (org-element-property :scheduled item)))
481 (and scheduled (org-timestamp-format scheduled "%Y-%02m-%02d"))
482 (and (memq 'start org-taskjuggler-valid-task-attributes)
483 (org-element-property :START item)))))
485 (defun org-taskjuggler-get-end (item)
486 "Return end date for task or resource ITEM.
487 ITEM is a headline. Return value is a string or nil if ITEM
488 doesn't have any end date defined."
489 (let ((deadline (org-element-property :deadline item)))
490 (and deadline (org-timestamp-format deadline "%Y-%02m-%02d"))))
494 ;;; Internal Functions
496 (defun org-taskjuggler--indent-string (s)
497 "Indent string S by 2 spaces.
498 Return new string. If S is the empty string, return it."
499 (if (equal "" s) s (replace-regexp-in-string "^ *\\S-" " \\&" s)))
501 (defun org-taskjuggler--build-attributes (item attributes)
502 "Return attributes string for ITEM.
503 ITEM is a project, task, resource or report headline. ATTRIBUTES
504 is a list of symbols representing valid attributes for ITEM."
505 (mapconcat
506 (lambda (attribute)
507 (let ((value (org-element-property
508 (intern (upcase (format ":%s" attribute)))
509 item)))
510 (and value (format "%s %s\n" attribute value))))
511 (remq nil attributes) ""))
513 (defun org-taskjuggler--build-unique-id (item unique-ids)
514 "Return a unique id for a given task or a resource.
515 ITEM is an `headline' type element representing the task or
516 resource. Its id is derived from its name and made unique
517 against UNIQUE-IDS. If the (downcased) first token of the
518 headline is not unique try to add more (downcased) tokens of the
519 headline or finally add more underscore characters (\"_\")."
520 (let ((id (org-string-nw-p (org-element-property :TASK_ID item))))
521 ;; If an id is specified, use it, as long as it's unique.
522 (if (and id (not (member id unique-ids))) id
523 (let* ((parts (split-string (org-element-property :raw-value item)))
524 (id (org-taskjuggler--clean-id (downcase (pop parts)))))
525 ;; Try to add more parts of the headline to make it unique.
526 (while (and (car parts) (member id unique-ids))
527 (setq id (concat id "_"
528 (org-taskjuggler--clean-id (downcase (pop parts))))))
529 ;; If it's still not unique, add "_".
530 (while (member id unique-ids)
531 (setq id (concat id "_")))
532 id))))
534 (defun org-taskjuggler--clean-id (id)
535 "Clean and return ID to make it acceptable for TaskJuggler.
536 ID is a string."
537 ;; Replace non-ascii by "_".
538 (replace-regexp-in-string
539 "[^a-zA-Z0-9_]" "_"
540 ;; Make sure id doesn't start with a number.
541 (replace-regexp-in-string "^\\([0-9]\\)" "_\\1" id)))
545 ;;; Dependencies
547 (defun org-taskjuggler-resolve-dependencies (task info)
548 "Return a list of all tasks TASK depends on.
549 TASK is a headline. INFO is a plist used as a communication
550 channel."
551 (let ((deps-ids
552 ;; Get all dependencies specified in BLOCKER and DEPENDS task
553 ;; properties. Clean options from them.
554 (let ((deps (concat (org-element-property :BLOCKER task)
555 (org-element-property :DEPENDS task))))
556 (and deps
557 (split-string (replace-regexp-in-string "{.*?}" "" deps)
558 "[ ,]* +"))))
559 depends)
560 (when deps-ids
561 ;; Find tasks with :task_id: property matching id in DEPS-IDS.
562 ;; Add them to DEPENDS.
563 (let* ((project (org-taskjuggler-get-project info))
564 (tasks (if org-taskjuggler-keep-project-as-task project
565 (org-element-contents project))))
566 (setq depends
567 (org-element-map tasks 'headline
568 (lambda (task)
569 (let ((task-id (or (org-element-property :TASK_ID task)
570 (org-element-property :ID task))))
571 (and task-id (member task-id deps-ids) task)))
572 info)))
573 ;; Check BLOCKER and DEPENDS properties. If "previous-sibling"
574 ;; belongs to DEPS-ID, add it to DEPENDS.
575 (when (and (member-ignore-case "previous-sibling" deps-ids)
576 (not (org-export-first-sibling-p task info)))
577 (let ((prev (org-export-get-previous-element task info)))
578 (and (not (memq prev depends)) (push prev depends)))))
579 ;; Check ORDERED status of parent.
580 (let ((parent (org-export-get-parent task)))
581 (when (and parent
582 (org-element-property :ORDERED parent)
583 (not (org-export-first-sibling-p task info)))
584 (push (org-export-get-previous-element task info) depends)))
585 ;; Return dependencies.
586 depends))
588 (defun org-taskjuggler-format-dependencies (dependencies task info)
589 "Format DEPENDENCIES to match TaskJuggler syntax.
590 DEPENDENCIES is list of dependencies for TASK, as returned by
591 `org-taskjuggler-resolve-depedencies'. TASK is a headline.
592 INFO is a plist used as a communication channel. Return value
593 doesn't include leading \"depends\"."
594 (let* ((dep-str (concat (org-element-property :BLOCKER task)
596 (org-element-property :DEPENDS task)))
597 (get-path
598 (lambda (dep)
599 ;; Return path to DEP relatively to TASK.
600 (let ((parent (org-export-get-parent task))
601 (exclamations 1)
602 (option
603 (let ((id (org-element-property :TASK_ID dep)))
604 (and id
605 (string-match (concat id " +\\({.*?}\\)") dep-str)
606 (match-string-no-properties 1 dep-str))))
607 path)
608 ;; Compute number of exclamation marks by looking for the
609 ;; common ancestor between TASK and DEP.
610 (while (not (org-element-map parent 'headline
611 (lambda (hl) (eq hl dep))))
612 (incf exclamations)
613 (setq parent (org-export-get-parent parent)))
614 ;; Build path from DEP to PARENT.
615 (while (not (eq parent dep))
616 (push (org-taskjuggler-get-id dep info) path)
617 (setq dep (org-export-get-parent dep)))
618 ;; Return full path. Add dependency options, if any.
619 (concat (make-string exclamations ?!)
620 (mapconcat 'identity path ".")
621 (and option (concat " " option)))))))
622 ;; Return dependencies string, without the leading "depends".
623 (mapconcat (lambda (dep) (funcall get-path dep)) dependencies ", ")))
627 ;;; Translator Functions
629 (defun org-taskjuggler-project-plan (contents info)
630 "Build TaskJuggler project plan.
631 CONTENTS is ignored. INFO is a plist holding export options.
632 Return complete project plan as a string in TaskJuggler syntax."
633 (let* ((tree (plist-get info :parse-tree))
634 (project (or (org-taskjuggler-get-project info)
635 (error "No project specified"))))
636 (concat
637 ;; 1. Insert header.
638 (org-element-normalize-string org-taskjuggler-default-global-header)
639 ;; 2. Insert project.
640 (org-taskjuggler--build-project project info)
641 ;; 3. Insert global properties.
642 (org-element-normalize-string org-taskjuggler-default-global-properties)
643 ;; 4. Insert resources. Provide a default one if none is
644 ;; specified.
645 (let ((main-resources
646 ;; Collect contents from various trees marked with
647 ;; `org-taskjuggler-resource-tag'. Only gather top level
648 ;; resources.
649 (apply 'append
650 (org-element-map tree 'headline
651 (lambda (hl)
652 (and (member org-taskjuggler-resource-tag
653 (org-export-get-tags hl info))
654 (org-element-map (org-element-contents hl) 'headline
655 'identity info nil 'headline)))
656 info nil 'headline))))
657 ;; Assign a unique ID to each resource. Store it under
658 ;; `:taskjuggler-unique-ids' property in INFO.
659 (setq info
660 (plist-put info :taskjuggler-unique-ids
661 (org-taskjuggler-assign-resource-ids
662 main-resources info)))
663 (concat
664 (if main-resources
665 (mapconcat
666 (lambda (resource) (org-taskjuggler--build-resource resource info))
667 main-resources "")
668 (format "resource %s \"%s\" {\n}\n" (user-login-name) user-full-name))
669 ;; 5. Insert tasks.
670 (let ((main-tasks
671 ;; If `org-taskjuggler-keep-project-as-task' is
672 ;; non-nil, there is only one task. Otherwise, every
673 ;; direct children of PROJECT is a top level task.
674 (if org-taskjuggler-keep-project-as-task (list project)
675 (or (org-element-map (org-element-contents project) 'headline
676 'identity info nil 'headline)
677 (error "No task specified")))))
678 ;; Assign a unique ID to each task. Add it to
679 ;; `:taskjuggler-unique-ids' property in INFO.
680 (setq info
681 (plist-put info :taskjuggler-unique-ids
682 (append
683 (org-taskjuggler-assign-task-ids main-tasks info)
684 (plist-get info :taskjuggler-unique-ids))))
685 ;; If no resource is allocated among tasks, allocate one to
686 ;; the first task.
687 (unless (org-element-map main-tasks 'headline
688 (lambda (task) (org-element-property :ALLOCATE task))
689 info t)
690 (org-element-put-property
691 (car main-tasks) :ALLOCATE
692 (or (org-taskjuggler-get-id (car main-resources) info)
693 (user-login-name))))
694 (mapconcat
695 (lambda (task) (org-taskjuggler--build-task task info))
696 main-tasks ""))
697 ;; 6. Insert reports. If no report is defined, insert default
698 ;; reports.
699 (let ((main-reports
700 ;; Collect contents from various trees marked with
701 ;; `org-taskjuggler-report-tag'. Only gather top level
702 ;; reports.
703 (apply 'append
704 (org-element-map tree 'headline
705 (lambda (hl)
706 (and (member org-taskjuggler-report-tag
707 (org-export-get-tags hl info))
708 (org-element-map (org-element-contents hl)
709 'headline 'identity info nil 'headline)))
710 info nil 'headline))))
711 (if main-reports
712 (mapconcat
713 (lambda (report) (org-taskjuggler--build-report report info))
714 main-reports "")
715 ;; insert title in default reports
716 (let* ((title (org-export-data (plist-get info :title) info))
717 (report-title (if (string= title "")
718 (org-taskjuggler-get-name project)
719 title)))
720 (mapconcat
721 'org-element-normalize-string
722 (mapcar
723 (function
724 (lambda (report)
725 (replace-regexp-in-string "%title" report-title report t t)))
726 org-taskjuggler-default-reports) "")))))))))
728 (defun org-taskjuggler--build-project (project info)
729 "Return a project declaration.
730 PROJECT is a headline. INFO is a plist used as a communication
731 channel. If no start date is specified, start today. If no end
732 date is specified, end `org-taskjuggler-default-project-duration'
733 days from now."
734 (concat
735 ;; Opening project.
736 (format "project %s \"%s\" \"%s\" %s %s {\n"
737 (org-taskjuggler-get-id project info)
738 (org-taskjuggler-get-name project)
739 ;; Version is obtained through :TASKJUGGLER_VERSION:
740 ;; property or `org-taskjuggler-default-project-version'.
741 (or (org-element-property :VERSION project)
742 org-taskjuggler-default-project-version)
743 (or (org-taskjuggler-get-start project)
744 (format-time-string "%Y-%m-%d"))
745 (let ((end (org-taskjuggler-get-end project)))
746 (or (and end (format "- %s" end))
747 (format "+%sd"
748 org-taskjuggler-default-project-duration))))
749 ;; Add attributes.
750 (org-taskjuggler--indent-string
751 (org-taskjuggler--build-attributes
752 project org-taskjuggler-valid-project-attributes))
753 ;; Closing project.
754 "}\n"))
756 (defun org-taskjuggler--build-resource (resource info)
757 "Return a resource declaration.
759 RESOURCE is a headline. INFO is a plist used as a communication
760 channel.
762 All valid attributes from RESOURCE are inserted. If RESOURCE
763 defines a property \"resource_id\" it will be used as the id for
764 this resource. Otherwise it will use the ID property. If
765 neither is defined a unique id will be associated to it."
766 (concat
767 ;; Opening resource.
768 (format "resource %s \"%s\" {\n"
769 (org-taskjuggler--clean-id
770 (or (org-element-property :RESOURCE_ID resource)
771 (org-element-property :ID resource)
772 (org-taskjuggler-get-id resource info)))
773 (org-taskjuggler-get-name resource))
774 ;; Add attributes.
775 (org-taskjuggler--indent-string
776 (org-taskjuggler--build-attributes
777 resource org-taskjuggler-valid-resource-attributes))
778 ;; Add inner resources.
779 (org-taskjuggler--indent-string
780 (mapconcat
781 'identity
782 (org-element-map (org-element-contents resource) 'headline
783 (lambda (hl) (org-taskjuggler--build-resource hl info))
784 info nil 'headline)
785 ""))
786 ;; Closing resource.
787 "}\n"))
789 (defun org-taskjuggler--build-report (report info)
790 "Return a report declaration.
791 REPORT is a headline. INFO is a plist used as a communication
792 channel."
793 (concat
794 ;; Opening report.
795 (format "%s \"%s\" {\n"
796 (or (org-element-property :REPORT_KIND report) "taskreport")
797 (org-taskjuggler-get-name report))
798 ;; Add attributes.
799 (org-taskjuggler--indent-string
800 (org-taskjuggler--build-attributes
801 report org-taskjuggler-valid-report-attributes))
802 ;; Add inner reports.
803 (org-taskjuggler--indent-string
804 (mapconcat
805 'identity
806 (org-element-map (org-element-contents report) 'headline
807 (lambda (hl) (org-taskjuggler--build-report hl info))
808 info nil 'headline)
809 ""))
810 ;; Closing report.
811 "}\n"))
813 (defun org-taskjuggler--build-task (task info)
814 "Return a task declaration.
816 TASK is a headline. INFO is a plist used as a communication
817 channel.
819 All valid attributes from TASK are inserted. If TASK defines
820 a property \"task_id\" it will be used as the id for this task.
821 Otherwise it will use the ID property. If neither is defined
822 a unique id will be associated to it."
823 (let* ((allocate (org-element-property :ALLOCATE task))
824 (complete
825 (if (eq (org-element-property :todo-type task) 'done) "100"
826 (org-element-property :COMPLETE task)))
827 (depends (org-taskjuggler-resolve-dependencies task info))
828 (effort (let ((property
829 (intern (concat ":" (upcase org-effort-property)))))
830 (org-element-property property task)))
831 (milestone
832 (or (org-element-property :MILESTONE task)
833 (not (or (org-element-map (org-element-contents task) 'headline
834 'identity info t) ; Has task any child?
835 effort
836 (org-element-property :LENGTH task)
837 (org-element-property :DURATION task)
838 (and (org-taskjuggler-get-start task)
839 (org-taskjuggler-get-end task))
840 (org-element-property :PERIOD task)))))
841 (priority
842 (let ((pri (org-element-property :priority task)))
843 (and pri
844 (max 1 (/ (* 1000 (- org-lowest-priority pri))
845 (- org-lowest-priority org-highest-priority)))))))
846 (concat
847 ;; Opening task.
848 (format "task %s \"%s\" {\n"
849 (org-taskjuggler-get-id task info)
850 (org-taskjuggler-get-name task))
851 ;; Add default attributes.
852 (and depends
853 (format " depends %s\n"
854 (org-taskjuggler-format-dependencies depends task info)))
855 (and allocate
856 (format " purge %s\n allocate %s\n"
857 ;; Compatibility for previous TaskJuggler versions.
858 (if (>= org-taskjuggler-target-version 3.0) "allocate"
859 "allocations")
860 allocate))
861 (and complete (format " complete %s\n" complete))
862 (and effort
863 (format " effort %s\n"
864 (let* ((minutes (org-duration-to-minutes effort))
865 (hours (/ minutes 60.0)))
866 (format "%.1fh" hours))))
867 (and priority (format " priority %s\n" priority))
868 (and milestone " milestone\n")
869 ;; Add other valid attributes.
870 (org-taskjuggler--indent-string
871 (org-taskjuggler--build-attributes
872 task org-taskjuggler-valid-task-attributes))
873 ;; Add inner tasks.
874 (org-taskjuggler--indent-string
875 (mapconcat 'identity
876 (org-element-map (org-element-contents task) 'headline
877 (lambda (hl) (org-taskjuggler--build-task hl info))
878 info nil 'headline)
879 ""))
880 ;; Closing task.
881 "}\n")))
885 ;;; Interactive Functions
887 ;;;###autoload
888 (defun org-taskjuggler-export (&optional async subtreep visible-only)
889 "Export current buffer to a TaskJuggler file.
891 The exporter looks for a tree with tag that matches
892 `org-taskjuggler-project-tag' and takes this as the tasks for
893 this project. The first node of this tree defines the project
894 properties such as project name and project period.
896 If there is a tree with tag that matches
897 `org-taskjuggler-resource-tag' this tree is taken as resources
898 for the project. If no resources are specified, a default
899 resource is created and allocated to the project.
901 Also the TaskJuggler project will be created with default reports
902 as defined in `org-taskjuggler-default-reports'.
904 If narrowing is active in the current buffer, only export its
905 narrowed part.
907 If a region is active, export that region.
909 A non-nil optional argument ASYNC means the process should happen
910 asynchronously. The resulting file should be accessible through
911 the `org-export-stack' interface.
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 output file's name."
921 (interactive)
922 (let ((outfile
923 (org-export-output-file-name org-taskjuggler-extension subtreep)))
924 (org-export-to-file 'taskjuggler outfile
925 async subtreep visible-only nil nil
926 (lambda (file)
927 (run-hook-with-args 'org-taskjuggler-final-hook file) nil))))
929 ;;;###autoload
930 (defun org-taskjuggler-export-and-process (&optional subtreep visible-only)
931 "Export current buffer to a TaskJuggler file and process it.
933 The exporter looks for a tree with tag that matches
934 `org-taskjuggler-project-tag' and takes this as the tasks for
935 this project. The first node of this tree defines the project
936 properties such as project name and project period.
938 If there is a tree with tag that matches
939 `org-taskjuggler-resource-tag' this tree is taken as resources
940 for the project. If no resources are specified, a default
941 resource is created and allocated to the project.
943 Also the TaskJuggler project will be created with default reports
944 as defined in `org-taskjuggler-default-reports'.
946 If narrowing is active in the current buffer, only export its
947 narrowed part.
949 If a region is active, export that region.
951 When optional argument SUBTREEP is non-nil, export the sub-tree
952 at point, extracting information from the headline properties
953 first.
955 When optional argument VISIBLE-ONLY is non-nil, don't export
956 contents of hidden elements.
958 Return a list of reports."
959 (interactive)
960 (let ((file (org-taskjuggler-export nil subtreep visible-only)))
961 (org-taskjuggler-compile file)))
963 ;;;###autoload
964 (defun org-taskjuggler-export-process-and-open (&optional subtreep visible-only)
965 "Export current buffer to a TaskJuggler file, process and open it.
967 Export and process the file using
968 `org-taskjuggler-export-and-process' and open the generated
969 reports with a browser.
971 If you are targeting TaskJuggler 2.4 (see
972 `org-taskjuggler-target-version') the processing and display of
973 the reports is done using the TaskJuggler GUI."
974 (interactive)
975 (if (< org-taskjuggler-target-version 3.0)
976 (let* ((process-name "TaskJugglerUI")
977 (command
978 (concat process-name " "
979 (org-taskjuggler-export nil subtreep visible-only))))
980 (start-process-shell-command process-name nil command))
981 (dolist (report (org-taskjuggler-export-and-process subtreep visible-only))
982 (org-open-file report))))
984 (defun org-taskjuggler-compile (file)
985 "Compile a TaskJuggler file.
987 FILE is the name of the file being compiled. Processing is done
988 through the command given in `org-taskjuggler-process-command'.
990 Return a list of reports."
991 (let* ((full-name (file-truename file))
992 (out-dir
993 (expand-file-name
994 org-taskjuggler-reports-directory (file-name-directory file)))
995 errors)
996 (message (format "Processing TaskJuggler file %s..." file))
997 (save-window-excursion
998 (let ((outbuf (get-buffer-create "*Org Taskjuggler Output*")))
999 (unless (file-directory-p out-dir)
1000 (make-directory out-dir t))
1001 (with-current-buffer outbuf (erase-buffer))
1002 (shell-command
1003 (replace-regexp-in-string
1004 "%f" (shell-quote-argument full-name)
1005 (replace-regexp-in-string
1006 "%o" (shell-quote-argument out-dir)
1007 org-taskjuggler-process-command t t) t t) outbuf)
1008 ;; Collect standard errors from output buffer.
1009 (setq errors (org-taskjuggler--collect-errors outbuf)))
1010 (if (not errors)
1011 (message "Process completed.")
1012 (error (format "TaskJuggler failed with errors: %s" errors))))
1013 (file-expand-wildcards (format "%s/*.html" out-dir))))
1015 (defun org-taskjuggler--collect-errors (buffer)
1016 "Collect some kind of errors from \"tj3\" command output.
1018 BUFFER is the buffer containing output.
1020 Return collected error types as a string, or nil if there was
1021 none."
1022 (with-current-buffer buffer
1023 (save-excursion
1024 (goto-char (point-min))
1025 (let ((case-fold-search t)
1026 (errors ""))
1027 (while (re-search-forward "^.+:[0-9]+: \\(.*\\)$" nil t)
1028 (setq errors (concat errors " " (match-string 1))))
1029 (and (org-string-nw-p errors) (org-trim errors))))))
1032 (provide 'ox-taskjuggler)
1034 ;; Local variables:
1035 ;; sentence-end-double-space: t
1036 ;; End:
1038 ;;; ox-taskjuggler.el ends here