1 ;;; ox-taskjuggler.el --- TaskJuggler Back-End for Org Export Engine
3 ;; Copyright (C) 2007-2014 Free Software Foundation, Inc.
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/>.
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.
47 ;; Put this file into your load-path and the following line into your
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'
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
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.
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,
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
122 ;; :task_id: training_material
125 ;; ** Markup Guidelines
129 ;; ** Workflow Guidelines
136 ;; :BLOCKER: training_material { gapduration 1d } some_other_task
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
148 (eval-when-compile (require 'cl
))
156 (defgroup org-export-taskjuggler nil
157 "Options specific for TaskJuggler export back-end."
158 :tag
"Org Export TaskJuggler"
161 (defcustom org-taskjuggler-extension
".tjp"
162 "Extension of TaskJuggler files."
163 :group
'org-export-taskjuggler
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
170 :group
'org-export-taskjuggler
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
177 :group
'org-export-taskjuggler
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
184 :group
'org-export-taskjuggler
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
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
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
214 (defcustom org-taskjuggler-default-reports
215 '("textreport report \"Plan\" {
217 header '== %title =='
220 [#Plan Plan] | [#Resource_Allocation Resource Allocation]
223 <[report id=\"plan\"]>
225 === Resource Allocation ===
226 <[report id=\"resourceGraph\"]>
230 # A traditional Gantt chart with a project overview.
231 taskreport plan \"\" {
232 headline \"Project Plan\"
233 columns bsi, name, start, end, effort, chart
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
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:
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\"
269 resourcereport \"Resource Graph\" {
270 headline \"Resource Allocation Graph\"
271 columns no, name, utilization, freeload, chart
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
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."
311 :group
'org-export-taskjuggler
)
313 (defcustom org-taskjuggler-valid-resource-attributes
314 '(limits vacation shift booking efficiency journalentry rate
316 "Valid attributes for Taskjuggler resources.
317 If one of these appears as a property for a headline, it will be
318 exported with the corresponding resource."
319 :group
'org-export-taskjuggler
)
321 (defcustom org-taskjuggler-valid-report-attributes
322 '(headline columns definitions timeformat hideresource hidetask
323 loadunit sorttasks formats period
)
324 "Valid attributes for Taskjuggler reports.
325 If one of these appears as a property for a headline, it will be
326 exported with the corresponding report."
327 :group
'org-export-taskjuggler
)
329 (defcustom org-taskjuggler-process-command
330 "tj3 --silent --no-color --output-dir %o %f"
331 "Command to process a Taskjuggler file.
332 The command will be given to the shell as a command to process a
333 Taskjuggler file. \"%f\" in the command will be replaced by the
334 full file name, \"%o\" by the reports directory (see
335 `org-taskjuggler-reports-directory').
337 If you are targeting Taskjuggler 2.4 (see
338 `org-taskjuggler-target-version') this setting is ignored."
339 :group
'org-export-taskjuggler
)
341 (defcustom org-taskjuggler-reports-directory
"reports"
342 "Default directory to generate the Taskjuggler reports in.
343 The command `org-taskjuggler-process-command' generates the
344 reports and associated files such as CSS inside this directory.
346 If the directory is not an absolute path it is relative to the
347 directory of the exported file. The directory is created if it
350 If you are targeting Taskjuggler 2.4 (see
351 `org-taskjuggler-target-version') this setting is ignored."
352 :group
'org-export-taskjuggler
)
354 (defcustom org-taskjuggler-keep-project-as-task t
355 "Non-nil keeps the project headline as an umbrella task for all tasks.
356 Setting this to nil will allow maintaining completely separated
357 task buckets, while still sharing the same resources pool."
358 :group
'org-export-taskjuggler
365 (defvar org-taskjuggler-final-hook nil
366 "Hook run after a TaskJuggler files has been saved.
367 This hook is run with the name of the file as argument.")
371 ;;; Back-End Definition
373 (org-export-define-backend 'taskjuggler
374 '((template . org-taskjuggler-project-plan
))
376 '(?J
"Export to TaskJuggler"
377 ((?j
"As TJP file" (lambda (a s v b
) (org-taskjuggler-export a s v
)))
378 (?p
"As TJP file and process"
380 (if a
(org-taskjuggler-export a s v
)
381 (org-taskjuggler-export-and-process s v
))))
382 (?o
"As TJP file, process and open"
384 (if a
(org-taskjuggler-export a s v
)
385 (org-taskjuggler-export-process-and-open s v
))))))
386 ;; This property will be used to store unique ids in communication
387 ;; channel. Ids will be retrieved with `org-taskjuggler-get-id'.
388 :options-alist
'((:taskjuggler-unique-ids nil nil nil
)))
394 (defun org-taskjuggler-assign-task-ids (tasks info
)
395 "Assign a unique ID to each task in TASKS.
396 TASKS is a list of headlines. INFO is a plist used as a
397 communication channel. Return value is an alist between
398 headlines and their associated ID. IDs are hierarchical, which
399 means they only need to be unique among the task siblings."
401 build-id
; For byte-compiler.
403 (lambda (tasks local-ids
)
404 (org-element-map tasks
'headline
406 (let ((id (org-taskjuggler--build-unique-id task local-ids
)))
408 (push (cons task id
) alist
)
409 (funcall build-id
(org-element-contents task
) nil
)))
410 info nil
'headline
))))
411 (funcall build-id tasks nil
)
414 (defun org-taskjuggler-assign-resource-ids (resources info
)
415 "Assign a unique ID to each resource within RESOURCES.
416 RESOURCES is a list of headlines. INFO is a plist used as a
417 communication channel. Return value is an alist between
418 headlines and their associated ID."
420 (org-element-map resources
'headline
422 (let ((id (org-taskjuggler--build-unique-id resource ids
)))
431 (defun org-taskjuggler-get-project (info)
432 "Return project in parse tree.
433 INFO is a plist used as a communication channel. First headline
434 in buffer with `org-taskjuggler-project-tag' defines the project.
435 If no such task is defined, pick the first headline in buffer.
436 If there is no headline at all, return nil."
437 (let ((tree (plist-get info
:parse-tree
)))
438 (or (org-element-map tree
'headline
440 (and (member org-taskjuggler-project-tag
441 (org-export-get-tags hl info
))
444 (org-element-map tree
'headline
'identity info t
))))
446 (defun org-taskjuggler-get-id (item info
)
447 "Return id for task or resource ITEM.
448 ITEM is a headline. INFO is a plist used as a communication
449 channel. Return value is a string."
450 (cdr (assq item
(plist-get info
:taskjuggler-unique-ids
))))
452 (defun org-taskjuggler-get-name (item)
453 "Return name for task or resource ITEM.
454 ITEM is a headline. Return value is a string."
455 ;; Quote double quotes in name.
456 (replace-regexp-in-string
457 "\"" "\\\"" (org-element-property :raw-value item
) t t
))
459 (defun org-taskjuggler-get-start (item)
460 "Return start date for task or resource ITEM.
461 ITEM is a headline. Return value is a string or nil if ITEM
462 doesn't have any start date defined."
463 (let ((scheduled (org-element-property :scheduled item
)))
465 (and scheduled
(org-timestamp-format scheduled
"%Y-%02m-%02d"))
466 (and (memq 'start org-taskjuggler-valid-task-attributes
)
467 (org-element-property :START item
)))))
469 (defun org-taskjuggler-get-end (item)
470 "Return end date for task or resource ITEM.
471 ITEM is a headline. Return value is a string or nil if ITEM
472 doesn't have any end date defined."
473 (let ((deadline (org-element-property :deadline item
)))
474 (and deadline
(org-timestamp-format deadline
"%Y-%02m-%02d"))))
478 ;;; Internal Functions
480 (defun org-taskjuggler--indent-string (s)
481 "Indent string S by 2 spaces.
482 Return new string. If S is the empty string, return it."
483 (if (equal "" s
) s
(replace-regexp-in-string "^ *\\S-" " \\&" s
)))
485 (defun org-taskjuggler--build-attributes (item attributes
)
486 "Return attributes string for task, resource or report ITEM.
487 ITEM is a headline. ATTRIBUTES is a list of symbols
488 representing valid attributes for ITEM."
491 (let ((value (org-element-property
492 (intern (upcase (format ":%s" attribute
)))
494 (and value
(format "%s %s\n" attribute value
))))
495 (remq nil attributes
) ""))
497 (defun org-taskjuggler--build-unique-id (item unique-ids
)
498 "Return a unique id for a given task or a resource.
499 ITEM is an `headline' type element representing the task or
500 resource. Its id is derived from its name and made unique
501 against UNIQUE-IDS. If the (downcased) first token of the
502 headline is not unique try to add more (downcased) tokens of the
503 headline or finally add more underscore characters (\"_\")."
504 (let ((id (org-string-nw-p (org-element-property :TASK_ID item
))))
505 ;; If an id is specified, use it, as long as it's unique.
506 (if (and id
(not (member id unique-ids
))) id
507 (let* ((parts (org-split-string (org-element-property :raw-value item
)))
508 (id (org-taskjuggler--clean-id (downcase (pop parts
)))))
509 ;; Try to add more parts of the headline to make it unique.
510 (while (and (car parts
) (member id unique-ids
))
511 (setq id
(concat id
"_"
512 (org-taskjuggler--clean-id (downcase (pop parts
))))))
513 ;; If it's still not unique, add "_".
514 (while (member id unique-ids
)
515 (setq id
(concat id
"_")))
518 (defun org-taskjuggler--clean-id (id)
519 "Clean and return ID to make it acceptable for TaskJuggler.
521 ;; Replace non-ascii by "_".
522 (replace-regexp-in-string
524 ;; Make sure id doesn't start with a number.
525 (replace-regexp-in-string "^\\([0-9]\\)" "_\\1" id
)))
531 (defun org-taskjuggler-resolve-dependencies (task info
)
532 "Return a list of all tasks TASK depends on.
533 TASK is a headline. INFO is a plist used as a communication
536 ;; Get all dependencies specified in BLOCKER and DEPENDS task
537 ;; properties. Clean options from them.
538 (let ((deps (concat (org-element-property :BLOCKER task
)
539 (org-element-property :DEPENDS task
))))
541 (org-split-string (replace-regexp-in-string "{.*?}" "" deps
)
545 ;; Find tasks with :task_id: property matching id in DEPS-IDS.
546 ;; Add them to DEPENDS.
547 (let* ((project (org-taskjuggler-get-project info
))
548 (tasks (if org-taskjuggler-keep-project-as-task project
549 (org-element-contents project
))))
551 (org-element-map tasks
'headline
553 (let ((task-id (or (org-element-property :TASK_ID task
)
554 (org-element-property :ID task
))))
555 (and task-id
(member task-id deps-ids
) task
)))
557 ;; Check BLOCKER and DEPENDS properties. If "previous-sibling"
558 ;; belongs to DEPS-ID, add it to DEPENDS.
559 (when (and (member-ignore-case "previous-sibling" deps-ids
)
560 (not (org-export-first-sibling-p task info
)))
561 (let ((prev (org-export-get-previous-element task info
)))
562 (and (not (memq prev depends
)) (push prev depends
)))))
563 ;; Check ORDERED status of parent.
564 (let ((parent (org-export-get-parent task
)))
566 (org-element-property :ORDERED parent
)
567 (not (org-export-first-sibling-p task info
)))
568 (push (org-export-get-previous-element task info
) depends
)))
569 ;; Return dependencies.
572 (defun org-taskjuggler-format-dependencies (dependencies task info
)
573 "Format DEPENDENCIES to match TaskJuggler syntax.
574 DEPENDENCIES is list of dependencies for TASK, as returned by
575 `org-taskjuggler-resolve-depedencies'. TASK is a headline.
576 INFO is a plist used as a communication channel. Return value
577 doesn't include leading \"depends\"."
578 (let* ((dep-str (concat (org-element-property :BLOCKER task
)
580 (org-element-property :DEPENDS task
)))
583 ;; Return path to DEP relatively to TASK.
584 (let ((parent (org-export-get-parent task
))
587 (let ((id (org-element-property :TASK_ID dep
)))
589 (string-match (concat id
" +\\({.*?}\\)") dep-str
)
590 (org-match-string-no-properties 1))))
592 ;; Compute number of exclamation marks by looking for the
593 ;; common ancestor between TASK and DEP.
594 (while (not (org-element-map parent
'headline
595 (lambda (hl) (eq hl dep
))))
597 (setq parent
(org-export-get-parent parent
)))
598 ;; Build path from DEP to PARENT.
599 (while (not (eq parent dep
))
600 (push (org-taskjuggler-get-id dep info
) path
)
601 (setq dep
(org-export-get-parent dep
)))
602 ;; Return full path. Add dependency options, if any.
603 (concat (make-string exclamations ?
!)
604 (mapconcat 'identity path
".")
605 (and option
(concat " " option
)))))))
606 ;; Return dependencies string, without the leading "depends".
607 (mapconcat (lambda (dep) (funcall get-path dep
)) dependencies
", ")))
611 ;;; Translator Functions
613 (defun org-taskjuggler-project-plan (contents info
)
614 "Build TaskJuggler project plan.
615 CONTENTS is ignored. INFO is a plist holding export options.
616 Return complete project plan as a string in TaskJuggler syntax."
617 (let* ((tree (plist-get info
:parse-tree
))
618 (project (or (org-taskjuggler-get-project info
)
619 (error "No project specified"))))
622 (org-element-normalize-string org-taskjuggler-default-global-header
)
623 ;; 2. Insert project.
624 (org-taskjuggler--build-project project info
)
625 ;; 3. Insert global properties.
626 (org-element-normalize-string org-taskjuggler-default-global-properties
)
627 ;; 4. Insert resources. Provide a default one if none is
629 (let ((main-resources
630 ;; Collect contents from various trees marked with
631 ;; `org-taskjuggler-resource-tag'. Only gather top level
634 (org-element-map tree
'headline
636 (and (member org-taskjuggler-resource-tag
637 (org-export-get-tags hl info
))
638 (org-element-map (org-element-contents hl
) 'headline
639 'identity info nil
'headline
)))
640 info nil
'headline
))))
641 ;; Assign a unique ID to each resource. Store it under
642 ;; `:taskjuggler-unique-ids' property in INFO.
644 (plist-put info
:taskjuggler-unique-ids
645 (org-taskjuggler-assign-resource-ids
646 main-resources info
)))
650 (lambda (resource) (org-taskjuggler--build-resource resource info
))
652 (format "resource %s \"%s\" {\n}\n" (user-login-name) user-full-name
))
655 ;; If `org-taskjuggler-keep-project-as-task' is
656 ;; non-nil, there is only one task. Otherwise, every
657 ;; direct children of PROJECT is a top level task.
658 (if org-taskjuggler-keep-project-as-task
(list project
)
659 (or (org-element-map (org-element-contents project
) 'headline
660 'identity info nil
'headline
)
661 (error "No task specified")))))
662 ;; Assign a unique ID to each task. Add it to
663 ;; `:taskjuggler-unique-ids' property in INFO.
665 (plist-put info
:taskjuggler-unique-ids
667 (org-taskjuggler-assign-task-ids main-tasks info
)
668 (plist-get info
:taskjuggler-unique-ids
))))
669 ;; If no resource is allocated among tasks, allocate one to
671 (unless (org-element-map main-tasks
'headline
672 (lambda (task) (org-element-property :ALLOCATE task
))
674 (org-element-put-property
675 (car main-tasks
) :ALLOCATE
676 (or (org-taskjuggler-get-id (car main-resources
) info
)
679 (lambda (task) (org-taskjuggler--build-task task info
))
681 ;; 6. Insert reports. If no report is defined, insert default
684 ;; Collect contents from various trees marked with
685 ;; `org-taskjuggler-report-tag'. Only gather top level
688 (org-element-map tree
'headline
690 (and (member org-taskjuggler-report-tag
691 (org-export-get-tags hl info
))
692 (org-element-map (org-element-contents hl
)
693 'headline
'identity info nil
'headline
)))
694 info nil
'headline
))))
697 (lambda (report) (org-taskjuggler--build-report report info
))
699 ;; insert title in default reports
700 (let* ((title (org-export-data (plist-get info
:title
) info
))
701 (report-title (if (string= title
"")
702 (org-taskjuggler-get-name project
)
705 'org-element-normalize-string
709 (replace-regexp-in-string "%title" report-title report t t
)))
710 org-taskjuggler-default-reports
) "")))))))))
712 (defun org-taskjuggler--build-project (project info
)
713 "Return a project declaration.
714 PROJECT is a headline. INFO is a plist used as a communication
715 channel. If no start date is specified, start today. If no end
716 date is specified, end `org-taskjuggler-default-project-duration'
718 (format "project %s \"%s\" \"%s\" %s %s {\n}\n"
719 (org-taskjuggler-get-id project info
)
720 (org-taskjuggler-get-name project
)
721 ;; Version is obtained through :TASKJUGGLER_VERSION:
722 ;; property or `org-taskjuggler-default-project-version'.
723 (or (org-element-property :VERSION project
)
724 org-taskjuggler-default-project-version
)
725 (or (org-taskjuggler-get-start project
)
726 (format-time-string "%Y-%m-%d"))
727 (let ((end (org-taskjuggler-get-end project
)))
728 (or (and end
(format "- %s" end
))
729 (format "+%sd" org-taskjuggler-default-project-duration
)))))
731 (defun org-taskjuggler--build-resource (resource info
)
732 "Return a resource declaration.
734 RESOURCE is a headline. INFO is a plist used as a communication
737 All valid attributes from RESOURCE are inserted. If RESOURCE
738 defines a property \"resource_id\" it will be used as the id for
739 this resource. Otherwise it will use the ID property. If
740 neither is defined a unique id will be associated to it."
743 (format "resource %s \"%s\" {\n"
744 (org-taskjuggler--clean-id
745 (or (org-element-property :RESOURCE_ID resource
)
746 (org-element-property :ID resource
)
747 (org-taskjuggler-get-id resource info
)))
748 (org-taskjuggler-get-name resource
))
750 (org-taskjuggler--indent-string
751 (org-taskjuggler--build-attributes
752 resource org-taskjuggler-valid-resource-attributes
))
753 ;; Add inner resources.
754 (org-taskjuggler--indent-string
757 (org-element-map (org-element-contents resource
) 'headline
758 (lambda (hl) (org-taskjuggler--build-resource hl info
))
764 (defun org-taskjuggler--build-report (report info
)
765 "Return a report declaration.
766 REPORT is a headline. INFO is a plist used as a communication
770 (format "%s \"%s\" {\n"
771 (or (org-element-property :REPORT_KIND report
) "taskreport")
772 (org-taskjuggler-get-name report
))
774 (org-taskjuggler--indent-string
775 (org-taskjuggler--build-attributes
776 report org-taskjuggler-valid-report-attributes
))
777 ;; Add inner reports.
778 (org-taskjuggler--indent-string
781 (org-element-map (org-element-contents report
) 'headline
782 (lambda (hl) (org-taskjuggler--build-report hl info
))
788 (defun org-taskjuggler--build-task (task info
)
789 "Return a task declaration.
791 TASK is a headline. INFO is a plist used as a communication
794 All valid attributes from TASK are inserted. If TASK defines
795 a property \"task_id\" it will be used as the id for this task.
796 Otherwise it will use the ID property. If neither is defined
797 a unique id will be associated to it."
798 (let* ((allocate (org-element-property :ALLOCATE task
))
800 (if (eq (org-element-property :todo-type task
) 'done
) "100"
801 (org-element-property :COMPLETE task
)))
802 (depends (org-taskjuggler-resolve-dependencies task info
))
803 (effort (let ((property
804 (intern (concat ":" (upcase org-effort-property
)))))
805 (org-element-property property task
)))
807 (or (org-element-property :MILESTONE task
)
808 (not (or (org-element-map (org-element-contents task
) 'headline
809 'identity info t
) ; Has task any child?
811 (org-element-property :LENGTH task
)
812 (org-element-property :DURATION task
)
813 (and (org-taskjuggler-get-start task
)
814 (org-taskjuggler-get-end task
))
815 (org-element-property :PERIOD task
)))))
817 (let ((pri (org-element-property :priority task
)))
819 (max 1 (/ (* 1000 (- org-lowest-priority pri
))
820 (- org-lowest-priority org-highest-priority
)))))))
823 (format "task %s \"%s\" {\n"
824 (org-taskjuggler-get-id task info
)
825 (org-taskjuggler-get-name task
))
826 ;; Add default attributes.
828 (format " depends %s\n"
829 (org-taskjuggler-format-dependencies depends task info
)))
831 (format " purge %s\n allocate %s\n"
832 ;; Compatibility for previous TaskJuggler versions.
833 (if (>= org-taskjuggler-target-version
3.0) "allocate"
836 (and complete
(format " complete %s\n" complete
))
838 (format " effort %s\n"
839 (let* ((minutes (org-duration-string-to-minutes effort
))
840 (hours (/ minutes
60.0)))
841 (format "%.1fh" hours
))))
842 (and priority
(format " priority %s\n" priority
))
843 (and milestone
" milestone\n")
844 ;; Add other valid attributes.
845 (org-taskjuggler--indent-string
846 (org-taskjuggler--build-attributes
847 task org-taskjuggler-valid-task-attributes
))
849 (org-taskjuggler--indent-string
851 (org-element-map (org-element-contents task
) 'headline
852 (lambda (hl) (org-taskjuggler--build-task hl info
))
860 ;;; Interactive Functions
863 (defun org-taskjuggler-export (&optional async subtreep visible-only
)
864 "Export current buffer to a TaskJuggler file.
866 The exporter looks for a tree with tag that matches
867 `org-taskjuggler-project-tag' and takes this as the tasks for
868 this project. The first node of this tree defines the project
869 properties such as project name and project period.
871 If there is a tree with tag that matches
872 `org-taskjuggler-resource-tag' this tree is taken as resources
873 for the project. If no resources are specified, a default
874 resource is created and allocated to the project.
876 Also the TaskJuggler project will be created with default reports
877 as defined in `org-taskjuggler-default-reports'.
879 If narrowing is active in the current buffer, only export its
882 If a region is active, export that region.
884 A non-nil optional argument ASYNC means the process should happen
885 asynchronously. The resulting file should be accessible through
886 the `org-export-stack' interface.
888 When optional argument SUBTREEP is non-nil, export the sub-tree
889 at point, extracting information from the headline properties
892 When optional argument VISIBLE-ONLY is non-nil, don't export
893 contents of hidden elements.
895 Return output file's name."
898 (org-export-output-file-name org-taskjuggler-extension subtreep
)))
899 (org-export-to-file 'taskjuggler outfile
900 async subtreep visible-only nil nil
902 (run-hook-with-args 'org-taskjuggler-final-hook file
) nil
))))
905 (defun org-taskjuggler-export-and-process (&optional subtreep visible-only
)
906 "Export current buffer to a TaskJuggler file and process it.
908 The exporter looks for a tree with tag that matches
909 `org-taskjuggler-project-tag' and takes this as the tasks for
910 this project. The first node of this tree defines the project
911 properties such as project name and project period.
913 If there is a tree with tag that matches
914 `org-taskjuggler-resource-tag' this tree is taken as resources
915 for the project. If no resources are specified, a default
916 resource is created and allocated to the project.
918 Also the TaskJuggler project will be created with default reports
919 as defined in `org-taskjuggler-default-reports'.
921 If narrowing is active in the current buffer, only export its
924 If a region is active, export that region.
926 When optional argument SUBTREEP is non-nil, export the sub-tree
927 at point, extracting information from the headline properties
930 When optional argument VISIBLE-ONLY is non-nil, don't export
931 contents of hidden elements.
933 Return a list of reports."
935 (let ((file (org-taskjuggler-export nil subtreep visible-only
)))
936 (org-taskjuggler-compile file
)))
939 (defun org-taskjuggler-export-process-and-open (&optional subtreep visible-only
)
940 "Export current buffer to a TaskJuggler file, process and open it.
942 Export and process the file using
943 `org-taskjuggler-export-and-process' and open the generated
944 reports with a browser.
946 If you are targeting TaskJuggler 2.4 (see
947 `org-taskjuggler-target-version') the processing and display of
948 the reports is done using the TaskJuggler GUI."
950 (if (< org-taskjuggler-target-version
3.0)
951 (let* ((process-name "TaskJugglerUI")
953 (concat process-name
" "
954 (org-taskjuggler-export nil subtreep visible-only
))))
955 (start-process-shell-command process-name nil command
))
956 (dolist (report (org-taskjuggler-export-and-process subtreep visible-only
))
957 (org-open-file report
))))
959 (defun org-taskjuggler-compile (file)
960 "Compile a TaskJuggler file.
962 FILE is the name of the file being compiled. Processing is done
963 through the command given in `org-taskjuggler-process-command'.
965 Return a list of reports."
966 (let* ((full-name (file-truename file
))
969 org-taskjuggler-reports-directory
(file-name-directory file
)))
971 (message (format "Processing TaskJuggler file %s..." file
))
972 (save-window-excursion
973 (let ((outbuf (get-buffer-create "*Org Taskjuggler Output*")))
974 (unless (file-directory-p out-dir
)
975 (make-directory out-dir t
))
976 (with-current-buffer outbuf
(erase-buffer))
978 (replace-regexp-in-string
979 "%f" (shell-quote-argument full-name
)
980 (replace-regexp-in-string
981 "%o" (shell-quote-argument out-dir
)
982 org-taskjuggler-process-command t t
) t t
) outbuf
)
983 ;; Collect standard errors from output buffer.
984 (setq errors
(org-taskjuggler--collect-errors outbuf
)))
986 (message "Process completed.")
987 (error (format "TaskJuggler failed with errors: %s" errors
))))
988 (file-expand-wildcards (format "%s/*.html" out-dir
))))
990 (defun org-taskjuggler--collect-errors (buffer)
991 "Collect some kind of errors from \"tj3\" command output.
993 BUFFER is the buffer containing output.
995 Return collected error types as a string, or nil if there was
997 (with-current-buffer buffer
999 (goto-char (point-min))
1000 (let ((case-fold-search t
)
1002 (while (re-search-forward "^.+:[0-9]+: \\(.*\\)$" nil t
)
1003 (setq errors
(concat errors
" " (match-string 1))))
1004 (and (org-string-nw-p errors
) (org-trim errors
))))))
1007 (provide 'ox-taskjuggler
)
1010 ;; sentence-end-double-space: t
1013 ;;; ox-taskjuggler.el ends here