Change relative weekday specifications (ex: fri or -tue) to exclude today
[org-mode.git] / contrib / lisp / ox-taskjuggler.el
blobf4df5bc16bd2005dcbe12f484544a338b7e41169
1 ;;; ox-taskjuggler.el --- TaskJuggler Back-End for Org Export Engine
2 ;;
3 ;; Copyright (C) 2007-2013 Free Software Foundation, Inc.
4 ;;
5 ;; Emacs Lisp Archive Entry
6 ;; Filename: ox-taskjuggler.el
7 ;; Author: Christian Egli
8 ;; Nicolas Goaziou <n dot goaziou at gmail dot com>
9 ;; Maintainer: Christian Egli
10 ;; Keywords: org, taskjuggler, project planning
11 ;; Description: Converts an Org mode buffer into a TaskJuggler project plan
13 ;; This file is not part of GNU Emacs.
15 ;; This program is free software: you can redistribute it and/or modify
16 ;; it under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation, either version 3 of the License, or
18 ;; (at your option) any later version.
20 ;; This program is distributed in the hope that it will be useful,
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 ;; GNU General Public License for more details.
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
28 ;;; Commentary:
30 ;; This library implements a TaskJuggler exporter for Org mode.
31 ;; TaskJuggler uses a text format to define projects, tasks and
32 ;; resources, so it is a natural fit for Org mode. It can produce all
33 ;; sorts of reports for tasks or resources in either HTML, CSV or PDF.
34 ;; The current version of TaskJuggler requires KDE but the next
35 ;; version is implemented in Ruby and should therefore run on any
36 ;; platform.
38 ;; The exporter does not export all the nodes of a document or
39 ;; strictly follow the order of the nodes in the document.
41 ;; Instead the TaskJuggler exporter looks for a tree that defines the
42 ;; tasks and a optionally tree that defines the resources for this
43 ;; project. It then creates a TaskJuggler file based on these trees
44 ;; and the attributes defined in all the nodes.
46 ;; * Installation
48 ;; Put this file into your load-path and the following line into your
49 ;; ~/.emacs:
51 ;; (add-to-list 'org-export-backends 'taskjuggler)
53 ;; or customize `org-export-backends' variable.
55 ;; The interactive functions are the following:
57 ;; M-x `org-taskjuggler-export'
58 ;; M-x `org-taskjuggler-export-and-open'
60 ;; * Tasks
62 ;; Let's illustrate the usage with a small example. Create your tasks
63 ;; as you usually do with org-mode. Assign efforts to each task using
64 ;; properties (it's easiest to do this in the column view). You
65 ;; should end up with something similar to the example by Peter Jones
66 ;; in:
68 ;; http://www.contextualdevelopment.com/static/artifacts/articles/2008/project-planning/project-planning.org.
70 ;; Now mark the top node of your tasks with a tag named
71 ;; "taskjuggler_project" (or whatever you customized
72 ;; `org-taskjuggler-project-tag' to). You are now ready to export the
73 ;; project plan with `org-taskjuggler-export-and-open' which will
74 ;; export the project plan and open a Gantt chart in TaskJugglerUI.
76 ;; * Resources
78 ;; Next you can define resources and assign those to work on specific
79 ;; tasks. You can group your resources hierarchically. Tag the top
80 ;; node of the resources with "taskjuggler_resource" (or whatever you
81 ;; customized `org-taskjuggler-resource-tag' to). You can optionally
82 ;; assign an identifier (named "resource_id") to the resources (using
83 ;; the standard org properties commands) or you can let the exporter
84 ;; generate identifiers automatically (the exporter picks the first
85 ;; word of the headline as the identifier as long as it is unique, see
86 ;; the documentation of `org-taskjuggler--build-unique-id'). Using that
87 ;; identifier you can then allocate resources to tasks. This is again
88 ;; done with the "allocate" property on the tasks. Do this in column
89 ;; view or when on the task type
91 ;; C-c C-x p allocate RET <resource_id> RET
93 ;; Once the allocations are done you can again export to TaskJuggler
94 ;; and check in the Resource Allocation Graph which person is working
95 ;; on what task at what time.
97 ;; * Export of properties
99 ;; The exporter also takes TODO state information into consideration,
100 ;; i.e. if a task is marked as done it will have the corresponding
101 ;; attribute in TaskJuggler ("complete 100"). Also it will export any
102 ;; property on a task resource or resource node which is known to
103 ;; TaskJuggler, such as limits, vacation, shift, booking, efficiency,
104 ;; journalentry, rate for resources or account, start, note, duration,
105 ;; end, journalentry, milestone, reference, responsible, scheduling,
106 ;; etc for tasks.
108 ;; * Dependencies
110 ;; The exporter will handle dependencies that are defined in the tasks
111 ;; either with the ORDERED attribute (see TODO dependencies in the Org
112 ;; mode manual) or with the BLOCKER attribute (see org-depend.el) or
113 ;; alternatively with a depends attribute. Both the BLOCKER and the
114 ;; depends attribute can be either "previous-sibling" or a reference
115 ;; to an identifier (named "task_id") which is defined for another
116 ;; task in the project. BLOCKER and the depends attribute can define
117 ;; multiple dependencies separated by either space or comma. You can
118 ;; also specify optional attributes on the dependency by simply
119 ;; appending it. The following examples should illustrate this:
121 ;; * Training material
122 ;; :PROPERTIES:
123 ;; :task_id: training_material
124 ;; :ORDERED: t
125 ;; :END:
126 ;; ** Markup Guidelines
127 ;; :PROPERTIES:
128 ;; :Effort: 2d
129 ;; :END:
130 ;; ** Workflow Guidelines
131 ;; :PROPERTIES:
132 ;; :Effort: 2d
133 ;; :END:
134 ;; * Presentation
135 ;; :PROPERTIES:
136 ;; :Effort: 2d
137 ;; :BLOCKER: training_material { gapduration 1d } some_other_task
138 ;; :END:
140 ;;;; * TODO
141 ;; - Look at org-file-properties, org-global-properties and
142 ;; org-global-properties-fixed
143 ;; - What about property inheritance and org-property-inherit-p?
144 ;; - Use TYPE_TODO as an way to assign resources
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 :group 'org-export-taskjuggler
190 :type 'number)
192 (defcustom org-taskjuggler-default-project-version "1.0"
193 "Default version string for the project.
194 This value can also be set with the \":VERSION:\" property
195 associated to the headline defining the project."
196 :group 'org-export-taskjuggler
197 :type 'string)
199 (defcustom org-taskjuggler-default-project-duration 280
200 "Default project duration.
201 The value will be used if no start and end date have been defined
202 in the root node of the task tree, i.e. the tree that has been
203 marked with `org-taskjuggler-project-tag'"
204 :group 'org-export-taskjuggler
205 :type 'integer)
207 (defcustom org-taskjuggler-default-reports
208 '("taskreport \"Gantt Chart\" {
209 headline \"Project Gantt Chart\"
210 columns hierarchindex, name, start, end, effort, duration, completed, chart
211 timeformat \"%Y-%m-%d\"
212 hideresource 1
213 loadunit shortauto
215 "resourcereport \"Resource Graph\" {
216 headline \"Resource Allocation Graph\"
217 columns no, name, utilization, freeload, chart
218 loadunit shortauto
219 sorttasks startup
220 hidetask ~isleaf()
222 "Default reports for the project."
223 :group 'org-export-taskjuggler
224 :type '(repeat (string :tag "Report")))
226 (defcustom org-taskjuggler-default-global-header ""
227 "Default global header for the project.
228 This goes before project declaration, and might be useful for
229 early macros."
230 :group 'org-export-taskjuggler
231 :type '(string :tag "Preamble"))
233 (defcustom org-taskjuggler-default-global-properties
234 "shift s40 \"Part time shift\" {
235 workinghours wed, thu, fri off
238 "Default global properties for the project.
240 Here you typically define global properties such as shifts,
241 accounts, rates, vacation, macros and flags. Any property that
242 is allowed within the TaskJuggler file can be inserted. You
243 could for example include another TaskJuggler file.
245 The global properties are inserted after the project declaration
246 but before any resource and task declarations."
247 :group 'org-export-taskjuggler
248 :type '(string :tag "Preamble"))
250 (defcustom org-taskjuggler-valid-task-attributes
251 '(account start note duration endbuffer endcredit end
252 flags journalentry length limits maxend maxstart minend
253 minstart period reference responsible scheduling
254 startbuffer startcredit statusnote chargeset charge)
255 "Valid attributes for Taskjuggler tasks.
256 If one of these appears as a property for a headline, it will be
257 exported with the corresponding task."
258 :group 'org-export-taskjuggler)
260 (defcustom org-taskjuggler-valid-resource-attributes
261 '(limits vacation shift booking efficiency journalentry rate
262 workinghours flags)
263 "Valid attributes for Taskjuggler resources.
264 If one of these appears as a property for a headline, it will be
265 exported with the corresponding resource."
266 :group 'org-export-taskjuggler)
268 (defcustom org-taskjuggler-valid-report-attributes
269 '(headline columns definitions timeformat hideresource hidetask
270 loadunit sorttasks formats period)
271 "Valid attributes for Taskjuggler reports.
272 If one of these appears as a property for a headline, it will be
273 exported with the corresponding report."
274 :group 'org-export-taskjuggler)
276 (defcustom org-taskjuggler-keep-project-as-task t
277 "Non-nil keeps the project headline as an umbrella task for all tasks.
278 Setting this to nil will allow maintaining completely separated
279 task buckets, while still sharing the same resources pool."
280 :group 'org-export-taskjuggler
281 :type 'boolean)
285 ;;; Hooks
287 (defvar org-taskjuggler-final-hook nil
288 "Hook run after a TaskJuggler files has been saved.
289 This hook is run with the name of the file as argument.")
293 ;;; Back-End Definition
295 (org-export-define-backend 'taskjuggler
296 '((template . org-taskjuggler-project-plan))
297 :menu-entry
298 '(?J "Export to TaskJuggler"
299 ((?j "As TJP file" (lambda (a s v b) (org-taskjuggler-export a s v)))
300 (?o "As TJP file and open"
301 (lambda (a s v b)
302 (if a (org-taskjuggler-export a s v)
303 (org-taskjuggler-export-and-open s v))))))
304 ;; This property will be used to store unique ids in communication
305 ;; channel. Ids will be retrieved with `org-taskjuggler-get-id'.
306 :options-alist '((:taskjuggler-unique-ids nil nil nil)))
310 ;;; Unique IDs
312 (defun org-taskjuggler-assign-task-ids (tasks info)
313 "Assign a unique ID to each task in TASKS.
314 TASKS is a list of headlines. Return value is an alist between
315 headlines and their associated ID. IDs are hierarchical, which
316 means they only need to be unique among the task siblings."
317 (let* (alist
318 (build-id
319 (lambda (tasks local-ids)
320 (org-element-map tasks 'headline
321 (lambda (task)
322 (let ((id (org-taskjuggler--build-unique-id task local-ids)))
323 (push id local-ids)
324 (push (cons task id) alist)
325 (funcall build-id (org-element-contents task) nil)))
326 info nil 'headline))))
327 (funcall build-id tasks nil)
328 alist))
330 (defun org-taskjuggler-assign-resource-ids (resources info)
331 "Assign a unique ID to each resource within RESOURCES.
332 RESOURCES is a list of headlines. Return value is an alist
333 between headlines and their associated ID."
334 (let (ids)
335 (org-element-map resources 'headline
336 (lambda (resource)
337 (let ((id (org-taskjuggler--build-unique-id resource ids)))
338 (push id ids)
339 (cons resource id)))
340 info)))
344 ;;; Accessors
346 (defun org-taskjuggler-get-project (info)
347 "Return project in parse tree.
348 INFO is a plist used as a communication channel. First headline
349 in buffer with `org-taskjuggler-project-tag' defines the project.
350 If no such task is defined, pick the first headline in buffer.
351 If there is no headline at all, return nil."
352 (or (org-element-map (plist-get info :parse-tree) 'headline
353 (lambda (hl)
354 (and (member org-taskjuggler-project-tag
355 (org-export-get-tags hl info))
356 hl))
357 info t)
358 (org-element-map tree 'headline 'identity info t)))
360 (defun org-taskjuggler-get-id (item info)
361 "Return id for task or resource ITEM.
362 ITEM is a headline. Return value is a string."
363 (cdr (assq item (plist-get info :taskjuggler-unique-ids))))
365 (defun org-taskjuggler-get-name (item)
366 "Return name for task or resource ITEM.
367 ITEM is a headline. Return value is a string."
368 ;; Quote double quotes in name.
369 (replace-regexp-in-string
370 "\"" "\\\"" (org-element-property :raw-value item) t t))
372 (defun org-taskjuggler-get-start (item)
373 "Return start date for task or resource ITEM.
374 ITEM is a headline. Return value is a string or nil if ITEM
375 doesn't have any start date defined.."
376 (let ((scheduled (org-element-property :scheduled item)))
377 (and scheduled (org-timestamp-format scheduled "%Y-%02m-%02d"))))
379 (defun org-taskjuggler-get-end (item)
380 "Return end date for task or resource ITEM.
381 ITEM is a headline. Return value is a string or nil if ITEM
382 doesn't have any end date defined.."
383 (let ((deadline (org-element-property :deadline item)))
384 (and deadline (org-timestamp-format deadline "%Y-%02m-%02d"))))
388 ;;; Internal Functions
390 (defun org-taskjuggler--indent-string (s)
391 "Indent string S by 2 spaces.
392 Return new string. If S is the empty string, return it."
393 (if (equal "" s) s (replace-regexp-in-string "^ *\\S-" " \\&" s)))
395 (defun org-taskjuggler--build-attributes (item attributes)
396 "Return attributes string for task, resource or report ITEM.
397 ITEM is a headline. ATTRIBUTES is a list of symbols
398 representing valid attributes for ITEM."
399 (mapconcat
400 (lambda (attribute)
401 (let ((value (org-element-property
402 (intern (upcase (format ":%s" attribute)))
403 item)))
404 (and value (format "%s %s\n" attribute value))))
405 (remq nil attributes) ""))
407 (defun org-taskjuggler--build-unique-id (item unique-ids)
408 "Return a unique id for a given task or a resource.
409 ITEM is an `headline' type element representing the task or
410 resource. Its id is derived from its name and made unique
411 against UNIQUE-IDS. If the (downcased) first token of the
412 headline is not unique try to add more (downcased) tokens of the
413 headline or finally add more underscore characters (\"_\")."
414 (let ((id (org-string-nw-p (org-element-property :TASK_ID item))))
415 ;; If an id is specified, use it, as long as it's unique.
416 (if (and id (not (member id unique-ids))) id
417 (let* ((parts (org-split-string (org-element-property :raw-value item)))
418 (id (org-taskjuggler--clean-id (downcase (pop parts)))))
419 ;; Try to add more parts of the headline to make it unique.
420 (while (and (car parts) (member id unique-ids))
421 (setq id (concat id "_"
422 (org-taskjuggler--clean-id (downcase (pop parts))))))
423 ;; If it's still not unique, add "_".
424 (while (member id unique-ids)
425 (setq id (concat id "_")))
426 id))))
428 (defun org-taskjuggler--clean-id (id)
429 "Clean and return ID to make it acceptable for TaskJuggler.
430 ID is a string."
431 ;; Replace non-ascii by "_".
432 (replace-regexp-in-string
433 "[^a-zA-Z0-9_]" "_"
434 ;; Make sure id doesn't start with a number.
435 (replace-regexp-in-string "^\\([0-9]\\)" "_\\1" id)))
439 ;;; Dependencies
441 (defun org-taskjuggler-resolve-dependencies (task info)
442 "Return a list of all tasks TASK depends on.
443 TASK is a headline. INFO is a plist used as a communication
444 channel."
445 (let ((deps-ids
446 ;; Get all dependencies specified in BLOCKER and DEPENDS task
447 ;; properties. Clean options from them.
448 (let ((deps (concat (org-element-property :BLOCKER task)
449 (org-element-property :DEPENDS task))))
450 (and deps
451 (org-split-string (replace-regexp-in-string "{.*?}" "" deps)
452 "[ ,]* +"))))
453 depends)
454 (when deps-ids
455 ;; Find tasks with :task_id: property matching id in DEPS-IDS.
456 ;; Add them to DEPENDS.
457 (let* ((project (org-taskjuggler-get-project info))
458 (tasks (if org-taskjuggler-keep-project-as-task project
459 (org-element-contents project))))
460 (setq depends
461 (org-element-map tasks 'headline
462 (lambda (task)
463 (let ((task-id (org-element-property :TASK_ID task)))
464 (and task-id (member task-id deps-ids) task)))
465 info)))
466 ;; Check BLOCKER and DEPENDS properties. If "previous-sibling"
467 ;; belongs to DEPS-ID, add it to DEPENDS.
468 (when (and (member-ignore-case "previous-sibling" deps-ids)
469 (not (org-export-first-sibling-p task info)))
470 (let ((prev (org-export-get-previous-element task info)))
471 (and (not (memq prev depends)) (push prev depends)))))
472 ;; Check ORDERED status of parent.
473 (let ((parent (org-export-get-parent task)))
474 (when (and parent
475 (org-element-property :ORDERED parent)
476 (not (org-export-first-sibling-p task info)))
477 (push (org-export-get-previous-element task info) depends)))
478 ;; Return dependencies.
479 depends))
481 (defun org-taskjuggler-format-dependencies (dependencies task info)
482 "Format DEPENDENCIES to match TaskJuggler syntax.
483 DEPENDENCIES is list of dependencies for TASK, as returned by
484 `org-taskjuggler-resolve-depedencies'. TASK is a headline.
485 INFO is a plist used as a communication channel. Return value
486 doesn't include leading \"depends\"."
487 (let ((dep-str (concat (org-element-property :BLOCKER task)
489 (org-element-property :DEPENDS task)))
490 (get-path
491 (lambda (dep)
492 ;; Return path to DEP relatively to TASK.
493 (let ((parent (org-export-get-parent dep))
494 (exclamations 1)
495 (option
496 (let ((id (org-element-property :TASK_ID dep)))
497 (and id
498 (string-match (concat id " +\\({.*?}\\)") dep-str)
499 (org-match-string-no-properties 1))))
500 path)
501 ;; Compute number of exclamation marks by looking for the
502 ;; common ancestor between TASK and DEP.
503 (while (not (org-element-map parent 'headline
504 (lambda (hl) (eq hl task))))
505 (incf exclamations)
506 (setq parent (org-export-get-parent parent)))
507 ;; Build path from DEP to PARENT.
508 (while (not (eq parent dep))
509 (push (org-taskjuggler-get-id dep info) path)
510 (setq dep (org-export-get-parent dep)))
511 ;; Return full path. Add dependency options, if any.
512 (concat (make-string exclamations ?!)
513 (mapconcat 'identity path ".")
514 (and option (concat " " option)))))))
515 ;; Return dependencies string, without the leading "depends".
516 (mapconcat (lambda (dep) (funcall get-path dep)) dependencies ", ")))
520 ;;; Translator Functions
522 (defun org-taskjuggler-project-plan (contents info)
523 "Build TaskJuggler project plan.
524 CONTENTS is ignored. INFO is a plist holding export options.
525 Return complete project plan as a string in TaskJuggler syntax."
526 (let* ((tree (plist-get info :parse-tree))
527 (project (or (org-taskjuggler-get-project info)
528 (error "No project specified"))))
529 (concat
530 ;; 1. Insert header.
531 (org-element-normalize-string org-taskjuggler-default-global-header)
532 ;; 2. Insert project.
533 (org-taskjuggler--build-project project info)
534 ;; 3. Insert global properties.
535 (org-element-normalize-string org-taskjuggler-default-global-properties)
536 ;; 4. Insert resources. Provide a default one if none is
537 ;; specified.
538 (let ((main-resources
539 ;; Collect contents from various trees marked with
540 ;; `org-taskjuggler-resource-tag'. Only gather top level
541 ;; resources.
542 (apply 'append
543 (org-element-map tree 'headline
544 (lambda (hl)
545 (and (member org-taskjuggler-resource-tag
546 (org-export-get-tags hl info))
547 (org-element-map (org-element-contents hl) 'headline
548 'identity info nil 'headline)))
549 info nil 'headline))))
550 ;; Assign a unique ID to each resource. Store it under
551 ;; `:taskjuggler-unique-ids' property in INFO.
552 (setq info
553 (plist-put info :taskjuggler-unique-ids
554 (org-taskjuggler-assign-resource-ids
555 main-resources info)))
556 (concat
557 (if main-resources
558 (mapconcat
559 (lambda (resource) (org-taskjuggler--build-resource resource info))
560 main-resources "")
561 (format "resource %s \"%s\" {\n}\n" (user-login-name) user-full-name))
562 ;; 5. Insert tasks.
563 (let ((main-tasks
564 ;; If `org-taskjuggler-keep-project-as-task' is
565 ;; non-nil, there is only one task. Otherwise, every
566 ;; direct children of PROJECT is a top level task.
567 (if org-taskjuggler-keep-project-as-task (list project)
568 (or (org-element-map (org-element-contents project) 'headline
569 'identity info nil 'headline)
570 (error "No task specified")))))
571 ;; Assign a unique ID to each task. Add it to
572 ;; `:taskjuggler-unique-ids' property in INFO.
573 (setq info
574 (plist-put info :taskjuggler-unique-ids
575 (append
576 (org-taskjuggler-assign-task-ids main-tasks info)
577 (plist-get info :taskjuggler-unique-ids))))
578 ;; If no resource is allocated among tasks, allocate one to
579 ;; the first task.
580 (unless (org-element-map main-tasks 'headline
581 (lambda (task) (org-element-property :ALLOCATE task))
582 info t)
583 (org-element-put-property
584 (car main-tasks) :ALLOCATE
585 (or (org-taskjuggler-get-id (car main-resources) info)
586 (user-login-name))))
587 (mapconcat
588 (lambda (task) (org-taskjuggler--build-task task info))
589 main-tasks ""))
590 ;; 6. Insert reports. If no report is defined, insert default
591 ;; reports.
592 (let ((main-reports
593 ;; Collect contents from various trees marked with
594 ;; `org-taskjuggler-report-tag'. Only gather top level
595 ;; reports.
596 (apply 'append
597 (org-element-map tree 'headline
598 (lambda (hl)
599 (and (member org-taskjuggler-report-tag
600 (org-export-get-tags hl info))
601 (org-element-map (org-element-contents hl)
602 'headline 'identity info nil 'headline)))
603 info nil 'headline))))
604 (if main-reports
605 (mapconcat
606 (lambda (report) (org-taskjuggler--build-report report info))
607 main-reports "")
608 (mapconcat 'org-element-normalize-string
609 org-taskjuggler-default-reports ""))))))))
611 (defun org-taskjuggler--build-project (project info)
612 "Return a project declaration.
613 PROJECT is a headline. INFO is a plist used as a communication
614 channel. If no start date is specified, start today. If no end
615 date is specified, end `org-taskjuggler-default-project-duration'
616 days from now."
617 (format "project %s \"%s\" \"%s\" %s %s {\n}\n"
618 (org-taskjuggler-get-id project info)
619 (org-taskjuggler-get-name project)
620 ;; Version is obtained through :TASKJUGGLER_VERSION:
621 ;; property or `org-taskjuggler-default-project-version'.
622 (or (org-element-property :VERSION project)
623 org-taskjuggler-default-project-version)
624 (or (org-taskjuggler-get-start project)
625 (format-time-string "%Y-%m-%d"))
626 (let ((end (org-taskjuggler-get-end project)))
627 (or (and end (format "- %s" end))
628 (format "+%sd" org-taskjuggler-default-project-duration)))))
630 (defun org-taskjuggler--build-resource (resource info)
631 "Return a resource declaration.
633 RESOURCE is a headline. INFO is a plist used as a communication
634 channel.
636 All valid attributes from RESOURCE are inserted. If RESOURCE
637 defines a property \"resource_id\" it will be used as the id for
638 this resource. Otherwise it will use the ID property. If
639 neither is defined a unique id will be associated to it."
640 (concat
641 ;; Opening resource.
642 (format "resource %s \"%s\" {\n"
643 (org-taskjuggler--clean-id
644 (or (org-element-property :RESOURCE_ID resource)
645 (org-element-property :ID resource)
646 (org-taskjuggler-get-id resource info)))
647 (org-taskjuggler-get-name resource))
648 ;; Add attributes.
649 (org-taskjuggler--indent-string
650 (org-taskjuggler--build-attributes
651 resource org-taskjuggler-valid-resource-attributes))
652 ;; Add inner resources.
653 (org-taskjuggler--indent-string
654 (mapconcat
655 'identity
656 (org-element-map (org-element-contents resource) 'headline
657 (lambda (hl) (org-taskjuggler--build-resource hl info))
658 info nil 'headline)
659 ""))
660 ;; Closing resource.
661 "}\n"))
663 (defun org-taskjuggler--build-report (report)
664 "Return a report declaration.
665 REPORT is a headline. INFO is a plist used as a communication
666 channel."
667 (concat
668 ;; Opening report.
669 (format "%s \"%s\" {\n"
670 (or (org-element-property :REPORT_KIND report) "taskreport")
671 (org-taskjuggler-get-name report))
672 ;; Add attributes.
673 (org-taskjuggler--indent-string
674 (org-taskjuggler--build-attributes
675 report org-taskjuggler-valid-report-attributes))
676 ;; Add inner reports.
677 (org-taskjuggler--indent-string
678 (mapconcat
679 'identity
680 (org-element-map (org-element-contents report) 'headline
681 (lambda (hl) (org-taskjuggler--build-report hl info))
682 info nil 'headline)
683 ""))
684 ;; Closing report.
685 "}\n"))
687 (defun org-taskjuggler--build-task (task info)
688 "Return a task declaration.
690 TASK is a headline. INFO is a plist used as a communication
691 channel.
693 All valid attributes from TASK are inserted. If TASK defines
694 a property \"task_id\" it will be used as the id for this task.
695 Otherwise it will use the ID property. If neither is defined
696 a unique id will be associated to it."
697 (let* ((allocate (org-element-property :ALLOCATE task))
698 (complete
699 (if (eq (org-element-property :todo-type task) 'done) "100"
700 (org-element-property :COMPLETE task)))
701 (depends (org-taskjuggler-resolve-dependencies task info))
702 (effort (org-element-property :EFFORT task))
703 (milestone
704 (or (org-element-property :MILESTONE task)
705 (not (or (org-element-map (org-element-contents task) 'headline
706 'identity info t) ; Has task any child?
707 effort
708 (org-element-property :LENGTH task)
709 (org-element-property :DURATION task)
710 (and (org-taskjuggler-get-start task)
711 (org-taskjuggler-get-end task))
712 (org-element-property :PERIOD task)))))
713 (priority
714 (let ((pri (org-element-property :priority task)))
715 (and pri
716 (max 1 (/ (* 1000 (- org-lowest-priority pri))
717 (- org-lowest-priority org-highest-priority)))))))
718 (concat
719 ;; Opening task.
720 (format "task %s \"%s\" {\n"
721 (org-taskjuggler-get-id task info)
722 (org-taskjuggler-get-name task))
723 ;; Add default attributes.
724 (and depends
725 (format " depends %s\n"
726 (org-taskjuggler-format-dependencies depends task info)))
727 (and allocate
728 (format " purge %s\n allocate %s\n"
729 ;; Compatibility for previous TaskJuggler versions.
730 (if (>= org-taskjuggler-target-version 3.0) "allocate"
731 "allocations")
732 allocate))
733 (and complete (format " complete %s\n" comptete))
734 (and effort
735 (format " effort %s\n"
736 (let* ((minutes (org-duration-string-to-minutes effort))
737 (hours (/ minutes 60.0)))
738 (format "%.1fh" hours))))
739 (and priority (format " priority %s\n" complete))
740 (and milestone " milestone\n")
741 ;; Add other valid attributes.
742 (org-taskjuggler--indent-string
743 (org-taskjuggler--build-attributes
744 task org-taskjuggler-valid-task-attributes))
745 ;; Add inner tasks.
746 (org-taskjuggler--indent-string
747 (mapconcat 'identity
748 (org-element-map (org-element-contents task) 'headline
749 (lambda (hl) (org-taskjuggler--build-task hl info))
750 info nil 'headline)
751 ""))
752 ;; Closing task.
753 "}\n")))
757 ;;; Interactive Functions
759 ;;;###autoload
760 (defun org-taskjuggler-export (&optional async subtreep visible-only)
761 "Export current buffer to a TaskJuggler file.
763 The exporter looks for a tree with tag that matches
764 `org-taskjuggler-project-tag' and takes this as the tasks for
765 this project. The first node of this tree defines the project
766 properties such as project name and project period.
768 If there is a tree with tag that matches
769 `org-taskjuggler-resource-tag' this tree is taken as resources
770 for the project. If no resources are specified, a default
771 resource is created and allocated to the project.
773 Also the TaskJuggler project will be created with default reports
774 as defined in `org-taskjuggler-default-reports'.
776 If narrowing is active in the current buffer, only export its
777 narrowed part.
779 If a region is active, export that region.
781 A non-nil optional argument ASYNC means the process should happen
782 asynchronously. The resulting file should be accessible through
783 the `org-export-stack' interface.
785 When optional argument SUBTREEP is non-nil, export the sub-tree
786 at point, extracting information from the headline properties
787 first.
789 When optional argument VISIBLE-ONLY is non-nil, don't export
790 contents of hidden elements.
792 Return output file's name."
793 (interactive)
794 (let ((outfile
795 (org-export-output-file-name org-taskjuggler-extension subtreep)))
796 (if async
797 (org-export-async-start
798 (lambda (f)
799 (org-export-add-to-stack f 'taskjuggler)
800 (run-hook-with-args 'org-taskjuggler-final-hook f))
801 `(expand-file-name
802 (org-export-to-file 'taskjuggler ,outfile ,subtreep ,visible-only)))
803 (org-export-to-file 'taskjuggler outfile subtreep visible-only)
804 (run-hook-with-args 'org-taskjuggler-final-hook outfile)
805 outfile)))
807 ;;;###autoload
808 (defun org-taskjuggler-export-and-open (&optional subtreep visible-only)
809 "Export current buffer to a TaskJuggler file and open it.
811 The exporter looks for a tree with tag that matches
812 `org-taskjuggler-project-tag' and takes this as the tasks for
813 this project. The first node of this tree defines the project
814 properties such as project name and project period.
816 If there is a tree with tag that matches
817 `org-taskjuggler-resource-tag' this tree is taken as resources
818 for the project. If no resources are specified, a default
819 resource is created and allocated to the project.
821 Also the TaskJuggler project will be created with default reports
822 as defined in `org-taskjuggler-default-reports'.
824 If narrowing is active in the current buffer, only export its
825 narrowed part.
827 If a region is active, export that region.
829 When optional argument SUBTREEP is non-nil, export the sub-tree
830 at point, extracting information from the headline properties
831 first.
833 When optional argument VISIBLE-ONLY is non-nil, don't export
834 contents of hidden elements.
836 Open file with the TaskJuggler GUI."
837 (interactive)
838 (let* ((file (org-taskjuggler-export nil subtreep visible-only))
839 (process-name "TaskJugglerUI")
840 (command (concat process-name " " file)))
841 (start-process-shell-command process-name nil command)))
844 (provide 'ox-taskjuggler)
846 ;; Local variables:
847 ;; sentence-end-double-space: t
848 ;; End:
850 ;;; ox-taskjuggler.el ends here